3 * metatemplate Vers. vERSIOn
4 * Copyright (C) cOPYDATe cOPYRIGHt
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
31 static FIFILE **zzh = NULL;
34 FIFILE *ffopen(char *nome,char **keys)
39 for (i = 0 ; i<nzzh ; i++) {
45 if ((zzh = (FIFILE **)realloc(zzh,sizeof(FIFILE *)*nzzh)) == NULL)
49 if ((ff = (FIFILE *)malloc(sizeof(FIFILE))) == NULL)
51 if ((ff->bf[0] = (char *)malloc(FFDIMBUF+1)) == NULL) {
56 // fprintf(stderr,"NOME %s",nome);
57 if ((ff->fp[0] = fopen(nome,"r")) == NULL) {
68 // fprintf(stderr,"\nZZH [%d]\n",i);
74 int ffinclude(FIFILE *ff,char *nome)
78 if (ff == NULL || ff->nfile == FFNESTED)
81 if ((ff->bf[ff->nfile] = (char *)malloc(FFDIMBUF+1)) == NULL)
83 if ((ff->fp[ff->nfile] = fopen(nome,"r")) == NULL) {
84 free(ff->bf[ff->nfile]);
87 ff->nc[ff->nfile] = 0;
94 int ffclose(FIFILE *ff) {
97 if (ff == NULL || zzh[ff->index] == NULL)
100 zzh[ff->index] = NULL;
101 for (i = 0 ; i < ff->nfile ; i++) {
110 void ffchankeys(FIFILE *ff,char **keys)
116 * legge file finche' non vengono trovate chiavi
119 int ffgetbin(char *bf,int size,FIFILE *ff)
123 int i, e, n, nf, minpos = FFDIMBUF,skip = 0,limin;
125 /* indice del file corrente */
127 /* file pointer del file corrente */
129 /* buffer del file corrente */
131 /* dimensione richiesta decurtata dello spazio per il \0 */
133 /* default della chiave trovata */
136 /* se il numero di caratteri attualmente nel buffer e' attualmente minore
137 della dim totale del buffer e il file non e' finito */
138 while (ff->nc[nf] < FFDIMBUF && !feof(fp)) {
139 /* pointer al primo char libero del buffer */
140 p = &(ffbf[ff->nc[nf]]);
141 /* tenta di riempire il buffer */
142 n = fread(p,1,FFDIMBUF-ff->nc[nf],fp);
144 /* se no control M cancella gli
145 eventuali ^M letti ricompattando il buffer */
147 for (i = 0 ; i < n ; i++) {
149 for (e = i ; e < n-1 ; e++)
156 /* incrementa il contatore dei caratteri memorizzati nel buffer */
158 /* aggiunge uno \0 in coda al buffer (per stampabilita')*/
159 ffbf[ff->nc[nf]] = '\0';
161 /* limin viene settato al minimo tra il numero di caratteri letti e la
162 meta della lunghezza del buffer */
163 limin = (ff->nc[nf] < FFMEZBUF ? ff->nc[nf] : FFMEZBUF);
164 /* se la dimensione richiesta e' maggiore di limin viene abbassata a tale
169 /* cerca una delle chiavi */
170 for (i = 0 ; ff->keys[i] ; i++) {
171 if ((p = strstr(ffbf,ff->keys[i])) != NULL) {
172 if ((p - ffbf) < minpos) {
175 skip = strlen(ff->keys[i]);
179 /* se la prima chiave trovata e' oltre in numero di car richiesti
180 non segnala il fatto che e' stata trovata */
187 /* copia nel buffer i byte richiesti o cio' che precede la chiave */
188 memcpy(bf,ffbf,size);
190 /* trasla il buffer del numero di byte letti + event. lo skip dato
192 memmove(ffbf, &(ffbf[size+skip]),FFDIMBUF-(size+skip));
193 /* allinea il numero di byte nel buffer */
194 ff->nc[nf] -= (size+skip);
196 /* se sonos stati letti 0 byte e non ci sono chiavi intercettate */
197 if (size == 0 && ff->fkey == -1) {
198 /* se il numero di file nello stack e' maggiore di 1 allora
199 chiude il file corrente e passa quello precedente altrimenti
200 rende -1 per segnalare la fine della poss. di leggere dati */
203 fclose(ff->fp[ff->nfile]);
204 free(ff->bf[ff->nfile]);
212 int ffkstat(FIFILE *ff) {
216 char **ffgetkeys(FIFILE *ff) {