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