root/tags/5.4.pre05/id2i.c

Revision 1, 6.4 kB (checked in by heitor.barbieri, 4 years ago)

Criação do svn para Cisis.

Line 
1/* ------------------------------ id2i.c ----------------------------- */
2
3#include <stdio.h>
4#include <string.h>
5#include <limits.h>
6#include <ctype.h>
7
8#include "cisis.h" /* CISIS Interface */
9#include "cirun.h" /* runtime area and defines by AOT */
10
11/* Global */
12#if BEFORE20011909
13UBYTE line[32768/*MAXMFRL*/],*buffup;
14#endif
15
16void main(argc,argv)
17int argc;
18char *argv[];
19{
20UBYTE *line,*buffup;
21
22char *filinp,*dbnoutp,*batchp;
23FILE *fpinp;    /* input file pointer */
24
25LONGX count1=0;
26LONGX count2=0;
27
28LONGX idmfn;
29LONGX idtag;
30
31    RECSTRU *recp;      /* mandatory for defines REC RDB MF0 MFR DIR, FIELDP */
32
33    LONGX irec;         /* current master file record */
34    LONGX crec;         /* output control record */
35
36    int argnext,parmtrace,n,append=0;
37    LONGX parmfrom,parmto,parmloop,parmcount,parm1000=0L;
38    LONGX parmoffset,parmtell;
39
40    LONGX mfn,dummy,nbytes,linenbytes;
41    UBYTE *p,*linep;
42
43    if (argc < 3) {
44        printf("%s",cicopyr("Utility ID2I"));
45        printf("\n");
46        printf("id2i <filein> [create[/app]=]<dbout> [option [option] ... ] \n");
47        printf("\n");
48        printf("options: {from/to/loop/count/offset/tell/id}=<n> \n");
49        printf("\n");
50        printf("\n");
51        printf("The logical record of <filin> has the format:\n");
52        printf("\n");
53        printf("!ID mmmmmm \n");
54        printf("!vXXX!...contents of tag XXX............. \n");
55        printf("!vYYY!...contents of tag YYY............. \n");
56        printf("... \n");
57        printf("\n");
58        printf("where mmmmmm is the MFN to be loaded \n");
59        printf("\n");
60        printf("The contents of a tag may be split in 2 or more lines \n");
61        /* printf("each of them up to %d characters LONGX \n",sizeof(line)-1-1); */
62        printf("\n");
63        exit(1);
64    }
65
66
67    argnext=1;
68
69    filinp=argv[argnext++];
70    dbnoutp=argv[argnext++];
71
72    parmtrace=0;
73    parmfrom=1; parmto=0; parmloop=1; parmcount=LONGX_MAX;
74    parmoffset=0; parmtell=0;
75
76    /*
77        get optional parameters
78    */
79    for ( ; argnext < argc; argnext++) {
80        p=argv[argnext];
81
82        if (strncmp(p,"from=",5) == 0) {
83            if (sscanf(p+5,"%ld",&parmfrom) != 1)
84                fatal(p);
85            continue;
86        }
87        if (strncmp(p,"to=",3) == 0) {
88            if (sscanf(p+3,"%ld",&parmto) != 1)
89                fatal(p);
90            continue;
91        }
92        if (strncmp(p,"count=",6) == 0) {
93            if (sscanf(p+6,"%ld",&parmcount) != 1)
94                fatal(p);
95            continue;
96        }
97        if (strncmp(p,"loop=",5) == 0) {
98            if (sscanf(p+5,"%ld",&parmloop) != 1)
99                fatal(p);
100            continue;
101        }
102        if (strncmp(p,"offset=",7) == 0) {
103            if (sscanf(p+7,"%ld",&parmoffset) != 1)
104                fatal(p);
105            continue;
106        }
107
108        if (strncmp(p,"tell=",5) == 0) {
109            if (sscanf(p+5,"%ld",&parmtell) != 1)
110                fatal(p);
111            continue;
112        }
113
114        if (strncmp(p,"id=",3) == 0) {
115            if (sscanf(p+3,"%ld",&parm1000) != 1)
116                fatal(p);
117            continue;
118        }
119
120        if (strcmp(p,"trace") == 0) {
121            parmtrace=1;
122            continue;
123        }
124
125        fatal(p);
126    }
127
128    if (parmtrace) {
129        printf("+++ input data base:  %s\n",filinp);
130        printf("+++ output data base: %s\n",dbnoutp);
131    }
132
133
134    /*
135        setup input file
136    */
137    fpinp=fopen(dbxcipar(NULL,filinp,'='),"r");
138    if (fpinp == NULL) fatal(filinp);
139
140    if (parmtrace && !parmtell) {
141        parmtell=1;
142    }
143
144    /*
145        setup output master file
146    */
147    if (strncmp(dbnoutp,"create/app=",11) == 0) {recisis0((dbnoutp+=11)); append=1;}
148    if (strncmp(dbnoutp,"create=",7) == 0) recisis0((dbnoutp+=7));
149
150    /*
151        open output and load output control record
152    */
153    recallok(irec=nrecs,rec_maxmfrl);
154    RECORD(irec,dbnoutp,1L);
155    recallok(crec=nrecs,BUFSIZ);
156    RECORD(crec,dbnoutp,0L);
157
158    /*
159        allocate input line
160    */
161    linenbytes=rec_maxmfrl;
162    line=(UBYTE *)ALLOC((ALLOPARM)linenbytes); /* AOT, 19/09/2001 */
163    if (line == (UBYTE *)ALLONULL) fatal("Not enough memory for line");
164
165    /*
166        allocate buffup
167    */
168    nbytes=2*rec_maxmfrl;
169    if (nbytes > ALLOMAXV) nbytes=ALLOMAXV;
170    buffup=(UBYTE *)ALLOC((ALLOPARM)/*ALLOMAXV*/nbytes); /* AOT, 05/04/2001 */
171    if (buffup == (UBYTE *)ALLONULL) fatal("Not enough memory for buffup");
172
173    /*
174        get 1st real input line in advance
175    */
176    linep=fgets(line,linenbytes-1,fpinp);             
177    while (linep)
178        if (line[0] == '\n') linep=fgets(line,linenbytes-1,fpinp);
179        else break;
180    line[strlen(line)-1]='\0';
181
182    /*
183        main loop
184    */
185    for (recp=vrecp[irec], mfn=parmfrom; linep; mfn+=parmloop) {
186
187        if (parmtell)
188            if (!(count1 % parmtell))
189                if (count1)
190                    fprintf(stderr,"+++ %ld/%ld recs\n",count1,count2);
191
192        if (parmto)
193            if (mfn > parmto)
194                break;
195
196        if (parmcount)
197            if (count2 >= parmcount)
198                break;
199
200        /*
201            =RECORD(irec,filinp,mfn);
202        */
203        count1++;
204        if (sscanf(line,"!ID %ld\n",&idmfn) != 1) fatal(line);
205        if (idmfn < 0 || idmfn > 9999999L) fatal(line);
206        if (parmtrace)
207            fprintf(stderr,"+++ %7ld  ID %7ld\n",count1,idmfn);
208        sprintf(buffup,"D*");
209        if ((p=fldupdat(irec,buffup)) != NULL) fatal(p);
210        if (parm1000) { sprintf(buffup,"D*A%ld/%07ld/",parm1000,idmfn);
211                        if ((p=fldupdat(irec,buffup)) != NULL) fatal(p); }
212
213        batchp=buffup; *buffup='\0';
214
215        linep=fgets(line,linenbytes-1,fpinp);
216        if (!linep) fatal("!v999! expected");
217        if (line[0] == '\n') {
218            while (linep)
219                if (line[0] == '\n') linep=fgets(line,linenbytes-1,fpinp);
220                else break;
221            continue;
222        }
223        line[strlen(line)-1]='\0';
224NEWTAG:
225        if (sscanf(line,"!v%ld!",&idtag) != 1) fatal(line);
226        for (n=2; ; n++) if (!isdigit(line[n])) break;
227        if (idtag < 0 || idtag > USHRT_MAX) fatal(line);
228        sprintf(batchp,"A%ld%c",idtag,0x01); batchp+=strlen(batchp);
229        strcpy(batchp,line+n+1); batchp+=strlen(batchp);
230
231        while (linep) {
232            linep=fgets(line,linenbytes-1,fpinp);
233            if (line[0] == '\n') linep=fgets(line,linenbytes-1,fpinp);
234            line[strlen(line)-1]='\0';
235            if (!linep) {
236                *batchp++=0x01;
237                break;
238            }
239
240            if (sscanf(line,"!ID %7ld\n",&dummy) == 1) {
241                *batchp++=0x01;
242                break;
243            }
244            if (sscanf(line,"!v%ld!",&idtag) == 1) {
245                *batchp++=0x01;
246                goto NEWTAG;
247            }
248
249            strcpy(batchp,line); batchp+=strlen(batchp);
250        }
251
252        /*
253            output the current master file record
254        */
255        if (*buffup) {
256            *batchp='\0';
257            if ((p=fldupdat(irec,buffup)) != NULL) fatal(p);
258            MFRmfn = (idmfn) ? idmfn: mfn+parmoffset;
259            if (append) MFRmfn=VMF0nxtmfn(crec);
260            MFRstatus=ACTIVE; /* good */
261            if (recupdat(crec,irec)) fatal("recupdat");
262            count2++;
263        }
264
265    } /* end of main loop */
266
267
268    /* libera areas */
269    FREE(buffup);
270    FREE(line);
271    FREE(vrecp[crec]); vrecp[crec]=NULL; nrecs--;
272    FREE(vrecp[irec]); vrecp[irec]=NULL; nrecs--;
273    fclose(fpinp);
274
275    if (parmtell) fprintf(stderr,"+++ %ld/%ld recs\n",count1,count2);
276
277    exit(0);
278}
Note: See TracBrowser for help on using the browser.