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