home *** CD-ROM | disk | FTP | other *** search
/ CP/M / CPM_CDROM.iso / cpm / pcpursut / pisrc.ark / READDB.C < prev   
Text File  |  1988-07-14  |  11KB  |  523 lines

  1. /*******************************************************************
  2. * read_db.c - read pcp database into memory 
  3. * Updated to support mnemonics 1-9-88 D.C.
  4. *
  5. ********************************************************************/
  6.  
  7. #include "stdio.h"
  8. #include "fcntl.h"
  9. #include "cpcp.h"
  10.  
  11.  
  12. #define IN_LENGTH    120    /* length of input buffer for each line */
  13. /* #define DEBUG */
  14.  
  15. extern struct cityrec *cities;    /* list of cities         */
  16.  
  17. /* read database into memory from text file 
  18. * return -1 if file does not exist 
  19. */
  20. int read_db(db_name)
  21. char *db_name;
  22. {
  23.     static int fd;            /* file descripter */
  24.     static FILE *fp;        /* file pointer for database */
  25.     char tempstr[120];    /* place to put line from database */
  26.     static int linenum;
  27.     static char *instring;
  28.     linenum = 0;
  29.     instring = tempstr;
  30.  
  31.     printf("\n          Reading database..");
  32.     if (-1 != (fd = open(db_name, O_RDONLY ))) {
  33. #ifdef DEBUG
  34.         printf("Line num = %d.\n", linenum);
  35. #endif
  36.         while (gstr(instring, IN_LENGTH - 1, fd)) {
  37.             if (parse(instring) == ERROR)
  38.                 return(-1);;
  39.         }
  40.         printf("\n");
  41.         close(fd);
  42.     }
  43.     else {
  44.         return (-1);
  45.     }
  46.  
  47.     return(0);
  48. }
  49.  
  50. gstr(string, numbytes, fd)
  51. char *string;    /* where to put data */
  52. int numbytes;    /* max number of bytes, ignored for now */
  53. int fd;        /* file descripter */
  54. {
  55.     static int count;    /* bytes left in buffer */
  56.     static char *buf;    /* buffer for data */
  57.     static char *ptr;
  58.     static int notfirst;
  59.  
  60.     extern char *alloc();
  61.  
  62.     if (!notfirst++) {
  63.         buf = alloc(1024);
  64.     }
  65.     
  66.     for (;;) {
  67.         if (!count) {
  68.             printf(".");
  69.             if (0 == (count = read(fd, buf, 1024)))
  70.                 return(0);
  71.             else
  72.                 ptr = buf;
  73.         }
  74.  
  75.         if (*ptr == 13) {
  76.             *string = NULL;
  77.             --count;
  78.             ptr++;
  79.             return(1);
  80.         } else if (*ptr == 0x0a) {
  81.             --count;
  82.             ptr++;
  83.         } else {
  84.             *string++ = *ptr++;
  85.             --count;
  86.         }
  87.     }
  88. }
  89.  
  90.     
  91.  
  92.  
  93. /* read each input line, parse it and put results in table */
  94. /* return ERROR if error in memory allocation */
  95. parse(string)
  96. char *string;    /* input line */
  97. {
  98.     static int status;        /* line status -1 = error */
  99.  
  100. #ifdef DEBUG
  101.     printf("  Entering Parse.\n");
  102. #endif
  103. #ifdef DEBUG
  104.     printf("In string is:\n");
  105.     printf("%s", string);
  106. #endif
  107.  
  108.     /* strip off spaces and tabs at beinning of the line */
  109.     while (*string && iswhite(*string))
  110.         string++;
  111. #ifdef DEBUG
  112.     printf("In string is:\n");
  113.     printf("%s", string);
  114. #endif
  115.     /* return if end of the string */
  116.     if (!*string)
  117.         return(0);
  118.  
  119.     status = bbs_name(string);
  120.     if (!status) {
  121.         status = area_name(string);
  122.     }
  123.     return(status);
  124. }
  125.  
  126.  
  127.  
  128.  
  129. /* check for BBS mnemonic, phone number , name and baud rate
  130.  * it should appear as:
  131.  * "xxxxx [1-][aaa-]ppp-pppp BBS_NAME [mm/dd/yy] [baudrate] [other]"
  132.  * where
  133.  *    xxxxx         = area mnemonic
  134.  *    ppp-pppp    = phone number
  135.  *    BBS_NAME    = string with BBS name
  136.  *    mm/dd/yy    = month day and year of verification (optional)
  137.  *    baudrate    = baudrate (300,1200 or 2400)
  138.  *    other        = only present if date or baudrate exist
  139.  *                has comments on baudrate
  140.  * examples are:
  141.  *    "NJNEW 792-9131 Randy's Rothen"
  142.  *    "NJNEW 884-2568 AT&T INFO     09/01/86     M"
  143.  *    "NJNEW 783-7458 Shelter        09/26/86 2400    UD
  144.  *    "NJNEW 1-301-940-3456 The Place
  145.  * 1200 baud will be assumed if no baudrate exists 
  146.  */
  147. int bbs_name(string)
  148. char *string;
  149. {
  150.     static int baudrate;        /* baudrate to use, default = 1200 */
  151.     static char *strptr;        /* current position in string */
  152.     static char number[12];        /* bbs phone number */
  153.     static char bbsname[120];     /* bbs name */
  154.     static char *tempptr;        /* temp. pointer         */
  155.     static int i;
  156.     static struct bbs_rec *bbs_link;
  157.     static char mnemonic[20];    /* area mnemonic */
  158.     static char *mptr;
  159.  
  160.     extern struct bbs_rec *getbbs();    
  161.     extern char *getstr();
  162.  
  163. #ifdef DEBUG
  164.     printf("    Checking for bbsname .\n");
  165. #endif
  166.     /* skip starting whitespace */
  167.     while (iswhite(*string))
  168.         string++;
  169.  
  170.     strptr = string;
  171.     baudrate = BAUD1200;
  172.  
  173.     mptr = mnemonic;
  174.     /* check for mnemonic first */
  175.     if (!is_mnemonic(string)) 
  176.         return(0);
  177. #ifdef DEBUG
  178.     printf("Mnemonic code found \n");
  179. #endif
  180.  
  181.     /* skip mnemonic and copy it to string */
  182.     while (!iswhite(*strptr))
  183.         *mptr++ = *strptr++;
  184.     *mptr = NULL;;
  185.  
  186.     while (iswhite(*strptr))
  187.         strptr++;
  188.     
  189.     /* now I expect the phone number */
  190.     if (!is_p_num(strptr, number)) {
  191.         return(0);
  192.     }
  193. #ifdef DEBUG
  194.     printf("Phone fond\n");
  195. #endif
  196.     /* skip number in string */
  197.     while (!iswhite(*strptr))
  198.         strptr++;
  199.     
  200.     /* now get bbs name and check for date or phone number */
  201.     tempptr = bbsname;
  202.     while (iswhite(*strptr))
  203.         strptr++;
  204.     if (!*strptr)
  205.         return(0);
  206.  
  207.     /* get bbs name */
  208.     while (*strptr && (*strptr != '\n') && (!isdigit(*strptr) ||
  209.         (!is_date(strptr) && !is_baud(strptr))) ) { 
  210.                         
  211.         *tempptr++ = *strptr++;
  212.     }
  213.     *tempptr = NULL;
  214.  
  215. #ifdef DEBUG
  216.     printf("BBS name is:\n");
  217.     printf("%s\n",tempptr);
  218. #endif
  219.  
  220.     /* take whitespace off the end of the bs name  */
  221.     while (iswhite(*(--tempptr)))
  222.         *tempptr = NULL; 
  223.  
  224.  
  225.  
  226.     if (is_date(strptr)) {
  227.         strptr += 8;
  228.         
  229. #ifdef DEBUG
  230.         printf("Date found\n");
  231. #endif
  232.         while (iswhite(*strptr))
  233.             strptr++;
  234.  
  235.         if (is_baud(strptr))
  236.             baudrate = is_baud(strptr);
  237.     }
  238.     else if (is_baud(strptr))
  239.         baudrate = is_baud(strptr);
  240.  
  241.     if (bbs_link = getbbs(mnemonic)) {
  242.         if (bbs_link->bbs_name = getstr(bbsname)) {
  243.             strncpy(bbs_link->bbs_number, number, BBS_NUM_LEN );
  244.             bbs_link->bbs_baud = baudrate;
  245.         }
  246.         else {
  247.             bbs_link->bbs_name = "No Name";
  248.             return(ERROR);
  249.         }
  250.     }
  251.     else
  252.         return (ERROR);
  253.  
  254.     return(1);
  255. }
  256.  
  257. /* check for mnemonic by looking for text followed by whitespace & number */
  258. /* don't let it start with a digit */
  259. int is_mnemonic(string)
  260. char *string;
  261. {
  262.     if (isdigit(*string))
  263.         return(0);
  264.  
  265.     /* skip text */
  266.     while (*string && !iswhite(*string))
  267.         string++;
  268.  
  269.     /* skip whitespace */
  270.     while(*string && iswhite(*string))
  271.         string++;
  272.  
  273.     /* better be a number here */
  274.     return(*string && isdigit(*string));
  275. }
  276.  
  277. /* check if string has date and return true if so */
  278. /* date is recognized by dd/dd/dd */
  279. int is_date(string)
  280. char *string;
  281. {
  282.     static int i, j;
  283.  
  284. #ifdef DEBUG
  285.     printf("      Checking for date.\n");
  286. #endif
  287.     for (i = 0; i < 3; i++)
  288.         for (j = 0; j < 3; j++) {
  289.             if (((j == 2) && (*string == '/')) || ((i == 2) && (j == 2)))
  290.                 ;
  291.             else if (isdigit(*string))
  292.                 ;
  293.             else
  294.                 return (FALSE);
  295.             string++;
  296.         }
  297.     return (TRUE);
  298. }
  299.  
  300. /* check if string has baud rate and if so then return baud rate */
  301. int is_baud(string)
  302. char *string;
  303. {
  304.  
  305.  
  306. #ifdef DEBUG
  307.     printf("      Looking for baud reate.\n");
  308. #endif
  309.  
  310.     while (iswhite(*string))
  311.         ++string;
  312.     
  313.     if (!(strncmp(string, "300", 3)))
  314.         return (BAUD300);
  315.     else if (!(strncmp(string, "1200", 4)))
  316.         return (BAUD1200);
  317.     else if (!(strncmp(string, "2400", 4)))
  318.         return (BAUD2400);
  319.     else
  320.         return (0);
  321. }
  322.  
  323.     
  324.  
  325.  
  326. /* return true if phone number of the form [1][-][aaa][-]ppp[-]pppp
  327.  * and also return the passed array with the phone number in ascii
  328.  * with up to 11 digits
  329.  */
  330. int is_p_num(string, number)
  331. char *string;
  332. char *number;
  333. {
  334.     static int num_count = 0;
  335.     static int i;
  336.  
  337. #ifdef DEBUG 
  338.     printf("      Looking for phone number.\n");
  339.     printf("String is ->\n");
  340.     printf("%s\n",string);
  341. #endif
  342.     /* check for '1-'  */
  343.     if (*string == '1') {
  344.         strncpy(number, string, 1);
  345.         string += 1;
  346.         number += 1;
  347.  
  348.         if (*string == '-')
  349.             string++;
  350.     }
  351.     
  352.     /* check for 1st 3 numbers and - */
  353.     if (is_area_code(string)) {
  354.         strncpy(number, string, 3);
  355.         string += 3;
  356.         number += 3;
  357.     }
  358.     else
  359.         return(FALSE);
  360.     if (*string == '-')
  361.         string++;
  362.     else
  363.         return(FALSE);
  364.  
  365.  
  366.     /* check for next 3 being numbers */
  367.     if (is_area_code(string)) {
  368.         strncpy(number, string, 3);
  369.         string += 3;
  370.         number += 3;
  371.         if (*string == '-') {
  372.             string++;
  373.             /* expect 4 more digits */
  374.             if (is_area_code(string)) {
  375.                 strncpy(number, string, 3);
  376.                 string += 3;
  377.                 number += 3;
  378.             }
  379.             else return(FALSE);
  380.         }
  381.         /* last digit */
  382.         if (isdigit(*string))
  383.             *number++ = *string++;
  384.         else return (FALSE);
  385.     }
  386.     else return(FALSE);
  387.  
  388.     *number = NULL;
  389.     return(TRUE);
  390. }
  391.  
  392.             
  393.  
  394. /* check for area code name and if so, enter into the  list 
  395.  * The area code name is recognized 2 different ways:
  396.  *     1. by the string "** BBS Listing For +xxxxx" where xxxxx is
  397.  *       the area code. Example :
  398.  *        "** BBS Listing For +TXHOU"
  399.  *    2. by the string "+xxxxx City_Name" where xxxxx is the area
  400.  *       code and City_Name the name of the city. Example:
  401.  *        "+DCWAS Washington, D.C."
  402.  * If found, the area code and name area inserted into the table.
  403.  */
  404. area_name(string)
  405. char *string;
  406. {
  407.     extern struct citylink *get_city();
  408.     extern char *getstr();
  409.  
  410.     struct cityrec *city_rec;    /* city area code
  411.                         and name */
  412.  
  413.     static char mnemonic[20];    /* mnemonic */
  414.     static char *numptr;    /* pointer to number */
  415.     static char *strptr;    /* pointer to current position in line */
  416.     char temp[120];    /* area code name */
  417.     static char *tempptr;
  418.     static char *area_name;
  419.  
  420.     area_name = temp;
  421. #ifdef DEBUG
  422.     printf("  Check for area name.\n");
  423. #endif
  424.     numptr = NULL;
  425.     strptr = string;
  426.     *area_name = NULL;
  427.     *mnemonic = NULL;
  428.     
  429.     /* gobble up whitespace */
  430.     while (iswhite(*strptr))
  431.         strptr++;
  432.  
  433.     /* check for "** Listing For +xxxxx" where xxxxx is area mnemonic */
  434.     if ((*strptr == '*') && (*(strptr + 1) == '*')) {
  435.         strptr++, strptr++;
  436.  
  437.         /* search up to next after "**"    */
  438.         while (iswhite(*strptr))
  439.             strptr++;
  440.  
  441.         /* make sure we aren't at the end of the string */
  442.         if (!*strptr)
  443.             return(0);
  444.  
  445.         /* get string and look for number */
  446.         tempptr = area_name;
  447.         numptr = mnemonic;
  448.         while (*strptr && (*strptr != LF) && (*strptr != '+')) 
  449.             *tempptr++ = *strptr++;
  450.         if (*strptr == '+') {
  451.             strptr++;    /* skip '+' */
  452.             while (*strptr && !iswhite(*strptr)) {
  453.                 *tempptr++ = *strptr;
  454.                 *numptr++ = *strptr++;
  455.             }
  456.             *tempptr = NULL;
  457.             *numptr = NULL;
  458.         }
  459.         else
  460.             return(0);    /* premature end of string */
  461.     }
  462.     else if (*strptr == '+') {
  463.         strptr++;
  464.         numptr = mnemonic; 
  465.         while (*strptr && !iswhite(*strptr))
  466.             *numptr++ = *strptr++;
  467.         *numptr = NULL;
  468.  
  469.     
  470.         /* gobble up whitespace */
  471.         while (iswhite(*strptr)) 
  472.             strptr++;
  473.  
  474.         if (!*strptr)
  475.             return(0);
  476.  
  477.         /* make sure next character is not a digit */
  478.         if (isdigit(*strptr))
  479.             return(0);
  480.  
  481.         tempptr = area_name;
  482.         while (*strptr && (*strptr != LF))
  483.             *tempptr++ = *strptr++;
  484.         *tempptr = NULL;
  485.     }
  486.  
  487.     /* check if name and area code parsed ok */
  488.     if (*mnemonic  && *area_name)
  489.         if (city_rec = get_city(mnemonic)) {
  490.             if (tempptr = getstr(area_name)) {
  491.                 city_rec->cityname = tempptr;
  492.                 return(1);
  493.             }
  494.             else {
  495.                 city_rec->cityname = "No Memory";
  496.                 return(ERROR);
  497.             }
  498.         }
  499.         else
  500.             return(ERROR);    /* no more space in memory */
  501.     else
  502.         return(0);    
  503.          
  504. }
  505.         
  506.  
  507.         
  508.         
  509.          
  510. /* return true if the next 3 digits in the string are numbers */
  511. is_area_code(string)
  512. char *string;
  513. {
  514. #ifdef DEBUG
  515.     printf("      Checking for area code.\n");
  516. #endif
  517.     if (isdigit(*string++))
  518.         if (isdigit(*string++))
  519.             if (isdigit(*string))
  520.                 return(TRUE);
  521.  
  522.     return(FALSE);
  523. }