root/tags/5.52/mxtb.c

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

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

Line 
1/* mxtb.c    -   CI/tabula casos segundo variavel/variaveis de controle
2
3   uso: mxtb <dbn> <dbnout> {len1:fmt1} {len2:fmt2} ...
4                                        [tab=<v>] [class=<n>]
5
6        <dbn>:      data base de entrada
7
8        <dbnout>:   arquivo de saida, contendo um registro para cada
9                    combinacao de categorias das variaveis de controle
10                    ocorrida em <dbnin>, como abaixo:
11
12                        c1|c2|...|cn|frequencia
13
14        <v1>:       tamanho/formato que sera' tomado como a primeira
15                    variavel de controle (ou 't' para total)
16
17        <v2>:       tamanho/formato que sera' tomado como a segunda
18                    variavel de controle
19           .
20           .
21
22        <vn>:       tamanho/formato que sera' tomado como a ultima
23                    variavel de controle
24
25        tab=<c>:    para somar o conteudo do campo <c> durante a
26                    tabulacao
27
28        class=<n>:  para alocar <n> casos na tabela de frequencias
29
30        memos=<n>:  para alocar <n> modulos para a area de tabulacao
31
32        tell=<n>:   para dar "tabulados" a cada <n> casos
33
34        hash=<n>:   para usar a <n>-esima funcao de hash
35
36
37    Autor:  tab0.c/AOT/ALP, 24/07/90
38
39    Alter:  AOT, 22/01/91
40            1. parametro memos=
41           
42            AOT/RP, 14/12/93
43            1. alocacao para UNIX (!PM)
44           
45            AOT/MMB, 30/01/94
46            1. ALLO64V = USHRT_MAX (vide hash())
47            2. correcao hashx=2 (agora default if !PC)
48
49            AOT, 20/08/97
50            1. parametro bool=
51
52            AOT, 15/09/2002
53            1. ALLO64V = UINT_MAX (vide hash())
54
55            AOT, 17/05/2004
56            1. execucao do parametro tab= apos tabkeys
57        2. fldupdat dos campos 32601, 32602, ...
58        3. parametro mfnsum=<n> para forcar tabulacao de mfn nas <n> primeiras chaves
59        4. parametros {min|max}{avg|freq}
60
61            AOT, 10/01/2006
62            1. mxtbofi.c source code
63
64--------------------------------------------------------------------- */
65
66#define xPC 0
67#include <stdio.h>
68#include <string.h>
69
70#include "cisis.h"          /* CISIS Interface header file */
71#include "cirun.h"          /* CISIS Interface runtime declarations */
72
73
74#define MXTBOL 1
75#if MXTBOL
76#if CIB71
77#include "cib70.h"
78char *boolqryp=NULL;
79LONGX boolridx=0;
80LONGX boolhits=0,boolmfn,boolh=0;
81
82char *booldbnp = NULL;          /* b4x query dbn */
83LONGX booltell=1L;               /* 0L = no msg */
84LONGX booltlir=0L;               /* b40 invx */
85
86b7_CIB7 *cib7p=NULL;
87char booltmpnam[CIMPL+1]={'\0'};    /* b72 MFQTHMEM tmp file */
88#else /* CIB71 */
89char *boolqryp=NULL;
90LONGX boolhits=0L;
91LONGX boolridx;
92LONGX nbytes1,nbytes2;
93LONGX boolh;
94char *boolp;
95int boolerr;
96LONGX boolmfn;
97#endif /* CIB71 */
98#endif
99
100#define CLASS   1000        /* default */
101
102#define MAXFMTA MAXMFRL     /* max lw */
103
104int parmmfnsum=0;
105LONGX parmmin996=0;
106LONGX parmmin997=0;
107LONGX parmmax996=0;
108LONGX parmmax997=0;
109int usecnt1=0;
110
111LONGX count1;                /* registros lidos */
112LONGX count2;                /* casos tabulados */
113
114LONGX parmclass;             /* parametro <class> */
115int parmhashx;              /* parametro <hash> */
116int parmmemos;              /* parametro <memos> */
117
118LONGX ntabs;                 /* numero de entradas de tabkey/tabcnt */
119char *tabkey;               /* chaves das entradas, agora via ALLOC */
120LONGX *tabcnt;               /* frequencias - agora pointer */
121LONGX *tabcnt1;              /* frequencia1 */
122LONGX maxhash;
123int tabwidth;               /* tamanho de cada entrada de tabkey */
124int tabwint;                /* tabwidth em 4 bytes inteiros */
125char wkey[MAXFMTA+1];       /* chave para pesquisa */
126
127char *gidbnp;
128char *dbnoutp;
129
130char *buffup=NULL;              /* outfreq/fldupdat, now allocated - AOT, 28/01/2002 */
131LONGX buffuplen=MAXMFRL+BUFSIZ;   /* ALLOMAXV? */
132
133LONGX cq1,cq2,cq3,cq4,cqt;   /* teste */
134
135
136#define UINT unsigned int
137#if PC && !DOS32BITS
138#define MAX64   200
139#define ALLO64V SHRT_MAX
140#else
141#define MAX64   2000
142#define ALLO64V UINT_MAX
143#endif
144
145int memoidx;                /* indice do modulo corrente */
146UINT vntabs[MAX64];         /* numero de entradas de vtabkey[i]/vtabcnt[i] */
147char *vtabkey[MAX64];       /* chaves das entradas, agora via ALLOC */
148LONGX *vtabcnt[MAX64];       /* frequencias - agora pointer */
149LONGX *vtabcnt1[MAX64];      /* frequencia1 */
150
151FMT_CODE *tabfmtp=NULL;     /* formato para tab= */
152char *tabfmta=NULL;         /* area para tab= */
153
154LONGX upirec,upcrec;
155
156#define MAXTABX 8
157int ntabx=0;
158char *areatabx=NULL;
159LONGX lintabx[MAXTABX];
160LONGX lentabx[MAXTABX];
161char *atabx[MAXTABX];
162FMT_CODE *ptabx[MAXTABX];
163#define MAXTABXL 32
164LONGX  linxtabx[MAXTABX];
165LONGX  xxlltabx[MAXTABX][MAXTABXL];
166char *xxlptabx[MAXTABX][MAXTABXL];
167int itabx;
168int ilin;
169LONGX  xxlftabx[MAXTABX][MAXTABXL];
170LONGX  vinxtabx[MAXTABX];
171int level;
172unsigned char ctabx;
173
174/* prototypes */
175#if ANSI
176int hash(void);
177void outfreq(UINT n);
178#else
179int hash();
180void outfreq();
181#endif
182
183void main(argc,argv)
184int argc;
185char *argv[];
186{
187    RECSTRU *recp;
188    LONGX irec,mfn;
189
190    int widx;
191    unsigned char *wptr;
192    int l;
193
194    LONGX maxprim,maxdiv,bytskey,bytscnt,divid;
195#if xPC
196    LONGX parmc;
197#endif
198    LONGX tabv;
199    unsigned char *p,*q;
200
201    int parmtrace=0;
202    int parmtrac2=0;
203    int parmtrac3=0;
204    LONGX parmfrom,parmto,parmloop,parmcount,parmtell;
205    int argnext;
206
207    if (argc < 4) {
208        printf("%s",cicopyr("Utility MXTB"));
209        printf(" \n");
210        printf("mxtb <dbn> [create=|append=]<dbnout> <key> [<key> ...] ");
211        printf("[<option> [...]] \n");
212        printf(" \n");
213        printf("keys:    keylen:key_fmtspec \n");
214        printf(" \n");
215        printf("options: {from|to|loop|count|tell|btell}=<n> \n");
216        printf("         tab=<tab_val_fmt> \n");
217        printf("         mfnsum=<#levels> [{min|max}{avg|freq}=<n>] \n");
218#if PM
219        printf("         {class|memos}=<n> \n");
220#else
221        printf("         class=%ld \n",(LONGX)CLASS);
222#endif
223#if MXTBOL
224        printf("         bool=<expr> \n");
225#endif
226        printf("         uctab={<file>|ansi} \n");
227        printf(" \n");
228        printf("Ex: mxtb in out len1:fmt1 len2:fmt2 len3:fmt3 \n");
229        printf(" \n");
230        printf("     out = 1  key/key1_value (max len1 chars)\n");
231        printf("           2  key/key2_value (max len2 chars)\n");
232        printf("           3  key/key3_value (max len3 chars)\n");
233        printf("         998  999999999 - key_frequency \n");
234        printf("         999  key_frequency \n");
235        printf(" \n");
236        printf("Ex: mxtb in out len:fmt tab=Vtag \n");
237        printf(" \n");
238        printf("     out = 1  key_value (max len chars)\n");
239        printf("         998  999999999 - Vtag_subtotal \n");
240        printf("         999  Vtag_subtotal \n");
241        printf(" \n");
242        exit(1);
243    }
244
245    argnext=1;
246    RECORD(irec=nrecs,gidbnp=argv[argnext++],0L);
247
248    dbnoutp=argv[argnext++];
249
250    /*
251        pre-processa variaveis de controle
252    */
253    parmfrom=1; parmto=999999999L; parmloop=1; parmcount=0; parmtell=0;
254
255    tabwidth=0; parmclass=CLASS; 
256#if PC && !DOS32BITS
257    parmhashx=1;
258#else
259    parmhashx=2;
260#endif
261#if PM
262    parmmemos=1;
263#endif
264
265    for (; argnext < argc; argnext++) {
266        p=argv[argnext];
267        if (parmtrac2) printf("+++ parm=%s\n",p);
268        if (strncmp(p,"from?",5) == 0 || strncmp(p,"from=",5) == 0) {
269            if (sscanf(p+5,"%ld",&parmfrom) != 1)
270                fatal(p);
271            continue;
272        }
273        if (strncmp(p,"to?",3) == 0 || strncmp(p,"to=",3) == 0) {
274            if (sscanf(p+3,"%ld",&parmto) != 1)
275                fatal(p);
276            continue;
277        }
278        if (strncmp(p,"loop?",5) == 0 || strncmp(p,"loop=",5) == 0) {
279            if (sscanf(p+5,"%ld",&parmloop) != 1)
280                fatal(p);
281            continue;
282        }
283        if (strncmp(p,"count?",6) == 0 || strncmp(p,"count=",6) == 0) {
284            if (sscanf(p+6,"%ld",&parmcount) != 1)
285                fatal(p);
286            continue;
287        }
288        if (strncmp(p,"tell?",5) == 0 || strncmp(p,"tell=",5) == 0) {
289            if (sscanf(p+5,"%ld",&parmtell) != 1)
290                fatal(p);
291            continue;
292        }
293
294        if (strcmp(p,"trace") == 0) {
295            parmtrace=1;
296            continue;
297        }
298        if (strcmp(p,"trac2") == 0) {
299            parmtrac2=1;
300            continue;
301        }
302        if (strcmp(p,"trac3") == 0) {
303            parmtrac3=1;
304            continue;
305        }
306
307
308        if (!strncmp(p,"class=",6)) {
309            if ((sscanf(p+6,"%ld",&parmclass)) != 1) fatal(p);
310            if (parmclass < 2) fatal(p);
311            continue;
312        }
313#if PM
314        if (!strncmp(p,"memos=",6)) {
315            if ((sscanf(p+6,"%d",&parmmemos)) != 1) fatal(p);
316            if (parmmemos < 1 || parmmemos >= MAX64) fatal(p);
317            continue;
318        }
319#endif
320        if (!strncmp(p,"hash=",5)) {
321            if ((sscanf(p+5,"%d",&parmhashx)) != 1) fatal(p);
322            if (parmhashx < 1 || parmhashx > 2) fatal(p);
323            continue;
324        }
325        if (!strncmp(p,"tab=",4)) {
326            if (tabfmta) fatal(p);
327            tabfmta=loadfile(gidbnp,'@',q=p+4,tabfmta,MAXFMTA+1,'\0');
328            if (!tabfmta) fatal(q);
329            if (fmt_gener(&tabfmtp,q)) fatal(q+fmt_errof);
330            continue;
331        }
332        if (!strncmp(p,"mfnsum=",7)) {
333                if ((sscanf(p+7,"%d",&parmmfnsum)) != 1) fatal(p);
334                if (parmmfnsum != 1) fatal(p);
335            continue;
336        }
337        if (!strncmp(p,"minavg=",7)) {
338                if ((sscanf(p+7,"%ld",&parmmin996)) != 1) fatal(p);
339                if (parmmax996) if(parmmin996>parmmax996) fatal(p);
340            continue;
341        }
342        if (!strncmp(p,"maxavg=",7)) {
343                if ((sscanf(p+7,"%ld",&parmmax996)) != 1) fatal(p);
344                if (parmmin996) if(parmmin996>parmmax996) fatal(p);
345            continue;
346        }
347        if (!strncmp(p,"minfreq=",8)) {
348                if ((sscanf(p+8,"%ld",&parmmin997)) != 1) fatal(p);
349                if (parmmax997) if(parmmin997>parmmax997) fatal(p);
350            continue;
351        }
352        if (!strncmp(p,"maxfreq=",8)) {
353                if ((sscanf(p+8,"%ld",&parmmax997)) != 1) fatal(p);
354                if (parmmin997) if(parmmin997>parmmax997) fatal(p);
355            continue;
356        }
357
358        if (!strncmp(p,"bool=",5)) {
359            if (boolqryp) fatal(p); q=p+5;
360            boolqryp=loadfile(gidbnp,'@',q,NULL,0L,'\n');
361            if (!boolqryp) fatal(p);
362            continue;
363        }
364        if (strncmp(p,"uctab?",6) == 0 || strncmp(p,"uctab=",6) == 0) {
365                            if (strcmp(p+6,"ansi") == 0) {
366                                                    memcpy(isisuctab,ansiuctab,256/*sizeof(ansiuctab)*/);
367                                                                } else {
368                                                                                        /* loaductb(mi_gidbnp,isisuctab,p+6); */
369                                                                                        if (!loaductb(gidbnp,isisuctab,p+6)) fatal(p); /* AOT, 02/04/2001 */
370                                                                                                    }
371                                        continue;
372                                                }
373
374        if (strncmp(p,"btell?",6) == 0 || strncmp(p,"btell=",6) == 0) {
375            if (sscanf(p+6,"%ld",&booltell) != 1)
376                fatal(p);
377            continue;
378        }
379
380        if ((sscanf(p=p,"%d:",&l)) == 1) {
381            if (ntabx >= MAXTABX) fatal(p);
382            if (l > MAXFMTA) fatal(p);
383            lentabx[ntabx]=l;
384            for (q=p; *q; q++) if (*q == ':') break;
385            if (*q != ':') fatal(p);
386            atabx[ntabx]=loadfile(gidbnp,'@',q+1,NULL,MAXFMTA+1,'\0');
387            if (!atabx[ntabx]) fatal(q+1);
388            q=atabx[ntabx];
389            if (fmt_gener(&ptabx[ntabx],q)) fatal(q+fmt_errof);
390            lintabx[ntabx]=MAXTABXL;
391            if (!ntabx) {
392                areatabx=loadfile(NULL,'@',"",NULL,MAXFMTA+1,'\0');
393                if (!areatabx) fatal("ALLOC/areatabx");
394            }
395            ntabx++;
396            tabwidth+=l;        /* '\0' */
397            continue;
398        }
399
400        fatal(p);
401    }
402
403    if (parmmin996 || parmmax996 || parmmin997 || parmmax997) usecnt1=1;
404
405    if (!ntabx) fatal("no tabulation key specified");
406
407    tabwidth++;                 /* '\0' final */
408    tabwint=(tabwidth+1)/4;     /* XOR a cada 4 bytes */
409
410    /* setup parmclass/parmmemos (bytskey) */
411#if !PM
412    parmmemos=1;
413#endif
414
415    maxprim=parmclass;          /* contagem em tabkey */
416#if PC
417    if ((unsigned LONGX)maxprim > UINT_MAX)
418        fatal("class overflow");
419#endif
420    maxhash=0;
421    while (!maxhash) {
422        maxdiv=maxprim/2;
423        for (divid=2;divid<maxdiv;divid++)
424            if (!(maxprim % divid)) {
425                if (parmtrac3)
426                    printf("+++ %ld divide by %ld\n",maxprim,divid);
427                maxprim--;
428                break;
429            }
430        if (divid >= maxdiv) {
431            maxhash=maxprim;
432            if (parmtrace) printf("+++ hash=%ld\n",maxhash);
433        }
434    }
435
436
437    bytskey=maxhash*tabwidth;
438    bytscnt=maxhash*sizeof(LONGX);
439    if (bytskey >= ALLOMAXV)
440        fatal("bytskey/ALLOMAXV");
441    if (bytscnt >= ALLOMAXV)
442        fatal("bytscnt/ALLOMAXV");
443
444#if xPC
445    if (parmtrace) printf("+++ bytes available: %7ld\n",(LONGX)coreleft());
446#endif
447
448for (memoidx=0; memoidx < parmmemos; vntabs[memoidx]=0, memoidx++) {
449
450    if (parmtrace) printf("+++ bytes for keys:  %7ld (%ld x %d)\n",
451                                bytskey,maxhash,tabwidth);
452
453    vtabkey[memoidx]=tabkey=(char *)ALLOC((ALLOPARM)(bytskey));
454    if (tabkey == ALLONULL) fatal("ALLOC/tabkey");
455    memset(tabkey,0x00,bytskey);
456
457    if (parmtrace) printf("+++ bytes for cnts:  %7ld (%ld x %d)\n",
458                                bytscnt,maxhash,sizeof(LONGX));
459
460    vtabcnt[memoidx]=tabcnt=(LONGX *)ALLOC((ALLOPARM)(bytscnt));
461    if (tabcnt == ALLONULL) fatal("ALLOC/tabcnt");
462    memset(tabcnt,0x00,bytscnt);
463
464    vtabcnt1[memoidx]=tabcnt1=(LONGX *)ALLOC((ALLOPARM)(bytscnt));
465    if (tabcnt1 == ALLONULL) fatal("ALLOC/tabcnt1");
466    memset(tabcnt1,0x00,bytscnt);
467
468}
469
470    /* allocate buffup
471    */
472    if (!buffup) {
473        char *dbnp=gidbnp; 
474        LONGX asize=0;
475        if (boolqryp) asize=
476               strlen(dbnp) +                     /* field MFQTDBN0=11 (may be longer than CIMPL+1) */
477               strlen(boolqryp) +                 /* the expression */
478               2*strlen(boolqryp) +               /* the terms + pseudo operators _1termX+11X */
479               BUFSIZ+strlen(dbnp) +              /* field MFQSWDBN=20 + a diferent db */
480               24*(strlen(B7DELADD)+3*B7UPLEN) +  /* 2 duzias de control fields */  /* DxxAxx|xxx| */
481               strlen(boolqryp)+4 +               /* tmpfile .%03x */
482               BUFSIZ;                            /* por si las moscas */
483        if (asize > buffuplen) buffuplen=asize;
484        buffup=loadfile(NULL,'@',"",NULL,buffuplen,'\0');
485    }
486   
487#if xPC
488    if (parmtrace) printf("+++ unused bytes:    %7ld\n",(LONGX)coreleft());
489#endif
490
491#if MXTBOL
492    /* initialize ridx and b4_xxx() parameters */
493
494    if (boolqryp) { /* mxbool/boolh */
495#if CIB71
496    RECSTRU *recp;
497    char *p;
498    LONGX mfn;
499    LONGX crec=0L; /* b5_exp usa crec condicionado a booldbnp */
500    int errno,xfd;
501
502    char *dbnp=gidbnp;
503
504    void *ciapip = NULL;
505#if CIAPI
506    ciapip = cisisApplication( MAXNREC,MAXNTRM );/* mandatory for CIAPI definitions */
507#endif /* CIAPI */
508
509    cib7p=b7_cib7(cib7p,ciapip);
510
511    for (boolridx=nrecs, mfn=1; ; mfn++) { /* get actual maxmfn */
512        RECORD(boolridx,dbnp,mfn);
513        if (RECrc == RCEOF) fatal(dbnp);
514        if (RECrc == RCNORMAL) break;
515    }
516
517    if (booldbnp) {
518        dbxopt_fatal=0; xfd=dbxopen("",booldbnp,xx1extp);
519        if (xfd > 0) CLOSE(xfd); else recisis0(booldbnp);
520        RECORD(crec=nrecs,booldbnp,0L);
521        b7setno=MF0nxtmfn-1L;
522/*
523<R>
524        vrecp[boolridx]->recdbxp=RECdbxp;
525*/
526        VRECdbxp(boolridx)=RECdbxp;
527    }
528    else {
529        VRECdbxp(boolridx)=NULL; /* nao usa base de log */
530        VMFRmfn(boolridx)=0L;
531    }
532
533    p=b7_exp(cib7p,boolridx,dbnp,boolqryp,buffup,booldbnp,crec,&errno);
534    if (p || errno) printf("Expression syntax error %d: '%s'\n",errno,p);
535    else {
536        /* set hit file name to "null=don't write" or as provided by "b70x=" */
537        if (!booldbnp || booltmpnam[0]) {
538            char *hitfnamp="null";
539            if (booltmpnam[0]) hitfnamp=booltmpnam;
540            sprintf(buffup,"D%dA%d|%s|",MFQTHMEM,MFQTHMEM,hitfnamp);
541            if (fldupdat(boolridx,buffup) != NULL) fatal("mxtb/boolridx/fldupdat");
542            if (b70trace) prtfields(vrecp[boolridx],VMFRmfn(boolridx));
543        }
544        /* b7_run faz "trirecp=vrep[tlirec]" */
545        if (!booltlir) record(booltlir=nrecs,"null",1L);
546
547        p=b7_run(cib7p,boolridx,dbnp,buffup,buffuplen,booltell,isisuctab,"",booltlir,crec,&errno);
548        if (p || errno) printf("Execution error %d: '%s'\n",errno,p);
549        else {
550            boolhits=b7_hit(cib7p,boolridx,0L,b7setno);
551            if (booltell) printf("Hits=%ld\n",boolhits);
552        }
553    }
554    if (p || errno) fatal("Expression execution error");
555#else /* CIB71 */
556        /* get maxmfn */
557        nbytes1=MF0nxtmfn/8+1;  /* one bit per mfn for b4_run bit strings */
558        recallok(boolridx=nrecs,BUFSIZ+nbytes1);
559        nbytes2=4096L; /* buffer size for b4_run() tmp file */
560
561        /* compile/execute boolean expression */
562        boolp=b4_exp(boolridx,gidbnp,boolqryp,NULL,&boolerr);
563        if (boolp) {
564          printf("Error #%d (%s)\n",boolerr,boolp);
565          fatal("Expression syntax error");
566        }
567        fldupdat(boolridx,"D12"); /* delete MFQTQRY0 */
568        boolp=b4_run(boolridx,gidbnp,nbytes1,nbytes2,parmtrace,isisuctab,0L,
569                                                                &boolerr);
570        if (boolp) {
571          printf("Error #%d (%s)\n",boolerr,boolp);
572          fatal("Expression execution error");
573        }
574        boolhits=b4_hit(boolridx,0L);
575#endif /* CIB71 */
576    } /* mxbool/boolh */
577#endif
578
579    count2=0; count1=0;
580
581    if (!tabfmtp) tabv=1;                         /* tab field default */
582
583    cq1=cq2=cq3=cq4=cqt=0;
584
585    if (!strncmp(dbnoutp,"create=",7)) {
586        dbnoutp+=7;
587        recisis0(dbnoutp);
588    }
589    if (!strncmp(dbnoutp,"append=",7)) {
590        dbnoutp+=7;
591    }
592
593    if (parmtrace) printf("+++ maxmfn=%ld\n",MF0nxtmfn-1);
594
595    record(upcrec=nrecs,dbnoutp,0L);
596    record(upirec=nrecs,dbnoutp,1L);
597
598
599    /*
600        loop principal
601    */
602
603    memoidx=0;
604
605#if MXTBOL
606    for (mfn=parmfrom, boolh=1L; ; mfn+=parmloop,
607                                   boolh = ((boolh+=parmloop) > boolhits) ?
608                                                                boolhits+1:
609                                                                boolh      ) {
610
611        if (boolqryp) { /* mxbool/boolh */
612#if CIB71
613            boolmfn=b7_hit(cib7p,boolridx,boolh,b7setno);
614#else /* CIB71 */
615            boolmfn=b4_hit(boolridx,boolh);
616#endif /* CIB71 */
617            if (parmtrace) printf("+++ boolmfn=%ld\n",boolmfn);
618            if (boolmfn <= 0L)      break;
619            if (boolmfn < parmfrom) continue;
620            mfn=boolmfn;
621        }
622        RECORD(irec,RDBname,mfn);
623        if (RECrc == RCEOF) break;
624#else
625    for (RECORD(irec,RDBname,mfn=parmfrom);
626                RECrc != RCEOF && mfn <= parmto;
627                        mfn+=parmloop, RECORD(irec,RDBname,mfn)) {
628#endif
629
630        if (mfn > parmto) break;
631
632        if (parmcount)
633            if (count1 >= parmcount) break;
634
635        if (RECrc != RCNORMAL)
636            continue;
637
638#if BEFORE20040517
639        if (tabfmtp) {
640            fmt_inter(tabfmtp,irec,(LONGX)MAXFMTA,tabfmta,(LONGX)MAXFMTA);
641            tabv=atol(tabfmta);
642        }
643#endif
644
645        for (itabx=0; itabx < ntabx; itabx++) {
646            p=atabx[itabx]; *p='\0';
647            fmt_inter(ptabx[itabx],irec,(LONGX)MAXFMTA,p,(LONGX)MAXFMTA);
648            linxtabx[itabx]=0;
649            xxlltabx[itabx][0]=0;
650            xxlptabx[itabx][0]=p;
651            ctabx='\0';
652            while (*p) {
653                xxlltabx[itabx][linxtabx[itabx]]=0;
654                xxlptabx[itabx][linxtabx[itabx]]=p;
655                for (; *p; p++) {
656                    ctabx = *p;
657                    if (ctabx == '\n' || ctabx == '\r') {
658                        p++;
659                        if (*p == '\n' || *p == '\r') p++;
660                        break;
661                    }
662                    xxlltabx[itabx][linxtabx[itabx]]++;
663                }
664                if (++linxtabx[itabx] >= lintabx[itabx]) break;
665            }
666#if 0
667            if (ctabx != '\n')
668                if (ctabx != '\r')
669                    if (atabx[itabx][0]) linxtabx[itabx]++;
670#endif
671        }
672        for (itabx=0; itabx < ntabx; itabx++) {
673            xxlftabx[itabx][0]=lentabx[itabx]; /* or 0 */
674            for (ilin=0; ilin < linxtabx[itabx]; ilin++)
675                if (xxlltabx[itabx][ilin] > lentabx[itabx]) {
676                    xxlltabx[itabx][ilin] = lentabx[itabx];
677                    xxlftabx[itabx][ilin] = 0;
678                }
679                else
680                    xxlftabx[itabx][ilin] = lentabx[itabx] -
681                                            xxlltabx[itabx][ilin];
682        }
683
684        for (itabx=0; itabx < ntabx; itabx++) vinxtabx[itabx]=0;
685        for (level=ntabx-1; ; ) {
686
687            /* process itabx = 0 .. ntabx-1 */
688            wptr=wkey;
689        p=tabfmta;
690            for (itabx=0; itabx < ntabx; itabx++) {
691                ilin=vinxtabx[itabx];
692                memcpy(wptr,xxlptabx[itabx][ilin],xxlltabx[itabx][ilin]);
693        if (tabfmtp) {
694            sprintf(p,"H%5d %5d ",32601+itabx,xxlltabx[itabx][ilin]); p+=13;
695                    memcpy(p,   xxlptabx[itabx][ilin],xxlltabx[itabx][ilin]); p+=xxlltabx[itabx][ilin]; *p='\0';
696        }
697                wptr+=xxlltabx[itabx][ilin];
698                memset(wptr,0x00,xxlftabx[itabx][ilin]);
699                wptr+=xxlftabx[itabx][ilin];
700            }
701            *wptr='\0';
702
703            if (*wkey) { /* if *wkey */
704#if BEFORE20040517
705#else
706        if (tabfmtp) {
707        if (parmtrac2) printf("\n+++%s\n",tabfmta);
708        if (fldupdat(irec,tabfmta)) fatal("mxtb/32601/H");
709        if (parmtrac2) prtfields(recp,mfn);
710            fmt_inter(tabfmtp,irec,(LONGX)MAXFMTA,tabfmta,(LONGX)MAXFMTA);
711            tabv=atol(tabfmta);
712        p=tabfmta;
713            for (itabx=0; itabx < ntabx; itabx++) {
714            sprintf(p,"D%5d",32601+itabx); p+=6;
715        }
716        if (fldupdat(irec,tabfmta)) fatal("mxtb/32601/D");
717        }
718#endif
719                if (parmmfnsum) {
720            memset(wkey,0x00,lentabx[0]);
721            sprintf(wkey,"%ld",mfn);
722        }
723        if (parmtrac2) {
724            char *p;
725            int n=tabwidth;
726                    printf("+++"); getchar();
727                    printf("+++count,wkey,tabv: %ld,",count2);
728            for (p=wkey; n--; p++) if (*p) putchar(*p); else putchar(';');
729                    printf(",%ld\n",tabv);
730                }
731        if (tabv) { /* if tabv */
732                widx=hash();
733                tabcnt[widx]+=tabv;
734        if (usecnt1) tabcnt1[widx]+=1;
735                if (parmtrac3) {
736                    printf("+++cq1/4: %ld,%ld,%ld,%ld\n",cq1,cq2,cq3,cq4);
737                    printf("+++widx,tabcnt: %d,%ld\n",widx,tabcnt[widx]);
738                }
739                count2++;
740        } /* end if tabv */
741            } else { /* else if *wkey */
742                if (parmtrac2) {
743                    printf("+++"); getchar();
744                    printf("+++count,,tabv: %ld,,%ld\n",count2,tabv);
745                }
746            } /* end if *wkey */
747
748            /* get next */
749            while (++vinxtabx[level] >= linxtabx[level])
750                if (level) vinxtabx[level--]=0; else {level= -1; break;}
751            if (level < 0) break;
752            level=ntabx-1;
753        }
754
755        cqt+=cq3; count1++;
756        if (parmtell)
757            if ((count1 % parmtell) == 0) {
758                fprintf(stderr,"+++ %ld: %ld/%ld/%ld\n",
759                    count1,count2,ntabs,cqt);
760                cq1=cq2=cq3=cq4=0;
761            }
762
763    }
764
765    if (parmtrace) fprintf(stderr,"+++ updating %s ..\n",dbnoutp);
766    for (memoidx=0; memoidx < parmmemos; memoidx++) {
767        tabkey=vtabkey[memoidx];
768        tabcnt=vtabcnt[memoidx];
769        tabcnt1=vtabcnt1[memoidx];
770        outfreq(vntabs[memoidx]);
771    }
772
773    if (parmtell) {
774        fprintf(stdout,"mxtb: %ld recs in\n",count1);
775        fprintf(stdout,"      %ld tabs\n",count2);
776        fprintf(stdout,"      %ld recs out\n",ntabs);
777    }
778
779#if CIB71
780    if (cib7p) cib7p=b7_cib7_delete(cib7p);
781#if CIAPI
782    cisisApplicationDelete(ciapip);
783    ciapip=NULL;
784#endif /* CIAPI */
785#endif
786
787    exit(0);
788}
789
790
791int hash()                                                          /*
792--- ----                                                            */
793
794{
795    unsigned int hashv,*hptr;
796    int i,hidx,widx;
797    char *wptr;
798
799    unsigned LONGX hashv2,hv2;
800    int k;
801    unsigned char *p;
802
803
804    switch (parmhashx) {
805    case 1:
806            hashv=0;
807            hptr=(unsigned int *)wkey;
808            for (i=tabwint; i > 0 ; i--)    /* XOR a cada 4 bytes */
809                hashv ^= *hptr++;
810            hidx=hashv%maxhash;             /* modulo maxhash */
811            break;
812    case 2:
813            hashv2=0L;
814            p=(unsigned char *)wkey;
815            k=0;
816            for (i=tabwidth-1; i--;) {     /* XOR byte a byte */
817                hv2 = *p++;
818#if 0
819                hv2 <<= k;
820                hashv2 ^= hv2;
821                if (++k == sizeof(LONGX))
822                    k=0;
823#else
824                if (*p == ' ') continue;
825                hv2 <<= k*8;
826                hashv2 ^= hv2;
827                if (++k == sizeof(LONGX))
828                    k=0;
829#endif
830            }
831            hidx=hashv2%maxhash;            /* modulo maxhash */
832            break;
833    default:
834            fatal("mxtb/parmhashx");
835    }
836
837    widx=hidx;
838
839    memoidx=widx%parmmemos;
840    tabkey=vtabkey[memoidx];
841    tabcnt=vtabcnt[memoidx];
842    tabcnt1=vtabcnt1[memoidx];
843
844#if 0
845printf("+0+ key=%s idx=%d memo=%d\n",wkey,widx,memoidx);
846#endif
847
848    wptr = &tabkey[widx * tabwidth];
849
850    if (*wptr == NULL) {
851        memcpy(wptr,wkey,tabwidth);
852        ntabs++; vntabs[memoidx]++;
853        cq1++;
854    }
855    else {
856        while (1) {
857            if (strncmp(wptr,wkey,tabwidth) == 0) {
858                cq2++;
859                break;
860            }
861            else {
862                if (++widx == hidx) {
863                    printf("wkey='%s'\n",wkey);
864                    /* outfreq(vntabs[memoidx]); */
865                    fatal("mxtb/maxhash overflow");
866                }
867                if (widx == maxhash) {
868                    widx=0;
869                    wptr=tabkey;
870                    cq4++;
871                }
872                else {
873                    wptr+=tabwidth;
874                    cq3++;
875                }
876                if (*wptr == NULL) {
877                    memcpy(wptr,wkey,tabwidth);
878                    ntabs++; vntabs[memoidx]++;
879                    break;
880                }
881            }
882        }
883    }
884
885    return(widx);
886}
887
888
889void outfreq(n)
890UINT n;
891{
892    int widx,wlen;
893    char *wptr;
894    char *batchp,*p;
895    int print;
896
897    widx=0;
898    wptr=tabkey;
899    do {
900        if (widx == maxhash)
901            fatal("mxtb/outfreq/widx == maxhash");
902        if (*wptr) {
903            batchp=buffup;
904            for (itabx=0;itabx<ntabx;itabx++) {
905                for (wlen=0, p=wptr; *p; p++)
906                    if (wlen < lentabx[itabx]) wlen++;
907                    else break;
908                sprintf(batchp,"H%d %d ",itabx+1,wlen); batchp+=strlen(batchp);
909                memcpy(batchp,wptr,wlen);
910                batchp+=wlen;
911                wptr+=lentabx[itabx];
912
913            }
914            wptr++; /* NULL */
915
916        *batchp='\0';
917        print=1;
918        if (tabcnt1[widx]) {
919            LONGX  frq=tabcnt1[widx];
920            float avg=(float)tabcnt[widx]/(float)tabcnt1[widx];
921            if (parmmin996) if (avg < parmmin996) print=0;
922            if (parmmax996) if (avg > parmmax996) print=0;
923            if (parmmin997) if (frq < parmmin997) print=0;
924            if (parmmax997) if (frq > parmmax997) print=0;
925            if (print) {
926/*
927                sprintf(batchp,"A998/%09ld/A999/%ld/A997/%ld/A996/%f/A995/%09ld/",
928                    999999999L-tabcnt[widx],tabcnt[widx],frq,avg,999999999L-(LONGX)avg);
929*/
930                sprintf(batchp,"A998/%09ld/A999/%ld/",999999999L-tabcnt[widx],tabcnt[widx]);
931                batchp+=strlen(batchp);
932                sprintf(batchp,"A997/%ld/A996/%f/A995/%09ld/",frq,avg,999999999L-(LONGX)avg);
933            }
934        }
935        else {
936                sprintf(batchp,"A998/%09ld/A999/%ld/",999999999L-tabcnt[widx],tabcnt[widx]);
937        }
938            batchp+=strlen(batchp);
939            *batchp='\0';
940    if (print) {
941            record(upirec,dbnoutp,VMF0nxtmfn(upcrec));
942            VMFRstatus(upirec)=ACTIVE;
943            if (fldupdat(upirec,buffup)) fatal(fldupdat(upirec,buffup));
944            recupdat(upcrec,upirec);
945    }
946            n--;
947        }
948        else
949            wptr+=tabwidth;
950        widx++;
951    } while (n);
952
953}
Note: See TracBrowser for help on using the browser.