#include "stdio.h" #include "ctype.h" #include "tib.h" static struct reftype{ char reffile[MAXSTR]; long int start, length; }; char *malloc(); char *rindex(); char *stripkeys(); int fetchref(); /* locate(keys, name, max_klen, common): Returns a string containing all references pointed to by name that contain all keys in keys. Common is name of common word file. Pointer returned comes from malloc. Use free to return storage. NB A zero length string returned if nothing is found. A NULL pointer indicates an error accessing the file "name". */ char *locate(keys,name,max_klen,common) char *keys, *name, *common; int max_klen; /* max key length */ { static char oldname[MAXSTR] = ""; /* oldname is name of stream index */ static FILE *index = NULL; static long int i_size; /* size of index */ static char oldtext[MAXSTR]; /* oldtext is the path to stream */ static FILE *text = NULL; /* text. if it is a relative */ static int pathlen; /* path, it is relative to index */ /* directory. */ /* oldname[0..pathlen-1] is index */ /* directory */ int len; char key[MAXSTR]; /* refs[i] is a line of index for */ struct reftype refs[MAXREFS]; /* all keys up to key */ int refcnt, copied, comp; /* refcnt = # of refs */ /* copied = # of refs copied */ /* comp = # of refs compared */ struct reftype ref; char str[MAXSTR]; int more; long int ans; int i,j; unsigned total; char *allrefs, *next; /* all refs (separated by null line)*/ char *p; /* open index */ if (strcmp(oldname,name)!=0) { if (index) fclose(index); if (text) fclose(text); strcpy(oldname,name); strcpy(oldtext,""); /* determine pathlen */ p= rindex(oldname, '/'); if (p!=NULL) pathlen= p-oldname+1; else pathlen= 0; index= fopen(oldname,"r"); if (index==NULL) { fprintf(stderr, "locate: cannot open %s\n", oldname); strcpy(oldname, ""); return(NULL); } else { fseek(index,0L,2); /* seeks last newline */ i_size= ftell(index); } } /* load references to first key */ keys= stripkeys(keys,key, max_klen, common); if (*key==NULL) { fprintf(stderr,"locate: no keys for citation\n"); allrefs = malloc(1); if (allrefs==NULL) { fprintf(stderr, "locate: insufficient space for references\n"); clnup(); exit(1); } *allrefs= NULL; return(allrefs); } len= strlen(key); strcat(key," "); alpha_seek(index, key, i_size, 0); key[len]= NULL; /* strip blank off */ refcnt= 0; fscanf(index,"%s ", str); if (strcmp(str,key) == 0) { str[0]= NULL; while (refcnt < MAXREFS && fetchref(index, str, &ref) ) { refs[refcnt]= ref; refcnt++; } } if (refcnt==MAXREFS) fprintf(stderr, "locate: first key (%s) matched too many refs\n", key); /* intersect the reference sets for remaining keys with first set */ while (*keys!=NULL) { keys= stripkeys(keys, key, max_klen, common); if (*key==NULL) continue; len= strlen(key); strcat(key," "); alpha_seek(index, key, i_size, 0); key[len]= NULL; fscanf(index,"%s ", str); if (strcmp(str,key) != 0) refcnt= 0; /* no matching refs */ copied= 0; comp= 0; more= fetchref(index, str, &ref); while (comp < refcnt && more) { /* ans= ref-refs[comp] */ ans= strcmp(ref.reffile, refs[comp].reffile); if (ans==0) ans= ref.start-refs[comp].start; if (ans==0) ans= ref.length-refs[comp].length; if (ans<0) more= fetchref(index, str, &ref); if (ans==0) { refs[copied]= refs[comp]; comp++; copied++; more= fetchref(index, str, &ref);} if (ans>0) comp++; } refcnt= copied; } total= 0; for (i=0; ireffile, oldfile); fscanf(stream, "%D/%D", &ref->start, &ref->length); return(1); }