home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume17 / calentool / part20 / mt2ct.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-07  |  3.3 KB  |  152 lines

  1. /*
  2.  * $Header: mt2ct.c,v 2.3 91/02/01 12:20:25 billr Exp $
  3.  */
  4. /*
  5.  * mt2ct - convert monthtool reminder 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 "ct.h"
  17. #include <stdio.h>
  18. #include <ctype.h>
  19.  
  20. struct appt_entry appts;
  21. struct appt_entry *aptr;
  22. char filename[128], *file;
  23. FILE *fp;
  24.  
  25. extern char *getenv();
  26.  
  27. main(argc, argv)
  28. int argc;
  29. char *argv[];
  30. {
  31.     if (argc > 1)
  32.         file = argv[1];
  33.     else {
  34.         strcpy(filename, getenv("HOME"));
  35.         strcat(filename, "/.monthtool");
  36.         file = filename;
  37.     }
  38.  
  39.     if ((fp = fopen(file, "r")) == NULL) {
  40.         fprintf(stderr, "can't open monthtool file <%s> for reading\n", file);
  41.         exit(1);
  42.     }
  43.     if (!read_mt_file()) {
  44.         fprintf(stderr, "no reminders read from %s\n", file);
  45.         exit(1);
  46.     }
  47.     fclose(fp);
  48.     strcpy(filename, getenv("HOME"));
  49.     strcat(filename, "/.appointments");
  50.     if ((fp = fopen(filename, "w")) == NULL) {
  51.         fprintf(stderr, "can't open .appointments file for writing\n");
  52.         exit(1);
  53.     }
  54.     write_ct_file();
  55. }
  56.  
  57. /*
  58.  * read in the monthtool file, filling the appts records
  59.  */
  60. int
  61. read_mt_file()
  62. {
  63.     char *ptr, *fgets();
  64.     char buf[512];
  65.     struct appt_entry *optr;
  66.  
  67.     aptr = &appts;
  68.     while (fgets(buf, 512, fp) != NULL) {
  69.         aptr->flags = aptr->repeat = aptr->lookahead = 0;
  70.         aptr->warn = 10;  /* default */
  71.         aptr->sindex = 0;
  72.         aptr->next = NULL;
  73.         ptr = buf;
  74.         while (isspace(*ptr))
  75.             ++ptr;
  76.         aptr->month = *ptr++ - '0';
  77.         if (isdigit(*ptr))
  78.             aptr->month = aptr->month * 10 + (*ptr++ - '0');
  79.         ++ptr;    /* skip ',' */
  80.         aptr->day = *ptr++ - '0';
  81.         if (isdigit(*ptr))
  82.             aptr->day = aptr->day * 10 + (*ptr++ - '0');
  83.         ++ptr;    /* skip ',' */
  84.         aptr->year = 0;
  85.         while (isdigit(*ptr))
  86.             aptr->year = aptr->year * 10 + (*ptr++ - '0');
  87.         if (aptr->year > 1900)
  88.             aptr->year -= 1900;
  89.         ++ptr;    /* skip ',' */
  90.         if (!strncmp(ptr, "    ", 4)) {
  91.             aptr->flags |= A_NOTE;
  92.             ptr += 5;
  93.         } else {
  94.             aptr->hour = (*ptr++ - '0') * 10;
  95.             aptr->hour += *ptr++ - '0';
  96.             aptr->minute = (*ptr++ - '0') * 10;
  97.             aptr->minute += *ptr++ - '0';
  98.             if (aptr->minute < 15)
  99.                 aptr->minute = 0;
  100.             else if (aptr->minute < 45)
  101.                 aptr->minute = 30;
  102.             else {
  103.                 aptr->minute = 0;
  104.                 aptr->hour++;
  105.             }
  106.             ++ptr;    /* skip ',' */
  107.         }
  108.         strcpy(aptr->str, ptr);
  109.         if (aptr->year == 0)
  110.             aptr->flags |= ALL_YEARS;
  111.         if (aptr->month == 0)
  112.             aptr->flags |= ALL_MONTHS;
  113.         else if (aptr->month == 99) {
  114.             /* weekly reminder */
  115.             aptr->flags |= ALL_MONTHS;
  116.             aptr->flags |= Setday(aptr->day-1);
  117.         } else
  118.             --aptr->month;
  119.         if (aptr->day == 0)
  120.             aptr->flags |= ALL_DAYS;
  121.         aptr->next = (struct appt_entry *)malloc(sizeof(struct appt_entry));
  122.         if (aptr->next == NULL) {
  123.             fprintf(stderr, "out of memory\n");
  124.             return;
  125.         }
  126.         optr = aptr;
  127.         aptr = aptr->next;
  128.     }
  129.     if (aptr == &appts)
  130.         return(0);    /* nothing read */
  131.     /* don't need the last one */
  132.     free(aptr);
  133.     optr->next = NULL;
  134.     return(1);
  135. }
  136.  
  137. /*
  138.  * write out the new .appointments file
  139.  */
  140. write_ct_file()
  141. {
  142.     aptr = &appts;
  143.     fputs(HEADER, fp);
  144.     while (aptr) {
  145.         if (put_aentry(fp, aptr)) {
  146.             fprintf(stderr, "error writing .appointments file\n");
  147.             return;
  148.         }
  149.         aptr = aptr->next;
  150.     }
  151. }
  152.