/****************************
filename:  cal.cpp
****************************/

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/*the structure for holding the dates for each month*/
typedef int (*typCalform)[6][7];

/*days-of-week header when building calendar*/
const char * mcharWeekHeader = { " S  M Tu  W Th  F  S" };

const char * mcharBlockStr[4][3] = {/*month headers when building calendar*/
   {"January", "February", "March"},
   {"April",   "May",      "June"},
   {"July",    "August",   "September"},
   {"October", "November", "December"}
};

char mcharRetLink[1000];/*link returned by getLink()*/

/*below two structures used in gridBuilder*/
const int CUM_MON[2][12] = {
	{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 },
	{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335 }
};	

const int LEN_MON[2][12] = {
	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};

/*prototype definitions*/
typCalform gridBuilder( int intYear, int intMonth );
int getLink(int intYear, int intMonth, int intDay);


/******************************/
int main(){
   int intYear;
   short row, block, col;
   typCalform typGrid[3];
   int intMonth, intDay;
   time_t time_start;
   struct tm *tmStruct;

   time(&time_start);
   tmStruct = localtime(&time_start);
   intYear = tmStruct->tm_year + 1900;


   printf("<HTML><HEAD><TITLE>Year %d</TITLE></HEAD><BODY><CENTER><PRE>\n", intYear);
   printf("<H2>Year %d</H2><P>\n", intYear);

   printf("<TABLE border=1>\n");

   printf("<TR>\n\t<TD></TD><TD  valign=top><CENTER>%d</CENTER></TD><TD></TD>\n</TR>\n", intYear);

   intMonth = 0;/*initialize counter to know which month to get from gridBuilder*/
   for(block=0; block<4; block++){

      printf("<TR>\n");
      for(col=0; col<3; col++)
         printf("\t<TD  valign=top><CENTER>%s</CENTER><PRE>%s</PRE></TD>\n", mcharBlockStr[block][col], mcharWeekHeader);

      printf("</TR>\n");

      for(col=0; col<3; col++)/*get three month's worth of calendars*/
         typGrid[col] = gridBuilder(intYear, intMonth++);

      printf("<TR>\n");

      for(col=0; col<3; col++){
         printf("<TD valign=top><PRE>\n");
         for(row=0; row<6; row++){
            for(intDay=0; intDay<=6; intDay++){
               if ((*typGrid[col])[row][intDay]){
                  printf("%2d ", (*typGrid[col])[row][intDay]);
               }
               else
                  printf("   ");
            }/*end for intDay*/
            printf("\n");
			}/*end for row*/
         printf("</PRE></TD>\n");
      }/*end for col*/
      printf( "</TR>\n" );
   }

   printf("</TABLE></CENTER></BODY></HTML>\n");

	return 0;
}/*end main*/


/*********************************
 *
 * function:  typCalform gridBuilder( int y, int m )
 *
 * purpose:  to provide a grid of dates based on a given year
 *
 * input:  ints year and month for determining dates to create
 *
 * output and return:  pointer to locally allocated memory
 *
 * addapted 08/18/98 by Keith Oxenrider from a calendar program written by
 * Raymond Hettinger called cal.c and located on the Web at the address:
 * http://www.javanet.com/~othello/download/download.htm
 *
 * original author's notes:

	Module provides a single function interface generating
	a one month calendar grid.  Returns a pointer to a 2-D
	array corresponding to a normal calendar presentation
	( viz. monthGrid[ weekNumber ][ dayOfWeek ].  The elements
	of the array are days-of-month (ranging from 1 to 31 ) or
	a zero to indicate a blank.  The dayOfWeek index starts
	from Sunday (zero index) and ends at Saturday (index six).
	The weekNumber is the row on a calendar print-out and is
	also zero-offset.

  other notes:

  module allocates memory for grid structure and is relying on
  the exit of the program to deallocate the memory.  If this program
  is ever modified to run more than once, this area will create problems.

 ********************************/

typCalform gridBuilder(int intYear, int intMonth){
	int intCounter_i, *curr;
	typCalform typGrid;
	int intDay = intYear;
	char charLeapYear = 1;

	if ((typGrid = (typCalform) calloc(42, sizeof(int))) == NULL)
		printf( "Unable to allocate memory for typGrid in subroutine gridBuilder, exiting!\n" ), exit(3);

   if( intYear & 0x3 ) charLeapYear = 0;
   else if( intYear % 100 ) charLeapYear = 1;
   else if( intYear % 400 ) charLeapYear = 0;
   intDay +=  (intYear/400) - (intYear/100);

	intDay += CUM_MON[charLeapYear][intMonth] + ( intYear >> 2 ) - charLeapYear;
	intDay %= 7;

	for( intCounter_i=1, curr=((int*)(*typGrid))+intDay; intCounter_i<=LEN_MON[charLeapYear][intMonth]; intCounter_i++, curr++ )
		*curr = intCounter_i;

	return typGrid;
}/*end gridBuilder*/



