home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / calentool / part16 / month2ct.c < prev    next >
C/C++ Source or Header  |  1991-04-06  |  4KB  |  152 lines

  1. /*
  2.  * $Header: month2ct.c,v 2.2 91/02/01 12:20:10 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, 1991 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.         /* DEBUG
  64.         fprintf(stderr,"evt struct:\n  mly=%d, yly=%d, evry=%d\n  nth=%d, last=%d, nthon=%d\n  str=%s\n",
  65.             evt->monthly, evt->yearly, evt->every, evt->nth, evt->last, evt->nth_is_on, evt->event_string);
  66.         */
  67.         appt.flags = appt.repeat = appt.lookahead = 0;
  68.         appt.warn = 10;  /* default */
  69.         appt.year = evt->start_date.year - 1900;
  70.         appt.month = evt->start_date.month - 1;
  71.         appt.day = evt->start_date.day;
  72.         strcpy(appt.str, evt->event_string);
  73.         if (evt->monthly)
  74.             appt.flags |= ALL_MONTHS;
  75.         if (evt->yearly)
  76.             appt.flags |= ALL_YEARS;
  77.         appt.hour = evt->start_time.hour;
  78.         appt.minute = evt->start_time.minute;
  79.         if (appt.hour > 23 || appt.hour < 0 || appt.minute > 59 || appt.minute < 0)
  80.             appt.flags |= A_NOTE;
  81.         if (appt.minute < 15)
  82.             appt.minute = 0;
  83.         else if (appt.minute < 45)
  84.             appt.minute = 30;
  85.         else {
  86.             appt.minute = 0;
  87.             appt.hour++;
  88.         }
  89.         length = evt->duration.hour * 60 + evt->duration.minute;
  90.         appt.arrows = length / 30 - 1;
  91.         if (appt.arrows < 0)
  92.             appt.arrows = 0;
  93.         if (evt->anti)
  94.             appt.flags |= DELETED;
  95.         if (evt->warning.hour >= 24)
  96.             appt.lookahead = evt->warning.hour / 24;
  97.         else
  98.             appt.warn = evt->warning.hour * 60 + evt->warning.minute;
  99.         if (evt->until) {
  100.             fprintf(stderr, "this appointment runs more than 1 day,\n");
  101.             fprintf(stderr, "modify the calentool entry after conversion:\n");
  102.             fprintf(stderr, "start date (m/d/y): %d/%d/%d, ",
  103.               evt->start_date.month, evt->start_date.day,
  104.               evt->start_date.year);
  105.             fprintf(stderr, "start time: %d:%d, ", evt->start_time.hour,
  106.               evt->start_time.minute);
  107.             fprintf(stderr, "runs until (m/d/y): %d/%d/%d\n",
  108.               evt->until_date.month, evt->until_date.day,
  109.               evt->until_date.year);
  110.         }
  111.         if (evt->every) {
  112.             /* event occurs on an every something */
  113.             appt.flags |= REPEAT;
  114.             if (evt->last)
  115.                 appt.repeat = LAST_WEEK;
  116.             else if (evt->nth_is_on)
  117.                 appt.repeat = 1<<(evt->nth_is_on-1);
  118.             else
  119.                 appt.repeat = ALL_WEEKS;
  120.             oflags = appt.flags;
  121.             /* check for mon-fri */
  122.             if (!evt->smtwtfs[0] && !evt->smtwtfs[6] &&
  123.                 evt->smtwtfs[1] && evt->smtwtfs[2] &&
  124.                 evt->smtwtfs[3] && evt->smtwtfs[4] &&
  125.                 evt->smtwtfs[5]) {
  126.                 appt.flags |= EVERY_MON_FRI;
  127.                 if (put_aentry(fp, &appt)) {
  128.                     fprintf(stderr,
  129.                       "error writing .appointments file\n");
  130.                     return;
  131.                 }
  132.             } else
  133.                 for (i=0; i<7; i++) {
  134.                     if (evt->smtwtfs[i]) {
  135.                         appt.flags = oflags | Setday(i);
  136.                         if (put_aentry(fp, &appt)) {
  137.                             fprintf(stderr,
  138.                               "error writing .appointments file\n");
  139.                             return;
  140.                         }
  141.                     }
  142.                 }
  143.         } else
  144.             if (put_aentry(fp, &appt)) {
  145.                 fprintf(stderr, "error writing .appointments file\n");
  146.                 return;
  147.             }
  148.         fputs("\n", fp);
  149.         evt = evt->next_event;
  150.     }
  151. }
  152.