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