Subversion Repositories wimsdev

Rev

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

Rev Author Line No. Line
10 reyssat 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
 
8155 bpr 18
/* Caches and its management */
10 reyssat 19
 
8185 bpr 20
#include "wimslogd.h"
10 reyssat 21
 
8185 bpr 22
struct classdata classdata[MAX_CLASSCACHE];
10 reyssat 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
 
8155 bpr 41
 /* searches a list. Returns index if found, -1 if nomatch.
42
  * Uses binary search, list must be sorted.
43
  */
10 reyssat 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;
8155 bpr 48
 
10 reyssat 49
    if(items<=0) return -1;
50
    j=0; p=list; k=*p-t;
11125 georgesk 51
    if(k==0) return k;
52
    if(k>0) return -1;
8155 bpr 53
    p=list+(items-1)*item_size;
10 reyssat 54
    k=*p-t; if(k==0) return items-1; if(k<0) return ~items;
55
    for(i1=0,i2=items-1;i2>i1+1;) {
8849 bpr 56
      j=(i2+i1)/2;
57
      p=list+(j*item_size);
58
      k=*p-t;
59
      if(k==0) return j;
60
      if(k>0) {i2=j; continue;}
61
      if(k<0) {i1=j; continue;}
10 reyssat 62
    }
63
    return ~i2;
64
}
65
 
8155 bpr 66
/* remove old cache items */
10 reyssat 67
void cleancache(void)
68
{
69
    int i;
70
    time_t now;
71
    struct classdata *cd;
72
    struct sheetdata *sd;
73
    now=time(NULL);
74
    for(i=0;i<classcaches;i++) {
8849 bpr 75
      cd=classcache[i].ptr;
76
      if(now<cd->start+CLASSCACHE_DELAY) continue;
77
      cd->access=0;
78
      memmove(classcache+i,classcache+i+1,(classcaches-i-1)*sizeof(classcache[0]));
79
      classcaches--;
10 reyssat 80
    }
81
    for(i=0;i<sheetcaches;i++) {
8849 bpr 82
      sd=sheetcache[i].ptr;
83
      if(now<sd->start+SHEETCACHE_DELAY) continue;
84
      sd->access=0;
85
      memmove(sheetcache+i,sheetcache+i+1,(sheetcaches-i-1)*sizeof(sheetcache[0]));
86
      sheetcaches--;
10 reyssat 87
    }
88
}
89
 
8155 bpr 90
/* Locate the cache number of a class */
10 reyssat 91
struct classdata *getclasscache(char *cl)
92
{
93
    int i,j,k,l,m,n, oldest;
94
    struct stat st;
95
    struct classdata *cd;
96
    char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
97
    char *p1, *p2, *q1, *q2;
98
    time_t tt;
99
    tt=0, oldest=0;
100
    for(i=0;i<classcaches;i++) {
8849 bpr 101
      cd=classcache[i].ptr;
102
      if(tt>cd->start) {tt=cd->start; oldest=i;}
103
      if(strcmp(cd->name,cl)==0) {
104
          cd->access++;
105
          return cd;
106
      }
10 reyssat 107
    }
108
    if(classcaches>=MAX_CLASSCACHE) {
8849 bpr 109
      i=oldest;cd=classcache[i].ptr;
110
      cd->access=0;
111
      memmove(classcache+i,classcache+i+1,(classcaches-i-1)*sizeof(classcache[0]));
112
      classcaches--;
10 reyssat 113
    }
114
    for(i=0;i<MAX_CLASSCACHE && classdata[i].access>0; i++);
115
    if(i>classcaches) return NULL;
116
    cd=classdata+i; cd->access=1;
117
    classcache[classcaches++].ptr=cd;
118
    snprintf(cd->name,sizeof(cd->name),"%s",cl);
119
    cd->start=time(NULL); cd->exocnt=0;
8155 bpr 120
/* Now get the exo data */
8342 bpr 121
    wlogdaccessfile(buf,"r","sheets/.require");
10 reyssat 122
    for(i=k=0,p1=buf; *p1; i++,p1=p2) {
8849 bpr 123
      p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
124
      for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
125
          q2=find_word_end(q1); if(*q2) *q2++=0;
126
          cd->exos[k].num=(i<<8)+j;cd->exos[k].require=atof(q1);
127
          cd->exos[k].weight=0;
128
          k++;
129
      }
8155 bpr 130
 
10 reyssat 131
    }
132
    if(k>=MAX_CLASSEXOS) return NULL;
133
    cd->exocnt=k; cd->examstart=k; cd->modif=0;
8342 bpr 134
    wlogdaccessfile(buf,"r","sheets/.weight");
10 reyssat 135
    for(i=k=0,p1=buf; *p1; i++,p1=p2) {
8849 bpr 136
      p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
137
      for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
138
          q2=find_word_end(q1); if(*q2) *q2++=0;
139
          if(cd->exos[k].num==(i<<8)+j) {
140
            cd->exos[k].weight=atof(q1);
141
            k++;
142
          }
143
          else while(k<cd->exocnt && cd->exos[k].num<(i<<8)+j) k++;
144
      }
10 reyssat 145
    }
146
    if(stat("exams/.exams",&st)==0) cd->modif=st.st_mtime; else return cd;
8342 bpr 147
    wlogdaccessfile(buf,"r","exams/.exams");
10 reyssat 148
    if(buf[0]==0) return cd;
149
    if(buf[0]==':') p1=buf-1; else p1=strstr(buf,"\n:");
150
    for(n=m=0,k=cd->exocnt; p1 && k<MAX_CLASSEXOS && m<MAX_EXOS; p1=p2,m++) {
8849 bpr 151
      p1+=2;
152
      p2=strstr(p1,"\n:"); if(p2) *p2=0;
153
//      if(*find_word_start(p1)<'1') continue;      /* status */
154
      fnd_line(p1,3,buf2); if(buf2[0]==0) continue;
155
      q1=find_word_start(buf2); q2=find_word_end(q1);
156
      if(*q2) *q2++=0;
157
      q2=find_word_start(q2); *find_word_end(q2)=0;
158
      i=atoi(q1); j=atoi(q2); if(i<=0 || j<=0) continue;
159
      cd->exos[k].num=0xFF00+m;
160
      cd->exos[k].weight=i; cd->exos[k].require=j;      /* weight: duration. require: retries */
161
      fnd_line(p1,6,buf2); q1=find_word_start(buf2);
162
      singlespace(q1); strip_trailing_spaces(q1);
163
      cd->ctptr[k-cd->exocnt]=n; cd->ctbuf[n]=0;
164
      if(n+strlen(q1)>CTBUFLEN-MAX_EXOS-16) *q1=0;      /* silent truncation */
165
      l=strlen(q1)+1; memmove(cd->ctbuf+n,q1,l); n+=l;
166
      k++;
10 reyssat 167
    }
168
    cd->examcnt=k-cd->exocnt; cd->exocnt=k;
169
    return cd;
170
}
7363 bpr 171
// misprint ?? sheetdata ?? does not seem to be used.
8155 bpr 172
/* prepare cache for a sheet */
10 reyssat 173
struct sheetata *getsheetcache(char *cl, char *sh)
174
{
8155 bpr 175
 
176
 
177
 
10 reyssat 178
    return NULL;
179
}
180