BSEARCHSTR

Section: C Library Functions (3)
Updated: HP Experimental
Index Return to Main Contents
 

NAME

bsearchstr - binary search a sorted file of random-length strings  

SYNOPSIS

long bsearchstr (stream, pattern, caseins, tellnext)
FILE *stream;
char *pattern;
int  caseins;
int  tellnext;  

HP-UX COMPATIBILITY

Level:
HP-UX/RUN ONLY
Origin:
Hewlett-Packard
 

DESCRIPTION

Bsearchstr is a binary search routine which understands ordinary files containing sorted, random-length strings (lines) terminated by newline characters. It returns the byte offset of the first line in the file which begins with the given pattern, taken as a simple string of characters (regular expressions are not supported). It also sets the file location to the line beginning at the offset returned, using fseek(3s).

Stream must be a currently open file containing lines previously sorted in increasing order by the simple numeric values of the characters.

Pattern must be a valid pointer to char. A nil pattern (e.g. *pattern == 0) matches anything, so bsearchstr returns zero (start of file) in this case. A nil line in the file (e.g. nothing but a newline) is treated as being less than any non-nil pattern.

If caseins is zero, searching is done case-sensitively, and the data file should be sorted accordingly. If caseins is non-zero, searching is done case-insensitively, and the data must have been sorted using "sort -f" or equivalent. The routine uses malloc(3) to get memory to make an uppercased copy of pattern, so as not to modify the passed-in value.

If tellnext is zero, and no line in the file begins with pattern, bsearchstr returns -1. If tellnext is non-zero, it returns the offset of the first line lexically after pattern. It returns the size of the file if the last line is before pattern.

Each time bsearchstr halves the remainder of the file, it seeks to a location which is usually not the start of a line. It searches forwards, then backwards, a character at a time, to locate the next start of line. This is normally more efficient than linear searching the file, especially if the file is large and no lines are extremely long. Also, bsearchstr works fine even if the last line in the file is not terminated by a newline.  

SEE ALSO

sort(1), bsearch(3c), fgets(3s), fopen(3s), hsearch(3c), lsearch(3c), qsort(3c), tsearch(3c).  

DIAGNOSTICS

Returns -1 if no line matches and tellnext is zero.

Returns -2 if any fseek(3s), ftell(3s), or malloc(3c) error occurs. In this case the current file location is unpredictable.


 

Index

NAME
SYNOPSIS
HP-UX COMPATIBILITY
DESCRIPTION
SEE ALSO
DIAGNOSTICS

This document was created by man2html, using the manual pages.
Time: 21:51:53 GMT, February 02, 2023