static char sccsid
[] = "@(#)locate.c 2.7 5/27/93";
/* 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 calloc. 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".
int fflag
; /* set if want the reference string to have 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 */
/* oldname[0..pathlen-1] is index */
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 */
char *allrefs
, *next
; /* all refs (separated by null line)*/
if (strcmp(oldname
,name
)!=0)
{ if (index
) fclose(index
);
if (p
!=NULL
) pathlen
= p
-oldname
+1;
index
= fopen(oldname
,"r");
fprintf(stderr
,"locate: cannot open %s\n", oldname
);
fseek(index
,0L,2); /* seeks last newline */
/* load references to first key */
keys
= stripkeys(keys
,key
, max_klen
, common
);
fprintf(stderr
,"locate: no keys for citation: %s\n", keys
);
allrefs
= (char *) calloc(1, sizeof (char));
fprintf(stderr
,"locate: insufficient space for references\n");
alpha_seek(index
, key
, i_size
, 0);
key
[len
]= NULL
; /* strip blank off */
fscanf(index
,"%s ", str
);
if (strcmp(str
,key
) == 0)
while (refcnt
< maxrefs
&& fetchref(index
, str
, &ref
) )
"locate: first key (%s) matched too many refs\n", key
);
/* intersect the reference sets for remaining keys with first set */
{ keys
= stripkeys(keys
, key
, max_klen
, common
);
if (*key
==NULL
) continue;
alpha_seek(index
, key
, i_size
, 0);
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
);}
for (i
=0; i
<refcnt
; i
++) {
total
+= refs
[i
].length
+1;
total
+= strlen(refs
[i
].reffile
) + 1;
allrefs
= (char *) calloc(total
+1, sizeof (char));
{ fprintf(stderr
,"locate: insufficient space for references\n");
/* copy refs into allrefs */
if (strcmp(oldtext
,refs
[i
].reffile
) != 0)
{ strcpy(oldtext
,refs
[i
].reffile
);
strncpy(str
, oldname
, pathlen
); str
[pathlen
]= NULL
;
{ fprintf(stderr
,"locate: cannot open %s\n", str
);
fseek(text
, refs
[i
].start
, 0);
strcat(next
, refs
[i
].reffile
);
for (j
=0; j
<refs
[i
].length
; j
++) *next
++ = getc(text
);
/* stripkeys(line,key,max_klen, common):
assigns to key the first key in line
and returns a pointer to the position following the key
char *stripkeys(line
,key
,max_klen
,common
)
{ while (isspace(*line
)) line
++;
while (*line
!=NULL
&& !isspace(*line
))
makekey(key
, max_klen
, common
);
} while (*key
==NULL
&& *line
!=NULL
);
/* read a reference pair from stream into *ref. if file not given,
use oldfile. return 1 if pair found, 0 ow.
int fetchref(stream
, oldfile
, ref
)
fscanf(stream
, "%c", &cntl
);
if (cntl
=='\n') {return (0);}
if (cntl
==':') fscanf(stream
, "%s", oldfile
);
strcpy(ref
->reffile
, oldfile
);
fscanf(stream
, "%D/%D", &ref
->start
, &ref
->length
);