home *** CD-ROM | disk | FTP | other *** search
/ Il CD di internet / CD.iso / SOURCE / CONTRIB / MBASE / MBASE51.TAR / mbase51 / src / parse.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-04  |  3.3 KB  |  102 lines

  1. /*
  2.  * METALBASE 5.1
  3.  *
  4.  * Released January 1st, 1993 by Huan-Ti [ t-richj@microsoft.com ]
  5.  *
  6.  */
  7.  
  8. #ifndef PARSE_H
  9. #define PARSE_H
  10.  
  11. #ifndef MBASE_H
  12. #include <mbase.h>
  13. #endif
  14.  
  15.  
  16. /*
  17.  * MAX_BUF, defined below, is the size of the read-buffer used during
  18.  * file reading.  Because of the method used, this doubles as the maximum
  19.  * line- and word-length; if this is too small for any parsing you need to
  20.  * do, define MAX_BUF higher when compiling.
  21.  *
  22.  */
  23.  
  24. #define MAX_BUF   512
  25. #define MAX_LINE  128
  26.  
  27.  
  28. /*
  29.  * File parsing is done on a word-by-word basis; words being delimited by
  30.  * whitespace, or changes between the type of string; that is, a word will
  31.  * continue as long as it is made of just letters and numbers, or just special
  32.  * characters (:;,!@#*& etc).  Grouping characters (parentheses, brackets
  33.  * and braces) are returned as separate words, as are C and C++ comment
  34.  * strings.  Quotes are honored (but not returned); and if fpInit() is called
  35.  * with TRUE as the second argument, non-quoted words (from fpWord) and non-
  36.  * quoted sections of lines (from fpLine) will be mapped to lower-case.
  37.  *
  38.  * In order to parse the data in a file, use the following operations:
  39.  *
  40.  *    FParse *fp;
  41.  *
  42.  *    if ((fp = fpInit ("filename", FALSE)) == NULL)  abort
  43.  *
  44.  *    for ( ; ! fpEOF(fp); )
  45.  *       {
  46.  *       printf ("WORD IS '%s'\n", fpWord (fp));
  47.  *       }
  48.  *
  49.  * fpBackup() will place the previously-read word back, so it will be read
  50.  * again the next time fpWord() is called.  In contrast, fpRewind() will
  51.  * reset the file to the very beginning, as if you'd just called fpInit().
  52.  *
  53.  */
  54.  
  55. typedef struct
  56.    {
  57.    int   fh;               /* Filehandle    ( if fpInit() )         */
  58.    char *str;              /* Source string ( if fpInitStr() )      */
  59.    bool  fLower;           /* Make all non-quoted words lower-case? */
  60.    bool  fEOF;             /* TRUE if EOF has been reached          */
  61.    bool  fQuoted;          /* TRUE if last word was quoted          */
  62.    bool  fLastEOF;         /* TRUE if EOF reached at last position  */
  63.    char  word[MAX_LINE+1]; /* Return buffer                         */
  64.    char  buf[MAX_BUF+1];   /* Read buffer                           */
  65.    int   nBuf;             /* Number of characters in read buffer   */
  66.    int   nLines;           /* Current line number (1==first)        */
  67.    int   nLast;            /* Number of lines at lastpos            */
  68.    long  lastpos;          /* Position before we read the last word */
  69.    long  curpos;           /* Current position (after buf[])        */
  70.    } FParse;
  71.  
  72.  
  73. /*
  74.  * MACROS ---------------------------------------------------------------------
  75.  *
  76.  */
  77.  
  78. #define fpLastno(fp)  ( fp->nLast )
  79. #define fpLineno(fp)  ( fp->nLines )
  80. #define fpQuoted(fp)  ( fp->fQuoted )
  81. #define fpEOF(fp)     ( fp->fEOF && (fp->nBuf == 0) )
  82.  
  83. #define fpRewind(fp)  fpSetPos (fp, 0L, 1)
  84. #define fpGetPos(fp)  ( fp->curpos )
  85.  
  86. /*
  87.  * FUNCTION PROTOTYPES --------------------------------------------------------
  88.  *
  89.  */
  90.  
  91.    FParse    *fpInit         XARGS( (charptr, bool) );
  92.    FParse    *fpInitStr      XARGS( (charptr, bool) );
  93.    void       fpClose        XARGS( (FParse *) );
  94.    charptr    fpWord         XARGS( (FParse *) );
  95.    charptr    fpLine         XARGS( (FParse *) );
  96.    bool       fpBackup       XARGS( (FParse *) );
  97.    void       fpSetPos       XARGS( (FParse *, long, int) );
  98.  
  99.  
  100. #endif  /* PARSE_H */
  101.  
  102.