/* * RDX.C Read indexed text data 29-Oct-86 * * Functions: * * openx(file.str, &dFILE, &xBUF.long, &timebuf) Open index * getx(&dFILE, &xBUF.long, index.int, &tBUF) Read by index * getxx(&dFILE, &xBUF, &tBUF, index.int) Get next line * prx(&dFILE, &xBUF, index.int) Print entry * */ #include #define FALSE 0 #define TRUE 1 /* Open index, slurp xfile, open dfile, set buf[0] to count */ openx(fnam, dfile, buf, timbuf) char *fnam, *timbuf; FILE **dfile; long *buf; { FILE *xtmp; char f1[32], f2[32]; /* Concatenate filenames */ concat(f1, fnam, ".DAT", NULL); concat(f2, fnam, ".IDX", NULL); /* Open data and index files */ if(((*dfile = fopen(f1, "rnu")) == NULL) || ((xtmp = fopen(f2, "rnu")) == NULL)) return(NULL); /* Slurp some data */ if(fget(timbuf, 12, xtmp) == NULL) /* Creation date & time */ return(NULL); if((fget(&buf[0], 4, xtmp) == NULL) || /* Number of records */ (fget(&buf[1], 4, xtmp) == NULL) || /* Record size */ /* Slurp ftell() index */ (fget(&buf[2], (int) (buf[0] * sizeof(long)), xtmp) == NULL)) return(NULL); /* Close index */ fclose(xtmp); return(TRUE); } /* Get first by index */ getx(dfile, xbuf, index, tbuf) FILE *dfile; long xbuf[]; int index; char *tbuf; { int tmp; /* This function calls FCS-11 .POINT */ seek(dfile, xbuf[index+1]); if(fget(&tmp, 2, dfile) == NULL) return(NULL); if(fget(tbuf, (int) xbuf[1], dfile) == NULL) return(NULL); return(TRUE); } /* Get next by index */ getxx(dfile, xbuf, tbuf, index) FILE *dfile; long xbuf[]; char *tbuf; int index; { int tmp; /* Get line index */ fget(&tmp, 2, dfile); /* Check match */ if(tmp != index) return(FALSE); /* Read line */ if(fget(tbuf, (int) xbuf[1], dfile) == NULL) return(FALSE); /* Ok */ return(TRUE); } /* Print entry */ prx(dfile, xbuf, index) FILE *dfile; long *xbuf; int index; { char tbuf[90]; /* Read entry */ if(getx(dfile, xbuf, index, tbuf) == FALSE) return(FALSE); /* Print it */ printf("%s\n", tbuf); /* Loop through rest */ while(getxx(dfile, xbuf, tbuf, index) == TRUE) printf("%s\n", tbuf); return(TRUE); }