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

  1. /*
  2.  * $Header: put_aentry.c,v 2.1 89/05/09 14:19:36 billr Exp $
  3.  */
  4. /*
  5.  * put_aentry - write 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. /*
  21.  * put entry into appointments file
  22.  */
  23. int
  24. put_aentry(apts_file, appt)
  25. FILE *apts_file;
  26. struct appt_entry *appt;
  27. {
  28.     char *to_str();
  29.     char inbuf[512];
  30.     int one_based = 1;
  31.  
  32.     if (appt->flags & READONLY)
  33.         /* don't copy include file entries */
  34.         /* (the include directive is copied as a comment) */
  35.         return(0);
  36.     if (appt->flags & A_COMMENT) {
  37.         fputs(inbuf, apts_file);
  38.         return(ferror(apts_file));
  39.     }
  40.     if (appt->flags & ALL_YEARS)
  41.         fputs("** ", apts_file);
  42.     else if (appt->year > 99)
  43.         fprintf(apts_file, "%03d ", appt->year);
  44.     else
  45.         fprintf(apts_file, "%02d ", appt->year);
  46.     if (appt->flags & ALL_MONTHS)
  47.         fputs("** ", apts_file);
  48.     else
  49.         fprintf(apts_file, "%02d ", one_based ? appt->month+1 : appt->month);
  50.     if (appt->flags & ALL_DAYS)
  51.         fputs("** ", apts_file);
  52.     else if (appt->flags & EVERY_SOMEDAY) {
  53.         switch (appt->flags & EVERY_SOMEDAY) {
  54.             case EVERY_SUN:
  55.                 fputs("Su ", apts_file);
  56.                 break;
  57.             case EVERY_MON:
  58.                 fputs("Mo ", apts_file);
  59.                 break;
  60.             case EVERY_TUE:
  61.                 fputs("Tu ", apts_file);
  62.                 break;
  63.             case EVERY_WED:
  64.                 fputs("We ", apts_file);
  65.                 break;
  66.             case EVERY_THU:
  67.                 fputs("Th ", apts_file);
  68.                 break;
  69.             case EVERY_FRI:
  70.                 fputs("Fr ", apts_file);
  71.                 break;
  72.             case EVERY_SAT:
  73.                 fputs("Sa ", apts_file);
  74.                 break;
  75.         }
  76.     } else
  77.         fprintf(apts_file, "%02d ", one_based ? appt->day : appt->day-1);
  78.     if (appt->flags & A_NOTE) {
  79.         appt->hour = 99;
  80.         appt->minute = 0;    /* assume unmarked note */
  81.     }
  82.     if ((appt->flags & MARKED_NOTE) == MARKED_NOTE)
  83.         appt->minute = 99;
  84.     if (!(appt->flags & (ALL_DAYS|DELETED)) && appt->flags & REPEAT) {
  85.         if (appt->flags & EVERY_SOMEDAY)
  86.             fprintf(apts_file, "%02d %02d %02d %s ", appt->hour, appt->minute, appt->arrows, to_str(appt->repeat));
  87.         else
  88.             fprintf(apts_file, "%02d %02d %02d [%d] ", appt->hour, appt->minute, appt->arrows, appt->repeat);
  89.     } else
  90.         fprintf(apts_file, "%02d %02d %02d ", appt->hour, appt->minute, appt->arrows);
  91.  
  92.     if (appt->flags & LOOKAHEAD)
  93.         fprintf(apts_file, "<%d> ", appt->lookahead);
  94.     if (appt->flags & DELETED)
  95.         fprintf(apts_file, "# %s", appt->str);
  96.     else
  97.         fprintf(apts_file, "%s", appt->str);
  98.     
  99.     /* check for failure (e.g. file system full) */
  100.     return(ferror(apts_file));
  101. }
  102.  
  103. char rptstr[10];
  104.  
  105. /* convert repeat bit map to printable string */
  106. char *
  107. to_str(repeat)
  108. int repeat;
  109. {
  110.     int i, j = 0;
  111.  
  112.     if (repeat == ALL_WEEKS)
  113.         /* if it's every week, then don't write [] spec */
  114.         rptstr[0] = '\0';
  115.     else {
  116.         rptstr[j++] = '[';
  117.         for (i=0; i<5; i++) {
  118.             if (repeat & (0x1<<i)) {
  119.                 rptstr[j++] = i+1 + '0';
  120.                 rptstr[j++] = ',';
  121.             }
  122.         }
  123.         if (repeat & LAST_WEEK) {
  124.             rptstr[j++] = 'L';
  125.             rptstr[j++] = ',';
  126.         }
  127.         rptstr[j] = '\0';
  128.         rptstr[--j] = ']';
  129.     }
  130.     return (rptstr);
  131. }
  132.