home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / editors / mutt / me2s_pl7.zoo / mu_edit2 / util / ranger1.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-07-05  |  3.8 KB  |  102 lines

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