home *** CD-ROM | disk | FTP | other *** search
- //*****************************************************************************
- //
- // Parse.m.
- //
- // Search a file for a specific pattern of (2)strings which will
- // identify the next str as a keyword to be captured and returned.
- //
- // by Felipe A. Rodriguez
- //
- // This code is supplied "as is" the author makes no warranty as to its
- // suitability for any purpose. This code is free and may be distributed
- // in accordance with the terms of the:
- //
- // GNU GENERAL PUBLIC LICENSE
- // Version 2, June 1991
- // copyright (C) 1989, 1991 Free Software Foundation, Inc.
- // 675 Mass Ave, Cambridge, MA 02139, USA
- //
- //*****************************************************************************
-
-
- #import "Parse.h"
-
- @implementation Parse
-
- //*****************************************************************************
- //
- // Open file to parse and search
- //
- //*****************************************************************************
-
- - (char *)parseFile:(const char *)filePath
- {
- NXStream *inFStream;
- static char buf[2] = {" \0"};
- int nread = 0, i = 0;
- char * resultStr;
-
- if((inFStream = NXMapFile(filePath, NX_READONLY)) == NULL)
- {
- strcpy(lBuf, "Could not open file: ");
- strcat(lBuf, filePath);
- NXRunAlertPanel(0, lBuf, 0, 0, 0);
- }
- else
- {
- nread = NXRead(inFStream, buf, 1);
- while(!(nread <= 0)) // read while char's in file
- { // cycle thru file until we find keyword or EOF
- strcpy(lBuf, buf);
- do { // build a line buffer from file data
- nread = NXRead(inFStream, buf, 1);
- strcat(lBuf, buf);
- i++;
- }
- while((buf[0] != '\n') && (buf[0] != '\r') && (i < MAXPATHLEN)
- && buf[0]);
- i = 0;
- while(((buf[0] == '\n') || (buf[0] == '\r')) && (!(nread <= 0)))
- nread = NXRead(inFStream, buf, 1); // eliminate cr, nl
- if(resultStr = [self parse:lBuf]) // parse line buffer
- return resultStr;
- }
- NXClose(inFStream);
- }
-
- return NULL;
- }
- //*****************************************************************************
- //
- // Specify keywords to be searched for. Keyword 1 must be found
- // prior to keyword 2. Ideally, these two should be adjacent in file
- //
- //*****************************************************************************
-
- - setKey1:(const char *)keyWord
- {
- keyOne = keyWord;
-
- return self;
- }
- - setKey2:(const char *)keyWord
- {
- keyTwo = keyWord;
-
- return self;
- }
- //*****************************************************************************
- //
- // Specify file delimiters to be used in parsing. Once the first keyword
- // is caught the second set of delimiters is used in parsing.
- //
- //*****************************************************************************
-
- - setDelim1:(const char *)delimiters
- {
- delim1 = delimiters;
-
- return self;
- }
- - setDelim2:(const char *)delimiters
- {
- delim2 = delimiters;
-
- return self;
- }
- //*****************************************************************************
- //
- // parses a line of text looking for a set of keywords
- //
- // **keyWords + delims must me set before calling this method as follows:
- // keyOne -- should point to first keyword to be caught
- // keyTwo -- should point to second keyword to be caught
- // delim1 -- should point to first set of delimiters
- // delim2 -- should point to second set of delimiters
- //
- //*****************************************************************************
-
- - (char *)parse:(char *)buffer
- {
- static char *str;
- static BOOL found = NO; // set if first keyword found in prev line
-
- str = strtok(buffer, delim1); // Parse string
- while(str != NULL)
- { // did we catch keyOne str?
- if((strcmp(str, keyOne) == 0) || found == YES)
- {
- found = YES; // first keyword has been found
- if(str = strtok(NULL, delim1)) // if not NULL
- {
- if(strcmp(str, keyTwo) == 0) // did we catch this str?
- {
- if(str = strtok(NULL, delim2)) // this must be the grail
- { // if str ! NULL
- found = NO;
- return NXCopyStringBuffer(str); // return search result
- }
- }
- }
- }
- str = strtok(NULL, delim1);
- }
-
- return NULL; // keyword not found
- }
-
- @end
-