nyctergatis.com

Contact

Projects
Sysquake Remote Live
NME
PDF
Hike
Sudoku
GifBuilder
jpeglib for Palm OS
MySQL Client
Cross-GCC for Mac OS
NMEPluginCalendar.c
Go to the documentation of this file.
00001 
00008 /* License: new BSD license (see NME.h) */
00009 
00010 #include "NMEPluginCalendar.h"
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <stdio.h>
00014 
00015 NMEErr NMEPluginCalendar(NMEConstText name, NMEInt nameLen,
00016         NMEConstText data, NMEInt dataLen,
00017         NMEContext *context,
00018         void *userData)
00019 {
00020     char *endp;
00021     int year, month;
00022     int weekday;    // weekday of first day of month (0=monday)
00023     int m1, y1; // temp. values used to compute weekday
00024     int d, dmax;
00025     char str[16];
00026     static int const nd[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
00027     (void)name;
00028     (void)nameLen;
00029     (void)userData;
00030     
00031     // read year and month in data
00032     year = strtol(data, &endp, 0);
00033     month = strtol(endp, NULL, 0);
00034     if (year < 0)
00035         year = 0;
00036     if (month < 1 || month > 12)
00037         month = 1;
00038     
00039     // find week day of 1st day of month
00040     m1 = (month - 14) / 12;
00041     y1 = year + 4800;
00042     weekday = (1461 * (y1 + m1) / 4 + 367 * (month - 2 - 12 * m1) / 12
00043                 - 3 * ((y1 + m1 + 100) / 100) / 4 - 32074) % 7;
00044     // write calendar to dest: "\n|=Mon|=Tue...\n|||1|2|..."
00045     if (!NMEAddString("\n|=Mon|=Tue|=Wed|=Thu|=Fri|=Sat|=Sun\n", -1,
00046             '\0', context))
00047         return kNMEErrNotEnoughMemory;
00048     dmax = nd[month - 1];
00049     if (month == 2 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
00050         dmax = 29;
00051     for (d = 0; d < weekday; d++)
00052         if (!NMEAddString("|", 1, '\0', context))
00053             return kNMEErrNotEnoughMemory;
00054     for (d = 1; d <= dmax; d++)
00055     {
00056         sprintf(str, "|%d", d);
00057         if (!NMEAddString(str, -1, '\0', context))
00058             return kNMEErrNotEnoughMemory;
00059         if ((d + weekday) % 7 == 0)
00060             if (!NMEAddString("\n", 1, '\0', context))
00061                 return kNMEErrNotEnoughMemory;
00062     }
00063     if (!NMEAddString("\n\n", 2, '\0', context))
00064         return kNMEErrNotEnoughMemory;
00065     
00066     return kNMEErrOk;
00067 }
Generated by Doxygen.
Copyright 2007-2013, Yves Piguet.
All rights reserved.