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