home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / TEXT / UTILITY / SSPELL11.ZIP / CACHE.C next >
Encoding:
C/C++ Source or Header  |  1992-01-26  |  4.6 KB  |  175 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. /* **************************************************************** */
  30.  
  31. #include "cache.h"
  32. #include <stdio.h>
  33. #include "strfn.h"
  34.  
  35. #define NOTINCACHE (0)
  36. #define INCACHE (1)
  37.  
  38. /* this provides a cache of enteries in a list format. The format is a 
  39.    move to front linked list. */
  40.  
  41. /* Author: Maurice Castro */
  42.  
  43. typedef struct liststr
  44. {
  45.    char *name;
  46.    struct liststr *next;
  47.    } LIST;
  48.  
  49. static int maxcachesize;
  50. static int incache;
  51. LIST *list;
  52.  
  53. void initcache(size)
  54. int size;
  55. {
  56.    list = NULL;
  57.    maxcachesize = size;
  58.    incache = 0;
  59.    }
  60.  
  61. int searchcache(match, actual)
  62. char *match;
  63. char *actual;
  64.    LIST *cur;
  65.    LIST *prev;
  66.    int mtc;
  67.   
  68.    if (list == NULL) return(NOTINCACHE);
  69.  
  70.    cur = list;
  71.    prev = NULL;
  72.    mtc = 0;
  73.  
  74.    while (cur->next != NULL)
  75.    {
  76.       if (!stricmp(match, cur->name)) 
  77.       {
  78.          mtc = 1;
  79.          break;
  80.          }
  81.       prev = cur;
  82.       cur = cur->next;
  83.       }
  84.    if (!mtc) 
  85.       return(NOTINCACHE);
  86.    if (prev != NULL)
  87.    {
  88.       prev->next = cur->next;
  89.       cur->next = list;
  90.       list = cur;
  91.       }
  92.    strcpy(actual, cur->name);
  93.    return(INCACHE);
  94.    }
  95.  
  96. int addtocache(item)
  97. char *item;
  98. {
  99.     int i;
  100.     LIST *cur;
  101.     LIST *prev;
  102.     LIST *newel;
  103.  
  104.     /* make new element */
  105.     newel = (LIST *) malloc(sizeof(LIST));
  106.     if (newel == NULL)
  107.        return(0);
  108.     newel->name = (char *) malloc(sizeof(char)*(strlen(item)+1));
  109.     strcpy(newel->name, item);
  110.     newel->next = NULL;
  111.  
  112.     /* cache full */
  113.     if (incache >= maxcachesize)   
  114.     {
  115.        /* traverse cache */
  116.        i = 0;
  117.        cur = list;
  118.        while (i < maxcachesize)
  119.        {
  120.           prev = cur;
  121.           cur = cur->next;
  122.           i++;
  123.           }
  124.  
  125.        /* add new element */
  126.        prev->next = NULL;
  127.        newel->next = list;
  128.        list = newel;
  129.        incache = maxcachesize;
  130.  
  131.        /* fix overspill */
  132.        while (cur != NULL)
  133.        {
  134.           prev = cur;
  135.           cur = cur->next; 
  136.           free(prev->name);
  137.           free(prev);
  138.           }
  139.        return(1);
  140.        }
  141.  
  142.     /* cache not full yet */
  143.     incache++;
  144.  
  145.     prev = NULL;
  146.     cur = list;
  147.     while (cur != NULL)
  148.     {
  149.        prev = cur;
  150.        cur = cur->next;
  151.        }
  152.     if (prev == NULL)
  153.     {
  154.        list = newel;
  155.        return(1);
  156.        }
  157.     newel->next = list;
  158.     list = newel;
  159.     return(1);
  160.     }
  161.  
  162. /* for debugging use only */
  163.  
  164. void printcache()
  165. {
  166.     LIST *cur;
  167.     cur = list;
  168.     while (cur != NULL)
  169.     {
  170.         printf("%s\n", cur->name);
  171.         cur = cur->next;
  172.         }
  173.     }
  174.