home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / me34src.zip / me3 / util / ranger1.c < prev    next >
C/C++ Source or Header  |  1995-01-14  |  4KB  |  94 lines

  1. /* ranger1.c
  2.  * ranger1:
  3.  *   This routine cruises through a list (sorted array of structures, one of
  4.  *     which is a pointer to a string) and picks a range that word falls in.
  5.  *   ???Blank ("") entries are skipped.  Depends.  Sometimes.  I don't think
  6.  *     this part works all that well.
  7.  *   "" does NOT match "".
  8.  *
  9.  * Input:
  10.  *   start:  A pointer to the string (char *) in the first structure of the
  11.  *     list.  Yes, a pointer to a pointer.
  12.  *   blobs:  The number of elements in the list.  MUST be >= 0.
  13.  *   blobsize:  sizeof() each structure in the list.
  14.  *   word:  What we are looking for in the list.
  15.  * Output:
  16.  *   matched:  number of characters in word ALSO in at least one item in
  17.  *     list.
  18.  *   commonchars:  given the range word falls in, these are the letters in
  19.  *     common in the range.
  20.  * Returns: TRUE if word is in list else FALSE
  21.  *
  22.  * Fact: The number of letters in commonchars >= matched.
  23.  *
  24.  * Alg:
  25.  *  1. find the max letters in word also in list (match-word)
  26.  *  2. find the range of words in list that have match-word in them
  27.  *  3. find max letters in common in range.
  28.  * C Durland
  29.  */
  30.  
  31. /* idea for ranger3:  pass in incit():  a routine to call when need to move
  32.  * to next structure.  Can be used for linked lists, etc.  Note:  can't use
  33.  * incptr as a default unless want to pass in blobsize (or could have
  34.  * blobsize double as offset).  Note:  If I do this, start will have to
  35.  * point to the start of the structure and ranger3 will have to know the
  36.  * offset of (char *).  Could sleeze and subtract offset to find start of
  37.  * structure.
  38.  */
  39.  
  40. /* Copyright 1989, 1990 Craig Durland
  41.  *   Distributed under the terms of the GNU General Public License.
  42.  *   Distributed "as is", without warranties of any kind, but comments,
  43.  *     suggestions and bug reports are welcome.
  44.  */
  45.  
  46. #include <const.h>
  47.  
  48.     /* increment a (char **) n bytes */
  49. #define incptr(ptr,n) (char**)((char *)ptr +n)
  50.  
  51. ranger1(start,blobs,blobsize,word,matched,commonchars)
  52.   char **start, *word, *commonchars;  int *matched, blobs, blobsize;
  53. {
  54.   char **a = NULL, **b = NULL, c, **ptr;
  55.   register int j, len, s;
  56.  
  57.   *commonchars = '\0'; *matched = 0;
  58.   if (0 == (len = strlen(word))) return FALSE;    /* "" don't match nuthing */
  59. tryagain:
  60.   for (j = blobs, ptr = start; j--; ptr = incptr(ptr,blobsize))
  61.   {
  62.     if (**ptr == '\0') continue;        /* ignore blank entry */
  63.     if ((s = strncmp(word,*ptr,len)) == 0)    /* len characters match */
  64.       if (a == NULL) a = ptr;    /* the start of the matched words */
  65.       else b = ptr;        /* the end of the range */
  66.     else if (s < 0) break;  /* don't search entire list if don't have to */
  67.   }
  68.  
  69.   if (a == NULL)            /* no match in the list */
  70.     if (len > 0)    /* is there a match on the first n-1 characters? */
  71.       { len--; goto tryagain; }
  72.     else return FALSE;        /* no possible match */
  73.   *matched = len;
  74.   if (b == NULL)    /* only one instance of at least part of word */
  75.   {
  76.     strcpy(commonchars,*a);
  77.     return (0 == strcmp(word,commonchars));    /* maybe a commplete match */
  78.   }
  79.  
  80.     /* Have a range of words in list that match the first len chars of
  81.      *   word.
  82.      * All words in range have their first len chars in common.
  83.      * Now find the most characters that the words in range have in
  84.      *   common.  This can be lots more than len.
  85.      */
  86.   for (len--; c = *(*a + len); len++)    /* ???remember "" entries */
  87.     for (ptr = incptr(a,blobsize); ptr <= b; ptr = incptr(ptr,blobsize))
  88.       if (**ptr != '\0' && *(*ptr+len) != c) goto done;    /* mismatch */
  89.  
  90. done:
  91.   strcpy(commonchars,*a); commonchars[len] = '\0';
  92.   return FALSE;
  93. }
  94.