home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / SSPELL11.ZIP / FILE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-26  |  6.7 KB  |  245 lines

  1. /* **************************************************************** */
  2. /*             sspell - similar to Unix spell                       */
  3. /*                        version 1.1                               */
  4. /*                                                                  */
  5. /* Author: Maurice Castro                                           */
  6. /* Release Date: 26 Jan 1992                                        */
  7. /* Bug Reports: maurice@bruce.cs.monash.edu.au                      */
  8. /*                                                                  */
  9. /* This code has been placed by the Author into the Public Domain.  */
  10. /* The code is NOT covered by any warranty, the user of the code is */
  11. /* solely responsible for determining the fitness of the program    */
  12. /* for their purpose. No liability is accepted by the author for    */
  13. /* the direct or indirect losses incurred through the use of this   */
  14. /* program.                                                         */
  15. /*                                                                  */
  16. /* Segments of this code may be used for any purpose that the user  */
  17. /* deems appropriate. It would be polite to acknowledge the source  */
  18. /* of the code. If you modify the code and redistribute it please   */
  19. /* include a message indicating your changes and how users may      */
  20. /* contact you for support.                                         */
  21. /*                                                                  */
  22. /* The author reserves the right to issue the official version of   */
  23. /* this program. If you have useful suggestions or changes for the  */
  24. /* code, please forward them to the author so that they might be    */
  25. /* incorporated into the official version                           */
  26. /*                                                                  */
  27. /* Please forward bug reports to the author via Internet.           */
  28. /*                                                                  */
  29. /* Contributors: mao@physics.su.oz.au                               */
  30. /*                                                                  */
  31. /* **************************************************************** */
  32.  
  33. /* This set of routines accesses and indexes the dictionary */
  34.  
  35. #include "iofn.h"
  36. #include <stdio.h>
  37. #include <sys/types.h>
  38. #include <sys/stat.h>
  39. #include "strfn.h"
  40. #include "file.h"
  41. #include "error.h"
  42. #include "config.h"
  43. #include "utility.h"
  44.  
  45. struct indexelm
  46. {
  47.    char word[HASHWID];
  48.    unsigned long location;
  49.    };
  50.  
  51. struct indexelm findx[IDXSIZ];
  52.  
  53. FILE *f;
  54.  
  55. void initfile(dict)
  56. char *dict;
  57. {
  58.    f=fopen(dict,"rt");
  59.    }
  60.  
  61. void closefile()
  62. {
  63.    fclose(f);
  64.    }
  65.  
  66. /* build the index */
  67. void buildindex()
  68. {
  69.     char instr[MAXSTR];
  70.     char prev[MAXSTR];
  71.     unsigned long lastprev;
  72.     unsigned long lastinstr;
  73.     unsigned long filesize;
  74.     long i;
  75.     long j;
  76.   
  77.     /* initialise the index array */ 
  78.     for (i=0; i < IDXSIZ; i++)
  79.       findx[i].location = 0; 
  80.  
  81.     /* put in the first entry, it has to be zero */
  82.     findx[0].location = ftell(f);
  83.     fgets(instr,MAXSTR-1,f);
  84.     strcpy(prev,instr);
  85.     i = 1;
  86.  
  87.     /* find the file length */
  88.     fseek(f, 0l, SEEK_END);
  89.     filesize = ftell(f);
  90.  
  91.     /* put the pointer back */
  92.     fseek(f, 0l, SEEK_SET);
  93.  
  94.     /* find the other entries */
  95.     while (!feof(f))
  96.     {
  97.        lastinstr = ftell(f);
  98.        if (fgets(instr,MAXSTR-1,f)==NULL)
  99.           break;
  100.        if (stricmp(instr,prev)<0)
  101.        {
  102.           errormesg("Error: In dictionary order! Re-sort dictionary",-2); 
  103.           exit(1);
  104.           }
  105.        if ((lastinstr*IDXSIZ)/ filesize > i)
  106.        {
  107.           strncpy(findx[i].word,instr,HASHWID-1);
  108.           findx[i].location = lastinstr;
  109.           i++;
  110.           }
  111.        strcpy(prev,instr);
  112.        }
  113.     /* guarantee that the list is full */
  114.     i--;     /* incremented so we decrement it */
  115.     for (j=i; j < IDXSIZ; j++)
  116.     {
  117.         /* copy the last good record to pad */
  118.         findx[j].location = findx[i].location;
  119.         strcpy(findx[j].word, findx[i].word);  
  120.         }
  121.     clearerr(f);
  122.     } 
  123.  
  124. int readindex(indx)
  125. char *indx;
  126. {
  127.    FILE *fi;
  128.    time_t time;
  129.    struct stat statbuf;
  130.    unsigned long t;
  131.  
  132.    fi = fopen(indx,"rb");
  133.    if (fi == NULL)
  134.       return(FAIL);
  135.    /* check that the modify times are concordant */
  136.    fread(&time,sizeof(time_t),1,fi);
  137.    fstat(fileno(f), &statbuf);
  138.    if (time != statbuf.st_mtime)
  139.       return(FAIL);
  140.    fread(&t,sizeof(long),1,fi);
  141.    if (t != IDXSIZ) return(FAIL);
  142.    fread(&t,sizeof(long),1,fi);
  143.    if (t != HASHWID) return(FAIL);
  144.    fread(findx,sizeof(struct indexelm),IDXSIZ,fi);
  145.    fclose(fi);
  146.    return(SUCCESS);
  147.    } 
  148.    
  149. void writeindex(indx)
  150. char *indx;
  151. {
  152.    FILE *fi;
  153.    struct stat statbuf;
  154.    unsigned long t;
  155.  
  156.    fi = fopen(indx,"wb");
  157.    if (fi == NULL)
  158.    {
  159.        fprintf(stderr, "Cannot create index: %s\n",indx);
  160.        return;
  161.        } 
  162.    /* set the modify time and write the file */
  163.    fstat(fileno(f), &statbuf);
  164.    fwrite(&(statbuf.st_mtime),sizeof(time_t),1,fi);
  165.    t = IDXSIZ;
  166.    fwrite(&t,sizeof(long),1,fi);
  167.    t = HASHWID;
  168.    fwrite(&t,sizeof(long),1,fi);
  169.    fwrite(findx,sizeof(struct indexelm),IDXSIZ,fi);
  170.    fclose(fi);
  171.    } 
  172.  
  173. int searchfile(match, entry)
  174. char *match;
  175. char *entry;
  176. {
  177.    char instr[MAXSTR];
  178.    char *stripped;
  179.    long i;
  180.    /* bsearch additions */
  181.    long up;
  182.    long down;
  183.    long avr;
  184.    int cmp;
  185.  
  186.    /* search through the list if something is there then fseek it */
  187. /*   i = 0;
  188.    while ((i < IDXSIZ) && (strnicmp(findx[i].word, match,HASHWID-1) <= 0)) 
  189.    {
  190.       i++;
  191.       }
  192.    i--; */   /* subtract one to look at the right location */
  193.  
  194.    /* bsearch */  
  195.    up = IDXSIZ;
  196.    down = 0;
  197.    while (1)
  198.    {
  199.        avr = (up+down)/2;
  200.        cmp =  strnicmp(findx[avr].word, match, HASHWID-1);
  201.        if (!cmp)
  202.        {
  203.            i = avr - 1;
  204.            break;
  205.            }
  206.        if (up - down < 2)
  207.        {
  208.            i = down;
  209.            break;
  210.            }
  211.        if (cmp < 0)
  212.        {
  213.            down = avr;
  214.            }
  215.        if (cmp > 0)
  216.        {
  217.            up = avr;
  218.            }
  219.        }
  220.    /* catch alls - the second one is not really needed */
  221.    if (i < 0)
  222.       i = 0; 
  223.    if (i > IDXSIZ)
  224.       i = IDXSIZ;
  225.    
  226.    fseek(f,findx[i].location,SEEK_SET);
  227.  
  228.    while (!feof(f))
  229.    {
  230.        if (fgets(instr,MAXSTR-1,f)==NULL)
  231.       break;
  232.        stripped = strip(instr,"\r\n");
  233.        if (stricmp(stripped, match) > 0)
  234.       return(FAIL);
  235.        if (!stricmp(stripped, match))
  236.        {
  237.        /* it matches send back the matching entry */
  238.        strcpy(entry, stripped);
  239.        return(SUCCESS);
  240.        }
  241.        }
  242.    clearerr(f);
  243.    return(FAIL);
  244.    }
  245.