home *** CD-ROM | disk | FTP | other *** search
- //----------------------------------------------------------------------------
- // TimePlanner V1.0 © 1993 L. Vanhelsuwé
- // ---------------- --------------------
- // Background deamon for starting periodic background processes or for reminding
- // users of appointments (birthdays, car insurance, ...).
- // Uses an Events file to find out when to become active and notify user or to
- // execute secondary batch processes.
- //
- // BUGS:
- // -----
- // -Program is not localization-proof. When locale is not English : corruption
- // occurs in EV.LST
- //
- // History:
- // --------
- // Sat 24-JAN-93: started this file.
- // Sun 31-JAN-93: finished basic useful version
- // Sun 09-MAY-93: added early reminder "X DAYS/HOURS/MINUTES to go before.."
- // Mon 10-MAY-93: added ENV:EV.LST marking to find out where we're waiting
- // bumped revision to 1.1
- // Tue 18-MAY-93: added graceful exit for 1.3 users.
- // Fri 21-JAN-94: implemented EXECUTE option to execute batch files or commands
- // added delay after initial loading for faster/clearer startup
- //
- //----------------------------------------------------------------------------
-
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
-
- #ifdef AMIGA
-
- #include <exec/types.h>
- #include <exec/memory.h>
- #include <dos/dos.h>
- #include <dos/datetime.h>
- #include <intuition/screens.h>
- #include <intuition/intuitionbase.h>
- #include <graphics/gfx.h>
-
- #include <clib/exec_protos.h> // ANSI function prototypes
- #include <clib/alib_protos.h>
- #include <clib/dos_protos.h>
- #include <clib/graphics_protos.h>
- #include <clib/intuition_protos.h>
-
- #endif
-
- #define MAX_REMINDERS (12) // upto N-1 early reminders
-
- #define COLON (':')
- #define SEMICOLON (';')
- #define COMMA (',')
- #define HYPHEN ('-')
- #define QUOTE ('"')
- #define VERT_BAR ('|')
- #define TAB (9)
- #define LF (10)
- #define ASTERISK ('*')
- #define SPACE (32)
-
- #define MINUTETICKS (60*50) // 50 Amiga TICKS in a second
- #define HOURTICKS (60*MINUTETICKS)
- #define DAYTICKS (24*HOURTICKS)
- #define WEEKTICKS (7*DAYTICKS)
- #define MONTHTICKS (30*DAYTICKS)
-
- #define NUM_DAYS (7)
- #define NUM_MONTHS (12)
-
- #define BUT_OK (1)
- #define BUT_CANCEL (2)
- //-----------------------------------------------------------------------------
- // Function prototypes
- // -------------------
-
- void parse_user_events ( void );
- void skip_line ( void );
- void skip_whitespc ( void );
- void print_line ( void );
- char popup_requester ( char *msg, char buttons);
- void make_exe_file ( void );
- void process_line ( void );
- void process_on_line ( void );
- void init_TP ( void );
- void adjust ( struct DateStamp *ds, ULONG ticks);
- void handle_events ( void );
- void mark_file ( char *file, int position, char *tag);
- void do_event ( char *req_msg, char *batch_file);
-
- char * load_file ( char * filename);
-
- BOOL is_string ( char * txt, char * string);
- BOOL DStampToStr ( struct DateStamp *ds, char *daystr, char *datestr, char *timestr);
-
- short get_num ( void );
- short get_month ( void );
- short get_dayname ( void );
- short process_remind_line ( void );
- short month_from_str ( char *month );
-
- //-----------------------------------------------------------------------------
- // Global statics
- // --------------
-
- struct IntuitionBase *IntuitionBase; // for access to ActiveWindow
-
- struct DateStamp datestamp; // for DateStamp(), DateToStr(),..
- struct DateTime datetime;
-
- char day_str[16]; // Wednesday is biggest
- char date_str[10]; // 24-JAN-93
- char time_str[10]; // 21:47:47
-
- char *file_buf, *events_file;
- char *txt; // text pointer (into buffer)
- ULONG file_size;
-
- struct REM // REMINDER structure
- {
- struct DateStamp REM_ds;
- char REM_string[80];
- };
-
- struct REM reminders[MAX_REMINDERS]; // 0..N reminder dates/times
-
- char month_codes[]="0123456789AB"; // month codes
-
- char *months[]={"JAN","FEB","MAR","APR","MAY","JUN",
- "JUL","AUG","SEP","OCT","NOV","DEC"};
-
- char *days[]={"MON","TUE","WED","THU","FRI","SAT","SUN"};
-
- char *version = "$VER: TimePlanner 1.0 ©LVA 22/JAN/94\n";
-
- FILE *fh; // file handle
-
- BOOL ok;
-
- int i;
-
- //-----------------------------------------------------------------------------
- // outline of program:
- // - from user events file, create a sorted formalized version
- // - process all future events by waiting for event time, then executing action
- //-----------------------------------------------------------------------------
-
- void main (void) {
-
- struct DosBase * libptr;
-
- printf("TimePlanner (TM) V1.0\n");
- printf("Designed and implemented by Laurence Vanhelsuwé © Jan 1993\n\n");
-
- libptr = ( struct DosBase*) OpenLibrary("dos.library", 37); // check for 2.0 OS
-
- if (libptr) {
- CloseLibrary((struct Library*)libptr);
-
- Delay(5*50); // let startup-sequence complete
-
- init_TP(); // initialize things
-
- events_file = load_file("S:EVENTS"); // cache user events file
-
- parse_user_events(); // generate formalised events list
-
- FreeMem(file_buf, file_size); // free user events file buffer
-
- Execute("Sort ENV:ev.raw TO ENV:ev.lst", NULL, NULL); // sort events on date
- DeleteFile("ENV:ev.raw"); // delete unsorted file
-
- handle_events(); // process queue, waiting for right times..
-
- FreeMem(file_buf, file_size); // free events file buffer
- } else {
- printf("TimePlanner needs AmigaDOS 2.0 (V37+)...Sorry!\n");
- }
-
- printf("TimePlanner quitting. Have a nice day.\n");
- }
-
- //-----------------------------------------------------------------------------
- // Initialize program.
- //-----------------------------------------------------------------------------
- void init_TP (void) {
-
- DateStamp(&datestamp);
- ok = DStampToStr(&datestamp, day_str, date_str, time_str);
- printf("Today is %s %s and the time is %s\n", day_str, date_str, time_str);
-
- IntuitionBase = (struct IntuitionBase*) OpenLibrary("intuition.library", 36);
- if (!IntuitionBase) {
- printf("TimePlanner ERROR: No IntuitionBase !\n");
- exit(100);
- }
- }
- //-----------------------------------------------------------------------------
- // Scan through sorted events list, waiting for next event and then executing
- // the required action.
- //
- // Example lines:
- //
- // 1994 0 22 15:55:00 R 5 Minutes to go before...Jonah !|
- // 1994 0 22 16:00:00 N Jonah !|
- //
- //-----------------------------------------------------------------------------
- void handle_events( void ) {
-
- unsigned int delay,offset;
- short day,month,year,hour,minute;
- ULONG tmin_days,tmin_mins,tmin_ticks; // T-minus X days, minutes, ticks
- char ch;
- char * req_msg; // pointer to text for requester
- char * file_end;
- char datestring[20], timestring[20];
- char * batch_file; // don't execute anything
- static char reminder_msg[256];
-
- txt = events_file = load_file("ENV:EV.LST"); // load sorted events file
-
- file_end = txt + file_size; // calculate EOF in buffer
-
- while (txt < file_end) { // while not past EOF
-
- req_msg = NULL;
- batch_file = NULL; // assume no BATCH file
-
- offset = txt - events_file;
- mark_file ("ENV:EV.LST", offset, ">"); // label line we're waiting on
-
- year = get_num(); txt++; // get event date and time...
- month = (*txt) < 'A' ? (*txt)-'0' : (*txt)-'A'+10;
- txt +=2;
- day = get_num(); txt++; // skip space
- hour = get_num(); txt++; // skip COLON
- minute = get_num(); txt++; // skip COLON
- get_num(); // discard seconds field
-
- txt++; // skip space char
-
- ch = *txt++;
-
- switch (ch) {
- case 'N': txt++; // skip space
-
- req_msg = txt; // notif. msg is user's string
-
- while (*txt++ != VERT_BAR) ;
- *(txt-1) = 0; // turn into C-string
-
- if (*txt == LF) break;
-
- txt++; // skip space
- if (*txt++ == 'E') {
- txt++; // skip space
- txt++; // skip filename quote
- batch_file = txt;
- while (*txt != QUOTE) txt++;
- *txt++ = 0; // turn filname into C-string
- }
- break;
-
- case 'R': txt++; // skip space
-
- req_msg = txt;
-
- while (*txt++ != VERT_BAR) ;
- *(txt-1) = 0; // turn into C-string
-
- sprintf(reminder_msg, "EARLY REMINDER MESSAGE:\n\n");
- sprintf(reminder_msg+24, req_msg);
- req_msg = reminder_msg;
-
- break;
-
- case 'E': txt++; // skip space
-
- txt++; // skip filename quote
- batch_file = txt;
- while (*txt != QUOTE) txt++;
- *txt++ = 0; // turn filname into C-string
-
- break;
- }
-
- skip_line(); // discard any comments after command
-
- //printf("READY TO ROLL ? %4d %c %02d %02d:%02d\n", year, month_codes[month], day, hour, minute);
-
- // create date & time strings for submission to StrToDate()
- sprintf(datestring, "%02d-%s-%02d",day, months[month], year-1900);
- sprintf(timestring, "%02d:%02d", hour, minute);
-
- datetime.dat_Format = FORMAT_DOS;
- datetime.dat_StrDate= datestring;
- datetime.dat_StrTime= timestring;
- datetime.dat_Flags = 0;
- StrToDate(&datetime);
-
- DateStamp(&datestamp); // find out what day/time is now
-
- if (hour == 0 && minute == 0) { // if event doesn't specify a time
- // check whether event happens today
- if (datestamp.ds_Days == datetime.dat_Stamp.ds_Days)
- do_event(req_msg, batch_file);
- }
-
- // check whether event is in past or future
- if (CompareDates(&datestamp, &datetime.dat_Stamp) > 0) {
-
- // calculate # of Ticks before event fires.
- tmin_days = datetime.dat_Stamp.ds_Days - datestamp.ds_Days;
- tmin_mins = datetime.dat_Stamp.ds_Minute - datestamp.ds_Minute;
- tmin_ticks = datetime.dat_Stamp.ds_Tick - datestamp.ds_Tick;
-
- printf ("%d Days, %d Minutes and %d Ticks to event!\n",
- tmin_days, tmin_mins, tmin_ticks);
- delay = tmin_ticks + MINUTETICKS*tmin_mins + DAYTICKS*tmin_days;
-
- printf("Delay(%d) !\n", delay);
- Delay(delay); // Sleep until event should happen
-
- do_event(req_msg, batch_file); // BANG !!
- }
-
- mark_file ("ENV:EV.LST", offset, " "); // label line as "processed"
-
- } // WHILE NOT EOF
- }
- //-----------------------------------------------------------------------------
- // An event's time has come... do it !
- //-----------------------------------------------------------------------------
-
- void do_event( char *req_msg, char *batch_file) {
- char choice;
- BOOL batch_ok;
- char str[256];
-
-
- batch_ok = TRUE; // assume no problems with batch file.
-
- if (req_msg) {
- if (batch_file) {
- choice = popup_requester(req_msg, BUT_OK|BUT_CANCEL); // Notify user of event.
- if (choice==BUT_OK)
- batch_ok = Execute(batch_file, NULL, NULL);
- } else {
- choice = popup_requester(req_msg, BUT_OK);
- }
- } else if (batch_file)
- batch_ok = Execute(batch_file, NULL, NULL);
-
- if(! batch_ok) {
- sprintf(str, "Batch file '%s' couldn't be started !\n", batch_file);
- popup_requester(str, BUT_OK);
- }
- }
-
- //-----------------------------------------------------------------------------
- //-----------------------------------------------------------------------------
- // Parse USER events file and generate a formal list to be sorted on date.
- //-----------------------------------------------------------------------------
- void parse_user_events( void ) {
-
- char firstchar;
- char * file_end;
-
- fh = fopen("ENV:ev.raw","w"); // create formal file
-
- txt = events_file; // starting from start of user file...
-
- file_end = txt + file_size; // calculate EOF in buffer
-
- while (txt < file_end) {
-
- // print_line(); **!! DEBUG
-
- firstchar = *txt;
-
- switch (firstchar) {
- case SEMICOLON:
- case ASTERISK: skip_line(); break; // skip comment lines
-
- case LF: txt++; break; // skip empty lines
-
- case TAB:
- case SPACE: skip_whitespc(); break; // skip leading indentation
-
- default: process_line(); // process command
- skip_line(); break; // and discard any comments
- }
- }
-
- fclose(fh);
- }
- //-----------------------------------------------------------------------------
- // Process a line from the USER events file.
- // Output one/many formalised version(s) for later interpretation.
- //-----------------------------------------------------------------------------
- void process_line(void) {
-
- short actions=1; // default number of events
- char *notify_str, *execute_str;
- short year,month,day;
-
-
- // Look for the start of a TimePlanner Event entry.
- // These all start with an "ON ...." line
-
- if (is_string(txt,"ON")) {
-
- notify_str = execute_str = NULL; // invalidate str ptrs.
-
- process_on_line(); // get date-time as Ticks values
- skip_line(); skip_whitespc(); // goto next line (junk poss. comment)
-
- // Now check for any of the possible actions to do ON that date/time
- // First a possible REMINDer line.
-
- if (is_string(txt,"REMIND")) {
- actions = process_remind_line();
- skip_line(); // trash any remainder
- skip_whitespc(); // skip indent on next line
- }
-
- // Then the NOTIFY line.
-
- if (is_string(txt,"NOTIFY")) {
-
- txt += 6; skip_whitespc();
-
- if(*txt != QUOTE) {
- printf("No QUOTE after NOTIFY Command.\n");
- exit(10);
- }
-
- notify_str = ++txt; // notify str starts past quote
-
- while (*txt != LF) // find end of notify string
- txt++;
-
- *txt = 0; // turn notif. str to C-string
-
- if (*(txt-1) == QUOTE)
- *(txt-1) = 0; // and strip closing quote if present
-
- txt++; // goto next line
- skip_whitespc(); // skip indent on next line
- }
-
- // Then an optional EXECUTE line.
-
- if (is_string(txt,"EXECUTE")) {
- txt += 7; skip_whitespc(); // skip white space between cmd and arg
-
- if(*txt != QUOTE) {
- printf("No QUOTE after EXECUTE Command.\n");
- exit(10);
- }
-
- execute_str = txt; // execute str INCLUDES quote
-
- while (*txt != LF) // find end of notify string
- txt++;
-
- *txt = 0; // turn notif. str to C-string
- skip_whitespc(); // skip indent on next line
- }
-
- // If there's no NOTIFY or EXECUTE to do for this Event : ERROR !
-
- if (notify_str == NULL && execute_str == NULL) {
- printf("Error: ON line not followed by any action!... Aborting.\n");
- exit(10);
- }
-
- // Now write out N lines (1 minimum, more if reminders)
-
- for (i=0; i<actions; i++) {
-
- ok = DStampToStr(&reminders[i].REM_ds, day_str, date_str, time_str);
- // printf("Reminder[%d] = %s %s %s ", i, day_str, date_str, time_str);
-
- year = 1900 + 10*(date_str[7]-'0') + date_str[8] -'0';
- month = month_from_str(date_str+3);
- day = 10*(date_str[0]-'0') + date_str[1] - '0';
-
- // printf("OUTPUT: %4d %c %02d %s\n", year, month_codes[month], day, time_str);
- fprintf(fh, "%4d %c %02d %s", year, month_codes[month], day, time_str);
-
- if (notify_str) {
-
- if (i==0) {
- fprintf( fh, " N "); // the first one is main event
- } else {
- fprintf( fh, " R "); // all others are reminders
- fprintf( fh, reminders[i].REM_string); // incl. CR
- }
-
- fprintf( fh, notify_str);
- fprintf( fh, "|"); // VERT_BAR ends notify str.
-
- if (execute_str && i==0) {
- fprintf( fh, " E ");
- fprintf( fh, execute_str);
- }
- } else {
- if (execute_str) {
- fprintf( fh, " E ");
- fprintf( fh, execute_str);
- }
- }
-
- fprintf( fh, "\n"); // terminate line in generated file
- } // FOR
-
- } // IF (IS_STRING("ON"))
- } // end of function
- //-----------------------------------------------------------------------------
- // Process a line starting with the "ON" keyword.
- // e.g.
- // ON TUE 16-JAN-93 AT 12:00
- // ON 22-MAR-87 AT 22:00
- // ON 01-JAN-99
- //-----------------------------------------------------------------------------
- void process_on_line (void) {
-
- short day,month,year;
- short hour=0,minute=0; // default time specification 00:00 (invalid)
- char ch;
- char *datestr, *timestr;
-
- txt += 2; skip_whitespc(); // skip over "ON" chars
-
- if (get_dayname() != -1) { // skip over optional day name
- txt += 3;
- }
- skip_whitespc();
-
- datestr = txt; // remember where date starts.
-
- day = get_num(); // parse date DD-MMM-YYYY
- if (day<1 || day >31) printf("Error in day number in ON statement.\n");
- if (*txt++ != '-') printf("Error: no hyphen after day number.\n");
-
- month = get_month(); // JAN, FEB, MAR, ...
- if (month == -1) printf("Error in month in ON statement.\n");
- if (*txt++ != '-') printf("Error: no hyphen after month.\n");
-
- year = get_num();
- if (year <1978 || year> 2077) printf("Error in year in ON statement.\n");
- ch = *txt;
-
- datestr[7] = datestr[9]; // turn -YYYY into -YY format
- datestr[8] = datestr[10];
- datestr[9] = 0; // turn date into C-string
-
- skip_whitespc();
-
- ch = *txt; timestr = NULL;
- if (ch != LF) { // check optional time specification
- if (is_string(txt,"AT")) {
- txt += 2; skip_whitespc(); // skip over "AT" chars
-
- timestr = txt; // remember where timestr starts
-
- hour = get_num(); if(hour<0 || hour>23) printf("Error in hour\n");
- if (*txt++ != COLON) printf("Error: no colon in time.\n");
-
- minute = get_num(); if (minute<0 || minute >59) printf("Error in minutes\n");
-
- ch = timestr[5]; // remember what we're overwriting
- timestr[5] = 0; // turn time into C-string
- }
- }
-
- datetime.dat_Format = FORMAT_DOS;
- datetime.dat_StrDate= datestr;
- datetime.dat_StrTime= timestr;
- datetime.dat_Flags = 0;
- StrToDate(&datetime);
-
- if (!timestr) { // if no time was specified:
- datetime.dat_Stamp.ds_Minute = 0; // set time to 00:00
- datetime.dat_Stamp.ds_Tick = 0;
- } else {
- timestr[5] = ch; // restore char in line
- }
-
- for (i=0; i< MAX_REMINDERS; i++) {
- reminders[i].REM_ds = datetime.dat_Stamp;
- }
- }
- //-----------------------------------------------------------------------------
- // Process a line starting with the "REMIND" keyword.
- // e.g. REMIND MONTH, DAY, HOUR, x DAYS, x HOURS, x MINUTES
- //-----------------------------------------------------------------------------
- short process_remind_line (void) {
-
- short times=1; // array index
- int units;
- BOOL theres_more=TRUE;
-
- txt += 6; skip_whitespc(); // skip over "REMIND" chars
-
- while (theres_more && times < MAX_REMINDERS) {
-
- if (is_string(txt, "MONTH")) {
- strcpy( reminders[times].REM_string, "1 Month to go before...");
- adjust ( &reminders[times++].REM_ds, MONTHTICKS);
- txt += 5; // skip over "MONTH" keyword
-
- if (*txt == COMMA) { // check if there are more
- txt++; skip_whitespc(); // reminders
- } else {
- theres_more = FALSE;
- continue; // goto WHILE
- }
- }
-
- if (is_string(txt, "DAY")) {
- strcpy( reminders[times].REM_string, "1 Day to go before...");
- adjust ( &reminders[times++].REM_ds, DAYTICKS);
- txt += 3; // skip over "DAY" keyword
-
- if (*txt == COMMA) {
- txt++; skip_whitespc();
- } else {
- theres_more = FALSE;
- continue;
- }
- }
-
- if (is_string(txt, "HOUR")) {
- strcpy( reminders[times].REM_string, "1 Hour to go before...");
- adjust ( &reminders[times++].REM_ds, HOURTICKS);
- txt += 4; // skip over "HOUR" keyword
-
- if (*txt == COMMA) {
- txt++; skip_whitespc();
- } else {
- theres_more = FALSE;
- continue;
- }
- }
-
- if (units = get_num()) { // if a non-zero number at the start
- skip_whitespc(); // skip over space
-
- if (is_string(txt, "DAYS")) {
- sprintf(reminders[times].REM_string, "%d Days to go before...", units);
- adjust ( &reminders[times++].REM_ds, DAYTICKS * units);
- txt += 4; skip_whitespc();
-
- if (*txt == COMMA) {
- txt++; skip_whitespc();
- } else {
- theres_more = FALSE;
- continue;
- }
- }
-
- if (is_string(txt, "HOURS")) {
- sprintf(reminders[times].REM_string, "%d Hours to go before...", units);
- adjust ( &reminders[times++].REM_ds, HOURTICKS * units);
- txt += 5; skip_whitespc();
-
- if (*txt == COMMA) {
- txt++; skip_whitespc();
- } else {
- theres_more = FALSE;
- continue;
- }
- }
-
- if (is_string(txt, "MINUTES")) {
- sprintf(reminders[times].REM_string, "%d Minutes to go before...", units);
- adjust ( &reminders[times++].REM_ds, MINUTETICKS * units);
- txt += 7; skip_whitespc();
-
- if (*txt == COMMA) {
- txt++; skip_whitespc();
- } else {
- theres_more = FALSE;
- continue;
- }
- }
- } // IF (GETNUM)
- } // WHILE
-
- return times; // tell how many reminders to generate
- }
- //-----------------------------------------------------------------------------
- // Modify a DateStamp by a number of Ticks.
- //-----------------------------------------------------------------------------
- void adjust (struct DateStamp *ds, ULONG ticks) {
-
- int dd,dm,dt;
-
- //printf("Ticks to adjust: %d\n", ticks);
- //printf("BEFORE ADJUSTED Days, Minutes, Ticks: %d, %d, %d\n",
- // ds->ds_Days, ds->ds_Minute, ds->ds_Tick);
-
- dd = ticks/DAYTICKS; ticks = ticks%DAYTICKS;
- dm = ticks/MINUTETICKS;
- dt = ticks%MINUTETICKS;
-
- //printf("Adjustments: %d, %d, %d\n", dd, dm, dt);
-
- ds->ds_Days -= dd;
- ds->ds_Minute -= dm;
- ds->ds_Tick -= dt;
-
- if(ds->ds_Tick < 0) {
- ds->ds_Tick += MINUTETICKS; // make positive again
- ds->ds_Minute--; // but adjust minutes
- }
-
- if (ds->ds_Minute < 0) {
- ds->ds_Minute += 60*60*24;
- ds->ds_Days--;
- }
- //printf("ADJUSTED Days, Minutes, Ticks: %d, %d, %d\n",
- // ds->ds_Days, ds->ds_Minute, ds->ds_Tick);
- }
- //-----------------------------------------------------------------------------
- // Get an embedded decimal number. Return 0 if no number !
- //-----------------------------------------------------------------------------
- short get_num(void) {
-
- short num=0;
-
- while( *txt>= '0' && *txt <='9') {
- num *=10;
- num = num + (*txt -'0');
- txt++;
- }
- return num;
- }
- //-----------------------------------------------------------------------------
- // Find month code from 3-letter string. (Jan,Feb,Mar,Apr,May,...)
- //-----------------------------------------------------------------------------
- short month_from_str(char *month) {
- short code;
-
- code= *month + *(month+1) + *(month+2); // fold string into unique num
-
- switch (code) {
- case 281: return 0; case 269: return 1; case 288: return 2;
- case 291: return 3; case 295: return 4; case 301: return 5;
- case 299: return 6; case 285: return 7; case 296: return 8;
- case 294: return 9; case 307: return 10;case 268: return 11;
- }
- }
- //-----------------------------------------------------------------------------
- // Check text for JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG, SEP, OCT, NOV, DEC.
- //-----------------------------------------------------------------------------
- short get_month(void) {
-
- for (i=0; i< NUM_MONTHS; i++) {
- if (is_string(txt, months[i])) { // check against array of names
- txt += 3;
- return (short) i;
- }
- }
-
- return -1; // return negative if not found
- }
- //-----------------------------------------------------------------------------
- // Check text for MON, TUE, WED, THU, FRI, SAT, SUN word.
- //-----------------------------------------------------------------------------
- short get_dayname(void) {
-
- for (i=0; i< NUM_DAYS; i++) {
- if (is_string(txt, days[i])) {
- return (short) i;
- }
- }
-
- return -1; // return negative if not found
- }
- //-----------------------------------------------------------------------------
- // Compare C-string with embedded word in text.
- //-----------------------------------------------------------------------------
- BOOL is_string( char * text, char * string) {
-
- char *ptr;
- char ch;
- int result;
-
- ptr = text; // remember start of string
-
- ch = *text++;
- while (ch != SPACE && ch != TAB && ch != HYPHEN &&
- ch != COMMA && ch != LF ) // find end of word.
- ch = *text++;
-
- *(text-1) = 0; // null-terminate word
- result = strcmp (ptr, string); // do the comparison
- *(text-1) = ch; // put original separator back.
-
- return (BOOL) ! result;
- }
- //-----------------------------------------------------------------------------
- // Printf the LF-terminated line at *txt. (txt is global text pointer)
- //-----------------------------------------------------------------------------
- void print_line(void) {
-
- char *ptr;
- char achar;
-
- ptr = txt;
-
- while(*ptr++ != LF) {}
-
- achar = *ptr; *ptr = 0; printf("%s", txt); *ptr = achar;
- }
- //-----------------------------------------------------------------------------
- // Discard rest of line, incl. LF.
- //-----------------------------------------------------------------------------
- void skip_line(void){
-
- while (*txt++ != LF) {}
- }
- //-----------------------------------------------------------------------------
- // Skip TABs and SPACEs in buffer.
- //-----------------------------------------------------------------------------
- void skip_whitespc(void) {
-
- while (*txt == TAB || *txt == SPACE)
- txt++;
- }
- //-----------------------------------------------------------------------------
- // Create day, date and time strings from a DateStamp
- //-----------------------------------------------------------------------------
- BOOL DStampToStr( struct DateStamp *ds, char *daystr, char *datestr, char *timestr) {
-
- datetime.dat_Stamp = *ds; // copy DateStamp struct
-
- datetime.dat_StrDay = daystr; // set string pointers
- datetime.dat_StrDate = datestr;
- datetime.dat_StrTime = timestr;
-
- datetime.dat_Format = FORMAT_DOS; // std format
-
- return (BOOL) DateToStr(&datetime);
- }
- //-----------------------------------------------------------------------------
- // Cache a specified file.
- // OUTPUTS: file_size, file_buf
- //-----------------------------------------------------------------------------
- char *load_file( char * filename) {
-
- ULONG bytes;
- BPTR fh;
-
- fh = Open(filename, MODE_OLDFILE);
-
- Seek(fh, 0L, OFFSET_END); // Seek to end of file
- bytes = Seek(fh, 0L, OFFSET_BEGINNING); // figure out size of file
- if (!bytes) {
- printf("File '%s' is empty ! Ouch... (ABORTING)\n", filename);
- exit(10);
- }
-
- file_buf = AllocMem(bytes, MEMF_CLEAR); // get a buffer for it
-
- if (!file_buf) {
- printf("No buffer for cacheing file '%s'! ABORTING...\n", filename);
- exit(10);
- }
-
- file_size = bytes;
- bytes = Read(fh, file_buf, bytes);
-
- if (bytes != file_size) {
- printf("Read error when reading file '%s'!\n", filename);
- FreeMem(file_buf, file_size); // free events file buffer
- exit(10);
- }
-
- Close(fh);
-
- return file_buf;
- }
- //-----------------------------------------------------------------------------
- //
- //-----------------------------------------------------------------------------
- void mark_file ( char * file, int position, char * tag) {
-
- BPTR fh;
-
- fh = Open(file, MODE_OLDFILE);
- if (fh) {
- Seek(fh, position, OFFSET_BEGINNING);
- Write(fh, tag, 1);
- Close(fh);
- }
- }
- //-----------------------------------------------------------------------------
- // Popup a nice information requester with a single "OK" button.
- //-----------------------------------------------------------------------------
-
- char popup_requester( char *msg, char buttons) {
-
- int gadg;
-
- static struct EasyStruct EventES = {
- sizeof (struct EasyStruct),
- 0,
- "TimePlanner Event Notification",
- NULL,
- "OK",
- };
-
- EventES.es_GadgetFormat = "OK"; // assume simplest buttons
-
- if (buttons & BUT_CANCEL) {
- EventES.es_GadgetFormat = "Do it.|Skip!"; //
- }
-
- EventES.es_TextFormat = msg;
-
- gadg = EasyRequest(IntuitionBase->ActiveWindow, &EventES, NULL);
- switch (gadg) {
- case 1: return BUT_OK; // user clicked on 'OK'
-
- case 0: if (buttons & BUT_CANCEL)
- return BUT_CANCEL; // user clicked on 'CANCEL'
- else
- return BUT_OK;
- }
- }
- //-----------------------------------------------------------------------------
-