root/trunk/id2i.c

Revision 389, 6.4 kB (checked in by heitor.barbieri, 3 weeks ago)

essage first commit

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
149    if (strncmp(dbnoutp,"create=",7) == 0) recisis0((dbnoutp+=7));
150
151    /*
152        open output and load output control record
153    */
154    recallok(irec=nrecs,rec_maxmfrl);
155    RECORD(irec,dbnoutp,1L);
156    recallok(crec=nrecs,BUFSIZ);
157    RECORD(crec,dbnoutp,0L);
158
159    /*
160        allocate input line
161    */
162    linenbytes=rec_maxmfrl;
163    line=(UBYTE *)ALLOC((ALLOPARM)linenbytes); /* AOT, 19/09/2001 */
164    if (line == (UBYTE *)ALLONULL) fatal("Not enough memory for line");
165
166    /*
167        allocate buffup
168    */
169    nbytes=2*rec_maxmfrl;
170    if (nbytes > ALLOMAXV) nbytes=ALLOMAXV;
171    buffup=(UBYTE *)ALLOC((ALLOPARM)/*ALLOMAXV*/nbytes); /* AOT, 05/04/2001 */
172    if (buffup == (UBYTE *)ALLONULL) fatal("Not enough memory for buffup");
173
174    /*
175        get 1st real input line in advance
176    */
177    linep=fgets(line,linenbytes-1,fpinp);             
178    while (linep)
179        if (line[0] == '\n') linep=fgets(line,linenbytes-1,fpinp);
180        else break;
181    line[strlen(line)-1]='\0';
182
183    /*
184        main loop
185    */
186    for (recp=vrecp[irec], mfn=parmfrom; linep; mfn+=parmloop) {
187
188        if (parmtell)
189            if (!(count1 % parmtell))
190                if (count1)
191                    fprintf(stderr,"+++ %"_LD_"/%"_LD_" recs\n",count1,count2);
192
193        if (parmto)
194            if (mfn > parmto)
195                break;
196
197        if (parmcount)
198            if (count2 >= parmcount)
199                break;
200
201        /*
202            =RECORD(irec,filinp,mfn);
203        */
204        count1++;
205        if (sscanf(line,"!ID %"_LD_"\n",&idmfn) != 1) fatal(line);
206        if (idmfn < 0 || idmfn > 9999999L) fatal(line);
207        if (parmtrace)
208            fprintf(stderr,"+++ %7"_LD_"  ID %7"_LD_"\n",count1,idmfn);
209        sprintf(buffup,"D*");
210        if ((p=fldupdat(irec,buffup)) != NULL) fatal(p);
211        if (parm1000) { sprintf(buffup,"D*A%"_LD_"/%07"_LD_"/",parm1000,idmfn);
212                        if ((p=fldupdat(irec,buffup)) != NULL) fatal(p); }
213
214        batchp=buffup; *buffup='\0';
215
216        linep=fgets(line,linenbytes-1,fpinp);
217        if (!linep) fatal("!v999! expected");
218        if (line[0] == '\n') {
219            while (linep)
220                if (line[0] == '\n') linep=fgets(line,linenbytes-1,fpinp);
221                else break;
222            continue;
223        }
224        line[strlen(line)-1]='\0';
225
226NEWTAG:
227        if (sscanf(line,"!v%"_LD_"!",&idtag) != 1) fatal(line);
228        for (n=2; ; n++) if (!isdigit(line[n])) break;
229        if (idtag < 0 || idtag > USHRT_MAX) fatal(line);
230        sprintf(batchp,"A%"_LD_"%c",idtag,0x01); batchp+=strlen(batchp);
231        strcpy(batchp,line+n+1); batchp+=strlen(batchp);
232
233        while (linep) {
234            linep=fgets(line,linenbytes-1,fpinp);
235            if (line[0] == '\n') linep=fgets(line,linenbytes-1,fpinp);
236            line[strlen(line)-1]='\0';
237            if (!linep) {
238                *batchp++=0x01;
239                break;
240            }
241            if (sscanf(line,"!ID %7"_LD_"\n",&dummy) == 1) {
242                *batchp++=0x01;
243                break;
244            }
245            if (sscanf(line,"!v%"_LD_"!",&idtag) == 1) {
246                *batchp++=0x01;
247                goto NEWTAG;
248            }
249
250            strcpy(batchp,line); batchp+=strlen(batchp);
251        }
252
253        /*
254            output the current master file record
255        */
256        if (*buffup) {
257            *batchp='\0';
258
259            if ((p=fldupdat(irec,buffup)) != NULL) fatal(p);
260            MFRmfn = (idmfn) ? idmfn: mfn+parmoffset;
261
262            if (append) MFRmfn=VMF0nxtmfn(crec);
263            MFRstatus=ACTIVE; /* good */
264           
265            if (recupdat(crec,irec)) fatal("recupdat");
266            count2++;
267        }
268
269    } /* end of main loop */
270
271
272    /* libera areas */
273    FREE(buffup);
274    FREE(line);
275    FREE(vrecp[crec]); vrecp[crec]=NULL; nrecs--;
276    FREE(vrecp[irec]); vrecp[irec]=NULL; nrecs--;
277    fclose(fpinp);
278
279    if (parmtell) fprintf(stderr,"+++ %"_LD_"/%"_LD_" recs\n",count1,count2);
280
281
282    exit(0);
283}
Note: See TracBrowser for help on using the browser.