root/tags/5.52/mxgw.c

Revision 4, 11.7 kB (checked in by heitor.barbieri, 2 years ago)

Versão 5.52 do cisis (28/04/2010)

Line 
1/* ------------------------------ mxgw.c ----------------------------- */
2
3#include <stdio.h>
4#include <string.h>
5#include <limits.h>
6#include <ctype.h>
7
8#if 1
9#include "cisis.h"   /* CISIS Interface header file */
10#include "cirun.h"   /* CISIS Interface runtime declarations */
11#else
12#include "\cisis\include\cisis.h"   /* CISIS Interface header file */
13#include "\cisis\include\cirun.h"   /* CISIS Interface runtime declarations */
14#endif
15
16#define MAXTAG  10000
17
18/* Global */
19UBYTE *buffup,line[CIMPL+1];
20char *dbnamep,*dbnoutp;
21
22LONGX count1=0;
23LONGX count2=0;
24
25#if CISISH
26typedef struct gizmstru {
27    short isize;
28    short osize;
29    unsigned char *ipatt;
30    unsigned char *opatt;
31    LONGX nused;
32    struct gizmstru *nextp;
33} GIZMSTRU;
34
35#define MAXGIZR 8
36typedef struct vgizpstru {
37    GIZMSTRU *ghdrp[256];
38    int grngs;
39    UWORD grng1[MAXGIZR];
40    UWORD grng2[MAXGIZR];
41    unsigned char *gdbnp;
42    struct vgizpstru *nextp;
43} VGIZPSTRU;
44
45typedef struct dbxstru {
46....VGIZPSTRU *dbxvgzrp;          /* gizmo processing */
47} DBXSTRU;
48#endif
49
50/* Prototypes */
51#if ANSI
52LONGX wecgizmo(LONGX irec, VGIZPSTRU *gizmap);
53#else
54LONGX wecgizmo();
55#endif
56
57
58void main(argc,argv)
59int argc;
60char *argv[];
61{
62    RECSTRU *recp;      /* mandatory for defines REC RDB MF0 MFR DIR, FIELDP */
63
64    LONGX irec;          /* current master file record */
65    LONGX upcrec;        /* output control record */
66
67    int argnext,parmtrace;
68    LONGX parmfrom,parmto,parmloop,parmcount,parmtell;
69
70    LONGX mfn;
71    UBYTE *p,*q;
72    UWORD mftype,tag,tag1,tag2;
73    LONGX xtag1,xtag2;
74    VGIZPSTRU *gizmap,*vgizmap = NULL;
75
76    if (argc < 3) {
77        printf("%s",cicopyr("Utility MXGW"));
78        printf("\n");
79        printf("mxgw <dbin> [create=]<dbout> [<option> [...]] \n");
80        printf("\n");
81        printf("options: {from|to|loop|count|tell}=<n> \n");
82        printf("         gizmo=<dbgiz>[,tag_list>] \n");
83        printf("\n");
84        printf("\n");
85        printf("Ex: mxgw in out gizmo=giz\n");
86        printf("\n");
87        printf("     giz = 1 %cagua%c\n",OPENFCHR,CLOSFCHR);
88        printf("           2 %c�gua%c\n",OPENFCHR,CLOSFCHR);
89        printf(" \n");
90        printf("      in = 9 %cagua%c\n",OPENFCHR,CLOSFCHR);
91        printf("           9 %caguardente%c\n",OPENFCHR,CLOSFCHR);
92        printf(" \n");
93        printf("     out = 9 %c�gua%c\n",OPENFCHR,CLOSFCHR);
94        printf("           9 %caguardente%c\n",OPENFCHR,CLOSFCHR);
95        printf("\n");
96        exit(1);
97    }
98
99
100    argnext=1;
101
102    dbnamep=argv[argnext++];
103    dbnoutp=argv[argnext++];
104
105    recp=vrecp[recallok(irec=nrecs,MAXMFRL)];
106    parmtrace=0;
107    parmfrom=1; parmto=0; parmloop=1; parmcount=0; parmtell=0;
108
109    /*
110        get optional parameters
111    */
112    for ( ; argnext < argc; argnext++) {
113        p=argv[argnext];
114
115        if (strncmp(p,"from=",5) == 0) {
116            if (sscanf(p+5,"%ld",&parmfrom) != 1)
117                fatal(p);
118            continue;
119        }
120        if (strncmp(p,"to=",3) == 0) {
121            if (sscanf(p+3,"%ld",&parmto) != 1)
122                fatal(p);
123            continue;
124        }
125        if (strncmp(p,"loop=",5) == 0) {
126            if (sscanf(p+5,"%ld",&parmloop) != 1)
127                fatal(p);
128            continue;
129        }
130        if (strncmp(p,"count?",6) == 0 || strncmp(p,"count=",6) == 0) {
131            if (sscanf(p+6,"%ld",&parmcount) != 1)
132                fatal(p);
133            continue;
134        }
135        if (strncmp(p,"tell?",5) == 0 || strncmp(p,"tell=",5) == 0) {
136            if (sscanf(p+5,"%ld",&parmtell) != 1)
137                fatal(p);
138            continue;
139        }
140
141        if (strncmp(p,"gizmo=",6) == 0) {
142            for (line[0]='\0', q=p+6; *q; q++)
143                if (*q == '=' || *q ==',') {
144                    *q='\0'; strcpy(line,p+6);
145                    if (!*++q) fatal(p);
146                    break;
147                }
148            if (!line[0]) {
149                strcpy(line,p+6);
150                q=p+strlen(p);
151            }
152            if (vgizmap) {
153                for (gizmap=vgizmap; gizmap->nextp; )
154                    gizmap=gizmap->nextp;
155                gizmread(line,&gizmap->nextp,irec);
156                gizmap=gizmap->nextp;                           /* 11/03/92 */
157            }
158            else {
159                gizmread(line,&vgizmap,irec);
160                gizmap=vgizmap;
161            }
162
163            while (*q) {
164                if (gizmap->grngs >= MAXGIZR)
165                    fatal("Too many tags in gizmo tag_list");
166                if (sscanf(q,"%ld/%ld",&xtag1,&xtag2) == 2) {
167                    tag1=(UWORD)xtag1;
168                    tag2=(UWORD)xtag2;
169                    if (tag1 > tag2) fatal(p);
170                    while (*q != '/') q++; q++;
171                    while (isdigit(*q)) q++;
172                    gizmap->grng1[gizmap->grngs]=tag1;
173                    gizmap->grng2[gizmap->grngs]=tag2;
174                    if (*q == ',') if (*(q+1)) q++;
175                    gizmap->grngs++;
176                    continue;
177                }
178                if (sscanf(q,"%ld",&xtag1) == 1) {
179                    tag=(UWORD)xtag1;
180                    while (isdigit(*q)) q++;
181                    if (tag >= MAXTAG) fatal(p);
182                    gizmap->grng1[gizmap->grngs]=tag;
183                    gizmap->grng2[gizmap->grngs]=tag;
184                    if (*q == ',') if (*(q+1)) q++;
185                    gizmap->grngs++;
186                    continue;
187                }
188                fatal(p);
189            }
190            if (parmtrace) {
191                for (gizmap=vgizmap; gizmap; gizmap=gizmap->nextp)
192                    printf("+++ gizmo %s \n",gizmap->gdbnp);
193            }
194            continue;
195        }
196
197        if (strcmp(p,"trace") == 0) {
198            parmtrace=1;
199            continue;
200        }
201
202        fatal(p);
203    }
204
205    if (parmtrace) {
206        printf("+++ input data base:  %s\n",dbnamep);
207        printf("+++ output data base: %s\n",dbnoutp);
208    }
209
210    /*
211        allocate buffup
212    */
213    if ((buffup=(char *)ALLOC((ALLOPARM)(BUFFUPL))) == ALLONULL)
214        fatal("ALLOC/buffup");
215
216    /*
217        setup input data base
218    */
219    RECORD(irec,dbnamep,0L);
220    mftype=MF0mftype;
221    if (parmtrace && !parmtell) {
222        parmtell=(MF0nxtmfn-1)/10;
223        if (parmtell < 1) parmtell=1;
224    }
225
226
227    /*
228        setup output master file
229    */
230    if (strncmp(dbnoutp,"create=",7) == 0)
231        recisis0((dbnoutp+=7));
232
233    /*
234        open output and load output control record
235    */
236    RECORD((upcrec=nrecs),dbnoutp,1L);
237    RECORD(upcrec,dbnoutp,0L);
238    MF0mftype=mftype;
239
240
241    /*
242        main loop
243    */
244    for (mfn=parmfrom; ; mfn+=parmloop) {
245
246        if (parmtell)
247            if (!(count1 % parmtell))
248                if (count1)
249                    fprintf(stderr,"+++ %ld/%ld recs\n",count1,count2);
250
251        if (parmto)
252            if (mfn > parmto)
253                break;
254
255        if (parmcount)
256            if (count2 >= parmcount)
257                break;
258
259        RECORD(irec,dbnamep,mfn);
260
261        if (RECrc == RCEOF)
262            break;
263        count1++;
264
265        if (RECrc != RCNORMAL)
266            continue;
267
268        /*
269            gizmo processing
270        */
271        for (gizmap=vgizmap; gizmap; gizmap=gizmap->nextp)
272            wecgizmo(irec,gizmap);
273
274        /*
275            output the current master file record
276        */
277        if (MFRnvf) {
278            MFRmfn=mfn+0;
279            MFRstatus=ACTIVE; /* good */
280            if (recupdat(upcrec,irec))
281                fatal("recupdat");
282
283            count2++;
284        }
285
286    } /* end of main loop */
287
288    if (parmtrace)
289        if (!(count1 % parmtell))
290            ;
291        else
292            fprintf(stderr,"+++ %ld/%ld recs\n",count1,count2);
293
294    exit(0);
295}
296
297
298/* ----------------------------- giz.c ------------------------------ */
299
300#define TRACEG1         0
301#define TRACEG2         0
302#define TRACEG3         0
303
304#define TRACEGT         00
305#if TRACEGT
306#define GETCHAR printf("\n");
307#else
308#define GETCHAR getchar()
309#endif
310
311LONGX wecgizmo(irec,gizmap)
312LONGX irec;                      /* indice de vrecp, para info/store */
313VGIZPSTRU *gizmap;              /* estrutura gizmo a considerar */
314{
315    RECSTRU *recp;
316
317    GIZMSTRU *gizmhp,*gizmp;
318
319    short match;
320    unsigned char *p,*q;
321    unsigned char *batchp,*fuplenp;
322    LONGX nmatchs;
323    int xdir,dirloop,fldloop,fuplen,irng,rngok,wl;
324    UWORD tag;
325#if TRACEG3
326    unsigned char *xp;
327    int n;
328#endif
329
330
331
332    if (rectrace)
333        printf("wecgizmo - irec=%ld %s=%p\n",irec,gizmap->gdbnp,gizmap);
334
335    if (!nrecs)
336        fatal("wecgizmo/nrecs");
337
338    recp=vrecp[irec];
339    if (!recp)
340        fatal("wecgizmo/irec");
341
342    if (RECtype != TYPEMFR) {
343        printf("*** irec=%ld  RECtype=%d\n",irec,RECtype);
344        fatal("wecgizmo/check");
345    }
346
347
348    nmatchs=0;
349    if (gizmap->ghdrp == NULL) fatal("wecgizmo/ghdrp");
350
351    for (batchp=buffup, xdir=0, dirloop=MFRnvf; dirloop--; xdir++) {
352        tag=DIRtag(xdir); rngok=1;
353#if TRACEG1
354printf("\\begin tag %d..",tag); GETCHAR;
355#endif
356
357        if (gizmap->grngs)
358            for (rngok=0, irng=0; irng < gizmap->grngs; irng++) {
359                if (tag < gizmap->grng1[irng]) continue;
360                if (tag > gizmap->grng2[irng]) continue;
361                rngok=1; break;
362            }
363
364        if (rngok) {
365#if TRACEG1
366printf("\\gizmo tag %d..",tag); GETCHAR;
367#endif
368            sprintf(batchp,"H%u 000000 ",tag); batchp+=strlen(batchp);
369            fuplenp=batchp-1-6; fuplen=0; /* fuplenp points to 000000 */
370#if TRACEG2
371printf("\\buffup=%s",buffup); GETCHAR;
372printf("%3d '",DIRtag(xdir));
373for (p=FIELDP(xdir), fldloop=DIRlen(xdir); fldloop-- > 0; p++) printf("%c",*p);
374printf("'\n");
375#endif
376            for (p=FIELDP(xdir), fldloop=DIRlen(xdir); fldloop > 0; ) {
377#if TRACEG3
378printf("\\fldloop=%d *p=%c",fldloop,*p); GETCHAR;
379#endif
380                if (!isiswctab[*p]) {
381#if TRACEG3
382printf("\\no word - copying %c",*p); GETCHAR;
383#endif
384                    *batchp++ = *p++;
385                    fldloop--; fuplen++;
386                    continue;
387                }
388                for (q=p, wl=1, fldloop--; fldloop > 0; )
389                    if (isiswctab[*++p]) {
390                        wl++;
391                        fldloop--;
392                    } else break;
393#if TRACEG3
394printf("\\word - wl=%d '",wl);
395for (xp=q, n=wl; n--; xp++) printf("%c",*xp);
396printf("'\n"); GETCHAR;
397#endif
398
399                match=0;
400                gizmhp=gizmap->ghdrp[*q];
401                for (gizmp=gizmhp; gizmp; gizmp=gizmp->nextp) {
402#if TRACEG3
403printf("\\need %d chars - ",wl);
404for (xp=gizmp->ipatt, n=gizmp->isize; n--; xp++) printf("%c",*xp); GETCHAR;
405#endif
406                    if (gizmp->isize == wl)
407                        if (memcmp(gizmp->ipatt,q,wl) == 0) {
408                            match=1;
409                            break;
410                        }
411                }
412
413                if (match) {
414#if TRACEG3
415printf("\\match %d -> %d chars",gizmp->isize,gizmp->osize); GETCHAR;
416#endif
417                    memcpy(batchp,gizmp->opatt,gizmp->osize);
418                    batchp+=gizmp->osize;
419                    fuplen+=gizmp->osize;
420                    gizmp->nused++;
421                    nmatchs++;
422                }
423                else {
424#if TRACEG3
425printf("\\no match - copying %c",*p); GETCHAR;
426#endif
427                    memcpy(batchp,q,wl);
428                    batchp+=wl;
429                    fuplen+=wl;
430                }
431
432            } /* end field chrs */
433
434            sprintf(fuplenp,"%06d",fuplen); *(fuplenp+6)=' ';
435
436        }
437
438        else {  /* just copy it */
439            sprintf(batchp,"H%u %u ",tag,DIRlen(xdir)); batchp+=strlen(batchp);
440            memcpy(batchp,FIELDP(xdir),DIRlen(xdir)); batchp+=DIRlen(xdir);
441        }
442
443    } /* end dir entries */
444
445    *batchp=NULL;
446
447    if (nmatchs) {
448
449        MFRmfrl=MFRbase=LEADER; MFRnvf=0;
450
451        p=fldupdat(irec,buffup);
452        if (p) {
453            printf("*** fldupdat='%s'\n",p);
454            fatal("wecgizmo/fldupdat");
455        }
456    }
457
458    if (rectrace)
459        printf("wecgizmo - %s %ld match(s)\n",gizmap->gdbnp,nmatchs);
460
461    return(nmatchs);
462}
463
464
465#undef GETCHAR
Note: See TracBrowser for help on using the browser.