home *** CD-ROM | disk | FTP | other *** search
- static char sccsid[] = "%Z%%M% %I% %E% (c)1993 thalerd";
- /* STATS.C
- * This module will cache subjects and authors for items in conferences
- * Loading entries is delayed until reference time
- * NUMCACHE conferences may be cached at a time
- */
- #include <stdio.h>
- #include <string.h>
- #include "config.h"
- #include "struct.h"
- #include "globals.h"
- #include "lib.h"
- #include "stats.h"
- #include "xalloc.h"
-
- #define NUMCACHE 2
-
- typedef struct {
- short idx; /* conference index */
- char **config; /* config info */
- char *subj[MAX_ITEMS];
- char *auth[MAX_ITEMS];
- } cache_t;
-
- static cache_t cache[NUMCACHE];
- static int start=1;
-
- /* GET INDEX OF CF */
- int
- get_cache(idx)
- int idx;
- {
- short i;
-
- /* Initialize cache */
- if (start) {
- for (i=0; i<NUMCACHE; i++)
- cache[i].idx = -1;
- start = 0;
- i=0;
- } else {
-
- /* Find cf if already cached */
- for (i=0; i<NUMCACHE && idx!=cache[i].idx; i++);
- if (i<NUMCACHE)
- return i;
-
- /* Find one to evict */
- for (i=0; cache[i].idx == confidx; i++); /* never evict current cf */
-
- /* Evict it */
- if (cache[i].idx>=0) {
- xfree(cache[i].config);
- free_elts(cache[i].subj);
- free_elts(cache[i].auth);
- }
- }
-
- /* Initialize with new conference info */
- cache[i].idx = idx;
- if (!(cache[i].config=grab_file(conflist[idx].location,"config",0)))
- return -1;
- return i;
- }
-
- void
- clear_cache()
- {
- int i;
- for (i=0; i<NUMCACHE; i++) {
- xfree(cache[i].config);
- free_elts(cache[i].subj);
- free_elts(cache[i].auth);
- }
- }
-
- /******************************************************************************/
- /* READ ITEM INFORMATION INTO THE CACHE */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- load_subj(i,idx,item,sum) /* ARGUMENTS: */
- int i;
- short idx; /* Conference */
- short item;
- sumentry_t *sum;
- {
- char **header;
- char path[MAX_LINE_LENGTH];
-
- sprintf(path,"%s/_%d",conflist[idx].location,item+1);
- header = grab_file(path,NULL,GF_HEADER);
- if (xsizeof(header)<6) {
- sum[item].nr=0;
- } else {
- if (!cache[i].subj[item])
- cache[i].subj[item] = xstrdup(header[1]+2);
- if (!cache[i].auth[item])
- cache[i].auth[item] = xstrdup(strchr(header[3]+2,',')+1);
- sscanf(header[5]+2,"%lx",&(sum[item].first));
- }
- xfree(header);
- }
-
- void
- store_auth(idx, item, str)
- short idx;
- short item;
- char *str;
- {
- int i;
-
- if ((i = get_cache(idx))<0)
- return;
- if (!cache[i].auth[item])
- cache[i].auth[item] = xstrdup(str);
- }
-
- void
- store_subj(idx, item, str)
- short idx;
- short item;
- char *str;
- {
- int i;
-
- if ((i = get_cache(idx))<0)
- return;
- if (!cache[i].subj[item])
- cache[i].subj[item] = xstrdup(str);
- }
-
- /* LOOKUP A SUBJECT */
- char *
- get_subj(idx, item, sum)
- short idx;
- short item;
- sumentry_t *sum;
- {
- int i;
-
- if (sum[item].flags & IF_RETIRED)
- return 0;
- if ((i = get_cache(idx))<0)
- return 0;
- if (!cache[i].subj[item])
- load_subj(i,idx,item, sum);
- return cache[i].subj[item];
- }
-
- /* LOOKUP AN AUTHOR */
- char *
- get_auth(idx, item, sum)
- short idx;
- short item;
- sumentry_t *sum;
- {
- int i;
-
- if (sum[item].flags & IF_RETIRED)
- return 0;
- if ((i = get_cache(idx))<0)
- return 0;
- if (!cache[i].auth[item])
- load_subj(i,idx,item, sum);
- return cache[i].auth[item];
- }
-
- char **
- get_config(idx)
- short idx;
- {
- int i;
-
- if ((i = get_cache(idx))<0)
- return 0;
- return cache[i].config;
- }
-
- /******************************************************************************/
- /* FREE SUBJECTS */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- free_elts(arr) /* ARGUMENTS: */
- char **arr; /* Array of subjects */
- {
- int i;
-
- for (i=0; i<MAX_ITEMS; i++) {
- if (arr[i])
- xfree(arr[i]);
- arr[i]=0;
- }
- }
-
-