/* calendar.cpp
by David Cary <d.cary at ieee.org>

Uncomment only the 1 style you want.
*/
//#define minimal_txt 1
//#define minimal_html 1
#define std_html 1

/*
emits simple calendars in HTML format.
Does *no* tricky date calculations.
Give it the day of the week that Jan 1 falls on,
and whether this year includes the leap day,
and it prints out some calendars in HTML format.

To eliminate even that input, perhaps just print out all 14 possible calendars --
-- then no input necessary.

Prints the day number (for standard 1997-020 style date notation)
as well as the day-of-the-month in each day.

Todo: make "Deutsch" and "English" modes.
( Deutsch weeks start with "Monday", and have different names for the days of the week and months.)

Change log:
1997-01-20:DAV:set up and tested the 3 modes.
1997-01-20:DAV:Works!
1997-01-20:DAV:Started
*/

//#include "daves.h" // for uint, ASSERT(), etc.
#include <iostream.h> // for "cout <<"
//#include <iso646.h> // for "and", "or", "bitor", etc.
#include <iomanip.h> // for "cout << setw(3)" and "cout << right"


#ifdef minimal_txt
void
print_week_start(unsigned short week){
};

void
print_day( unsigned short doy, unsigned short dom, char * message){
	// "setw() only remains in effect for the next insertion."
	cout << setw(2) << dom << message << " ";
};

void
print_week_end(char * message){
	cout << message << endl;
};

void
print_table_start(void){
}

void
print_table_end(void){
}
#endif



#ifdef minimal_html
void
print_week_start(unsigned short week){
	cout << "<tr>" << endl;
	// cout << week << " ";
};

void
print_day( unsigned short doy, unsigned short dom, char * message){
	// "setw() only remains in effect for the next insertion."	
	cout << "\t" << "<td>" << setw(2) << dom << message << "</td>" << endl;
};

void
print_week_end(char * message){
	cout << "\t" << "<td>" << message << "</td>" << endl;
	cout << "</tr>" <<endl;
};

void
print_table_start(void){
	cout << "<table border>" << endl;
}

void
print_table_end(void){
	cout << "</table>" << endl;
}
#endif


#ifdef std_html
void
print_week_start(unsigned short week){
	cout << "<tr align=right valign=top>" << endl;
	// cout << week << " ";
};

void
print_day( short int doy, unsigned short dom, char * message){
	// "setw() only remains in effect for the next insertion."	
	cout << "\t" << "<td>" <<
		setw(3) << doy <<
		"<br>" <<
		setw(2) << dom <<
		message << "</td>" <<
		endl;
};

void
print_week_end(char * message){
	cout << "\t" << "<td>" << message << "</td>" << endl;
	cout << "</tr>" <<endl;
};

void
print_table_start(void){
	cout << "<table border VALIGN=TOP>" << endl;
}

void
print_table_end(void){
	cout << "</table>" << endl;
}
#endif


/* call with
start_day = 0 to have Sun, Jan 1;
start_day = 1 to have Mon, Jan 1.
*/
void
printcalendar(short int start_day, bool leap_year){

struct month_t{
	char * name;
	unsigned short int length[2];
};
	cout << "<!-- calendar generated by 'calendar.cpp' by David Cary <d.cary&#x40;ieee.org> -->" << endl;

struct month_t m[14] = {
	{"December", 31, 31},
	{"January",  31, 31},
	{"February", 28, 29},
	{"March", 31, 31},
	{"April", 30, 30},
	{"May", 31, 31},
	{"June", 30, 30},
	{"July", 31, 31},
	{"August", 31, 31},
	{"September", 30, 30},
	{"October", 31, 31},
	{"November", 30, 30},
	{"December", 31, 31},
	{"January",  31, 31},
};


	short int doy = 1 - start_day; // pseudo-Julian day of this year (negative for previous year).
	unsigned short int current_month = 0; // December
	unsigned short int dom = m[current_month].length[leap_year] + 1 - start_day; // day of this month

	unsigned short int week = 0; // week number -- *not* ISO standard.
	unsigned short int dow = 0; // Sunday -- day of the week

	print_table_start();

	while( current_month < 13 ){
		// Maximum number of weeks to print out occurs when Jan 1 is a Saturday (6 extra days need to be printed at start of year),
		// and there is a leap day in Feb -- 366 days.
		// This is a total of 372 days, which are just a bit longer than 53 weeks ==> sometimes print 54 weeks.
		// (Dec 31 is on a sunday, right ?)
		// Minimum number of weeks occurs when Jan 1 is Sunday, and not a leap year -- 356 days.
		// a bit over 52 weeks, so print 53 weeks (Dec 31 is on a Sunday, right ?)

		char * weekmessage = "";

		//always print complete weeks.
		print_week_start( week );
		for( dow = 0; dow < 7; dow++, doy++, dom++){

			if( m[current_month].length[leap_year] < dom ){
				// we've run past end-of-month, must start next month.
				current_month++;
				dom = 1;
				weekmessage = m[current_month].name;
			};
			print_day( doy, dom, "");

		};
		print_week_end( weekmessage );
		week++;

	};

	print_table_end();


}


main(){

	//printcalendar(0, false); // print the non-leap-calendar that makes Jan 1 the first day of the week.
	printcalendar(4, false); // print the non-leap-calendar that makes Jan 1 the 4th day of the week.



	return(0);// Success.
}
