home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / Connectivity / GateKeeper-2.1 / Parse.m < prev    next >
Encoding:
Text File  |  1996-03-08  |  4.1 KB  |  149 lines

  1. //*****************************************************************************
  2. //
  3. //    Parse.m.  
  4. //        
  5. //        Search a file for a specific pattern of (2)strings which will 
  6. //        identify the next str as a keyword to be captured and returned.
  7. // 
  8. //        by    Felipe A. Rodriguez        
  9. //
  10. //    This code is supplied "as is" the author makes no warranty as to its 
  11. //    suitability for any purpose.  This code is free and may be distributed 
  12. //    in accordance with the terms of the:
  13. //        
  14. //            GNU GENERAL PUBLIC LICENSE
  15. //            Version 2, June 1991
  16. //            copyright (C) 1989, 1991 Free Software Foundation, Inc.
  17. //             675 Mass Ave, Cambridge, MA 02139, USA
  18. //
  19. //*****************************************************************************
  20.  
  21.  
  22. #import "Parse.h"
  23.  
  24. @implementation Parse
  25.  
  26. //*****************************************************************************
  27. //
  28. //         Open file to parse and search 
  29. //
  30. //*****************************************************************************
  31.  
  32. - (char *)parseFile:(const char *)filePath
  33. {
  34. NXStream *inFStream;
  35. static char buf[2] = {" \0"};
  36. int nread = 0, i = 0;
  37. char * resultStr;
  38.  
  39.     if((inFStream = NXMapFile(filePath, NX_READONLY)) == NULL)
  40.         {
  41.         strcpy(lBuf, "Could not open file: ");
  42.         strcat(lBuf, filePath);
  43.         NXRunAlertPanel(0, lBuf, 0, 0, 0);
  44.         }
  45.     else
  46.         {
  47.         nread = NXRead(inFStream, buf, 1);
  48.         while(!(nread <= 0))                    // read while char's in file
  49.             {                // cycle thru file until we find keyword or EOF
  50.             strcpy(lBuf, buf);
  51.             do {                    // build a line buffer from file data
  52.                 nread = NXRead(inFStream, buf, 1);
  53.                 strcat(lBuf, buf);
  54.                 i++;
  55.                 }
  56.             while((buf[0] != '\n') && (buf[0] != '\r') && (i < MAXPATHLEN) 
  57.                                                                     && buf[0]);    
  58.             i = 0;
  59.             while(((buf[0] == '\n') || (buf[0] == '\r')) && (!(nread <= 0)))        
  60.                 nread = NXRead(inFStream, buf, 1);        // eliminate cr, nl    
  61.             if(resultStr = [self parse:lBuf])            // parse line buffer
  62.                 return resultStr;
  63.             }
  64.         NXClose(inFStream);
  65.         }
  66.  
  67.     return NULL;
  68. }
  69. //*****************************************************************************
  70. //
  71. //         Specify keywords to be searched for.  Keyword 1 must be found
  72. //        prior to keyword 2.  Ideally, these two should be adjacent in file
  73. //
  74. //*****************************************************************************
  75.  
  76. - setKey1:(const char *)keyWord
  77. {
  78.     keyOne = keyWord;
  79.  
  80.     return self;
  81. }
  82. - setKey2:(const char *)keyWord
  83. {
  84.     keyTwo = keyWord;
  85.  
  86.     return self;
  87. }
  88. //*****************************************************************************
  89. //
  90. //         Specify file delimiters to be used in parsing.  Once the first keyword
  91. //        is caught the second set of delimiters is used in parsing.
  92. //
  93. //*****************************************************************************
  94.  
  95. - setDelim1:(const char *)delimiters
  96. {
  97.     delim1 = delimiters;
  98.  
  99.     return self;
  100. }
  101. - setDelim2:(const char *)delimiters
  102. {
  103.     delim2 = delimiters;
  104.  
  105.     return self;
  106. }
  107. //*****************************************************************************
  108. //
  109. //         parses a line of text looking for a set of keywords
  110. //
  111. //        **keyWords + delims must me set before calling this method as follows:
  112. //        keyOne -- should point to first keyword to be caught
  113. //        keyTwo -- should point to second keyword to be caught
  114. //        delim1 -- should point to first set of delimiters
  115. //        delim2 -- should point to second set of delimiters 
  116. //
  117. //*****************************************************************************
  118.  
  119. - (char *)parse:(char *)buffer
  120. {
  121. static char *str;
  122. static BOOL found = NO;                // set if first keyword found in prev line
  123.  
  124.     str = strtok(buffer, delim1);                        // Parse string 
  125.     while(str != NULL)                                
  126.         {                                        // did we catch keyOne str?
  127.         if((strcmp(str, keyOne) == 0) || found == YES)        
  128.             {
  129.             found = YES;                        // first keyword has been found
  130.             if(str = strtok(NULL, delim1))        // if not NULL
  131.                 {
  132.                 if(strcmp(str, keyTwo) == 0)        // did we catch this str?
  133.                     {
  134.                     if(str = strtok(NULL, delim2))    // this must be the grail
  135.                         {                            // if str ! NULL
  136.                         found = NO;    
  137.                         return NXCopyStringBuffer(str);    // return search result
  138.                         }
  139.                     }
  140.                 }
  141.             }
  142.         str = strtok(NULL, delim1);
  143.         }
  144.  
  145.     return NULL;                                    // keyword not found
  146. }
  147.  
  148. @end
  149.