"Fossies" - the Fresh Open Source Software Archive

Member "tokfile.c" (9 May 1995, 4009 Bytes) of package /linux/misc/old/cpost.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "tokfile.c" see the Fossies "Dox" file reference documentation.

    1 /*------------------------------------------------------------------
    2  * tokfile.c :
    3  *------------------------------------------------------------------
    4  * 02-13-93 originally by Patrick J. Mueller
    5  *------------------------------------------------------------------*/
    6 
    7 #include <stdio.h>
    8 #include <stdlib.h>
    9 #include <string.h>
   10 
   11 #include "tokfile.h"
   12 
   13 /*------------------------------------------------------------------
   14  * types
   15  *------------------------------------------------------------------*/
   16 typedef struct
   17    {
   18    FILE *fHandle;
   19    int   index;
   20    int   length;
   21    char  line[TOKFILE_MAX_LINE+1];
   22    } TokFileData;
   23 
   24 /*------------------------------------------------------------------
   25  * open a file to be tokenized
   26  *------------------------------------------------------------------*/
   27 TokFileInfo TokFileOpen(
   28    char *fileName
   29    )
   30    {
   31    TokFileData *tfd;
   32 
   33    /*---------------------------------------------------------------
   34     * allocate memory for handle
   35     *---------------------------------------------------------------*/
   36    tfd = malloc(sizeof(TokFileData));
   37    if (!tfd)
   38       return NULL;
   39 
   40    memset(tfd,0,sizeof(TokFileData));
   41 
   42    /*---------------------------------------------------------------
   43     * open the file
   44     *---------------------------------------------------------------*/
   45    if (!strcmp(fileName,"-"))
   46       tfd->fHandle = stdin;
   47    else
   48       tfd->fHandle = fopen(fileName,"r");
   49 
   50    if (!tfd->fHandle)
   51       {
   52       free(tfd);
   53       return NULL;
   54       }
   55 
   56    /*---------------------------------------------------------------
   57     * return handle
   58     *---------------------------------------------------------------*/
   59    return (TokFileInfo) tfd;
   60    }
   61 
   62 #define WHITE_SPACE " \a\b\f\n\r\t\v"
   63 
   64 /*------------------------------------------------------------------
   65  * get the next token from the file
   66  *------------------------------------------------------------------*/
   67 char *TokFileNext(
   68    TokFileInfo tfi
   69    )
   70    {
   71    TokFileData *tfd = tfi;
   72    char        *tok;
   73 
   74    if (!tfd)
   75       return NULL;
   76 
   77    if (!tfd->fHandle)
   78       return NULL;
   79 
   80    /*---------------------------------------------------------------
   81     * loop trying to get the next token
   82     *---------------------------------------------------------------*/
   83    while (1)
   84       {
   85 
   86       /*------------------------------------------------------------
   87        * do we need to get another line
   88        *------------------------------------------------------------*/
   89       while (tfd->index >= tfd->length)
   90          {
   91          fgets(tfd->line,TOKFILE_MAX_LINE,tfd->fHandle);
   92 
   93          if (feof(tfd->fHandle))
   94             {
   95             fclose(tfd->fHandle);
   96             free(tfd);
   97             return NULL;
   98             }
   99 
  100          tfd->length = strlen(tfd->line);
  101          tfd->index  = strspn(tfd->line,WHITE_SPACE);
  102 
  103          /*---------------------------------------------------------
  104           * check for comment
  105           *---------------------------------------------------------*/
  106          if ((tfd->index < tfd->length) && ('#' == tfd->line[tfd->index]))
  107             tfd->index = tfd->length;
  108          }
  109 
  110       /*------------------------------------------------------------
  111        * skip past white space
  112        *------------------------------------------------------------*/
  113       tfd->index += strspn(tfd->line + tfd->index, WHITE_SPACE);
  114 
  115       if (tfd->index >= tfd->length)
  116          continue;
  117 
  118       tok = tfd->line + tfd->index;
  119 
  120       /*------------------------------------------------------------
  121        * skip past non-white space
  122        *------------------------------------------------------------*/
  123       tfd->index += strcspn(tfd->line + tfd->index, WHITE_SPACE);
  124 
  125       tfd->line[tfd->index] = 0;
  126 
  127       tfd->index++;
  128 
  129       return tok;
  130       }
  131 
  132    return NULL;
  133    }
  134 
  135 #define TESTER_NOT
  136 #if defined(TESTER)
  137 
  138 int main(void)
  139    {
  140    TokFileInfo  tfi;
  141    char        *token;
  142 
  143    tfi = TokFileOpen("-");
  144 
  145    while (token = TokFileNext(tfi))
  146       printf("%s\n",token);
  147 
  148    return 0;
  149    }
  150 
  151 #endif