Rev 7363 | Rev 8185 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 7363 | Rev 8155 | ||
---|---|---|---|
Line 13... | Line 13... | ||
13 | * You should have received a copy of the GNU General Public License |
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 |
14 | * along with this program; if not, write to the Free Software |
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
15 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
16 | */ |
16 | */ |
17 | 17 | ||
18 |
|
18 | /* Caches and its management */ |
19 | 19 | ||
20 |
|
20 | /* General information of an exercise. Size: 10 bytes. */ |
21 | typedef struct exodata { |
21 | typedef struct exodata { |
22 | unsigned short int num; |
22 | unsigned short int num; |
23 | float weight, require; |
23 | float weight, require; |
24 | } exodata; |
24 | } exodata; |
25 | 25 | ||
Line 47... | Line 47... | ||
47 | struct sheetcache { |
47 | struct sheetcache { |
48 | struct sheetdata *ptr; |
48 | struct sheetdata *ptr; |
49 | } sheetcache[MAX_SHEETCACHE]; |
49 | } sheetcache[MAX_SHEETCACHE]; |
50 | int sheetcaches; |
50 | int sheetcaches; |
51 | 51 | ||
52 |
|
52 | /* searches a list. Returns index if found, -1 if nomatch. |
53 | |
53 | * Uses binary search, list must be sorted. |
- | 54 | */ |
|
54 | int search_data(void *list, int items, size_t item_size, unsigned short int t) |
55 | int search_data(void *list, int items, size_t item_size, unsigned short int t) |
55 | { |
56 | { |
56 | int i1,i2,j,k; |
57 | int i1,i2,j,k; |
57 | unsigned short int *p; |
58 | unsigned short int *p; |
58 | 59 | ||
59 | if(items<=0) return -1; |
60 | if(items<=0) return -1; |
60 | j=0; p=list; k=*p-t; |
61 | j=0; p=list; k=*p-t; |
61 | if(k==0) return k; if(k>0) return -1; |
62 | if(k==0) return k; if(k>0) return -1; |
62 | p=list+(items-1)*item_size; |
63 | p=list+(items-1)*item_size; |
63 | k=*p-t; if(k==0) return items-1; if(k<0) return ~items; |
64 | k=*p-t; if(k==0) return items-1; if(k<0) return ~items; |
64 | for(i1=0,i2=items-1;i2>i1+1;) { |
65 | for(i1=0,i2=items-1;i2>i1+1;) { |
65 | j=(i2+i1)/2; |
66 | j=(i2+i1)/2; |
66 | p=list+(j*item_size); |
67 | p=list+(j*item_size); |
67 | k=*p-t; |
68 | k=*p-t; |
68 | if(k==0) return j; |
69 | if(k==0) return j; |
69 | if(k>0) {i2=j; continue;} |
70 | if(k>0) {i2=j; continue;} |
70 | if(k<0) {i1=j; continue;} |
71 | if(k<0) {i1=j; continue;} |
71 | } |
72 | } |
72 | return ~i2; |
73 | return ~i2; |
73 | } |
74 | } |
74 | 75 | ||
75 |
|
76 | /* remove old cache items */ |
76 | void cleancache(void) |
77 | void cleancache(void) |
77 | { |
78 | { |
78 | int i; |
79 | int i; |
79 | time_t now; |
80 | time_t now; |
80 | struct classdata *cd; |
81 | struct classdata *cd; |
Line 94... | Line 95... | ||
94 | memmove(sheetcache+i,sheetcache+i+1,(sheetcaches-i-1)*sizeof(sheetcache[0])); |
95 | memmove(sheetcache+i,sheetcache+i+1,(sheetcaches-i-1)*sizeof(sheetcache[0])); |
95 | sheetcaches--; |
96 | sheetcaches--; |
96 | } |
97 | } |
97 | } |
98 | } |
98 | 99 | ||
99 |
|
100 | /* Locate the cache number of a class */ |
100 | struct classdata *getclasscache(char *cl) |
101 | struct classdata *getclasscache(char *cl) |
101 | { |
102 | { |
102 | int i,j,k,l,m,n, oldest; |
103 | int i,j,k,l,m,n, oldest; |
103 | struct stat st; |
104 | struct stat st; |
104 | struct classdata *cd; |
105 | struct classdata *cd; |
Line 124... | Line 125... | ||
124 | if(i>classcaches) return NULL; |
125 | if(i>classcaches) return NULL; |
125 | cd=classdata+i; cd->access=1; |
126 | cd=classdata+i; cd->access=1; |
126 | classcache[classcaches++].ptr=cd; |
127 | classcache[classcaches++].ptr=cd; |
127 | snprintf(cd->name,sizeof(cd->name),"%s",cl); |
128 | snprintf(cd->name,sizeof(cd->name),"%s",cl); |
128 | cd->start=time(NULL); cd->exocnt=0; |
129 | cd->start=time(NULL); cd->exocnt=0; |
129 |
|
130 | /* Now get the exo data */ |
130 | accessfile(buf,"r","sheets/.require"); |
131 | accessfile(buf,"r","sheets/.require"); |
131 | for(i=k=0,p1=buf; *p1; i++,p1=p2) { |
132 | for(i=k=0,p1=buf; *p1; i++,p1=p2) { |
132 | p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1); |
133 | p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1); |
133 | for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) { |
134 | for(j=0,q1=find_word_start(p1); *q1 && k<MAX_CLASSEXOS; j++,q1=find_word_start(q2)) { |
134 | q2=find_word_end(q1); if(*q2) *q2++=0; |
135 | q2=find_word_end(q1); if(*q2) *q2++=0; |
135 | cd->exos[k].num=(i<<8)+j;cd->exos[k].require=atof(q1); |
136 | cd->exos[k].num=(i<<8)+j;cd->exos[k].require=atof(q1); |
136 | cd->exos[k].weight=0; |
137 | cd->exos[k].weight=0; |
137 | k++; |
138 | k++; |
138 | } |
139 | } |
139 | 140 | ||
140 | } |
141 | } |
141 | if(k>=MAX_CLASSEXOS) return NULL; |
142 | if(k>=MAX_CLASSEXOS) return NULL; |
142 | cd->exocnt=k; cd->examstart=k; cd->modif=0; |
143 | cd->exocnt=k; cd->examstart=k; cd->modif=0; |
143 | accessfile(buf,"r","sheets/.weight"); |
144 | accessfile(buf,"r","sheets/.weight"); |
144 | for(i=k=0,p1=buf; *p1; i++,p1=p2) { |
145 | for(i=k=0,p1=buf; *p1; i++,p1=p2) { |
Line 176... | Line 177... | ||
176 | } |
177 | } |
177 | cd->examcnt=k-cd->exocnt; cd->exocnt=k; |
178 | cd->examcnt=k-cd->exocnt; cd->exocnt=k; |
178 | return cd; |
179 | return cd; |
179 | } |
180 | } |
180 | // misprint ?? sheetdata ?? does not seem to be used. |
181 | // misprint ?? sheetdata ?? does not seem to be used. |
181 |
|
182 | /* prepare cache for a sheet */ |
182 | struct sheetata *getsheetcache(char *cl, char *sh) |
183 | struct sheetata *getsheetcache(char *cl, char *sh) |
183 | { |
184 | { |
184 | 185 | ||
185 | 186 | ||
186 | 187 | ||
187 | return NULL; |
188 | return NULL; |
188 | } |
189 | } |
189 | 190 |