Subversion Repositories wimsdev

Rev

Rev 12474 | Rev 16081 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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