home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_200 / 266_01 / datlib.c < prev    next >
C/C++ Source or Header  |  1990-07-04  |  5KB  |  168 lines

  1. /*         Data Handling Package    06 May 1989        File:DATLIB.C
  2.                          revised    03 Jul 1990
  3.                 Robert L. Patton, Jr.
  4.                 1713 Parkcrest Terrace
  5.                 Arlington, TX 76012
  6.  
  7.     The first value in each array is the number of values following.
  8.     Data types are: 'N' - numeric
  9.                     'D' - date as yymmdd
  10.                     'W' - word (later)
  11. */
  12. #include <stddef.h>
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include "datlib.h"
  16. #include "wordlib.h"
  17. #define  POINTS  200
  18. #define  FNLEN    30
  19. #define  ADHOC   '@'
  20. #define  EMPTY   '*'
  21. #define  IBID    '*'
  22. #define  FINIS   '$'
  23. #define  SPLOX   '@'
  24. #define  EQUAL(A,B) !strcmp(A,B)
  25.  
  26. FILE   *Data,*Plox;
  27. static char  AdHocFile[FNLEN+1] = {EMPTY,'\0'};
  28. static float Val [2] [POINTS+1];
  29.  
  30. void AdHoc (char *FileName) {
  31. /*   =====                                Accept ad hoc file name */
  32.     strncpy (AdHocFile, FileName, FNLEN);
  33. }
  34. int CumDays (Month, Year)
  35. /*  =======              No. of days in year up to start of month */
  36. int Month, Year;
  37. {
  38.   static int Cum[] = {0,31,59,90,120,151,181,212,243,273,304,334};
  39.  
  40.   return (Cum[Month-1] + (Month>2? Leap(Year) : 0) );
  41. }
  42.  
  43. int Evaluate (Word, Type, Value)
  44. /*  ======== */
  45. char  *Word,Type;   /* Find the float value of a     */
  46. float *Value;       /* string with a given data type */
  47. {
  48.   int D,M,Y;
  49.  
  50.   if      (Word[0]==EMPTY)
  51.     return 1;
  52.   else if (Type=='N')            /* Numeric */
  53.     *Value = (float) atof(Word);
  54.  
  55.   else if (Type=='D') {        /* Date as YYMMDD */
  56.     Split (Word, &Y, &M, &D);  /* gives YY.frac  */
  57.     if (Y < 50) Y += 100;
  58.     *Value = (float) Y + ((float) (CumDays(M,Y) + D - 1))
  59.                        / ((float) (365 + Leap(Y)));
  60.   }
  61.   else
  62.     *Value = 0.0;
  63.  
  64.   return 0;
  65. }
  66.  
  67. int Leap (Year)
  68. /*  ====       = 1 for leap year, 0 if not */
  69. int Year;
  70. {
  71.   return !(Year %(Year%100? 4: 400));
  72. }
  73. /*@@*/
  74. void GetTwo (FileName, Xptr, Xtype, Xitem, Yptr, Ytype, Yitem)
  75. /*   ====== */
  76. char    *FileName;      /* Reads a set of X,Y points as     */
  77. char    Xtype, Ytype;   /* designated items on a data line  */
  78. float   **Xptr, **Yptr;   /* and returns pointers to the data */
  79. int     Xitem, Yitem;
  80. {
  81.   char Sentence[81], Word[21];
  82.   int  n, Count, Xvoid, Yvoid;
  83. /* -----------------------------------------
  84. printf("GetTwo(%s,%u,%c,%d,%u,%c,%d)\n",
  85. FileName,*Xptr,Xtype,Xitem,*Yptr,Ytype,Yitem);
  86. ------------------------------------------ */
  87.   Count = 0;
  88.   if (FileName[0] == IBID) Data = Plox;
  89.   else {
  90.       if (FileName[0] == ADHOC) {
  91.           if (AdHocFile[0] == EMPTY) {
  92.               printf ("   Enter ad hoc dat file name: ");
  93.               scanf ("%30s",AdHocFile);
  94.           }
  95.           Data = fopen (AdHocFile,"r");
  96.       }
  97.       else Data=fopen (FileName,"r");
  98.   }
  99.   if (Data != NULL) {
  100.     Count = 1;
  101.     while (fgets (Sentence, 80, Data) != NULL) {
  102.       if (Sentence[0] != '/' &&        /* bypass comment */
  103.           Sentence[0] !='\032') {      /* and CTRL Z */
  104.  
  105.         if (Sentence[0] == FINIS) break;
  106.         if (Sentence[0] == SPLOX) {     /* Handle PLOX statements in data */
  107.             Clean (Sentence);
  108.             Sentence[0] = ' ';
  109.             Sentence[strlen(Sentence)-1] = '\0';
  110.             if ((Sentence[1]-'0') == Yitem) {
  111.                 Sentence[1] = ' ';
  112.                 GetWord (Sentence, Word, 12);
  113.                 Capitalize (Word);
  114.                 if      (EQUAL (Word,"TITLE")) TLcon (Sentence);
  115.                 else if (EQUAL (Word,"LABEL")) LAcon (Sentence);
  116.                 else if (EQUAL (Word,"ISO"))   IScon (Sentence);
  117.                 else if (EQUAL (Word,"HUE"))   HUcon (Sentence);
  118.                 else if (EQUAL (Word,"AXIS"))  AXcon (Sentence);
  119.                 else PauseMsg("Unknown control word after @ in data file");
  120.             }
  121.         }
  122.         else {
  123.             for (n=1; n<=(Xitem>Yitem? Xitem: Yitem); n++) {
  124.               GetWord (Sentence, Word, 20);
  125.               if (n == Xitem)
  126.                 Xvoid = Evaluate (Word, Xtype, &Val [0] [Count]);
  127.               else if (n == Yitem)
  128.                 Yvoid = Evaluate (Word, Ytype, &Val [1] [Count]);
  129.             }
  130.             if (Xvoid || Yvoid) Count--;
  131.             if (++Count > POINTS) break;
  132.         }
  133.       }
  134.     }
  135.     Count--;
  136.     if (Data != Plox) fclose (Data);
  137.   }
  138.   Val [0] [0] = (float) Count;
  139.   Val [1] [0] = (float) Count;
  140.   *Xptr = &Val [0] [0];
  141.   *Yptr = &Val [1] [0];
  142. }
  143.  
  144. void SavePlox (PloxPtr)
  145. /*   ========           Accept the PLOX file pointer */
  146. FILE *PloxPtr;
  147. {
  148.   Plox = PloxPtr;
  149. }
  150.  
  151. void Split (YYMMDD,Y,M,D)
  152. /*   =====               string date to int year, month, day */
  153. char *YYMMDD;
  154. int  *Y,*M,*D;
  155. {
  156.   char Date[3][3];
  157.   register int  n;
  158.  
  159.   for (n=0; n<3; n++) {
  160.     Date[n][0] = YYMMDD[2*n];
  161.     Date[n][1] = YYMMDD[2*n+1];
  162.     Date[n][2] = '\0';
  163.   }
  164.   *Y = atoi (Date[0]);
  165.   *M = atoi (Date[1]);
  166.   *D = atoi (Date[2]);
  167. }
  168.