Fixed Makefile(s).
[yaz-moved-to-github.git] / ztest / ztest.c
1 /*
2  * Copyright (c) 1995-1997, Index Data.
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * NT Service interface by
7  *    Chas Woodfield, Fretwell Downing Datasystems.
8  *
9  * $Log: ztest.c,v $
10  * Revision 1.5  1997-11-07 13:31:58  adam
11  * Added NT Service name part of statserv_options_block. Moved NT
12  * service utility to server library.
13  *
14  * Revision 1.4  1997/09/17 12:10:43  adam
15  * YAZ version 1.4.
16  *
17  * Revision 1.3  1997/09/09 10:10:20  adam
18  * Another MSV5.0 port. Changed projects to include proper
19  * library/include paths.
20  * Server starts server in test-mode when no options are given.
21  *
22  * Revision 1.2  1997/09/04 13:50:31  adam
23  * Bug fix in ztest.
24  *
25  */
26
27 /*
28  * Demonstration of simple server
29  */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <ctype.h>
34 #include <assert.h>
35
36 #include <backend.h>
37 #include <xmalloc.h>
38 #include <proto.h>
39
40 Z_GenericRecord *read_grs1(FILE *f, ODR o);
41
42 bend_initresult *bend_init(bend_initrequest *q)
43 {
44     bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
45     static char *dummy = "Hej fister";
46
47     r->errcode = 0;
48     r->errstring = 0;
49     r->handle = dummy;
50     return r;
51 }
52
53 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
54 {
55     bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
56
57     r->errcode = 0;
58     r->errstring = 0;
59     r->hits = rand() % 22;
60
61     return r;
62 }
63
64 static int atoin (const char *buf, int n)
65 {
66     int val = 0;
67     while (--n >= 0)
68     {
69         if (isdigit(*buf))
70             val = val*10 + (*buf - '0');
71         buf++;
72     }
73     return val;
74 }
75
76 char *marc_read(FILE *inf)
77 {
78     char length_str[5];
79     size_t size;
80     char *buf;
81
82     if (fread (length_str, 1, 5, inf) != 5)
83         return NULL;
84     size = atoin (length_str, 5);
85     if (size <= 6)
86         return NULL;
87     if (!(buf = xmalloc (size+1)))
88         return NULL;
89     if (fread (buf+5, 1, size-5, inf) != (size-5))
90     {
91         xfree (buf);
92         return NULL;
93     }
94     memcpy (buf, length_str, 5);
95     buf[size] = '\0';
96     return buf;
97 }
98
99 static char *dummy_database_record (int num)
100 {
101     FILE *inf = fopen ("dummy-records", "r");
102     char *buf = 0;
103
104     if (!inf)
105         return NULL;
106     while (--num >= 0)
107     {
108         if (buf)
109            xfree(buf);
110         if (num == 98)
111         {
112             assert(buf = xmalloc(2101));
113             memset(buf, 'A', 2100);
114             buf[2100] = '\0';
115             break;
116         }
117         else
118             buf = marc_read (inf);
119         if (!num || !buf)
120             break;
121     }
122     fclose(inf);
123     if (num < 0)
124         return 0;
125     return buf;
126 }
127
128 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
129 {
130     FILE *f = fopen("dummy-grs", "r");
131     char line[512];
132     Z_GenericRecord *r = 0;
133     int n;
134
135     if (!f)
136         return 0;
137     while (fgets(line, 512, f))
138         if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
139         {
140             r = read_grs1(f, o);
141             break;
142         }
143     fclose(f);
144     return r;
145 }
146
147 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
148 {
149     bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
150     static char *bbb = 0;
151
152     r->errstring = 0;
153     r->last_in_set = 0;
154     r->basename = "DUMMY";
155     if (bbb)
156     {
157         xfree(bbb);
158         bbb = 0;
159     }
160     r->format = q->format;  
161     if (q->format == VAL_SUTRS)
162     {
163         char buf[100];
164
165         sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
166         assert(r->record = bbb = xmalloc(strlen(buf)+1));
167         strcpy(bbb, buf);
168         r->len = strlen(buf);
169     }
170     else if (q->format == VAL_GRS1)
171     {
172         r->len = -1;
173         r->record = (char*) dummy_grs_record(q->number, q->stream);
174         if (!r->record)
175         {
176             r->errcode = 13;
177             return r;
178         }
179     }
180     else if (!(r->record = bbb = dummy_database_record(q->number)))
181     {
182         r->errcode = 13;
183         r->format = VAL_USMARC;
184         return r;
185     }
186     else
187         r->len = strlen(r->record);
188     r->errcode = 0;
189     return r;
190 }
191
192 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
193 {
194     return 0;
195 }
196
197 #if 0
198 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
199 {
200     static struct scan_entry list[200];
201     static char buf[200][200];
202     static bend_scanresult r;
203     int i;
204
205     r.term_position = q->term_position;
206     r.num_entries = q->num_entries;
207     r.entries = list;
208     for (i = 0; i < r.num_entries; i++)
209     {
210         list[i].term = buf[i];
211         sprintf(list[i].term, "term-%d", i+1);
212         list[i].occurrences = rand() % 100000;
213     }
214     r.errcode = 0;
215     r.errstring = 0;
216     return &r;
217 }
218 #else
219 /*
220  * silly dummy-scan what reads words from a file.
221  */
222 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
223 {
224     bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
225     static FILE *f = 0;
226     static struct scan_entry list[200];
227     static char entries[200][80];
228     int hits[200];
229     char term[80], *p;
230     int i, pos;
231
232     r->errstring = 0;
233     r->entries = list;
234     r->status = BEND_SCAN_SUCCESS;
235     if (!f && !(f = fopen("dummy-words", "r")))
236     {
237         perror("dummy-words");
238         exit(1);
239     }
240     if (q->term->term->which != Z_Term_general)
241     {
242         r->errcode = 229; /* unsupported term type */
243         return r;
244     }
245     if (q->term->term->u.general->len >= 80)
246     {
247         r->errcode = 11; /* term too long */
248         return r;
249     }
250     if (q->num_entries > 200)
251     {
252         r->errcode = 31;
253         return r;
254     }
255     memcpy(term, q->term->term->u.general->buf, q->term->term->u.general->len);
256     term[q->term->term->u.general->len] = '\0';
257     for (p = term; *p; p++)
258         if (islower(*p))
259             *p = toupper(*p);
260
261     fseek(f, 0, 0);
262     r->num_entries = 0;
263     for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
264         i++, pos < 199 ? pos++ : (pos = 0))
265     {
266         if (!r->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
267         {
268             if ((r->term_position = q->term_position) > i + 1)
269             {
270                 r->term_position = i + 1;
271                 r->status = BEND_SCAN_PARTIAL;
272             }
273             for (; r->num_entries < r->term_position; r->num_entries++)
274             {
275                 int po;
276
277                 po = pos - r->term_position + r->num_entries + 1; /* find pos */
278                 if (po < 0)
279                     po += 200;
280                 list[r->num_entries].term = entries[po];
281                 list[r->num_entries].occurrences = hits[po];
282             }
283         }
284         else if (r->num_entries)
285         {
286             list[r->num_entries].term = entries[pos];
287             list[r->num_entries].occurrences = hits[pos];
288             r->num_entries++;
289         }
290         if (r->num_entries >= q->num_entries)
291             break;
292     }
293     if (feof(f))
294         r->status = BEND_SCAN_PARTIAL;
295     return r;
296 }
297
298 #endif
299
300 void bend_close(void *handle)
301 {
302     return;
303 }
304
305 int main(int argc, char **argv)
306 {
307     return statserv_main(argc, argv);
308 }