3157512528238886e512fe42696d80a1e385be38
[idzebra-moved-to-github.git] / index / kdump.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: kdump.c,v $
7  * Revision 1.21  2002-04-04 20:50:37  adam
8  * Multi register works with record paths and data1 profile path
9  *
10  * Revision 1.20  2002/04/04 14:14:13  adam
11  * Multiple registers (alpha early)
12  *
13  * Revision 1.19  2000/12/05 10:01:44  adam
14  * Fixed bug regarding user-defined attribute sets.
15  *
16  * Revision 1.18  1999/09/07 07:19:21  adam
17  * Work on character mapping. Implemented replace rules.
18  *
19  * Revision 1.17  1999/02/02 14:50:55  adam
20  * Updated WIN32 code specific sections. Changed header.
21  *
22  * Revision 1.16  1998/05/20 10:12:17  adam
23  * Implemented automatic EXPLAIN database maintenance.
24  * Modified Zebra to work with ASN.1 compiled version of YAZ.
25  *
26  * Revision 1.15  1998/03/05 08:45:12  adam
27  * New result set model and modular ranking system. Moved towards
28  * descent server API. System information stored as "SGML" records.
29  *
30  * Revision 1.14  1997/10/27 14:33:04  adam
31  * Moved towards generic character mapping depending on "structure"
32  * field in abstract syntax file. Fixed a few memory leaks. Fixed
33  * bug with negative integers when doing searches with relational
34  * operators.
35  *
36  * Revision 1.13  1997/09/09 13:38:07  adam
37  * Partial port to WIN95/NT.
38  *
39  * Revision 1.12  1997/09/05 09:52:32  adam
40  * Extra argument added to function chr_read_maptab (tab path).
41  *
42  * Revision 1.11  1996/10/29 14:06:49  adam
43  * Include zebrautl.h instead of alexutil.h.
44  *
45  * Revision 1.10  1996/06/04 14:56:12  quinn
46  * Fix
47  *
48  * Revision 1.9  1996/06/04  14:18:53  quinn
49  * Charmap work
50  *
51  * Revision 1.8  1996/06/04  10:18:59  adam
52  * Minor changes - removed include of ctype.h.
53  *
54  * Revision 1.7  1995/10/10  12:24:38  adam
55  * Temporary sort files are compressed.
56  *
57  * Revision 1.6  1995/09/29  14:01:42  adam
58  * Bug fixes.
59  *
60  * Revision 1.5  1995/09/11  13:09:35  adam
61  * More work on relevance feedback.
62  *
63  * Revision 1.4  1995/09/08  14:52:27  adam
64  * Minor changes. Dictionary is lower case now.
65  *
66  * Revision 1.3  1995/09/06  16:11:17  adam
67  * Option: only one word key per file.
68  *
69  * Revision 1.2  1995/09/04  12:33:42  adam
70  * Various cleanup. YAZ util used instead.
71  *
72  * Revision 1.1  1995/09/04  09:10:36  adam
73  * More work on index add/del/update.
74  * Merge sort implemented.
75  * Initial work on z39 server.
76  *
77  */
78 #include <stdio.h>
79 #include <string.h>
80 #include <assert.h>
81 #ifdef WIN32
82 #include <io.h>
83 #else
84 #include <unistd.h>
85 #endif
86 #include <assert.h>
87
88 #include <charmap.h>
89 #include "index.h"
90
91 char *prog;
92
93
94 int key_file_decode (FILE *f)
95 {
96     int c, d;
97
98     c = getc (f);
99     switch (c & 192) 
100     {
101     case 0:
102         d = c;
103         break;
104     case 64:
105         d = ((c&63) << 8) + (getc (f) & 0xff);
106         break;
107     case 128:
108         d = ((c&63) << 8) + (getc (f) & 0xff);
109         d = (d << 8) + (getc (f) & 0xff);
110         break;
111     case 192:
112         d = ((c&63) << 8) + (getc (f) & 0xff);
113         d = (d << 8) + (getc (f) & 0xff);
114         d = (d << 8) + (getc (f) & 0xff);
115         break;
116     }
117     return d;
118 }
119
120
121 static int read_one (FILE *inf, char *name, char *key, struct it_key *prevk)
122 {
123     int c;
124     int i = 0;
125     struct it_key itkey;
126     do
127     {
128         if ((c=getc(inf)) == EOF)
129             return 0;
130         name[i++] = c;
131     } while (c);
132     if (i > 1)
133         prevk->sysno = 0;
134     c = key_file_decode (inf);
135     key[0] = c & 1;
136     c = c >> 1;
137     itkey.sysno = c + prevk->sysno;
138     if (c)
139     {
140         prevk->sysno = itkey.sysno;
141         prevk->seqno = 0;
142     }
143     c = key_file_decode (inf);
144     itkey.seqno = c + prevk->seqno;
145     prevk->seqno = itkey.seqno;
146
147     memcpy (key+1, &itkey, sizeof(itkey));
148     return 1;
149 }
150
151 int main (int argc, char **argv)
152 {
153     int ret;
154     char *arg;
155     char *key_fname = NULL;
156     char key_string[IT_MAX_WORD];
157     char key_info[256];
158     ZebraMaps zm;
159     FILE *inf;
160     Res res = NULL;
161     struct it_key prevk;
162
163     prevk.sysno = 0;
164     prevk.seqno = 0;
165
166     prog = *argv;
167     while ((ret = options ("c:v:", argv, argc, &arg)) != -2)
168     {
169         if (ret == 0)
170         {
171             key_fname = arg;
172         }
173         else if (ret == 'v')
174         {
175             yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
176         }
177         else if (ret == 'c')
178         {
179             if (!(res = res_open (arg, 0)))
180             {
181                 logf(LOG_FATAL, "Failed to open resource file %s", arg);
182                 exit (1);
183             }
184         }
185         else
186         {
187             logf (LOG_FATAL, "Unknown option '-%s'", arg);
188             exit (1);
189         }
190     }
191     if (!key_fname)
192     {
193         fprintf (stderr, "kdump [-c config] [-v log] file\n");
194         exit (1);
195     }
196     if (!res)
197         res = res_open ("zebra.cfg", 0);
198     zm = zebra_maps_open (res, 0);
199     if (!(inf = fopen (key_fname, "r")))
200     {
201         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", key_fname);
202         exit (1);
203     }
204     printf ("t  rg op  sysno seqno txt\n");
205     while (read_one (inf, key_string, key_info, &prevk))
206     {
207         struct it_key k;
208         int op;
209         char keybuf[IT_MAX_WORD+1];
210         char *to = keybuf;
211         const char *from = key_string;
212         int usedb_type = from[0];
213         int reg_type = from[1];
214
215         op = key_info[0];
216         memcpy (&k, 1+key_info, sizeof(k));
217
218         from += 2;  
219         while (*from)
220         {
221             const char *res = zebra_maps_output (zm, reg_type, &from);
222             if (!res)
223                 *to++ = *from++;
224             else
225                 while (*res)
226                     *to++ = *res++;
227         }
228         *to = '\0';
229         printf ("%c %3d %c %7d %5d %s\n", reg_type, usedb_type, op ? 'i':'d',
230                 k.sysno, k.seqno, keybuf);
231     }
232     zebra_maps_close (zm);
233     if (fclose (inf))
234     {
235         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", key_fname);
236         exit (1);
237     }
238     
239     exit (0);
240 }