home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
cpm
/
pcpursut
/
pisrc.ark
/
READDB.C
< prev
Wrap
Text File
|
1988-07-14
|
11KB
|
523 lines
/*******************************************************************
* read_db.c - read pcp database into memory
* Updated to support mnemonics 1-9-88 D.C.
*
********************************************************************/
#include "stdio.h"
#include "fcntl.h"
#include "cpcp.h"
#define IN_LENGTH 120 /* length of input buffer for each line */
/* #define DEBUG */
extern struct cityrec *cities; /* list of cities */
/* read database into memory from text file
* return -1 if file does not exist
*/
int read_db(db_name)
char *db_name;
{
static int fd; /* file descripter */
static FILE *fp; /* file pointer for database */
char tempstr[120]; /* place to put line from database */
static int linenum;
static char *instring;
linenum = 0;
instring = tempstr;
printf("\n Reading database..");
if (-1 != (fd = open(db_name, O_RDONLY ))) {
#ifdef DEBUG
printf("Line num = %d.\n", linenum);
#endif
while (gstr(instring, IN_LENGTH - 1, fd)) {
if (parse(instring) == ERROR)
return(-1);;
}
printf("\n");
close(fd);
}
else {
return (-1);
}
return(0);
}
gstr(string, numbytes, fd)
char *string; /* where to put data */
int numbytes; /* max number of bytes, ignored for now */
int fd; /* file descripter */
{
static int count; /* bytes left in buffer */
static char *buf; /* buffer for data */
static char *ptr;
static int notfirst;
extern char *alloc();
if (!notfirst++) {
buf = alloc(1024);
}
for (;;) {
if (!count) {
printf(".");
if (0 == (count = read(fd, buf, 1024)))
return(0);
else
ptr = buf;
}
if (*ptr == 13) {
*string = NULL;
--count;
ptr++;
return(1);
} else if (*ptr == 0x0a) {
--count;
ptr++;
} else {
*string++ = *ptr++;
--count;
}
}
}
/* read each input line, parse it and put results in table */
/* return ERROR if error in memory allocation */
parse(string)
char *string; /* input line */
{
static int status; /* line status -1 = error */
#ifdef DEBUG
printf(" Entering Parse.\n");
#endif
#ifdef DEBUG
printf("In string is:\n");
printf("%s", string);
#endif
/* strip off spaces and tabs at beinning of the line */
while (*string && iswhite(*string))
string++;
#ifdef DEBUG
printf("In string is:\n");
printf("%s", string);
#endif
/* return if end of the string */
if (!*string)
return(0);
status = bbs_name(string);
if (!status) {
status = area_name(string);
}
return(status);
}
/* check for BBS mnemonic, phone number , name and baud rate
* it should appear as:
* "xxxxx [1-][aaa-]ppp-pppp BBS_NAME [mm/dd/yy] [baudrate] [other]"
* where
* xxxxx = area mnemonic
* ppp-pppp = phone number
* BBS_NAME = string with BBS name
* mm/dd/yy = month day and year of verification (optional)
* baudrate = baudrate (300,1200 or 2400)
* other = only present if date or baudrate exist
* has comments on baudrate
* examples are:
* "NJNEW 792-9131 Randy's Rothen"
* "NJNEW 884-2568 AT&T INFO 09/01/86 M"
* "NJNEW 783-7458 Shelter 09/26/86 2400 UD
* "NJNEW 1-301-940-3456 The Place
* 1200 baud will be assumed if no baudrate exists
*/
int bbs_name(string)
char *string;
{
static int baudrate; /* baudrate to use, default = 1200 */
static char *strptr; /* current position in string */
static char number[12]; /* bbs phone number */
static char bbsname[120]; /* bbs name */
static char *tempptr; /* temp. pointer */
static int i;
static struct bbs_rec *bbs_link;
static char mnemonic[20]; /* area mnemonic */
static char *mptr;
extern struct bbs_rec *getbbs();
extern char *getstr();
#ifdef DEBUG
printf(" Checking for bbsname .\n");
#endif
/* skip starting whitespace */
while (iswhite(*string))
string++;
strptr = string;
baudrate = BAUD1200;
mptr = mnemonic;
/* check for mnemonic first */
if (!is_mnemonic(string))
return(0);
#ifdef DEBUG
printf("Mnemonic code found \n");
#endif
/* skip mnemonic and copy it to string */
while (!iswhite(*strptr))
*mptr++ = *strptr++;
*mptr = NULL;;
while (iswhite(*strptr))
strptr++;
/* now I expect the phone number */
if (!is_p_num(strptr, number)) {
return(0);
}
#ifdef DEBUG
printf("Phone fond\n");
#endif
/* skip number in string */
while (!iswhite(*strptr))
strptr++;
/* now get bbs name and check for date or phone number */
tempptr = bbsname;
while (iswhite(*strptr))
strptr++;
if (!*strptr)
return(0);
/* get bbs name */
while (*strptr && (*strptr != '\n') && (!isdigit(*strptr) ||
(!is_date(strptr) && !is_baud(strptr))) ) {
*tempptr++ = *strptr++;
}
*tempptr = NULL;
#ifdef DEBUG
printf("BBS name is:\n");
printf("%s\n",tempptr);
#endif
/* take whitespace off the end of the bs name */
while (iswhite(*(--tempptr)))
*tempptr = NULL;
if (is_date(strptr)) {
strptr += 8;
#ifdef DEBUG
printf("Date found\n");
#endif
while (iswhite(*strptr))
strptr++;
if (is_baud(strptr))
baudrate = is_baud(strptr);
}
else if (is_baud(strptr))
baudrate = is_baud(strptr);
if (bbs_link = getbbs(mnemonic)) {
if (bbs_link->bbs_name = getstr(bbsname)) {
strncpy(bbs_link->bbs_number, number, BBS_NUM_LEN );
bbs_link->bbs_baud = baudrate;
}
else {
bbs_link->bbs_name = "No Name";
return(ERROR);
}
}
else
return (ERROR);
return(1);
}
/* check for mnemonic by looking for text followed by whitespace & number */
/* don't let it start with a digit */
int is_mnemonic(string)
char *string;
{
if (isdigit(*string))
return(0);
/* skip text */
while (*string && !iswhite(*string))
string++;
/* skip whitespace */
while(*string && iswhite(*string))
string++;
/* better be a number here */
return(*string && isdigit(*string));
}
/* check if string has date and return true if so */
/* date is recognized by dd/dd/dd */
int is_date(string)
char *string;
{
static int i, j;
#ifdef DEBUG
printf(" Checking for date.\n");
#endif
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++) {
if (((j == 2) && (*string == '/')) || ((i == 2) && (j == 2)))
;
else if (isdigit(*string))
;
else
return (FALSE);
string++;
}
return (TRUE);
}
/* check if string has baud rate and if so then return baud rate */
int is_baud(string)
char *string;
{
#ifdef DEBUG
printf(" Looking for baud reate.\n");
#endif
while (iswhite(*string))
++string;
if (!(strncmp(string, "300", 3)))
return (BAUD300);
else if (!(strncmp(string, "1200", 4)))
return (BAUD1200);
else if (!(strncmp(string, "2400", 4)))
return (BAUD2400);
else
return (0);
}
/* return true if phone number of the form [1][-][aaa][-]ppp[-]pppp
* and also return the passed array with the phone number in ascii
* with up to 11 digits
*/
int is_p_num(string, number)
char *string;
char *number;
{
static int num_count = 0;
static int i;
#ifdef DEBUG
printf(" Looking for phone number.\n");
printf("String is ->\n");
printf("%s\n",string);
#endif
/* check for '1-' */
if (*string == '1') {
strncpy(number, string, 1);
string += 1;
number += 1;
if (*string == '-')
string++;
}
/* check for 1st 3 numbers and - */
if (is_area_code(string)) {
strncpy(number, string, 3);
string += 3;
number += 3;
}
else
return(FALSE);
if (*string == '-')
string++;
else
return(FALSE);
/* check for next 3 being numbers */
if (is_area_code(string)) {
strncpy(number, string, 3);
string += 3;
number += 3;
if (*string == '-') {
string++;
/* expect 4 more digits */
if (is_area_code(string)) {
strncpy(number, string, 3);
string += 3;
number += 3;
}
else return(FALSE);
}
/* last digit */
if (isdigit(*string))
*number++ = *string++;
else return (FALSE);
}
else return(FALSE);
*number = NULL;
return(TRUE);
}
/* check for area code name and if so, enter into the list
* The area code name is recognized 2 different ways:
* 1. by the string "** BBS Listing For +xxxxx" where xxxxx is
* the area code. Example :
* "** BBS Listing For +TXHOU"
* 2. by the string "+xxxxx City_Name" where xxxxx is the area
* code and City_Name the name of the city. Example:
* "+DCWAS Washington, D.C."
* If found, the area code and name area inserted into the table.
*/
area_name(string)
char *string;
{
extern struct citylink *get_city();
extern char *getstr();
struct cityrec *city_rec; /* city area code
and name */
static char mnemonic[20]; /* mnemonic */
static char *numptr; /* pointer to number */
static char *strptr; /* pointer to current position in line */
char temp[120]; /* area code name */
static char *tempptr;
static char *area_name;
area_name = temp;
#ifdef DEBUG
printf(" Check for area name.\n");
#endif
numptr = NULL;
strptr = string;
*area_name = NULL;
*mnemonic = NULL;
/* gobble up whitespace */
while (iswhite(*strptr))
strptr++;
/* check for "** Listing For +xxxxx" where xxxxx is area mnemonic */
if ((*strptr == '*') && (*(strptr + 1) == '*')) {
strptr++, strptr++;
/* search up to next after "**" */
while (iswhite(*strptr))
strptr++;
/* make sure we aren't at the end of the string */
if (!*strptr)
return(0);
/* get string and look for number */
tempptr = area_name;
numptr = mnemonic;
while (*strptr && (*strptr != LF) && (*strptr != '+'))
*tempptr++ = *strptr++;
if (*strptr == '+') {
strptr++; /* skip '+' */
while (*strptr && !iswhite(*strptr)) {
*tempptr++ = *strptr;
*numptr++ = *strptr++;
}
*tempptr = NULL;
*numptr = NULL;
}
else
return(0); /* premature end of string */
}
else if (*strptr == '+') {
strptr++;
numptr = mnemonic;
while (*strptr && !iswhite(*strptr))
*numptr++ = *strptr++;
*numptr = NULL;
/* gobble up whitespace */
while (iswhite(*strptr))
strptr++;
if (!*strptr)
return(0);
/* make sure next character is not a digit */
if (isdigit(*strptr))
return(0);
tempptr = area_name;
while (*strptr && (*strptr != LF))
*tempptr++ = *strptr++;
*tempptr = NULL;
}
/* check if name and area code parsed ok */
if (*mnemonic && *area_name)
if (city_rec = get_city(mnemonic)) {
if (tempptr = getstr(area_name)) {
city_rec->cityname = tempptr;
return(1);
}
else {
city_rec->cityname = "No Memory";
return(ERROR);
}
}
else
return(ERROR); /* no more space in memory */
else
return(0);
}
/* return true if the next 3 digits in the string are numbers */
is_area_code(string)
char *string;
{
#ifdef DEBUG
printf(" Checking for area code.\n");
#endif
if (isdigit(*string++))
if (isdigit(*string++))
if (isdigit(*string))
return(TRUE);
return(FALSE);
}