home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 6 / FreshFish_September1994.bin / new / dev / c / hce / examples / amiga / pools / read.c < prev   
C/C++ Source or Header  |  1992-09-02  |  9KB  |  352 lines

  1. /*
  2.  * Copyright (c) 1994. Author: Jason Petty.
  3.  *
  4.  * Permission is granted to anyone to use this software for any purpose
  5.  * on any computer system, and to redistribute it freely, with the
  6.  * following restrictions:
  7.  * 1) No charge may be made other than reasonable charges for reproduction.
  8.  * 2) Modified versions must be clearly marked as such.
  9.  * 3) The authors are not responsible for any harmful consequences
  10.  *    of using this software, even if they result from defects in it.
  11.  *
  12.  *    read.c:
  13.  *
  14.  *      Read league names and league tables from 'P_TABLES' file.
  15.  *
  16.  */
  17.  
  18. #include <exec/types.h>
  19. #include <clib/stdio.h>
  20. #include <clib/string.h>
  21. #include <clib/ctype.h>
  22. #include <libraries/dos.h>
  23. #include <dos/dosextens.h>
  24.  
  25. #include "pools.h"
  26.  
  27. #define EOP         '\0'    /* End of file or pools input.     */
  28. #define EOK          1      /* Success and not EOP and not EOT. */
  29. #define EOT          2      /* Possible Success but endof table.*/
  30. #define ENT          3      /* No more tables. */
  31.  
  32. #define TEND        '}'     /* Symbol used to find EOT. */
  33.  
  34. #define IN_LEN       1024   /* Max chars read at any one time.(file). */
  35. #define SEARCH_NUM   5      /* Number of dirs to search for 'P_TABLES'*/
  36.  
  37. #define ISWHITE(c)  ((c) == '\t' || (c) == ' ' || (c) == '\n')
  38.  
  39. char inbuffer[IN_LEN];      /* Read buffer.   */
  40. char prbuf[250];            /* Extract buffer.*/
  41. char g_BUF[128];            /* Any use. */
  42.  
  43. /* Search paths for the script file. */
  44. char *SCAN_PATH[6] = {"RAM:P_TABLES","P_TABLES", "SYS:P_TABLES",
  45.              "SYS:s/P_TABLES","SYS:devs/P_TABLES",NULL};
  46.  
  47. /* What to look for in the script file. */
  48. char *W_TYPE[3] = {"LEAGUE", "TABLE", NULL};
  49.  
  50. /* What to find and avoid getting. */
  51. char V_TYPE[4] = { '{', '=', ',', '\0'};
  52.  
  53. P_TABLE *t_head=NULL;
  54. P_TABLE *t_end=NULL;
  55.  
  56. void Test_Incode();
  57. char charin();
  58. int readfile(), readtable();
  59.  
  60. char charin (infp)        /* Fill inbuffer and keep returning single char,*/
  61. struct FileHandle *infp;  /* repeat till end of file. */
  62. {
  63.     static LONG inpoint=(IN_LEN  - 1);
  64.     static LONG maxin=(IN_LEN - 1);
  65.     LONG Read ();
  66.     char ch;
  67.  
  68.       if(++inpoint>=maxin)
  69.         {
  70.          maxin=Read(infp,inbuffer,IN_LEN);
  71.          inpoint=0;
  72.          }
  73.     if (maxin==0)
  74.         return (EOP);                   /* End of file. */
  75.     if (maxin == -1) {
  76.         /* printf("Read ERROR!!\n"); */
  77.         return (EOP);                   /* End of file. */
  78.         }
  79.  
  80.     ch=inbuffer[inpoint];               /* Get a Single char. */
  81.  
  82.     if (ch < ' ' && ch != '\0' && ch != '\n') /* Remove any weird chars. */
  83.         ch = ' ';
  84.  
  85.    return (ch);
  86. }
  87.  
  88.  
  89. /* Get league name, team & table entries. */
  90.  
  91. int readtable(infp)
  92. struct FileHandle *infp;
  93. {
  94.   P_TABLE *p;
  95.   int rv;
  96.  
  97. moret:
  98.  
  99.   if(!(p = (P_TABLE *) malloc((int)sizeof(P_TABLE)))) {
  100.        /* printf("no memory!!\n"); */
  101.        return(NULL);
  102.        }
  103.  
  104. /* First find 'LEAGUE' and get league name. */ 
  105.  
  106.        if(!(rv = Find_SYM(infp,W_TYPE[0])))
  107.             return(ENT);                 /* No more tables. */
  108.        if(rv == EOT)                     /* Should not find EOT yet! */
  109.             return(NULL);
  110.        if(!(rv = Next_SYM(infp,g_BUF)))  /* This should be league name. */
  111.           return(NULL);
  112.        if(rv == EOT)
  113.           return(NULL);
  114.           strcpy(p->league,g_BUF);       /* Get what should be league name*/
  115.  
  116. /* Next find 'TABLE' and fill *p with team names + team form numbers. */
  117.  
  118.        if(!(rv = Find_SYM(infp,W_TYPE[1])))
  119.             return(NULL);
  120.        if(rv == EOT)                    /* Should not find EOT yet! */
  121.             return(NULL);
  122.        if(!(rv = Fill_TABLE(infp,p)))   /* Fill table. team names+entries. */
  123.             return(NULL);
  124.        if(rv != EOT)                    /* Should be EOT here. */
  125.             return(NULL);
  126.  
  127.   if(t_head == NULL) {    /* Add new table to list. */
  128.      p->next = NULL;
  129.      t_head = p;
  130.      t_end = t_head;
  131.      }
  132.     else {
  133.            t_end->next = p;
  134.            t_end = t_end->next;
  135.            t_end->next=NULL;
  136.           }
  137.  
  138.  goto moret;
  139. }
  140.  
  141. /* Fill a pools table with team name + form numbers. */
  142.  
  143. int Fill_TABLE(infp,p)
  144. struct FileHandle *infp;
  145. P_TABLE *p;
  146. {
  147.  char c;
  148.  int rv;
  149.  int xcount=0;
  150.  int ycount=0;
  151.  
  152.   while((rv = Next_SYM(infp,g_BUF)))
  153.         {
  154.          c = g_BUF[0];
  155.  
  156.         if(rv == EOT) {             /* End of table? */
  157.            p->count = ycount-1;
  158.            return(EOT);
  159.            }
  160.         if((ycount-1) >= MAX_TABLE) {   /* More teams than allowed?. */
  161.            p->count = ycount-1;
  162.            return(EOT);
  163.            }
  164.         if(isdigit(c)) {            /* Number to get?. */
  165.            if(xcount >= MAX_ENTRIES) {
  166. /*            printf("Team: %s has to many numbers\n", p->team[ycount-1]); */
  167.               xcount++;
  168.               }
  169.              else {
  170. /*                 printf(":%s:    - %d\n", g_BUF, atoi(g_BUF)); */
  171.                    p->table[ycount-1][xcount] = atoi(g_BUF);
  172.                    xcount++;
  173.                    }
  174.            }
  175.          else                     /* Team to get?. */
  176.            {
  177.             strcpy(p->team[ycount],g_BUF);
  178.             ycount++;
  179.             xcount=0;
  180.             }
  181.         }
  182.   p->count = 0;
  183. return(NULL);
  184. }
  185.  
  186. int readfile()    /* Open P_TABLE file and read tables in. */
  187. {
  188.     struct FileHandle *infp=NULL;
  189.     BPTR lock=NULL;
  190.     int i=0;
  191.  
  192.                      /* Search RAM:," ",SYS:, SYS:s/ ,SYS:devs/ */
  193.                      /* for the script file.                    */
  194.    while(lock==NULL && SCAN_PATH[i] != NULL)
  195.            {
  196.             lock=(BPTR)Lock(SCAN_PATH[i],(int)ACCESS_READ);
  197.             i++;
  198.             }
  199.  
  200.  if(lock==NULL) {
  201.     gfx_FPEN(1);
  202.     gfx_TXT("Could not open 'P_TABLES' file! - (BYE ... BYE ...)",25,6);
  203.     Delay(200);
  204.     return(NULL);    /* no script file!. */
  205.     }
  206.      if(!(infp=(struct FileHandle *)OpenFromLock((BPTR)lock))) {
  207.        /*  printf("No open from lock!!\n"); */
  208.            UnLock(lock);
  209.            return(NULL);
  210.            }
  211.  
  212.   /* Search for league heading then read table in. */
  213.  
  214.       if(!(readtable(infp))) {
  215.            gfx_FPEN(1);
  216.            gfx_TXT("WARN: Error reading 'P_TABLES' file!!",25,6);
  217.            Close(infp);
  218.            return(NULL);
  219.            }
  220.  
  221.       Close(infp);
  222.  return (1);
  223. }
  224.  
  225.  
  226. int ch_OK(c)  /* Check for symbols to be avoided Example: '{' or '=' */
  227. char c;       /* if symbol found return 0 else 1. */
  228. {
  229.  int i=0;
  230.  
  231.   while(V_TYPE[i] != '\0')
  232.       {
  233.       if(V_TYPE[i++] == c)
  234.          return(0);
  235.        }
  236.  
  237. return(1);
  238. }
  239.  
  240. int Next_SYM(infp,s)       /* Find next valid Word/Symbol and put it in *s */
  241. struct FileHandle *infp;
  242. char *s;
  243. {
  244.  char c;
  245.  int i;
  246.  
  247. redo:
  248.  
  249.      i=0;
  250.  
  251.    do {                  /* Find first legal character. */
  252.           c = charin(infp);
  253.  
  254.        if(c == '*' || c == ';') /* Skip Comment, goto next line. */
  255.             while(c != '\n' && c != EOP)
  256.                   c = charin(infp);                
  257.        }
  258.    while(c != EOP && ISWHITE(c));
  259.  
  260.   if(c == EOP) {
  261.      return(NULL);
  262.      }
  263.  
  264.    do {                  /* Found first so see if its a word. */
  265.        if(ch_OK(c)) {
  266.           prbuf[i++] = c;
  267.           c = charin(infp);
  268.           }
  269.         else {
  270.                c = ' ';
  271.               }
  272.        }
  273.    while(c != EOP && c != ' ' && c != '\n');
  274.  
  275.           prbuf[i] = '\0';
  276.  
  277.        if(prbuf[0] != '\0') {   /* Found a valid Word/Symbol */
  278.               strcpy(s,prbuf);
  279.            if(prbuf[0] == TEND)
  280.               return(EOT);
  281.             else
  282.               return(EOK);
  283.               }
  284.          
  285.          prbuf[0] = '\0';
  286.  
  287.   if(c == EOP) {
  288.      return(NULL);
  289.      }
  290.  
  291.  goto redo;
  292. }
  293.  
  294. int Find_SYM(infp,s)     /* Find symbol *s, if reached EOP return NULL. */
  295. struct FileHandle *infp;
  296. char *s;
  297. {
  298.  char c;
  299.  int rv;
  300.  
  301.   while((rv = Next_SYM(infp,g_BUF)))
  302.        {
  303.         if(rv == EOT)             /* End of table? */
  304.            return(EOT);
  305.         if(!(stricmp(g_BUF,s)))   /* Found symbol? */
  306.            return(EOK);
  307.         }
  308.  
  309. return(NULL);
  310. }
  311.  
  312.  
  313. void Test_Incode()      /* Show all tables found. */
  314. {
  315.  int i;
  316.  
  317.   if(!(readfile()))     /* find and read in P_TABLE file. */
  318.      {
  319.       exit(0);
  320.       }
  321.  
  322.  if(t_head != NULL) 
  323.   {
  324.   while(t_head != NULL) 
  325.     {
  326.      if(t_head->league[0] != '\0')
  327.         printf("League: %s\n",t_head->league);     /