home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / sun / volume1 / calentool / part05 / month2ct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-27  |  2.9 KB  |  122 lines

  1. /*
  2.  * $Header: month2ct.c,v 2.1 89/05/09 14:19:14 billr Exp $
  3.  */
  4. /*
  5.  * month2ct - convert month schedule files to calentool style files
  6.  *
  7.  * Author: Bill Randle, Tektronix, Inc. <billr@saab.CNA.TEK.COM>
  8.  *
  9.  * Copyright (C) 1989 Tektronix, Inc.  All Rights Reserved
  10.  *
  11.  * Permission is hereby granted to use and modify this code in source
  12.  * or binary form as long as it is not sold for profit and this copyright
  13.  * notice remains intact.
  14.  */
  15.  
  16. #include "month.h"
  17. #include "ct.h"
  18. #include <stdio.h>
  19.  
  20. struct appt_entry appt;
  21. struct event_rec events;
  22. FILE *fp;
  23. char filename[128];
  24. char *dir, *getenv();
  25.  
  26. main(argc, argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.     if (argc > 1)
  31.         dir = argv[1];
  32.     else {
  33.         strcpy(filename, getenv("HOME"));
  34.         dir = filename;
  35.     }
  36.  
  37.     if (read_schedule(dir, READ_ONLY)) {
  38.         fprintf(stderr, "no reminders read from %s/.month\n", dir);
  39.         exit(1);
  40.     }
  41.     strcpy(filename, getenv("HOME"));
  42.     strcat(filename, "/.appointments");
  43.     if ((fp = fopen(filename, "w")) == NULL) {
  44.         fprintf(stderr, "can't open .appointments file for writing\n");
  45.         exit(1);
  46.     }
  47.     write_ct_file();
  48. }
  49.  
  50. /*
  51.  * write out the new .appointments file
  52.  */
  53. write_ct_file()
  54. {
  55.     struct event_rec *evt;
  56.     int length, oflags, i;
  57.  
  58.     evt = &events;
  59.     fputs(HEADER, fp);
  60.     /* first event is empty */
  61.     evt = evt->next_event;
  62.     while (evt) {
  63.         fprintf(stderr,"evt struct:\n  mly=%d, yly=%d, evry=%d\n  nth=%d, last=%d, nthon=%d\n  str=%s\n",
  64.             evt->monthly, evt->yearly, evt->every, evt->nth, evt->last, evt->nth_is_on, evt->event_string);
  65.         appt.flags = appt.repeat = appt.lookahead = 0;
  66.         appt.year = evt->start_date.year - 1900;
  67.         appt.month = evt->start_date.month - 1;
  68.         appt.day = evt->start_date.day;
  69.         strcpy(appt.str, evt->event_string);
  70.         if (evt->monthly)
  71.             appt.flags |= ALL_MONTHS;
  72.         if (evt->yearly)
  73.             appt.flags |= ALL_YEARS;
  74.         appt.hour = evt->start_time.hour;
  75.         appt.minute = evt->start_time.minute;
  76.         if (appt.hour > 23 || appt.hour < 0 || appt.minute > 59 || appt.minute < 0)
  77.             appt.flags |= A_NOTE;
  78.         if (appt.minute < 15)
  79.             appt.minute = 0;
  80.         else if (appt.minute < 45)
  81.             appt.minute = 30;
  82.         else {
  83.             appt.minute = 0;
  84.             appt.hour++;
  85.         }
  86.         length = evt->duration.hour * 60 + evt->duration.minute;
  87.         appt.arrows = length / 30 - 1;
  88.         if (appt.arrows < 0)
  89.             appt.arrows = 0;
  90.         if (evt->anti)
  91.             appt.flags |= DELETED;
  92.         if (evt->warning.hour >= 24)
  93.             appt.lookahead = evt->warning.hour / 24;
  94.         if (evt->every) {
  95.             /* event occurs on an every something */
  96.             appt.flags |= REPEAT;
  97.             if (evt->last)
  98.                 appt.repeat = LAST_WEEK;
  99.             else if (evt->nth_is_on)
  100.                 appt.repeat = 1<<(evt->nth_is_on-1);
  101.             else
  102.                 appt.repeat = ALL_WEEKS;
  103.             oflags = appt.flags;
  104.             for (i=0; i<7; i++) {
  105.                 if (evt->smtwtfs[i]) {
  106.                     appt.flags = oflags | Setday(i);
  107.                     if (put_aentry(fp, &appt)) {
  108.                         fprintf(stderr, "error writing .appointments file\n");
  109.                         return;
  110.                     }
  111.                 }
  112.             }
  113.         } else
  114.             if (put_aentry(fp, &appt)) {
  115.                 fprintf(stderr, "error writing .appointments file\n");
  116.                 return;
  117.             }
  118.         fputs("\n", fp);
  119.         evt = evt->next_event;
  120.     }
  121. }
  122.