113109Srrh #ifndef lint 2*15061Sgarrison static char sccsid[] = "@(#)alpha.seek.c 2.2 09/23/83"; 313109Srrh #endif not lint 4*15061Sgarrison # 512908Sgarrison 612908Sgarrison # include "stdio.h" 712908Sgarrison # include "ctype.h" 812908Sgarrison # include "streams.h" 912908Sgarrison # define nexttry ((high+low)/2) 1012908Sgarrison 1112908Sgarrison /* alpha_seek(stream, word, s_size, fold) 1212908Sgarrison seeks the first line in stream that is at least word. 1312908Sgarrison assumes that stream is a sorted file of lines. (last char must be \n) 1412908Sgarrison if fold, assumes that word is lowercase and folds stream to lowercase. 1512908Sgarrison s_size = size of stream 1612908Sgarrison returns 1 if word = line, 0 o.w. 1712908Sgarrison */ 1812908Sgarrison int alpha_seek(stream, word, s_size, fold) 1912908Sgarrison FILE *stream; 2012908Sgarrison char *word; 2112908Sgarrison long int s_size; 2212908Sgarrison int fold; 2312908Sgarrison { long int high, low, mid; /* point to beginning of a line in stream */ 2412908Sgarrison int ans; /* line(low) < word <= line(high) */ 2512908Sgarrison char line[maxstr]; 2612908Sgarrison 2712908Sgarrison 2812908Sgarrison /* initialize low (return if first line >= word) */ 2912908Sgarrison low= 0L; 3012908Sgarrison pos(low); getline(stream, line); 3112908Sgarrison if (fold) foldline(line); 3212908Sgarrison ans= strcmp(line,word); 3312908Sgarrison 3412908Sgarrison if ( ans >= 0) 3512908Sgarrison { pos(low); return(ans==0); } 3612908Sgarrison 3712908Sgarrison /* initialize high to "line" after last line */ 3812908Sgarrison high= s_size; 3912908Sgarrison 4012908Sgarrison mid= nextline(stream, nexttry ); 4112908Sgarrison while (mid < high ) 4212908Sgarrison { getline(stream,line); 4312908Sgarrison if (fold) foldline(line); 4412908Sgarrison if (strcmp(line,word) < 0) low= mid; 4512908Sgarrison else high= mid; 4612908Sgarrison mid= nextline(stream, nexttry ); 4712908Sgarrison } 4812908Sgarrison 4912908Sgarrison /* linear search from low to high */ 5012908Sgarrison low= nextline(stream,low); 5112908Sgarrison for(;;) 5212908Sgarrison { if (low>=high) break; 5312908Sgarrison 5412908Sgarrison getline(stream,line); 5512908Sgarrison if (fold) foldline(line); 5612908Sgarrison ans=strcmp(line,word); 5712908Sgarrison 5812908Sgarrison if (ans>=0) break; 5912908Sgarrison low= ftell(stream); 6012908Sgarrison } 6112908Sgarrison 6212908Sgarrison pos(low); 63*15061Sgarrison if (low==high) return(0); 6412908Sgarrison else return(ans==0); 6512908Sgarrison } 6612908Sgarrison 6712908Sgarrison 6812908Sgarrison /* foldline(p): change all uppercase to lowercase in string p 6912908Sgarrison */ 7012908Sgarrison foldline(p) 7112908Sgarrison char *p; 7212908Sgarrison { for (; *p!=NULL; p++) 7312908Sgarrison { if (isupper(*p)) *p = tolower(*p); 7412908Sgarrison } 7512908Sgarrison } 76