home *** CD-ROM | disk | FTP | other *** search
- /* ==( bench/iadelim.c )== */
-
- /* ----------------------------------------------- */
- /* Pro-C Copyright (C) 1988 - 1990 Vestronix Inc. */
- /* Modification to this source is not supported */
- /* by Vestronix Inc. */
- /* All Rights Reserved */
- /* ----------------------------------------------- */
- /* Written BRC 18-Apr-90 */
- /* Modified ??? 28-Mar-90 See comments below */
- /* ----------------------------------------------- */
- /* %W% (%H% %T%) */
-
- /*
- ** Modifications
- **
- ** 18-Apr-90 BRC - code based on earlier IO6.C
- */
-
- /*
- ** -- D R A F T -- Delimited ASCII Sequential I/O via IOGEN.C interface
- **
- ** Beta version ... this is not yet certain -- BRC
- */
-
- /*
- ** This module handles the interface between the i/o calls from within
- ** PRO-C and a delimited ASCII file with varying length records and fields
- */
-
- #include <stdio.h>
- #include <iodef.h>
- #include <iomsg.h>
- #include <proc.io>
- #include <bench.h>
- #include <iosup.h>
-
-
- /* Open control (Geo will correct this into o_open & o_creat 13-Dec-89) */
-
- #ifdef QNX
- #define OPN_FILE(fname) sopen(fname,O_BINARY|O_RDWR|O_CACHE,SH_DENYNO)
- #define CRT_FILE(fname) sopen(fname,O_BINARY|O_RDWR|O_CACHE|O_CREAT,SH_DENYNO,S_IREAD|S_IWRITE)
- #define LOCK_BLK(fd,pos,len) lockf(fd,F_LOCK,len)
- #define LOCK_TST(fd,pos,len) lockf(fd,F_TLOCK,len)
- #define LOCK_REL(fd,pos,len) lockf(fd,F_ULOCK,len)
- #endif
-
-
- #ifdef MSC
- #define OPN_FILE(fname) open(fname,O_BINARY|O_RDWR)
- #define CRT_FILE(fname) open(fname,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)
- #define LOCK_BLK(fd,pos,len) locking(fd,LK_LOCK,len)
- #define LOCK_TST(fd,pos,len) locking(fd,LK_NBLCK,len)
- #define LOCK_REL(fd,pos,len) locking(fd,LK_UNLCK,len)
- #endif
-
-
- #ifdef __TURBOC__
- #define OPN_FILE(fname) open(fname,O_BINARY|O_RDWR)
- #define CRT_FILE(fname) open(fname,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)
- #define LOCK_BLK(fd,pos,len) lock(fd,pos,(long)len)
- #define LOCK_TST(fd,pos,len) lock(fd,pos,(long)len)
- #define LOCK_REL(fd,pos,len) lock(fd,pos,(long)len)
- #endif
-
-
- #ifdef __WATCOMC__
- #define OPN_FILE(fname) open(fname,O_BINARY|O_RDWR|,S_IREAD|S_IWRITE)
- #define CRT_FILE(fname) open(fname,O_BINARY|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)
- #define LOCK_BLK(fd,pos,len) (0)
- #define LOCK_TST(fd,pos,len) (0)
- #define LOCK_REL(fd,pos,len) (0)
- #endif
-
-
- #ifdef LATTICE
- #define OPN_FILE(fname) open(fname,O_RAW|O_RDWR|)
- #define CRT_FILE(fname) open(fname,O_RAW|O_RDWR|O_CREAT,S_IREAD|S_IWRITE)
- #define LOCK_BLK(fd,pos,len) lockf(fd,F_LOCK,len)
- #define LOCK_TST(fd,pos,len) lockf(fd,F_TLOCK,len)
- #define LOCK_REL(fd,pos,len) lockf(fd,F_ULOCK,len)
- #endif
-
-
- #ifdef __ZTC__
- #define OPN_FILE(fname) open(fname,O_RDWR)
- #define CRT_FILE(fname) creat(fname,S_IREAD|S_IWRITE)
- #define LOCK_BLK(fd,pos,len) (0)
- #define LOCK_TST(fd,pos,len) (0)
- #define LOCK_REL(fd,pos,len) (0)
- #endif
-
-
- #ifdef UNIX
- #define OPN_FILE(fname) open(fname,O_RDWR)
- #define CRT_FILE(fname) open(fname,O_RDWR|O_CREAT,0664)
- #define LOCK_BLK(fd,pos,len) lockf(fd,F_LOCK,len)
- #define LOCK_TST(fd,pos,len) lockf(fd,F_TLOCK,len)
- #define LOCK_REL(fd,pos,len) lockf(fd,F_ULOCK,len)
- #endif
-
-
- #ifndef USELOCK
- #undef USELOCK /* Dummy out this if no locking */
- #endif
-
-
- /****************************************************************************/
- /* Function Prototypes */
- /****************************************************************************/
-
- #ifdef ANSI
-
- static int i_addrec(int, char *);
- static int i_close_file(int, char *);
- static int i_commit(int, char *);
- static int i_delrec(int, char *);
- static int i_filename(int, char *);
- static int i_findkey(int, char *);
- static int i_firstkey(int, char *);
- static int i_init_file(int, char *);
- static int i_lastkey(int, char *);
- static int i_lockrec(int, char *);
- static int i_login(int, char *);
- static int i_logoff(int, char *);
- static int i_nextrec(int, char *);
- static int i_open_file(int, char *);
- static int i_prevrec(int, char *);
- static int i_rereadrec(int, char *);
- static int i_rollback(int, char *);
- static int i_selectinx(int, char *);
- static int i_transact(int, char *);
- static int i_unlock_rec(int, char *);
- static int i_updrec(int, char *);
-
- static int uniqueness(int, char *, long);
- static int eatline(FILE *fp);
- static int eatsep(FILE *fp);
- static int keycmp(int, char *);
- static int read_rec(int, char *);
- static int slipback(int);
- static int write_rec(int, char *);
-
- extern char *io_tran(int);
- extern void assign_IO_AD(int);
-
- #else
-
- static int i_addrec();
- static int i_close_file();
- static int i_commit();
- static int i_delrec();
- static int i_filename();
- static int i_findkey();
- static int i_firstkey();
- static int i_init_file();
- static int i_lastkey();
- static int i_lockrec();
- static int i_login();
- static int i_logoff();
- static int i_nextrec();
- static int i_open_file();
- static int i_prevrec();
- static int i_rereadrec();
- static int i_rollback();
- static int i_selectinx();
- static int i_transact();
- static int i_unlock_rec();
- static int i_updrec();
-
- static int uniqueness();
- static int eatline();
- static int eatsep();
- static int keycmp();
- static int read_rec();
- static int slipback();
- static int write_rec();
-
- extern char *io_tran();
- extern void assign_IO_AD();
-
- #endif
-
-
-
- /*
- ** Plus any externs required by the file manager for error codes etc.
- **
- ** Definition for Each Open File
- */
-
- /* structure for ASCII file specific values */
- struct asc_def
- {
- FILE *fp; /* fdopen() return */
- long offset; /* Current offset into file for Read/Write */
- char *localbuf; /* Local Record Buffer for Read/Write */
- char *localbuf2; /* Spare Local Record Buffer for Read/Write */
- long lockoff;
- };
-
- #define COMMENT_CHAR '#'
-
- static struct asc_def afd[MAX_FILES];
-
-
- /*
- **
- ** Interface Functions
- **
- */
-
-
- /*
- ** Sets up File Name extension - dummy function here
- */
- static int
- i_filename(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- /*
- ** File initialization - all descriptors start out as -1
- */
- static int
- i_init_file(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- int n;
-
- /* initialize all descriptors to -1 */
- for (n = 0; n < MAX_FILES; n++)
- {
- fd[n].fd_num = -1;
- afd[n].fp = (FILE *)0;
- }
-
- return(IOGOOD);
- }
-
-
- /*
- ** File open function - required parameters are in fd structure
- */
- static int
- i_open_file(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int fds, iostat = IOGOOD;
-
- /* delete the file if the mode is OUTPUT */
- if (fptr->openmode & OUTPUT_FLAG)
- unlink(fptr->filname);
-
- if ((fds = OPN_FILE(fptr->filname)) < 0)
- fds = CRT_FILE(fptr->filname);
-
- /* failed to Open or Create */
- if (fds < 0)
- {
- errmsg(FileNotFound_s, fptr->filname);
- iostat = IOBADOPEN;
- }
- else
- {
- if (!(aptr->fp = fdopen(fds, "r+")))
- iostat = IOBADOPEN;
- else
- {
- fptr->fd_num = fds;
- aptr->localbuf = alloc(fptr->rec_len);
- aptr->localbuf2 = alloc(fptr->rec_len);
- aptr->lockoff = -1L;
- }
- }
-
- return(iostat);
- }
-
-
- /*
- ** Close down FILE pointer and blot out the descriptor as well
- */
- static int
- i_close_file(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- free(afd[fd_sys].localbuf);
- afd[fd_sys].localbuf = NULL;
-
- free(afd[fd_sys].localbuf2);
- afd[fd_sys].localbuf2 = NULL;
-
- fclose(afd[fd_sys].fp);
- afd[fd_sys].fp = (FILE *)0;
- fd[fd_sys].fd_num = -1;
-
- return(IOGOOD);
- }
-
-
- /*
- ** Unlock the current record if the index changes -- the rest in IOGEN.C
- */
- static int
- i_selectinx(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- if (afd[fd_sys].lockoff >= 0L)
- /* just paranioa so ignore the return value */
- i_unlock_rec(fd_sys, NULL);
-
- return(IOGOOD);
- }
-
-
- /*
- ** Retrieve the first record matching an EXACT or a PARTIAL key spec.
- */
- static int
- i_findkey(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int cmp_ok, iostat = IOGOOD;
-
- /* Set up record offsets */
- cmp_ok = keycmp(fd_sys, buffer);
-
- fseek(aptr->fp, 0L, SEEK_SET);
- do
- {
- if ((iostat = read_rec(fd_sys, aptr->localbuf)) == IOGOOD)
- {
- cmp_ok = keycmp(-1, NULL);
- cmp_ok = fptr->exact ? cmp_ok != 0 : cmp_ok < 0;
- }
- }
- while (iostat == IOGOOD && cmp_ok);
-
- if (iostat == IOEOF)
- iostat = IONOKEY;
- else if (iostat == IOGOOD && (iostat = i_lockrec(fd_sys, NULL)) == IOGOOD)
- memcpy(buffer, aptr->localbuf, fptr->rec_len);
-
- return(iostat);
- }
-
-
- /*
- ** Find the first physical record in the file
- */
- static int
- i_firstkey(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int iostat = IOGOOD;
-
- fseek(aptr->fp, 0L, SEEK_SET);
- if ((iostat = read_rec(fd_sys, aptr->localbuf)) == IOGOOD)
- if ((iostat = i_lockrec(fd_sys, NULL)) == IOGOOD)
- memcpy(buffer, aptr->localbuf, fptr->rec_len);
-
- return(iostat);
- }
-
-
- /*
- ** Find the last physical record in the file
- */
- static int
- i_lastkey(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int iostat = IOGOOD;
-
- fseek(aptr->fp, 0L, SEEK_END);
- aptr->offset = ftell(aptr->fp);
- if ((iostat = slipback(fd_sys)) == IOGOOD)
- iostat = read_rec(fd_sys, aptr->localbuf);
-
- if (iostat == IOGOOD && (iostat = i_lockrec(fd_sys, NULL)) == IOGOOD)
- memcpy(buffer, aptr->localbuf, fptr->rec_len);
-
- return(iostat);
- }
-
-
- /*
- ** Find the next record in the file
- **
- ** - IOGEN does checking for blocks of duplicate key records in an EXACT
- ** search but sequential files are not indexed; thus this function
- ** will continue traversing the file to find all matches
- */
- static int
- i_nextrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int cmp_ok, iostat = IONOKEY;
- long lastoff;
-
- /* Set up record offsets */
- cmp_ok = keycmp(fd_sys, buffer);
-
- if (aptr->offset >= 0L)
- {
- fseek(aptr->fp, aptr->offset, SEEK_SET);
- do
- {
- lastoff = aptr->offset;
- if ((iostat = read_rec(fd_sys, aptr->localbuf)) == IOGOOD)
- if (lastoff == aptr->offset)
- iostat = read_rec(fd_sys, aptr->localbuf);
- cmp_ok = fptr->exact ? keycmp(-1, NULL) != 0 : FALSE;
- }
- while (iostat == IOGOOD && cmp_ok);
- }
-
- if (iostat == IOEOF)
- aptr->offset = lastoff;
-
- if (fptr->exact && iostat == IOEOF)
- iostat = IONONEXT;
-
- if (iostat == IOGOOD && (iostat = i_lockrec(fd_sys, NULL)) == IOGOOD)
- memcpy(buffer, aptr->localbuf, fptr->rec_len);
-
- return(iostat);
- }
-
-
- /*
- ** Find the previous record in the file
- **
- ** - IOGEN does checking for blocks of duplicate key records in an EXACT
- ** search but sequential files are not indexed; thus this function
- ** will continue traversing the file to find all matches
- */
- static int
- i_prevrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int cmp_ok, iostat = IONOKEY;
-
- /* Set up record offsets */
- cmp_ok = keycmp(fd_sys, buffer);
-
- if (aptr->offset >= 0L)
- {
- fseek(aptr->fp, aptr->offset, SEEK_SET);
- do
- {
- if ((iostat = slipback(fd_sys)) != IOGOOD)
- break;
- else if ((iostat = read_rec(fd_sys, aptr->localbuf)) != IOGOOD)
- break;
- else
- cmp_ok = fptr->exact ? keycmp(-1, NULL) != 0 : FALSE;
- }
- while (iostat == IOGOOD && cmp_ok);
- }
-
- if (fptr->exact && iostat == IOTOF)
- iostat = IONONEXT;
-
- if (iostat == IOGOOD && (iostat = i_lockrec(fd_sys, NULL)) == IOGOOD)
- memcpy(buffer, aptr->localbuf, fptr->rec_len);
-
- return(iostat);
- }
-
-
- /*
- ** re-read/reposition record pointer function - dummy function here
- */
- static int
- i_rereadrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- /*
- ** ADD a the record in BUFFER
- */
- static int
- i_addrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int iostat;
-
- if ((iostat = uniqueness(fd_sys, buffer, -1L)) == IOGOOD)
- {
- do
- {
- fseek(aptr->fp, 0L, SEEK_END);
- aptr->offset = ftell(aptr->fp);
- }
- while ((iostat = i_lockrec(fd_sys, NULL)) == IOLOCKED);
-
- if (iostat == IOGOOD)
- {
- if ((iostat = write_rec(fd_sys, buffer)) == IOGOOD)
- iostat = i_unlock_rec(fd_sys, NULL);
- else
- i_unlock_rec(fd_sys, NULL);
- }
- }
-
- return(iostat);
- }
-
-
- /*
- ** Update the locked record with the contents of BUFFER
- */
- static int
- i_updrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- long lock;
- int iostat = IONOLOCK;
-
- if ((lock = aptr->lockoff) >= 0L &&
- (iostat = uniqueness(fd_sys, buffer, lock)) == IOGOOD)
- {
- if (aptr->lockoff != lock)
- {
- aptr->offset = lock;
- iostat = i_lockrec(fd_sys, NULL);
- }
-
- if (iostat == IOGOOD)
- {
- fseek(aptr->fp, lock, SEEK_SET);
- fputc(COMMENT_CHAR, aptr->fp);
- iostat = i_unlock_rec(fd_sys, NULL);
-
- do
- {
- fseek(aptr->fp, 0L, SEEK_END);
- aptr->offset = ftell(aptr->fp);
- }
- while ((iostat = i_lockrec(fd_sys, NULL)) == IOLOCKED);
-
- if (iostat == IOGOOD)
- {
- if ((iostat = write_rec(fd_sys, buffer)) == IOGOOD)
- iostat = i_unlock_rec(fd_sys, NULL);
- else
- i_unlock_rec(fd_sys, NULL);
- }
- }
- }
-
- if (iostat == IONOKEY)
- {
- if (aptr->lockoff != lock)
- {
- aptr->offset = lock;
- iostat = i_lockrec(fd_sys, NULL);
- }
- }
-
- return(iostat);
- }
-
-
- /*
- ** Delete the locked record -- records are not actually removed,
- ** they are just commented out
- */
- static int
- i_delrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int iostat = IONOLOCK;
-
- /* Deleted records are just commented out and left */
-
- if (aptr->lockoff >= 0L)
- {
- fseek(aptr->fp, aptr->lockoff, SEEK_SET);
- fputc(COMMENT_CHAR, aptr->fp);
- iostat = i_unlock_rec(fd_sys, NULL);
- }
-
- return(iostat);
- }
-
-
- /*
- ** LOCK the current record
- */
- static int
- i_lockrec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int iostat = IOGOOD;
-
- #ifdef USELOCK
- if (fptr->lockmode)
- {
- if (aptr->lockoff >= 0L)
- i_unlock_rec(fd_sys);
-
- fflush(aptr->fp);
-
- if (lseek(fptr->fd_num, aptr->offset, SEEK_SET) != aptr->offset)
- iostat = IOERROR;
- else if (fptr->lockmode == -1);
- {
- if (LOCK_BLK(fptr->fd_num, aptr->offset, fptr->rec_len))
- iostat = IOLOCKED;
- }
- else
- {
- if (LOCK_TST(fptr->fd_num, aptr->offset, fptr->rec_len))
- iostat = IOLOCKED;
- }
- }
- #endif
-
- aptr->lockoff = aptr->offset;
-
- return(iostat);
- }
-
-
- /*
- ** UNLOCK the currently locked record
- */
- static int
- i_unlock_rec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int iostat = IOGOOD;
-
- #ifdef USELOCK
- if (aptr->lockoff >= 0L)
- {
- fflush(aptr->fp);
-
- if (lseek(fptr->fd_num, aptr->lockoff, SEEK_SET) != aptr->lockoff)
- iostat = IOERROR;
- else if (LOCK_REL(fptr->fd_num, aptr->lockoff, fptr->rec_len))
- iostat = IONOLOCK;
- }
- #endif
-
- aptr->lockoff = -1L;
-
- return(iostat);
- }
-
-
- /*
- ** Login - dummy call
- */
- static int
- i_login(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- /*
- ** Logoff - dummy call
- */
- static int
- i_logoff(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- /*
- ** End (Commit) transaction - dummy call
- */
- static int
- i_commit(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- /*
- ** Rollback (Abort) transaction - dummy call
- */
- static int
- i_rollback(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- /*
- ** Start transaction - dummy call
- */
- static int
- i_transact(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- return(IOGOOD);
- }
-
-
- static int
- uniqueness(fd_sys, buffer, offset)
- int fd_sys;
- char *buffer;
- long offset;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int cur_key, exact, lockmode;
- int key, iostat = IOGOOD;
-
- for (key = 0; key < fptr->key_cnt; key++)
- {
- if (fptr->keys[key].keytype == KEY_UNIQUE)
- {
- memcpy(aptr->localbuf2, buffer, fptr->rec_len);
-
- cur_key = fptr->cur_key, fptr->cur_key = key;
- exact = fptr->exact, fptr->exact = 1;
- lockmode = fptr->lockmode, fptr->lockmode = 0;
-
- iostat = i_findkey(fd_sys, aptr->localbuf2);
- while (iostat == IOGOOD && aptr->offset == offset)
- iostat = i_nextrec(fd_sys, aptr->localbuf2);
-
- fptr->cur_key = cur_key;
- fptr->exact = exact;
- fptr->lockmode = lockmode;
-
- if (iostat == IONOKEY)
- iostat = IOGOOD;
- else if (iostat == IOGOOD)
- iostat = IODUP;
- }
- if (iostat != IOGOOD)
- break;
- }
-
- return(iostat);
- }
-
-
- static int
- keycmp(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- static char *src, *key;
- static int klen, kstart, ktype;
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- struct keyinfo *kptr = &fptr->keys[fptr->cur_key];
- int cmp = -1, iostat = IOGOOD;
-
- /* Set up record offsets */
- if (fd_sys >= 0)
- {
- ktype = fptr->flds[fptr->cur_key].fldtype;
- klen = fptr->flds[fptr->cur_key].fldlen;
- if (ktype == DATTYP && klen == sizeof(long))
- ktype = LNGTYP;
- kstart = fptr->flds[fptr->cur_key].fldstart;
- key = buffer + kstart;
- src = aptr->localbuf + kstart;
- }
-
- /* treat unknown types as unequal (less than) */
-
- switch (ktype)
- {
- case CHRTYP:
- case DATTYP:
- case MEMTYP:
- cmp = strncmp(src, key, klen);
- break;
-
- case INTTYP:
- case LNGTYP:
- if (klen == sizeof(short))
- cmp = (int)(*(short *)src - *(short *)key);
- else if (klen == sizeof(int))
- cmp = (int)(*(int *)src - *(int *)key);
- else if (klen == sizeof(long))
- cmp = (int)(*(long *)src - *(long *)key);
- break;
-
- case FLTTYP:
- case DBLTYP:
- if (klen == sizeof(float))
- cmp = (int)(*(float *)src - *(float *)key);
- else if (klen == sizeof(double))
- cmp = (int)(*(double *)src - *(double *)key);
- break;
- }
-
- if (cmp < -1)
- cmp = -1;
- else if (cmp > 1)
- cmp = 1;
-
- return(cmp);
- }
-
-
- /*
- ** Slurps up a string of field separators and returns the next char.
- */
- static int
- eatsep(fp)
- FILE *fp;
- {
- int c;
-
- while ((c = getc(fp)) != EOF || !feof(fp))
- {
- if (c == ',' || c == ';' || c == ':' || c == ' ')
- continue;
- else if (c == '\n' || isprint(c))
- break;
- }
-
- return(c);
- }
-
-
- /*
- ** Gobble the remainder of a line
- */
- static int
- eatline(fp)
- FILE *fp;
- {
- int c;
-
- while ((c = getc(fp)) != EOF || !feof(fp))
- if (c == '\n')
- break;
-
- return(c);
- }
-
-
- /*
- ** Read an ASCII delimited record, skipping comments
- */
- static int
- read_rec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int field, i, c, c2, iostat = IOGOOD;
- int fstart, flen, ftype;
- char *fbuf, *fname;
- long bigint;
- double bigfloat;
- char buf[30];
-
- do
- {
- aptr->offset = ftell(aptr->fp);
- if ((c = eatsep(aptr->fp)) == COMMENT_CHAR)
- c = eatline(aptr->fp);
- }
- while (c == '\n');
-
- for (field = 0; field < fptr->fld_cnt; field++)
- {
- ftype = fptr->flds[field].fldtype;
- flen = fptr->flds[field].fldlen;
- if (ftype == DATTYP && flen == sizeof(long))
- ftype = LNGTYP;
- fstart = fptr->flds[field].fldstart;
- fbuf = buffer + fstart;
- fname = fptr->flds[field].fldname;
-
- if (field)
- c = eatsep(aptr->fp);
-
- if (c == EOF && feof(aptr->fp))
- iostat = field ? IOERROR : IOEOF;
- else
- {
- ungetc(c, aptr->fp);
-
- strcpy(buf, "UNKNOWN");
- switch (ftype)
- {
- case CHRTYP:
- case DATTYP:
- case MEMTYP:
- if ((c = fgetc(aptr->fp)) != '"')
- iostat = IOERROR;
- else
- {
- c = i = 0;
- do
- {
- c2 = c, c = fgetc(aptr->fp);
- if (c == EOF && feof(aptr->fp))
- iostat = IOEOF;
- else if (c2 == '"' && c != '"')
- {
- ungetc(c, aptr->fp);
- if (i < flen)
- fbuf[i] = 0;
- break;
- }
- else if (c != '"' || c2 == '"')
- {
- if (i < flen)
- fbuf[i] = (char)c;
- i++;
- if (c == '"')
- c = 0;
- }
- }
- while (iostat == IOGOOD);
- }
- sprintf(buf, "\"%s\"", fbuf);
- break;
-
- case INTTYP:
- case LNGTYP:
- bigint = 0L;
- if (fscanf(aptr->fp, "%ld", &bigint) != 1)
- iostat = IOERROR;
- else if (flen == sizeof(short))
- *(short *)fbuf = (short)bigint;
- else if (flen == sizeof(int))
- *(int *)fbuf = (int)bigint;
- else if (flen == sizeof(long))
- *(long *)fbuf = bigint;
- sprintf(buf, "%ld", bigint);
- break;
-
- case FLTTYP:
- case DBLTYP:
- bigfloat = 0.0;
- if (fscanf(aptr->fp, "%lg", &bigfloat) != 1)
- iostat = IOERROR;
- else if (flen == sizeof(float))
- *(float *)fbuf = (float)bigfloat;
- else if (flen == sizeof(double))
- *(double *)fbuf = bigfloat;
- sprintf(buf, "%lg (FP)", bigfloat);
- break;
- }
- }
-
- if (iostat != IOGOOD)
- break;
- }
-
- /* this is so multiple EOF's don't mess the pointer */
- if (iostat != IOEOF)
- {
- if (iostat != IOGOOD)
- aptr->offset = -1L;
- else if ((c = eatsep(aptr->fp)) != '\n')
- iostat = IOERROR;
- }
-
- return(iostat);
- }
-
-
- /*
- ** Write an ASCII delimited record
- */
- static int
- write_rec(fd_sys, buffer)
- int fd_sys;
- char *buffer;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int field, i, c, iostat = IOGOOD;
- int fstart, flen, ftype;
- char *fbuf, *fname;
- long bigint;
- double bigfloat;
-
- fseek(aptr->fp, aptr->offset, SEEK_SET);
-
- for (field = 0; field < fptr->fld_cnt; field++)
- {
- ftype = fptr->flds[field].fldtype;
- flen = fptr->flds[field].fldlen;
- if (ftype == DATTYP && flen == sizeof(long))
- ftype = LNGTYP;
- fstart = fptr->flds[field].fldstart;
- fbuf = buffer + fstart;
- fname = fptr->flds[field].fldname;
-
- if (field)
- fputc(',', aptr->fp);
-
- switch (ftype)
- {
- case CHRTYP:
- case DATTYP:
- case MEMTYP:
- fputc('"', aptr->fp);
- for (i = 0; i < flen; i++)
- {
- if (!(c = fbuf[i]))
- break;
- else if (c == '"')
- fputc('"', aptr->fp);
- fputc(c, aptr->fp);
- }
- fputc('"', aptr->fp);
- break;
-
- case INTTYP:
- case LNGTYP:
- bigint = 0L;
- if (flen == sizeof(short))
- bigint = (long)*(short *)fbuf;
- else if (flen == sizeof(int))
- bigint = (long)*(int *)fbuf;
- else if (flen == sizeof(long))
- bigint = *(long *)fbuf;
- else
- fprintf(aptr->fp, "<len=%d>", flen);
- fprintf(aptr->fp, "%ld", bigint);
- break;
-
- case FLTTYP:
- case DBLTYP:
- bigfloat = 0.0;
- if (flen == sizeof(float))
- bigfloat = (double)*(float *)fbuf;
- else if (flen == sizeof(double))
- bigfloat = *(double *)fbuf;
- else
- fprintf(aptr->fp, "<len=%d>", flen);
- fprintf(aptr->fp, "%lg", bigfloat);
- break;
-
- default:
- fputc('?', aptr->fp);
- break;
- }
- }
- fputc('\n', aptr->fp);
- fflush(aptr->fp);
-
- return(IOGOOD);
- }
-
-
- static int
- slipback(fd_sys)
- int fd_sys;
- {
- struct fd_def *fptr = &fd[fd_sys];
- struct asc_def *aptr = &afd[fd_sys];
- int c, iostat = IOGOOD;
- long topoff, here, startoff;
- int inc = 0;
-
- topoff = here = startoff = aptr->offset;
-
- do
- {
- /*
- errmsg("SLIPBACK-%d: looping (topoff=%ld, here=%ld, startoff=%ld)", ++inc, topoff, here, startoff);
- */
-
- if (here < startoff)
- {
- /* find the next real record */
- fseek(aptr->fp, topoff = here, SEEK_SET);
- /* c = (topoff > 0L) ? eatline(aptr->fp) : '\n'; */
- c = eatline(aptr->fp);
- while (c == '\n')
- {
- here = ftell(aptr->fp);
- if ((c = eatsep(aptr->fp)) == COMMENT_CHAR)
- c = eatline(aptr->fp);
- }
-
- if (here == startoff)
- break;
- else if (here > startoff)
- iostat = IOTOF;
- }
- else
- {
- /* backup a bit */
- if (topoff > BUFSIZ / 2)
- topoff -= BUFSIZ / 2;
- else if (topoff)
- topoff = 0L;
- else
- {
- iostat = IOTOF;
- break;
- }
-
- /* find the next real record */
- fseek(aptr->fp, topoff, SEEK_SET);
- c = (topoff > 0L) ? eatline(aptr->fp) : '\n';
- while (c == '\n')
- {
- here = ftell(aptr->fp);
- if ((c = eatsep(aptr->fp)) == COMMENT_CHAR)
- c = eatline(aptr->fp);
- }
- }
-
- if (c == EOF && feof(aptr->fp))
- iostat = IOERROR;
- }
- while (iostat == IOGOOD);
-
- if (iostat == IOGOOD)
- {
- aptr->offset = topoff;
- fseek(aptr->fp, aptr->offset, SEEK_SET);
- }
- else if (iostat != IOTOF)
- aptr->offset = -1L;
-
- /*
- errmsg("SLIPBACK=%d: offset=%ld iostat=%s (topoff=%ld, here=%ld, startoff=%ld)", inc, aptr->offset, io_tran(iostat), topoff, here, startoff);
- */
-
- return(iostat);
- }
-
-
- /*
- ** Assign section
- */
-
-
- void assign_IO_AD(dbnum)
- int dbnum;
- {
- Fntab[dbnum - 1][0] = (int(*)())0; /* Empty */
- Fntab[dbnum - 1][1] = i_init_file;
- Fntab[dbnum - 1][2] = i_open_file;
- Fntab[dbnum - 1][3] = i_close_file;
- Fntab[dbnum - 1][4] = i_addrec;
- Fntab[dbnum - 1][5] = i_delrec;
- Fntab[dbnum - 1][6] = i_findkey;
- Fntab[dbnum - 1][7] = i_firstkey;
- Fntab[dbnum - 1][8] = i_lastkey;
- Fntab[dbnum - 1][9] = i_lockrec;
- Fntab[dbnum - 1][10] = i_nextrec;
- Fntab[dbnum - 1][11] = i_prevrec;
- Fntab[dbnum - 1][12] = i_unlock_rec;
- Fntab[dbnum - 1][13] = i_updrec;
- Fntab[dbnum - 1][14] = i_commit;
- Fntab[dbnum - 1][15] = i_login;
- Fntab[dbnum - 1][16] = i_logoff;
- Fntab[dbnum - 1][17] = i_rollback;
- Fntab[dbnum - 1][18] = i_transact;
- Fntab[dbnum - 1][19] = i_selectinx;
- Fntab[dbnum - 1][20] = i_rereadrec;
- Fntab[dbnum - 1][21] = i_filename;
- }
-