home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d101 / microspell.lha / MicroSpell / common.c < prev    next >
C/C++ Source or Header  |  1987-09-05  |  3KB  |  140 lines

  1. /*    COMMON:    Most Common word processing for MicroSPELL 1.0
  2.         Spell Checker and Corrector
  3.  
  4.         (C)opyright May 1987 by Daniel Lawrence
  5.         All Rights Reserved
  6. */
  7.  
  8. #include    <stdio.h>
  9. #include    "dopt.h"
  10. #include    "dstruct.h"
  11. #include    "ddef.h"
  12.  
  13. loadcom()    /* load the most used words in to the common list */
  14.  
  15. {
  16.     register char *sp;    /* temp string pointer */
  17.     register int i;        /* index */
  18.     FILE *cf;        /* ptr to common word list file */
  19.     char buf[NSTRING+1];    /* buffer to hold current word */
  20.  
  21.     /* if we are already loaded....don't bother again */
  22.     if (commonf == TRUE)
  23.         return(TRUE);
  24.  
  25.     /* try to open it.... */
  26.     if ((cf = popen(comlist)) == NULL)
  27.         return(FALSE);
  28.  
  29.     /* and load them up */
  30.     commonf = TRUE;
  31.     while (numcom < MAXCOM) {
  32.         /* get the next word */
  33.         if (fgets(buf, NSTRING, cf) == NULL)
  34.             break;
  35.         buf[strlen(buf)-1] = 0;    /* get rid of the newline */
  36.  
  37.         /* get room for it...*/
  38.         if ((sp = malloc(strlen(buf)+1)) == NULL) {
  39.             fclose(cf);
  40.             return(FALSE);
  41.         }
  42.  
  43.         /* and store it */
  44.         strcpy(sp, buf);
  45.         cword[numcom++] = sp;
  46.     }
  47.  
  48.     fclose(cf);
  49.     if (swdebug)
  50.         printf("[%u common words loaded]\n", numcom);
  51.     numfiltr = numcom;
  52.     return(TRUE);
  53. }
  54.  
  55. dumpcom()    /* unload the common word list */
  56.  
  57. {
  58.     /* only bother if we are really here */
  59.     if (commonf == FALSE)
  60.         return(TRUE);
  61.  
  62.     /* dump them */
  63.     while (numfiltr--)
  64.         free(cword[numcom]);
  65.     if (swdebug)
  66.         printf("[common word list dumped]");
  67.     return(TRUE);
  68. }
  69.  
  70. iscom(word)    /* is a word in the common/user word list? */
  71.  
  72. char *word;    /* word to check */
  73.  
  74. {
  75.     register int lower;    /* lower limit for search */
  76.     register int upper;    /* upper limit for search */
  77.     register int mid;    /* current word in search */
  78.     register int cresult;    /* result of current compare */
  79.     register char *tp;    /* temp string pointer */
  80.     static char tword[NSTRING];    /* temp copy of word */
  81.  
  82.     /* set up limits */
  83.     lower = 0;
  84.     upper = numfiltr - 1;
  85.  
  86.     /* Binary search the list */
  87.     while (upper >= lower) {
  88.  
  89.         /* find the current mid item */
  90.         mid = (upper + lower) / 2;
  91.  
  92.         /* compare it */
  93.         cresult = strcmp(word, cword[mid]);
  94.         if (cresult == 0)    /* success!!!! */
  95.             return(TRUE);
  96.  
  97.         /* reset the search bounds */
  98.         if (cresult > 0)
  99.             lower = mid + 1;
  100.         else
  101.             upper = mid - 1;
  102.     }
  103.  
  104.     /* we haven't found it... try lowercasing it */
  105.     if (islower(*word))
  106.         goto fail;
  107.     tp = tword;
  108.     while (*word)
  109.         if (isupper(*word))
  110.             *tp++ = *word++ + DIFCASE;
  111.         else
  112.             *tp++ = *word++;
  113.     *tp = 0;
  114.  
  115.     /* set up limits */
  116.     lower = 0;
  117.     upper = numfiltr - 1;
  118.  
  119.     /* Binary search the list */
  120.     while (upper >= lower) {
  121.  
  122.         /* find the current mid item */
  123.         mid = (upper + lower) / 2;
  124.  
  125.         /* compare it */
  126.         cresult = strcmp(tword, cword[mid]);
  127.         if (cresult == 0)    /* success!!!! */
  128.             return(TRUE);
  129.  
  130.         /* reset the search bounds */
  131.         if (cresult > 0)
  132.             lower = mid + 1;
  133.         else
  134.             upper = mid - 1;
  135.     }
  136.  
  137. fail:    /* it's not here, jim.... */
  138.     return(FALSE);
  139. }
  140.