creation of /usr/local/man/man1 dir added
[metatemplate.git] / ffilter.c
1 /*
2  *   ffilter.c
3  *   metatemplate Vers. vERSIOn
4  *                    Copyright (C)  cOPYDATe  cOPYRIGHt
5  *
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.
10  *
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.
15  *
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
19  *
20  *     CONTACT:    cONTACt
21  *                 cONTAC2t
22  *
23  */
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include "ffilter.h"
30
31 static FIFILE **zzh = NULL;
32 static int nzzh = 0;
33
34 FIFILE *ffopen(char *nome,char **keys)
35 {
36   FIFILE *ff;
37   int i;
38
39   for (i = 0 ; i<nzzh ; i++) {
40     if (zzh[i] == NULL)
41       break;
42   }
43   if (i == nzzh) {
44     nzzh++;
45     if ((zzh = (FIFILE **)realloc(zzh,sizeof(FIFILE *)*nzzh)) == NULL)
46       exit(100);
47     zzh[i] = NULL;
48   }
49   if ((ff = (FIFILE *)malloc(sizeof(FIFILE))) == NULL)
50     exit(101);
51   if ((ff->bf[0] = (char *)malloc(FFDIMBUF+1)) == NULL) {
52     free(ff);    
53     exit(102);
54   }
55   /* PIPPO */
56   // fprintf(stderr,"NOME %s",nome);
57   if ((ff->fp[0] = fopen(nome,"r")) == NULL) {
58     free(ff->bf[0]);
59     free(ff);    
60     exit(103);
61   }
62   ff->nc[0] = 0;
63   ff->nfile = 1;
64   ff->keys = keys;
65   ff->index = i;
66   ff->fkey = -1;
67   ff->noctrlm = 1;
68   // fprintf(stderr,"\nZZH [%d]\n",i);
69   zzh[i] = ff;
70
71   return (ff);
72 }
73
74 int ffinclude(FIFILE *ff,char *nome)
75 {
76
77
78   if (ff == NULL || ff->nfile == FFNESTED)
79     return (0);
80
81   if ((ff->bf[ff->nfile] = (char *)malloc(FFDIMBUF+1)) == NULL)
82     return(0);
83   if ((ff->fp[ff->nfile] = fopen(nome,"r")) == NULL) {
84     free(ff->bf[ff->nfile]);
85     return(0);
86   }
87   ff->nc[ff->nfile] = 0;
88   ff->nfile++;
89
90   return (1);
91 }
92
93
94 int ffclose(FIFILE *ff) {
95   int i;
96
97   if (ff == NULL || zzh[ff->index] == NULL)
98     return (0);
99   
100   zzh[ff->index] = NULL;
101   for (i = 0 ; i < ff->nfile ; i++) {
102     fclose(ff->fp[i]);
103     free(ff->bf[i]);
104   }
105   free(ff);    
106
107   return (1);
108 }
109
110 void ffchankeys(FIFILE *ff,char **keys)
111 {
112   ff->keys = keys;
113 }
114
115 /*
116  *  legge file finche' non vengono trovate chiavi
117  */
118
119 int ffgetbin(char *bf,int size,FIFILE *ff)
120 {
121   FILE *fp;
122   char *p, *ffbf;
123   int i, e, n, nf, minpos = FFDIMBUF,skip = 0,limin;
124
125   /* indice del file corrente */
126   nf = ff->nfile-1;
127   /* file pointer del file corrente */
128   fp = ff->fp[nf];
129   /* buffer del file corrente */
130   ffbf = ff->bf[nf];
131   /* dimensione richiesta decurtata dello spazio per il \0 */
132   size--;
133   /* default della chiave trovata */
134   ff->fkey = -1;
135
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);
143
144     /* se no control M cancella gli
145        eventuali ^M letti ricompattando il buffer */
146     if (ff->noctrlm) {
147       for (i = 0 ; i < n ; i++) {
148         if (p[i] == '\r') {
149           for (e = i ; e < n-1 ; e++)
150             p[e] = p[e+1];
151           n--;
152           i--;
153         }
154       }
155     }  
156     /* incrementa il contatore dei caratteri memorizzati nel buffer */
157     ff->nc[nf] += n;
158     /* aggiunge uno \0 in coda al buffer (per stampabilita')*/
159     ffbf[ff->nc[nf]] = '\0';
160   }
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
165      valore */
166   if (size > limin)
167     size = limin;
168   
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) {
173         minpos = p - ffbf;
174         ff->fkey = i;
175         skip = strlen(ff->keys[i]);
176       }
177     }
178   }
179   /* se la prima chiave trovata e' oltre in numero di car richiesti 
180      non segnala il fatto che e' stata trovata */
181   if (size < minpos) {
182     ff->fkey = -1;
183     skip = 0;
184   }
185   else 
186     size = minpos;
187   /* copia nel buffer i byte richiesti o cio' che precede la chiave */
188   memcpy(bf,ffbf,size);
189   bf[size] = '\0';
190   /* trasla il buffer del numero di byte letti + event. lo skip dato
191      dalla chiave */
192   memmove(ffbf, &(ffbf[size+skip]),FFDIMBUF-(size+skip));
193   /* allinea il numero di byte nel buffer */
194   ff->nc[nf] -= (size+skip);
195   
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 */
201     if (ff->nfile > 1) {
202       ff->nfile--;
203       fclose(ff->fp[ff->nfile]);
204       free(ff->bf[ff->nfile]);
205     }
206     else
207       size = -1;
208   }
209   return (size);
210 }
211
212 int ffkstat(FIFILE *ff) {
213   return (ff->fkey);
214 }
215
216 char **ffgetkeys(FIFILE *ff) {
217   return (ff->keys);
218 }
219