Subversion Repositories wimsdev

Rev

Rev 12474 | Rev 16081 | 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. /* remove old cache items */
  30. void cleancache(void)
  31. {
  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.   }
  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");
  89. }
  90.  
  91. /* Locate the cache number of a class */
  92. struct classdata *getclasscache(char *cl)
  93. {
  94.   int i,j,k,l,m,n, oldest, offset;
  95.   struct stat st;
  96.   struct classdata *cd;
  97.   char buf[MAX_LINELEN+1], buf2[MAX_LINELEN+1];
  98.   char *p1, *p2, *p3, *q1, *q2;
  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;
  107.     }
  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);
  120.   cd->techs[0]=0; offset=1;
  121.   cd->start=time(NULL); cd->exocnt=0;
  122.   /* Now get the exo data */
  123.   wlogdaccessfile(buf,"r","sheets/.require");
  124.   p1=strchr (buf,':');
  125.   if (p1==NULL) {
  126.     for(i=k=0,p1=buf; *p1; i++,p1=p2) {
  127.       cd->sheets[i].start=cd->sheets[i].indstart=k;
  128.       p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  129.       for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
  130.         q2=find_word_end(q1); if(*q2) *q2++=0;
  131.         cd->exos[k].require=atof(q1);
  132.         cd->exos[k].weight=0;
  133.         cd->exos[k].active=1;
  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,
  162.     }
  163.     cd->sheetcnt=i; /* number of sheets */
  164.   }
  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;
  169.   cd->exam.start=cd->exocnt;
  170.   cd->exam.exocnt=0;
  171.   cd->modif=0;
  172.   wlogdaccessfile(buf,"r","sheets/.weight");
  173.   p1=strchr (buf,':');
  174.   /*old format*/
  175.   if (p1==NULL)
  176.     for(i=k=0,p1=buf; *p1; i++,p1=p2) {
  177.       p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  178.       for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) {
  179.         q2=find_word_end(q1); if(*q2) *q2++=0;
  180.         cd->exos[k].weight=atof(q1);
  181.         k++;
  182.       }
  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.     }
  225.   }
  226.   //class_dump(cd);
  227.   if(stat("exams/.exams",&st)==0) cd->modif=st.st_mtime; else return cd;
  228.   wlogdaccessfile(buf,"r","exams/.exams");
  229.   if(buf[0]==0) return cd;
  230.   if(buf[0]==':') p1=buf-1; else p1=strstr(buf,"\n:");
  231.   for(n=m=0,k=cd->exam.indstart; p1 && k<MAX_CLASSEXOS && m<MAX_EXOS; p1=p2,m++,k++) {
  232.     p1+=2;
  233.     p2=strstr(p1,"\n:"); if(p2) *p2=0;
  234.     //      if(*find_word_start(p1)<'1') continue;      /* status */
  235.     fnd_line(p1,3,buf2); if(buf2[0]==0) continue;
  236.     q1=find_word_start(buf2); q2=find_word_end(q1);
  237.     if(*q2) *q2++=0;
  238.     q2=find_word_start(q2); *find_word_end(q2)=0;
  239.     i=atoi(q1); j=atoi(q2); if(i<=0 || j<=0) continue;
  240.     cd->exos[k].weight=i; cd->exos[k].require=j;      /* weight: duration. require: retries */
  241.     fnd_line(p1,6,buf2); q1=find_word_start(buf2);
  242.     singlespace(q1); strip_trailing_spaces(q1);
  243.     cd->ctptr[m]=n; cd->ctbuf[n]=0;
  244.     if(n+strlen(q1)>CTBUFLEN-MAX_EXOS-16) *q1=0;      /* silent truncation */
  245.     l=strlen(q1)+1; memmove(cd->ctbuf+n,q1,l); n+=l;
  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;
  250.   cd->sheets[cd->sheetcnt]=cd->exam;
  251.   return cd;
  252. }
  253.