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