Subversion Repositories wimsdev

Rev

Rev 16391 | 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=0,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.     /*old format*/
  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,
  163.     }
  164.     cd->sheetcnt=i; /* number of sheets */
  165.   }
  166.   if(k>=MAX_CLASSEXOS) return NULL;
  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;
  173.   wlogdaccessfile(buf,"r","sheets/.weight");
  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;
  181.         cd->exos[k].weight=atof(q1);
  182.         k++;
  183.       }
  184.     }
  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,':');
  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.         }
  213.       }
  214.     }
  215.     wlogdaccessfile(buf,"r","sheets/.active");
  216.     p1=strchr (buf,':');
  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.           }
  227.         }
  228.       }
  229.     }
  230.   }
  231.   //class_dump(cd);
  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:");
  236.   for(n=m=0,k=cd->exam.indstart; p1 && k<MAX_CLASSEXOS && m<MAX_EXAMS; p1=p2,m++,k++) {
  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);
  248.     cd->ctptr[m]=n; cd->ctbuf[n]=0;
  249.     if(n+strlen(q1)>CTBUFLEN-MAX_EXAMS-16) *q1=0;      /* silent truncation */
  250.     l=strlen(q1)+1; memmove(cd->ctbuf+n,q1,l); n+=l;
  251.   }
  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;
  256.   return cd;
  257. }
  258.