root/trunk/mkxrf.c

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

essage first commit

Line 
1/*
2mkxrf.c         -       Make a .XRF MicroISIS file from its .MST data file
3
4                        Skip EACHSKIP bytes and keep trying to find a valid
5                        MicroISIS master file record leader whenever the one
6                        just read do not pass the consistency criteria.
7
8                        Up to the point the execution ends the .XRF is ok.
9
10                        A.O.Tardelli, BRM/1989
11*/
12
13#include <stdio.h>
14#include <string.h>
15
16#include "cisis.h"      /* CISIS Interface */
17#include "cirun.h"      /* runtime area and defines by AOT */
18
19#define EACHSKIP        2
20#define FIRSTCOMB       1
21#define FIRSTCOMP       64
22
23#define M0BSIZ          sizeof(M0STRU)
24
25#define TRACE1          0
26#define TRACE2          0
27
28char ctlarea[BUFSIZ];
29
30void main(argc,argv)
31int argc;
32char *argv[];
33{
34    RECSTRU *recp; /* mandatory for defines REC RDB MF0 MFR DIR, FIELDP */
35
36    unsigned char *dbnp,line[CIMPL+1];
37    LONGX lastmfn;
38
39    LONGX comb;      /* gdb getmfr comb */
40    int comp;       /* gdb getmfr comp */
41
42    LONGX xrftiv;    /* .xrf pointer */
43
44    int n,fd,nr;
45#if CNV_PCFILES
46    unsigned char *p;
47#endif
48
49    off_t nbytes;
50    LONGX nxtmfb,nxtmfn,ok=0L,totread=0L;
51    int  nxtmfp,x;
52
53#if CNV_PCFILES
54    char unibuff[LEADER];
55#endif
56
57
58    if (argc < 2) {
59        printf("%s",cicopyr("Utility MKXRF"));
60        printf("\n");
61        printf("mkxrf <dbn> \n"); 
62        printf("\n");
63        exit(1);
64    }
65   
66    dbnp=argv[1];
67
68    comb=FIRSTCOMB; comp=FIRSTCOMP;
69
70    fd=dbxopen("",dbnp,".mst");
71    nbytes=LSEEK64(fd,0L,SEEK_END);
72
73#if 0
74    printf("Max MFN: ");
75    if (scanf("%"_LD_,&nxtmfn) != 1) fatal("Max MFN");
76    nxtmfn++;
77    nxtmfb=nbytes/MSBSIZ;
78    nxtmfp=MSBSIZ;
79#else
80    LSEEK64(fd,0L,SEEK_SET);
81    recp=(RECSTRU *)ctlarea;
82    if (CIREAD(fd,MFX,M0BSIZ) != M0BSIZ) fatal("read/ctlmfn");
83#if CNV_PCBINUM
84    ConvertMST_CTLSTRUCT(MFX);
85#endif
86
87    printf("\n");
88    prtcontrol(recp,dbnp);
89    printf("\nok ?  ");
90    if (strcmp(gets(line),"ok")) fatal("ok expected");
91    printf("\n");
92
93    nxtmfn=MF0nxtmfn;
94    nxtmfb=MF0nxtmfb;
95    nxtmfp=MF0nxtmfp;
96    if (nxtmfn < 1 || nxtmfn > MAXUPDMFN)
97        fatal("invalid nxtmfn - use CTLMFN first");
98    if (nxtmfb < 1 || nxtmfb > nbytes/MSBSIZ)
99        if (nxtmfb != nbytes/MSBSIZ+1 || nxtmfp != 1)
100            fatal("invalid nxtmfb/nxtmfp - use CTLMFN first");
101    if (nxtmfp < 1 || nxtmfp > MSBSIZ)
102        fatal("invalid nxtmfp - use CTLMFN first");
103#endif
104
105    CLOSE(fd);
106
107    recisis0_m=0; /* recisis0() keep .mst */
108    if (recisis0(dbnp) <= 0) fatal("mkxrf/cannot reinitialize your .XRF");
109
110    lastmfn=0;
111
112    RECORD(nrecs,dbnp,1L); /* RECdbxp and mstsetup() */
113    dbxopenw("",RDBname,xx1extp,&RDBxropn,&RDBxropw,"xropn/w"); /* recwxref() */
114    fd=RDBmsopn;
115
116    if (LSEEK64(fd,(off_t)comp,SEEK_SET) != comp)
117        fatal("mkxrf/lseek");
118    totread+=comp;
119
120
121LOOP:
122
123#if TRACE1
124    printf("\n+++ comb=%"_LD_"  comp=%d \n",comb,comp);
125#endif
126
127    while (comp >= MSBSIZ) {
128        comb++;
129        comp-=MSBSIZ;
130#if TRACE1
131        printf("new comb=%"_LD_"  comp=%d \n",comb,comp);
132#endif
133        if ((comb%10) == 0)
134            fprintf(stderr,"+++ %"_LD_" blks, %"_LD_" recs\n",comb,ok);
135    }
136
137    if (comp > (MSBSIZ-MSNVSPLT)) {
138        comb++;
139        LSEEK64(fd,(off_t)(MSBSIZ-comp),SEEK_CUR);
140        comp=0;
141#if TRACE1
142        printf("now comb=%"_LD_"  comp=%d \n",comb,comp);
143#endif
144        if ((comb%10) == 0)
145            fprintf(stderr,"+++ %"_LD_" blks, %"_LD_" recs\n",comb,ok);
146    }
147
148    if (comb >= nxtmfb)
149        if (comp+LEADER >= nxtmfp) {
150            fprintf(stderr,"+++ %"_LD_" blks, %"_LD_" recs\n",comb,ok);
151            printf("end of mkxrf\n");
152            exit(0);
153        }
154
155#if CNV_PCFILES
156    if ((n=CIREAD(fd,unibuff,LEADER)) == LEADER) {
157        p=MFX;
158        memcpy(p+0,unibuff+0,4); /* mfn= */
159        memcpy(p+4,unibuff+6,4); /* mfbwb= */
160        memcpy(p+8,unibuff+4,2); /* mfrl= */
161        memcpy(p+10,unibuff+10,LEADER-10); /* LEADER was read */
162    } else {
163#else
164    if ((n=CIREAD(fd,MFX,LEADER)) != LEADER) {
165#endif
166        fprintf(stderr,"+++ %"_LD_" blks, %"_LD_" recs\n",comb,ok);
167        /* fatal("read/LEADER"); */
168        printf("end of mkxrf [read/[LEADER=%d]=%d] [totread=%"_LD_"]\n",LEADER,n,totread);
169        exit(0);
170    }
171
172#if CNV_PCBINUM
173    ConvertMST_LEADER(MFX);
174#endif   
175
176    if (MFRmfn < 1 || MFRmfn >= nxtmfn ||
177        MFRmfrl < LEADER || MFRmfrl > RECnbytes  ||
178        MFRbase > MFRmfrl ||
179        MFRmfbwb < 0 || MFRmfbwb > comb ||
180        MFRmfbwp < 0 || MFRmfbwp > MSBSIZ ||
181        MFRnvf < 0 ||
182        MFRstatus != ACTIVE && MFRstatus != DELETED ||
183        MFRbase != (LEADER + MFRnvf*sizeof(DIRSTRU))) {
184#if TRACE2
185        if (argc > 2)
186            printf(
187            "*** check (%"_LD_"/%d) - mfn=%"_LD_" mfrl=%d base=%d (%d x %d)\n",
188                comb,comp,MFRmfn,MFRmfrl,MFRbase,MFRnvf,sizeof(DIRSTRU));
189#endif
190        LSEEK64(fd,(off_t)(-(LEADER-EACHSKIP)),SEEK_CUR);
191        comp+=EACHSKIP; totread+=EACHSKIP;
192        goto LOOP;
193    }
194
195    totread+=LEADER;
196
197    if ((nr=CIREAD(fd,MFX+LEADER,(n=MFRmfrl-LEADER))) != n) {
198        fprintf(stderr,"+++ %"_LD_" blks, %"_LD_" recs\n",comb,ok);
199        /* fatal("read/n"); */
200        printf("end of mkxrf [read/[n=%d]=%d] [totread=%"_LD_"]\n",n,nr,totread);
201        exit(0);
202    }
203
204    for (x=0; x < MFRnvf; x++) {
205        if (DIRpos(x) + DIRlen(x) + MFRbase > MFRmfrl) {
206#if TRACE2
207            if (argc > 2)
208                printf(
209                "*** check dir - mfn=%"_LD_" dir=%d (tag=%"_LD_" pos=%"_LD_" len=%"_LD_")\n",
210                    MFRmfn,x+1,
211                    (LONGX)DIRtag(x),(LONGX)DIRpos(x),(LONGX)DIRlen(x));
212#endif
213            LSEEK64(fd,(off_t)(-(MFRmfrl-EACHSKIP)),SEEK_CUR);
214            comp+=EACHSKIP; totread+=EACHSKIP;
215            totread-=LEADER;
216            goto LOOP;
217        }
218    }
219
220    totread+=nr;
221    ok++;
222
223    if (argc > 2)
224        printf("#%"_LD_": %"_LD_"/%d - %"_LD_",%d,%"_LD_",%d,%d,%d,%d\n",
225                ok,comb,comp,
226                MFRmfn,MFRmfrl,MFRmfbwb,MFRmfbwp,MFRbase,
227                MFRnvf,MFRstatus);
228
229    if (argc > 3)
230        prtfields(recp,MFRmfn);
231
232    xrftiv = comb * XRXDIVIDE + comp;
233    if (MFRstatus == DELETED)
234        xrftiv= -xrftiv;
235
236    recwxref(recp,xrftiv,lastmfn);
237
238    if (MFRmfn > lastmfn)
239        lastmfn=MFRmfn;
240
241    comp+=(MFRmfrl);
242
243    goto LOOP;
244}
Note: See TracBrowser for help on using the browser.