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

  1. /* **************************************************************** */
  2. /*             sspell - similar to Unix spell                       */
  3. /*                        version 1.2                               */
  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.    {
  140.       fclose(fi);                     /* Mike O'Carroll reminded me of the */
  141.       return(FAIL);                   /* need to close files on exiting this */
  142.       }                               /* routine. M. Castro 4/3/92 */
  143.    fread(&t,sizeof(long),1,fi);
  144.    if (t != IDXSIZ) 
  145.    {
  146.       fclose(fi);
  147.       return(FAIL);
  148.       }
  149.    fread(&t,sizeof(long),1,fi);
  150.    if (t != HASHWID) 
  151.    {
  152.       fclose(fi);
  153.       return(FAIL);
  154.       }
  155.    fread(findx,sizeof(struct indexelm),IDXSIZ,fi);
  156.    fclose(fi);
  157.    return(SUCCESS);
  158.    } 
  159.    
  160. void writeindex(indx)
  161. char *indx;
  162. {
  163.    FILE *fi;
  164.    struct stat statbuf;
  165.    unsigned long t;
  166.  
  167.    fi = fopen(indx,"wb");
  168.    if (fi == NULL)
  169.    {
  170.        fprintf(stderr, "Cannot create index: %s\n",indx);
  171.        return;
  172.        } 
  173.    /* set the modify time and write the file */
  174.    fstat(fileno(f), &statbuf);
  175.    fwrite(&(statbuf.st_mtime),sizeof(time_t),1,fi);
  176.    t = IDXSIZ;
  177.    fwrite(&t,sizeof(long),1,fi);
  178.    t = HASHWID;
  179.    fwrite(&t,sizeof(long),1,fi);
  180.    fwrite(findx,sizeof(struct indexelm),IDXSIZ,fi);
  181.    fclose(fi);
  182.    } 
  183.  
  184. int searchfile(match, entry)
  185. char *match;
  186. char *entry;
  187. {
  188.    char instr[MAXSTR];
  189.    char *stripped;
  190.    long i;
  191.    /* bsearch additions */
  192.    long up;
  193.    long down;
  194.    long avr;
  195.    int cmp;
  196.  
  197.    /* search through the list if something is there then fseek it */
  198. /*   i = 0;
  199.    while ((i < IDXSIZ) && (strnicmp(findx[i].word, match,HASHWID-1) <= 0)) 
  200.    {
  201.       i++;
  202.       }
  203.    i--; */   /* subtract one to look at the right location */
  204.  
  205.    /* bsearch */  
  206.    up = IDXSIZ;
  207.    down = 0;
  208.    while (1)
  209.    {
  210.        avr = (up+down)/2;
  211.        cmp =  strnicmp(findx[avr].word, match, HASHWID-1);
  212.        if (!cmp)
  213.        {
  214.            i = avr - 1;
  215.            break;
  216.            }
  217.        if (up - down < 2)
  218.        {
  219.            i = down;
  220.            break;
  221.            }
  222.        if (cmp < 0)
  223.        {
  224.            down = avr;
  225.            }
  226.        if (cmp > 0)
  227.        {
  228.            up = avr;
  229.            }
  230.        }
  231.    /* catch alls - the second one is not really needed */
  232.    if (i < 0)
  233.       i = 0; 
  234.    if (i > IDXSIZ)
  235.       i = IDXSIZ;
  236.    
  237.    fseek(f,findx[i].location,SEEK_SET);
  238.  
  239.    while (!feof(f))
  240.    {
  241.        if (fgets(instr,MAXSTR-1,f)==NULL)
  242.       break;
  243.        stripped = strip(instr,"\r\n");
  244.        if (stricmp(stripped, match) > 0)
  245.       return(FAIL);
  246.        if (!stricmp(stripped, match))
  247.        {
  248.        /* it matches send back the matching entry */
  249.        strcpy(entry, stripped);
  250.        return(SUCCESS);
  251.        }
  252.        }
  253.    clearerr(f);
  254.    return(FAIL);
  255.    }
  256.