Update configure to generate config.h
[yaz-moved-to-github.git] / ztest / gfs-example.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2010 Index Data
3  * See the file LICENSE for details.
4  */
5 /** \file
6  * \brief Demonstration of Generic Frontend Server API
7  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <ctype.h>
15
16 #include <yaz/log.h>
17 #include <yaz/backend.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/matchstr.h>
20 #include <yaz/snprintf.h>
21
22 /* session handle . Put anything you like in here! */
23 struct my_handle {
24     int counter;
25 };
26
27 static int my_search(void *handle, bend_search_rr *rr)
28 {
29     struct my_handle *my_p = (struct my_handle *) handle;
30
31     if (rr->num_bases != 1)
32     {
33         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
34         return 0;
35     }
36     /* Throw Database unavailable if other than Default */
37     if (!yaz_matchstr (rr->basenames[0], "Default"))
38         ;  /* Default is OK in our test */
39     else
40     {
41         rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
42         rr->errstring = rr->basenames[0];
43         return 0;
44     }
45
46     rr->hits = 123; /* dummy hit count */
47     my_p->counter++;
48     return 0;
49 }
50
51 /* retrieval of a single record (present, and piggy back search) */
52 static int my_fetch(void *handle, bend_fetch_rr *r)
53 {
54     const Odr_oid *oid = r->request_format;
55
56     r->last_in_set = 0;
57     r->basename = "Default";
58     r->output_format = r->request_format;
59
60     /* if no record syntax was given assume XML */
61     if (!oid || !oid_oidcmp(oid, yaz_oid_recsyn_xml))
62     {
63         char buf[40];
64         yaz_snprintf(buf, sizeof(buf), "<record>%d</record>\n", r->number);
65
66         r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_xml);
67         r->record = odr_strdup(r->stream, buf);
68         r->len = strlen(r->record);
69     }
70     else
71     {   /* only xml syntax supported . Return diagnostic */
72         char buf[OID_STR_MAX];
73         r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
74         r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
75     }
76     return 0;
77 }
78
79 static bend_initresult *my_init(bend_initrequest *q)
80 {
81     bend_initresult *r = (bend_initresult *)
82         odr_malloc (q->stream, sizeof(*r));
83     struct my_handle *my_p = (struct my_handle *) xmalloc (sizeof(*my_p));
84
85     my_p->counter = 0;
86     r->errcode = 0;
87     r->errstring = 0;
88     r->handle = my_p;            /* user handle */
89     q->bend_search = my_search;  /* register search handler */
90     q->bend_fetch = my_fetch;    /* register fetch handle */
91     q->query_charset = "UTF-8";
92     q->records_in_same_charset = 1;
93
94     return r;
95 }
96
97 static void my_close(void *handle)
98 {
99     xfree(handle);              /* release our user-defined handle */
100     return;
101 }
102
103 int main(int argc, char **argv)
104 {
105     return statserv_main(argc, argv, my_init, my_close);
106 }
107 /*
108  * Local variables:
109  * c-basic-offset: 4
110  * c-file-style: "Stroustrup"
111  * indent-tabs-mode: nil
112  * End:
113  * vim: shiftwidth=4 tabstop=8 expandtab
114  */
115