Subversion Repositories wimsdev

Rev

Rev 16391 | 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 {
12474 bpr 25
  struct classdata *ptr;
10 reyssat 26
} classcache[MAX_CLASSCACHE];
27
int classcaches;
28
 
8155 bpr 29
/* remove old cache items */
10 reyssat 30
void cleancache(void)
31
{
12474 bpr 32
  int i;
33
  time_t now;
34
  struct classdata *cd;
35
  now=time(NULL);
36
  for(i=0;i<classcaches;i++) {
37
    cd=classcache[i].ptr;
38
    if(now<cd->start+CLASSCACHE_DELAY) continue;
39
    cd->access=0;
40
    memmove(classcache+i,classcache+i+1,(classcaches-i-1)*sizeof(classcache[0]));
41
    classcaches--;
42
  }
10 reyssat 43
}
44
 
15847 bpr 45
void class_dump(struct classdata *cd)
46
{
47
 int i, j, k, nb;
48
 char *s;
49
 exodata *ed;
50
 my_debug("Classe: %s\n", cd->name);
51
 my_debug("Feuilles: %i, Exercices: %i\n", cd->sheetcnt, cd->exocnt);
52
 for (i=0;i<cd->sheetcnt;i++)
53
 {
54
   my_debug("Feuille %i: %i exercices\n",i+1,cd->sheets[i].exocnt);
55
   nb=cd->sheets[i].techcnt;
56
   if (nb==1)
57
     my_debug("Pas de variable technique\n");
58
   else
59
     {
60
       s = cd->techs+cd->sheets[i].techoffset;
61
       my_debug("Variable technique: %s, %i valeurs (actuel: %i)\n",s,nb,cd->sheets[i].techval);
62
       s += strlen(s); s++;
63
        for (j = 0; j < nb;j++)
64
        {
65
         my_debug("%s ",s);  s += strlen(s); s++;
66
        }
67
       my_debug("\n");
68
     }
69
 
70
   ed = cd->exos + cd->sheets[i].indstart;
71
   for (j = 0; j < nb; j++)
72
   {
73
     for (k = 0; k < cd->sheets[i].exocnt; k++, ed++)
74
       my_debug("[a: %i r: %f w:%f] ", ed->active, ed->require, ed->weight);
75
     my_debug("\n");
76
   }
77
 }
78
 if (cd->exam.exocnt>0)
79
 {
80
    ed = cd->exos + cd->exam.indstart;
81
    my_debug("Examen: %i exercices\n", cd->exam.exocnt);
82
    for (k = 0; k < cd->exam.exocnt; k++, ed++)
83
       my_debug("[a: %i r: %f w:%f] ", ed->active, ed->require, ed->weight);
84
     my_debug("\n");
85
 }
86
 else
87
   my_debug("Pas d'examen\n");
88
 my_debug("Fin de la classe\n");
89
}
90
 
8155 bpr 91
/* Locate the cache number of a class */
10 reyssat 92
struct classdata *getclasscache(char *cl)
93
{
16081 bpr 94
  int i,j=0,k,l,m,n, oldest, offset;
12474 bpr 95
  struct stat st;
96
  struct classdata *cd;
97
  char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
15847 bpr 98
  char *p1, *p2, *p3, *q1, *q2;
12474 bpr 99
  time_t tt;
100
  tt=0, oldest=0;
101
  for(i=0;i<classcaches;i++) {
102
    cd=classcache[i].ptr;
103
    if(tt>cd->start) {tt=cd->start; oldest=i;}
104
    if(strcmp(cd->name,cl)==0) {
105
      cd->access++;
106
      return cd;
10 reyssat 107
    }
12474 bpr 108
  }
109
  if(classcaches>=MAX_CLASSCACHE) {
110
    i=oldest;cd=classcache[i].ptr;
111
    cd->access=0;
112
    memmove(classcache+i,classcache+i+1,(classcaches-i-1)*sizeof(classcache[0]));
113
    classcaches--;
114
  }
115
  for(i=0;i<MAX_CLASSCACHE && classdata[i].access>0; i++);
116
  if(i>classcaches) return NULL;
117
  cd=classdata+i; cd->access=1;
118
  classcache[classcaches++].ptr=cd;
119
  snprintf(cd->name,sizeof(cd->name),"%s",cl);
15847 bpr 120
  cd->techs[0]=0; offset=1;
12474 bpr 121
  cd->start=time(NULL); cd->exocnt=0;
122
  /* Now get the exo data */
123
  wlogdaccessfile(buf,"r","sheets/.require");
15847 bpr 124
  p1=strchr (buf,':');
125
  if (p1==NULL) {
16391 bpr 126
    /*old format*/
15847 bpr 127
    for(i=k=0,p1=buf; *p1; i++,p1=p2) {
128
      cd->sheets[i].start=cd->sheets[i].indstart=k;
129
      p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
130
      for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
131
        q2=find_word_end(q1); if(*q2) *q2++=0;
132
        cd->exos[k].require=atof(q1);
133
        cd->exos[k].weight=0;
134
        cd->exos[k].active=1;
135
        k++;
136
      }
137
      cd->sheets[i].exocnt=j;
138
      cd->exocnt +=j;
139
      cd->sheets[i].techcnt=1;
140
   }
141
    cd->sheetcnt=i;
142
  }
143
  else {
144
  /* i: sheet, k: exo numero counted with multiplicity, l: tech version*/
145
    for (i=k=0,p1++; *p1; i++,p1=p2) {
146
      cd->sheets[i].indstart=k; /* numero of the first exo of the i sheet with multiplicity */
147
      cd->sheets[i].start=cd->exocnt;
148
      p2=strchr(p1,':'); if(p2) *p2++=0; else p2=p1+strlen(p1);
149
      for (l=0; *p1; l++,p1=p3) {
150
        p3=strchr(p1,'\n'); if(p3) *p3++=0; else p3=p1+strlen(p1);
151
        for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
152
          q2=find_word_end(q1); if(*q2) *q2++=0;
153
          cd->exos[k].require=atof(q1);
154
          /* default values, will be changed after reading the appropriate file */
155
          cd->exos[k].weight=0;
156
          cd->exos[k].active=1;
157
          k++;
158
        }
159
      }
160
      cd->sheets[i].exocnt=j; /* number of exos in the sheet i */
161
      cd->exocnt+=j; /* total number of exos sans multiplicité */
162
      cd->sheets[i].techcnt=l; //nombre de lignes=nombre de variables techniques,
10 reyssat 163
    }
15847 bpr 164
    cd->sheetcnt=i; /* number of sheets */
12474 bpr 165
  }
166
  if(k>=MAX_CLASSEXOS) return NULL;
15847 bpr 167
  cd->examstart=cd->exocnt; // nb exos sans multipl. dans les feuilles précédant l'examen
168
  cd->examcnt=0;
169
  cd->exam.indstart=k;
170
  cd->exam.start=cd->exocnt;
171
  cd->exam.exocnt=0;
172
  cd->modif=0;
12474 bpr 173
  wlogdaccessfile(buf,"r","sheets/.weight");
15847 bpr 174
  p1=strchr (buf,':');
175
  /*old format*/
176
  if (p1==NULL)
177
    for(i=k=0,p1=buf; *p1; i++,p1=p2) {
178
      p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
179
      for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
180
        q2=find_word_end(q1); if(*q2) *q2++=0;
12474 bpr 181
        cd->exos[k].weight=atof(q1);
182
        k++;
8849 bpr 183
      }
10 reyssat 184
    }
15847 bpr 185
  else
186
    for (i=k=0,p1++; *p1; i++,p1=p2) {
187
      p2=strchr(p1,':'); if(p2) *p2++=0; else p2=p1+strlen(p1);
188
      for (; *p1; p1=p3) {
189
        p3=strchr(p1,'\n'); if(p3) *p3++=0; else p3=p1+strlen(p1);
190
        for(q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS;q1=find_word_start(q2)) {
191
          q2=find_word_end(q1); if(*q2) *q2++=0;
192
          cd->exos[k].weight=atof(q1);
193
          k++;
194
        }
195
      }
196
    }
197
  if(stat("sheets/.vars",&st)==0) {
198
    wlogdaccessfile(buf,"r","sheets/.vars");
199
    p1=strchr (buf,':');
16391 bpr 200
    if (p1!=NULL) {
201
      for (i=0; i < cd->sheetcnt; i++, p1=p2){
202
        p1++;
203
        cd->sheets[i].techoffset=0;
204
        p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
205
        if (cd->sheets[i].techcnt == 0) continue;
206
        cd->sheets[i].techoffset=offset;
207
        for(l=0; l <= cd->sheets[i].techcnt;l++,p1=q2) {
208
          p1=find_word_start(p1);
209
          q2=find_word_end(p1); if(*q2) *q2++=0;
210
          strcpy(cd->techs+offset,p1);
211
          offset+=strlen(p1); offset++;
212
        }
15847 bpr 213
      }
214
    }
215
    wlogdaccessfile(buf,"r","sheets/.active");
216
    p1=strchr (buf,':');
16391 bpr 217
    if (p1!=NULL) {
218
      for (i=k=0,p1++; *p1; i++,p1=p2) {
219
        p2=strchr(p1,':'); if(p2) *p2++=0; else p2=p1+strlen(p1);
220
        for (; *p1; p1=p3) {
221
          p3=strchr(p1,'\n'); if(p3) *p3++=0; else p3=p1+strlen(p1);
222
          for(q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS;q1=find_word_start(q2)) {
223
            q2=find_word_end(q1); if(*q2) *q2++=0;
224
            cd->exos[k].active=strcmp(q1,"0");
225
            k++;
226
          }
15847 bpr 227
        }
228
      }
229
    }
12474 bpr 230
  }
15847 bpr 231
  //class_dump(cd);
12474 bpr 232
  if(stat("exams/.exams",&st)==0) cd->modif=st.st_mtime; else return cd;
233
  wlogdaccessfile(buf,"r","exams/.exams");
234
  if(buf[0]==0) return cd;
235
  if(buf[0]==':') p1=buf-1; else p1=strstr(buf,"\n:");
16704 guerimand 236
  for(n=m=0,k=cd->exam.indstart; p1 && k<MAX_CLASSEXOS && m<MAX_EXAMS; p1=p2,m++,k++) {
12474 bpr 237
    p1+=2;
238
    p2=strstr(p1,"\n:"); if(p2) *p2=0;
239
    //      if(*find_word_start(p1)<'1') continue;      /* status */
240
    fnd_line(p1,3,buf2); if(buf2[0]==0) continue;
241
    q1=find_word_start(buf2); q2=find_word_end(q1);
242
    if(*q2) *q2++=0;
243
    q2=find_word_start(q2); *find_word_end(q2)=0;
244
    i=atoi(q1); j=atoi(q2); if(i<=0 || j<=0) continue;
245
    cd->exos[k].weight=i; cd->exos[k].require=j;      /* weight: duration. require: retries */
246
    fnd_line(p1,6,buf2); q1=find_word_start(buf2);
247
    singlespace(q1); strip_trailing_spaces(q1);
15847 bpr 248
    cd->ctptr[m]=n; cd->ctbuf[n]=0;
16704 guerimand 249
    if(n+strlen(q1)>CTBUFLEN-MAX_EXAMS-16) *q1=0;      /* silent truncation */
12474 bpr 250
    l=strlen(q1)+1; memmove(cd->ctbuf+n,q1,l); n+=l;
251
  }
15847 bpr 252
  cd->examcnt=m; // number of exos in all exams
253
  cd->exocnt+=m; // number of all exos without multiplicity
254
  cd->exam.exocnt=m;
255
  cd->sheets[cd->sheetcnt]=cd->exam;
12474 bpr 256
  return cd;
10 reyssat 257
}