2 * Copyright (c) 1995-1997, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * NT Service interface by
7 * Chas Woodfield, Fretwell Downing Datasystems.
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.
14 * Revision 1.4 1997/09/17 12:10:43 adam
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.
22 * Revision 1.2 1997/09/04 13:50:31 adam
28 * Demonstration of simple server
40 Z_GenericRecord *read_grs1(FILE *f, ODR o);
42 bend_initresult *bend_init(bend_initrequest *q)
44 bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
45 static char *dummy = "Hej fister";
53 bend_searchresult *bend_search(void *handle, bend_searchrequest *q, int *fd)
55 bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
59 r->hits = rand() % 22;
64 static int atoin (const char *buf, int n)
70 val = val*10 + (*buf - '0');
76 char *marc_read(FILE *inf)
82 if (fread (length_str, 1, 5, inf) != 5)
84 size = atoin (length_str, 5);
87 if (!(buf = xmalloc (size+1)))
89 if (fread (buf+5, 1, size-5, inf) != (size-5))
94 memcpy (buf, length_str, 5);
99 static char *dummy_database_record (int num)
101 FILE *inf = fopen ("dummy-records", "r");
112 assert(buf = xmalloc(2101));
113 memset(buf, 'A', 2100);
118 buf = marc_read (inf);
128 static Z_GenericRecord *dummy_grs_record (int num, ODR o)
130 FILE *f = fopen("dummy-grs", "r");
132 Z_GenericRecord *r = 0;
137 while (fgets(line, 512, f))
138 if (*line == '#' && sscanf(line, "#%d", &n) == 1 && n == num)
147 bend_fetchresult *bend_fetch(void *handle, bend_fetchrequest *q, int *num)
149 bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
150 static char *bbb = 0;
154 r->basename = "DUMMY";
160 r->format = q->format;
161 if (q->format == VAL_SUTRS)
165 sprintf(buf, "This is dummy SUTRS record number %d\n", q->number);
166 assert(r->record = bbb = xmalloc(strlen(buf)+1));
168 r->len = strlen(buf);
170 else if (q->format == VAL_GRS1)
173 r->record = (char*) dummy_grs_record(q->number, q->stream);
180 else if (!(r->record = bbb = dummy_database_record(q->number)))
183 r->format = VAL_USMARC;
187 r->len = strlen(r->record);
192 bend_deleteresult *bend_delete(void *handle, bend_deleterequest *q, int *num)
198 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
200 static struct scan_entry list[200];
201 static char buf[200][200];
202 static bend_scanresult r;
205 r.term_position = q->term_position;
206 r.num_entries = q->num_entries;
208 for (i = 0; i < r.num_entries; i++)
210 list[i].term = buf[i];
211 sprintf(list[i].term, "term-%d", i+1);
212 list[i].occurrences = rand() % 100000;
220 * silly dummy-scan what reads words from a file.
222 bend_scanresult *bend_scan(void *handle, bend_scanrequest *q, int *num)
224 bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
226 static struct scan_entry list[200];
227 static char entries[200][80];
234 r->status = BEND_SCAN_SUCCESS;
235 if (!f && !(f = fopen("dummy-words", "r")))
237 perror("dummy-words");
240 if (q->term->term->which != Z_Term_general)
242 r->errcode = 229; /* unsupported term type */
245 if (q->term->term->u.general->len >= 80)
247 r->errcode = 11; /* term too long */
250 if (q->num_entries > 200)
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++)
263 for (i = 0, pos = 0; fscanf(f, " %79[^:]:%d", entries[pos], &hits[pos]) == 2;
264 i++, pos < 199 ? pos++ : (pos = 0))
266 if (!r->num_entries && strcmp(entries[pos], term) >= 0) /* s-point fnd */
268 if ((r->term_position = q->term_position) > i + 1)
270 r->term_position = i + 1;
271 r->status = BEND_SCAN_PARTIAL;
273 for (; r->num_entries < r->term_position; r->num_entries++)
277 po = pos - r->term_position + r->num_entries + 1; /* find pos */
280 list[r->num_entries].term = entries[po];
281 list[r->num_entries].occurrences = hits[po];
284 else if (r->num_entries)
286 list[r->num_entries].term = entries[pos];
287 list[r->num_entries].occurrences = hits[pos];
290 if (r->num_entries >= q->num_entries)
294 r->status = BEND_SCAN_PARTIAL;
300 void bend_close(void *handle)
305 int main(int argc, char **argv)
307 return statserv_main(argc, argv);