home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 (1993) / nebula.bin / SourceCode / MiniExamples / FindIt / cUtils.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-09  |  1.6 KB  |  86 lines

  1. /* 
  2.  * cUtils.c
  3.  *
  4.  * Purpose:
  5.  *        These c routines implement a text search.
  6.  *
  7.  * You may freely copy, distribute, and reuse the code in this example.
  8.  * NeXT disclaims any warranty of any kind, expressed or  implied, as to its
  9.  * fitness for any particular use.
  10.  *
  11.  * Written by: Mary McNabb
  12.  * Created: Apr 91
  13.  *
  14.  */
  15. #import <strings.h>
  16. #import <libc.h>
  17. #import <zone.h>
  18.  
  19. /*
  20.  * Returns true if 'text' exists within 'string'.
  21.  */
  22. char *textInString(text, string, ignoreCase)
  23. char *text, *string;
  24. int ignoreCase;
  25. {
  26.     int    textLength, i;
  27.     
  28.     textLength = strlen(text);
  29.     
  30.     i = strlen(string) - textLength + 1;
  31.     
  32.     if (i <= 0) {
  33.     return 0;
  34.     }
  35.     
  36.     if (ignoreCase) {
  37.     while (i--) {
  38.         if (!strncasecmp(string++, text, textLength)) {
  39.         return (string-1);
  40.         }
  41.     }
  42.     } else {
  43.     while (i--) {
  44.         if (!strncmp(string++, text, textLength)) {
  45.         return (string-1);
  46.         }
  47.     }
  48.     }
  49.    
  50.     return 0;
  51. }
  52.  
  53. /*
  54.  * Returns true if 'text' exists within 'string' (searches in
  55.  * reverse direction).
  56.  */
  57. char *textInStringReverse(text, string, stringStart, ignoreCase)
  58. char *text, *string, *stringStart;
  59. int ignoreCase;
  60. {
  61.     int    textLength;
  62.     
  63.     textLength = strlen(text);
  64.     string -= textLength;
  65.     
  66.     if ((strlen(stringStart) - strlen(string)) < strlen(text)) {
  67.     return 0;
  68.     }
  69.     
  70.     if (ignoreCase) {
  71.     while (string >= stringStart) {
  72.         if (!strncasecmp(string--, text, textLength)) {
  73.         return (string + 1);
  74.         }
  75.     }
  76.     } else {
  77.     while (string >= stringStart) {
  78.         if (!strncmp(string--, text, textLength)) {
  79.         return (string + 1);
  80.         }
  81.     }
  82.     }
  83.    
  84.     return 0;
  85. }
  86.