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

  1. /*
  2.  * $Header: mt2ct.c,v 2.1 89/05/09 14:19:31 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 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->sindex = 0;
  71.         aptr->next = NULL;
  72.         ptr = buf;
  73.         while (isspace(*ptr))
  74.             ++ptr;
  75.         aptr->month = *ptr++ - '0';
  76.         if (isdigit(*ptr))
  77.             aptr->month = aptr->month * 10 + (*ptr++ - '0');
  78.         ++ptr;    /* skip ',' */
  79.         aptr->day = *ptr++ - '0';
  80.         if (isdigit(*ptr))
  81.             aptr->day = aptr->day * 10 + (*ptr++ - '0');
  82.         ++ptr;    /* skip ',' */
  83.         aptr->year = 0;
  84.         while (isdigit(*ptr))
  85.             aptr->year = aptr->year * 10 + (*ptr++ - '0');
  86.         if (aptr->year > 1900)
  87.             aptr->year -= 1900;
  88.         ++ptr;    /* skip ',' */
  89.         if (!strncmp(ptr, "    ", 4)) {
  90.             aptr->flags |= A_NOTE;
  91.             ptr += 5;
  92.         } else {
  93.             aptr->hour = (*ptr++ - '0') * 10;
  94.             aptr->hour += *ptr++ - '0';
  95.             aptr->minute = (*ptr++ - '0') * 10;
  96.             aptr->minute += *ptr++ - '0';
  97.             if (aptr->minute < 15)
  98.                 aptr->minute = 0;
  99.             else if (aptr->minute < 45)
  100.                 aptr->minute = 30;
  101.             else {
  102.                 aptr->minute = 0;
  103.                 aptr->hour++;
  104.             }
  105.             ++ptr;    /* skip ',' */
  106.         }
  107.         strcpy(aptr->str, ptr);
  108.         if (aptr->year == 0)
  109.             aptr->flags |= ALL_YEARS;
  110.         if (aptr->month == 0)
  111.             aptr->flags |= ALL_MONTHS;
  112.         if (aptr->day == 0)
  113.             aptr->flags |= ALL_DAYS;
  114.         if (aptr->month == 99) {
  115.             /* weekly reminder */
  116.             aptr->flags |= ALL_MONTHS;
  117.             aptr->flags |= Setday(aptr->day-1);
  118.         }
  119.         aptr->next = (struct appt_entry *)malloc(sizeof(struct appt_entry));
  120.         if (aptr->next == NULL) {
  121.             fprintf(stderr, "out of memory\n");
  122.             return;
  123.         }
  124.         optr = aptr;
  125.         aptr = aptr->next;
  126.     }
  127.     if (aptr == &appts)
  128.         return(0);    /* nothing read */
  129.     /* don't need the last one */
  130.     free(aptr);
  131.     optr->next = NULL;
  132.     return(1);
  133. }
  134.  
  135. /*
  136.  * write out the new .appointments file
  137.  */
  138. write_ct_file()
  139. {
  140.     aptr = &appts;
  141.     fputs(HEADER, fp);
  142.     while (aptr) {
  143.         if (put_aentry(fp, aptr)) {
  144.             fprintf(stderr, "error writing .appointments file\n");
  145.             return;
  146.         }
  147.         aptr = aptr->next;
  148.     }
  149. }
  150.