Happy new year.
[idzebra-moved-to-github.git] / index / kdump.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2011 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <stdlib.h>
24 #ifdef WIN32
25 #include <io.h>
26 #endif
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30 #include <assert.h>
31
32 #include <charmap.h>
33 #include "index.h"
34
35 char *prog;
36
37 int main(int argc, char **argv)
38 {
39     exit(0);
40 }
41
42 #if 0
43 /* old kdumper.. must be updated to use new codec .. */
44 int key_file_decode (FILE *f)
45 {
46     int c, d;
47
48     c = getc (f);
49     switch (c & 192) 
50     {
51     case 0:
52         d = c;
53         break;
54     case 64:
55         d = ((c&63) << 8) + (getc (f) & 0xff);
56         break;
57     case 128:
58         d = ((c&63) << 8) + (getc (f) & 0xff);
59         d = (d << 8) + (getc (f) & 0xff);
60         break;
61     case 192:
62         d = ((c&63) << 8) + (getc (f) & 0xff);
63         d = (d << 8) + (getc (f) & 0xff);
64         d = (d << 8) + (getc (f) & 0xff);
65         break;
66     default:
67         d = 0;
68         assert (0);
69     }
70     return d;
71 }
72
73
74 static int read_one (FILE *inf, char *name, char *key, struct it_key *prevk)
75 {
76     int c;
77     int i = 0;
78     struct it_key itkey;
79     do
80     {
81         if ((c=getc(inf)) == EOF)
82             return 0;
83         name[i++] = c;
84     } while (c);
85     if (i > 1)
86         prevk->sysno = 0;
87     c = key_file_decode (inf);
88     key[0] = c & 1;
89     c = c >> 1;
90     itkey.sysno = c + prevk->sysno;
91     if (c)
92     {
93         prevk->sysno = itkey.sysno;
94         prevk->seqno = 0;
95     }
96     c = key_file_decode (inf);
97     itkey.seqno = c + prevk->seqno;
98     prevk->seqno = itkey.seqno;
99
100     memcpy (key+1, &itkey, sizeof(itkey));
101     return 1;
102 }
103
104
105 int main (int argc, char **argv)
106 {
107     int ret;
108     char *arg;
109     char *key_fname = NULL;
110     char key_string[IT_MAX_WORD];
111     char key_info[256];
112     ZebraMaps zm;
113     FILE *inf;
114     Res res = NULL;
115     struct it_key prevk;
116
117     prevk.sysno = 0;
118     prevk.seqno = 0;
119
120     prog = *argv;
121     while ((ret = options ("c:v:", argv, argc, &arg)) != -2)
122     {
123         if (ret == 0)
124         {
125             key_fname = arg;
126         }
127         else if (ret == 'v')
128         {
129             yaz_log_init (yaz_log_mask_str(arg), prog, NULL);
130         }
131         else if (ret == 'c')
132         {
133             if (!(res = res_open (arg, 0, 0)))
134             {
135                 yaz_log(YLOG_FATAL, "Failed to open resource file %s", arg);
136                 exit (1);
137             }
138         }
139         else
140         {
141             yaz_log (YLOG_FATAL, "Unknown option '-%s'", arg);
142             exit (1);
143         }
144     }
145     if (!key_fname)
146     {
147         fprintf (stderr, "kdump [-c config] [-v log] file\n");
148         exit (1);
149     }
150     if (!res)
151         res = res_open ("zebra.cfg", 0, 0);
152     zm = zebra_maps_open (res, 0);
153     if (!(inf = fopen (key_fname, "r")))
154     {
155         yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", key_fname);
156         exit (1);
157     }
158     printf ("t  rg op  sysno seqno txt\n");
159     while (read_one (inf, key_string, key_info, &prevk))
160     {
161         struct it_key k;
162         int op;
163         char keybuf[IT_MAX_WORD+1];
164         char *to = keybuf;
165         const char *from = key_string;
166         int usedb_type = from[0];
167         int reg_type = from[1];
168
169         op = key_info[0];
170         memcpy (&k, 1+key_info, sizeof(k));
171
172         from += 2;  
173         while (*from)
174         {
175             const char *res = zebra_maps_output (zm, reg_type, &from);
176             if (!res)
177                 *to++ = *from++;
178             else
179                 while (*res)
180                     *to++ = *res++;
181         }
182         *to = '\0';
183         printf ("%c %3d %c %7d %5d %s\n", reg_type, usedb_type, op ? 'i':'d',
184                 k.sysno, k.seqno, keybuf);
185     }
186     zebra_maps_close (zm);
187     if (fclose (inf))
188     {
189         yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", key_fname);
190         exit (1);
191     }
192     exit (0);
193 }
194 #endif    
195 /*
196  * Local variables:
197  * c-basic-offset: 4
198  * c-file-style: "Stroustrup"
199  * indent-tabs-mode: nil
200  * End:
201  * vim: shiftwidth=4 tabstop=8 expandtab
202  */
203