Re: Text search routine

 BBS: Inland Empire Archive
Date: 03-04-92 (23:02)             Number: 163
From: DONN BLY                     Refer#: NONE
  To: TRENT SHIRLEY                 Recvd: NO  
Subj: Re: Text search routine        Conf: (11) Modula-2

> I am writing a program that needs to search through a > text file for any given word or text string. I need the > search to be Case indifferent and was hoping that > someone out there had a routine, or shareware/freeware > library routine that would do what i need. Otherwise I > see lots of hours of effort figuring out a semi-pain in > the ass routine that will be larger than it is useful. Searching for the text is no problem -- but what do you want to do with it when you find it? > I am considering searching by individual characters and > testing the ascii value to determine upper or lower > case, then converting to whichever standard I decide on, > and then testing for the next character in the original > search string. Could be a pain in the long run. Yes, it would be a pain -- and rather slow. Try this one on for size: _ _ _ O_/_ _C_U_T_ _H_E_R_E_ _ _ _ _ _ _ O \ DECLARE FUNCTION SearchFile& (FileName$, Text$) LINE INPUT " Name of file to search : ", FileName$ LINE INPUT "Text for which to search : ", Text$ Offset& = SearchFile&(FileName$, Text$) IF Offset& THEN PRINT "Information Found in file '"; FileName$; "' at offset"; Offset& OPEN FileName$ FOR BINARY AS #1 GET #1, Offset&, Text$ PRINT "Actual Text Found = "; CHR$(34); Text$; CHR$(34) CLOSE #1 ELSE PRINT "Information Not Found" END IF FUNCTION SearchFile& (FileName$, Text$) ' SearchFile&() is a simple routine to search a file for the occurance of ' the text found in the parameter Text$ (The search is case insensitive) ' If found, the offset into the file is returned, otherwise the value of ' zero is returned. If the file passed has a length of zero, it will be ' deleted and the value of zero will be returned. '
-----
' This routine was written by Donn Bly & released to Public Domain on ' March 4, 1992. '
-----
ConvertedText$ = UCASE$(Text$) TextLen% = LEN(ConvertedText$) FileNum% = FREEFILE OPEN FileName$ FOR BINARY AS #FileNum% IF LOF(FileNum%) = 0 THEN CLOSE #FileNum% KILL FileName$ SearchFile& = 0 ELSE BuffSize% = FRE("") \ 4 BaseOffset& = 0 FileRemain& = LOF(FileNum%) FileBuff$ = SPACE$(BuffSize%) SearchBuff$ = "" DO IF FileRemain& < BuffSize% THEN BuffSize% = FileRemain& FileBuff$ = SPACE$(BuffSize%) END IF GET #FileNum%, , FileBuff$ FileRemain& = FileRemain& - BuffSize% SearchBuff$ = SearchBuff$ + UCASE$(FileBuff$) BuffOffset% = INSTR(SearchBuff$, ConvertedText$) IF BuffOffset% = 0 THEN BaseOffset& = BaseOffset& + LEN(SearchBuff$) - TextLen% SearchBuff$ = RIGHT$(SearchBuff$, TextLen%) END IF LOOP WHILE (FileRemain& > 0) AND (BuffOffset% = 0) IF BuffOffset% THEN SearchFile& = BaseOffset& + BuffOffset% ELSE SearchFile& = 0 END IF CLOSE #FileNum% END IF END FUNCTION --- OPMED 3.00 * Origin: The Loft *HST/DS/V42bis* [NC, SDS] (1:236/7)
Outer Court
Echo Basic Postings

Books at Amazon:

Back to BASIC: The History, Corruption, and Future of the Language

Hackers: Heroes of the Computer Revolution (including Tiny BASIC)

Go to: The Story of the Math Majors, Bridge Players, Engineers, Chess Wizards, Scientists and Iconoclasts who were the Hero Programmers of the Software Revolution

The Advent of the Algorithm: The Idea that Rules the World

Moths in the Machine: The Power and Perils of Programming

Mastering Visual Basic .NET