Subversion Repositories wimsdev

Rev

Rev 8155 | Rev 8849 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*    Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17.  
  18. /* Caches and its management */
  19.  
  20. #include "wimslogd.h"
  21.  
  22. struct classdata classdata[MAX_CLASSCACHE];
  23.  
  24. struct classcache {
  25.     struct classdata *ptr;
  26. } classcache[MAX_CLASSCACHE];
  27. int classcaches;
  28.  
  29. struct sheetdata {
  30.     char buffer[SHEETBUFLEN];
  31.     int name,header,status,exocnt,exo[MAX_EXOS];
  32.     time_t start, last;
  33.     int access;
  34. } sheetdata[MAX_SHEETCACHE];
  35.  
  36. struct sheetcache {
  37.     struct sheetdata *ptr;
  38. } sheetcache[MAX_SHEETCACHE];
  39. int sheetcaches;
  40.  
  41.  /* searches a list. Returns index if found, -1 if nomatch.
  42.   * Uses binary search, list must be sorted.
  43.   */
  44. int search_data(void *list, int items, size_t item_size, unsigned short int t)
  45. {
  46.     int i1,i2,j,k;
  47.     unsigned short int *p;
  48.  
  49.     if(items<=0) return -1;
  50.     j=0; p=list; k=*p-t;
  51.     if(k==0) return k; if(k>0) return -1;
  52.     p=list+(items-1)*item_size;
  53.     k=*p-t; if(k==0) return items-1; if(k<0) return ~items;
  54.     for(i1=0,i2=items-1;i2>i1+1;) {
  55.         j=(i2+i1)/2;
  56.         p=list+(j*item_size);
  57.         k=*p-t;
  58.         if(k==0) return j;
  59.         if(k>0) {i2=j; continue;}
  60.         if(k<0) {i1=j; continue;}
  61.     }
  62.     return ~i2;
  63. }
  64.  
  65. /* remove old cache items */
  66. void cleancache(void)
  67. {
  68.     int i;
  69.     time_t now;
  70.     struct classdata *cd;
  71.     struct sheetdata *sd;
  72.     now=time(NULL);
  73.     for(i=0;i<classcaches;i++) {
  74.         cd=classcache[i].ptr;
  75.         if(now<cd->start+CLASSCACHE_DELAY) continue;
  76.         cd->access=0;
  77.         memmove(classcache+i,classcache+i+1,(classcaches-i-1)*sizeof(classcache[0]));
  78.         classcaches--;
  79.     }
  80.     for(i=0;i<sheetcaches;i++) {
  81.         sd=sheetcache[i].ptr;
  82.         if(now<sd->start+SHEETCACHE_DELAY) continue;
  83.         sd->access=0;
  84.         memmove(sheetcache+i,sheetcache+i+1,(sheetcaches-i-1)*sizeof(sheetcache[0]));
  85.         sheetcaches--;
  86.     }
  87. }
  88.  
  89. /* Locate the cache number of a class */
  90. struct classdata *getclasscache(char *cl)
  91. {
  92.     int i,j,k,l,m,n, oldest;
  93.     struct stat st;
  94.     struct classdata *cd;
  95.     char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  96.     char *p1, *p2, *q1, *q2;
  97.     time_t tt;
  98.     tt=0, oldest=0;
  99.     for(i=0;i<classcaches;i++) {
  100.         cd=classcache[i].ptr;
  101.         if(tt>cd->start) {tt=cd->start; oldest=i;}
  102.         if(strcmp(cd->name,cl)==0) {
  103.             cd->access++;
  104.             return cd;
  105.         }
  106.     }
  107.     if(classcaches>=MAX_CLASSCACHE) {
  108.         i=oldest;cd=classcache[i].ptr;
  109.         cd->access=0;
  110.         memmove(classcache+i,classcache+i+1,(classcaches-i-1)*sizeof(classcache[0]));
  111.         classcaches--;
  112.     }
  113.     for(i=0;i<MAX_CLASSCACHE && classdata[i].access>0; i++);
  114.     if(i>classcaches) return NULL;
  115.     cd=classdata+i; cd->access=1;
  116.     classcache[classcaches++].ptr=cd;
  117.     snprintf(cd->name,sizeof(cd->name),"%s",cl);
  118.     cd->start=time(NULL); cd->exocnt=0;
  119. /* Now get the exo data */
  120.     accessfile(buf,"r","sheets/.require");
  121.     for(i=k=0,p1=buf; *p1; i++,p1=p2) {
  122.         p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  123.         for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
  124.             q2=find_word_end(q1); if(*q2) *q2++=0;
  125.             cd->exos[k].num=(i<<8)+j;cd->exos[k].require=atof(q1);
  126.             cd->exos[k].weight=0;
  127.             k++;
  128.         }
  129.  
  130.     }
  131.     if(k>=MAX_CLASSEXOS) return NULL;
  132.     cd->exocnt=k; cd->examstart=k; cd->modif=0;
  133.     accessfile(buf,"r","sheets/.weight");
  134.     for(i=k=0,p1=buf; *p1; i++,p1=p2) {
  135.         p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  136.         for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
  137.             q2=find_word_end(q1); if(*q2) *q2++=0;
  138.             if(cd->exos[k].num==(i<<8)+j) {
  139.                 cd->exos[k].weight=atof(q1);
  140.                 k++;
  141.             }
  142.             else while(k<cd->exocnt && cd->exos[k].num<(i<<8)+j) k++;
  143.         }
  144.     }
  145.     if(stat("exams/.exams",&st)==0) cd->modif=st.st_mtime; else return cd;
  146.     accessfile(buf,"r","exams/.exams");
  147.     if(buf[0]==0) return cd;
  148.     if(buf[0]==':') p1=buf-1; else p1=strstr(buf,"\n:");
  149.     for(n=m=0,k=cd->exocnt; p1 && k<MAX_CLASSEXOS && m<MAX_EXOS; p1=p2,m++) {
  150.         p1+=2;
  151.         p2=strstr(p1,"\n:"); if(p2) *p2=0;
  152. //      if(*find_word_start(p1)<'1') continue;  /* status */
  153.         fnd_line(p1,3,buf2); if(buf2[0]==0) continue;
  154.         q1=find_word_start(buf2); q2=find_word_end(q1);
  155.         if(*q2) *q2++=0;
  156.         q2=find_word_start(q2); *find_word_end(q2)=0;
  157.         i=atoi(q1); j=atoi(q2); if(i<=0 || j<=0) continue;
  158.         cd->exos[k].num=0xFF00+m;
  159.         cd->exos[k].weight=i; cd->exos[k].require=j;    /* weight: duration. require: retries */
  160.         fnd_line(p1,6,buf2); q1=find_word_start(buf2);
  161.         singlespace(q1); strip_trailing_spaces(q1);
  162.         cd->ctptr[k-cd->exocnt]=n; cd->ctbuf[n]=0;
  163.         if(n+strlen(q1)>CTBUFLEN-MAX_EXOS-16) *q1=0;    /* silent truncation */
  164.         l=strlen(q1)+1; memmove(cd->ctbuf+n,q1,l); n+=l;
  165.         k++;
  166.     }
  167.     cd->examcnt=k-cd->exocnt; cd->exocnt=k;
  168.     return cd;
  169. }
  170. // misprint ?? sheetdata ?? does not seem to be used.
  171. /* prepare cache for a sheet */
  172. struct sheetata *getsheetcache(char *cl, char *sh)
  173. {
  174.  
  175.  
  176.  
  177.     return NULL;
  178. }
  179.  
  180.