home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / yapp / part01 / stats.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  3.9 KB  |  195 lines

  1. static   char sccsid[] = "%Z%%M% %I% %E% (c)1993 thalerd";
  2. /* STATS.C 
  3.  * This module will cache subjects and authors for items in conferences
  4.  * Loading entries is delayed until reference time
  5.  * NUMCACHE conferences may be cached at a time
  6.  */
  7. #include <stdio.h>
  8. #include <string.h>
  9. #include "config.h"
  10. #include "struct.h"
  11. #include "globals.h"
  12. #include "lib.h"
  13. #include "stats.h"
  14. #include "xalloc.h"
  15.  
  16. #define NUMCACHE 2
  17.  
  18. typedef struct {
  19.    short  idx;                /* conference index              */
  20.     char **config;             /* config info                   */
  21.    char  *subj[MAX_ITEMS];
  22.    char  *auth[MAX_ITEMS];
  23. } cache_t;
  24.  
  25. static cache_t cache[NUMCACHE];
  26. static int start=1;
  27.  
  28. /* GET INDEX OF CF */
  29. int
  30. get_cache(idx)
  31. int idx;
  32. {
  33.    short i;
  34.  
  35.     /* Initialize cache */
  36.     if (start) {
  37.        for (i=0; i<NUMCACHE; i++)
  38.             cache[i].idx = -1;
  39.         start = 0;
  40.         i=0;
  41.     } else {
  42.  
  43.        /* Find cf if already cached */
  44.        for (i=0; i<NUMCACHE && idx!=cache[i].idx; i++);
  45.        if (i<NUMCACHE)
  46.            return i;
  47.  
  48.       /* Find one to evict */
  49.        for (i=0; cache[i].idx == confidx; i++); /* never evict current cf */
  50.  
  51.        /* Evict it */
  52.        if (cache[i].idx>=0) {
  53.          xfree(cache[i].config);
  54.           free_elts(cache[i].subj);
  55.           free_elts(cache[i].auth);
  56.        }
  57.     }
  58.  
  59.     /* Initialize with new conference info */
  60.    cache[i].idx = idx;
  61.    if (!(cache[i].config=grab_file(conflist[idx].location,"config",0)))
  62.        return -1;
  63.     return i;
  64. }
  65.  
  66. void
  67. clear_cache()
  68. {
  69.    int i;
  70.     for (i=0; i<NUMCACHE; i++) {
  71.       xfree(cache[i].config);
  72.        free_elts(cache[i].subj);
  73.        free_elts(cache[i].auth);
  74.    }
  75. }
  76.  
  77. /******************************************************************************/
  78. /* READ ITEM INFORMATION INTO THE CACHE                                       */
  79. /******************************************************************************/
  80. void                      /* RETURNS: (nothing) */
  81. load_subj(i,idx,item,sum) /* ARGUMENTS: */
  82. int         i;
  83. short       idx;          /*    Conference */
  84. short       item;
  85. sumentry_t *sum;
  86. {
  87.    char **header;
  88.    char   path[MAX_LINE_LENGTH];
  89.  
  90.    sprintf(path,"%s/_%d",conflist[idx].location,item+1);
  91.    header = grab_file(path,NULL,GF_HEADER);
  92.    if (xsizeof(header)<6) {
  93.       sum[item].nr=0;
  94.    } else {
  95.         if (!cache[i].subj[item])
  96.          cache[i].subj[item]   = xstrdup(header[1]+2);
  97.         if (!cache[i].auth[item])
  98.          cache[i].auth[item]   = xstrdup(strchr(header[3]+2,',')+1);
  99.       sscanf(header[5]+2,"%lx",&(sum[item].first));
  100.     }
  101.    xfree(header);
  102. }
  103.  
  104. void
  105. store_auth(idx, item, str)
  106. short idx;
  107. short item;
  108. char *str;
  109. {
  110.     int i;
  111.  
  112.     if ((i = get_cache(idx))<0)
  113.         return;
  114.     if (!cache[i].auth[item])
  115.         cache[i].auth[item] = xstrdup(str);
  116. }
  117.  
  118. void
  119. store_subj(idx, item, str)
  120. short idx;
  121. short item;
  122. char *str;
  123. {
  124.     int i;
  125.  
  126.     if ((i = get_cache(idx))<0)
  127.         return;
  128.     if (!cache[i].subj[item])
  129.         cache[i].subj[item] = xstrdup(str);
  130. }
  131.  
  132. /* LOOKUP A SUBJECT */
  133. char *
  134. get_subj(idx, item, sum)
  135. short idx;
  136. short item;
  137. sumentry_t *sum;
  138. {
  139.    int i;
  140.     
  141.    if (sum[item].flags & IF_RETIRED)
  142.         return 0;
  143.     if ((i = get_cache(idx))<0)
  144.         return 0;
  145.     if (!cache[i].subj[item])
  146.         load_subj(i,idx,item, sum);
  147.    return cache[i].subj[item];
  148. }
  149.  
  150. /* LOOKUP AN AUTHOR */
  151. char *
  152. get_auth(idx, item, sum)
  153. short idx;
  154. short item;
  155. sumentry_t *sum;
  156. {
  157.    int i;
  158.     
  159.    if (sum[item].flags & IF_RETIRED)
  160.         return 0;
  161.     if ((i = get_cache(idx))<0)
  162.         return 0;
  163.     if (!cache[i].auth[item])
  164.         load_subj(i,idx,item, sum);
  165.    return cache[i].auth[item];
  166. }
  167.  
  168. char **
  169. get_config(idx)
  170. short idx;
  171. {
  172.     int i;
  173.  
  174.     if ((i = get_cache(idx))<0)
  175.         return 0;
  176.     return cache[i].config;
  177. }
  178.  
  179. /******************************************************************************/
  180. /* FREE SUBJECTS                                                              */
  181. /******************************************************************************/
  182. void           /* RETURNS: (nothing)   */
  183. free_elts(arr) /* ARGUMENTS:           */
  184. char **arr;    /*    Array of subjects */
  185. {
  186.    int i;
  187.  
  188.    for (i=0; i<MAX_ITEMS; i++) {
  189.       if (arr[i])
  190.             xfree(arr[i]);
  191.       arr[i]=0;
  192.    }
  193. }
  194.  
  195.