Avoid crash if facet use attribute is not supplied
[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 /* session handle . Put anything you like in here! */
21 struct my_handle {
22     int counter;
23 };
24
25 static int my_search(void *handle, bend_search_rr *rr)
26 {
27     struct my_handle *my_p = (struct my_handle *) handle;
28
29     if (rr->num_bases != 1)
30     {
31         rr->errcode = YAZ_BIB1_COMBI_OF_SPECIFIED_DATABASES_UNSUPP;
32         return 0;
33     }
34     /* Throw Database unavailable if other than Default */
35     if (!yaz_matchstr (rr->basenames[0], "Default"))
36         ;  /* Default is OK in our test */
37     else
38     {
39         rr->errcode = YAZ_BIB1_DATABASE_UNAVAILABLE;
40         rr->errstring = rr->basenames[0];
41         return 0;
42     }
43
44     rr->hits = 123; /* dummy hit count */
45     my_p->counter++;
46     return 0;
47 }
48
49 /* retrieval of a single record (present, and piggy back search) */
50 static int my_fetch(void *handle, bend_fetch_rr *r)
51 {
52     const Odr_oid *oid = r->request_format;
53
54     r->last_in_set = 0;
55     r->basename = "Default";
56     r->output_format = r->request_format;
57
58     /* if no record syntax was given assume XML */
59     if (!oid || !oid_oidcmp(oid, yaz_oid_recsyn_xml))
60     {
61         char buf[40];
62         yaz_snprintf(buf, sizeof(buf), "<record>%d</record>\n", r->number);
63
64         r->output_format = odr_oiddup(r->stream, yaz_oid_recsyn_xml);
65         r->record = odr_strdup(r->stream, buf);
66         r->len = strlen(r->record);
67     }
68     else
69     {   /* only xml syntax supported . Return diagnostic */
70         char buf[OID_STR_MAX];
71         r->errcode = YAZ_BIB1_RECORD_SYNTAX_UNSUPP;
72         r->errstring = odr_strdup(r->stream, oid_oid_to_dotstring(oid, buf));
73     }
74     return 0;
75 }
76
77 static bend_initresult *my_init(bend_initrequest *q)
78 {
79     bend_initresult *r = (bend_initresult *)
80         odr_malloc (q->stream, sizeof(*r));
81     struct my_handle *my_p = (struct my_handle *) xmalloc (sizeof(*my_p));
82
83     my_p->counter = 0;
84     r->errcode = 0;
85     r->errstring = 0;
86     r->handle = my_p;            /* user handle */
87     q->bend_search = my_search;  /* register search handler */
88     q->bend_fetch = my_fetch;    /* register fetch handle */
89     q->query_charset = "UTF-8";
90     q->records_in_same_charset = 1;
91
92     return r;
93 }
94
95 static void my_close(void *handle)
96 {
97     xfree(handle);              /* release our user-defined handle */
98     return;
99 }
100
101 int main(int argc, char **argv)
102 {
103     return statserv_main(argc, argv, my_init, my_close);
104 }
105 /*
106  * Local variables:
107  * c-basic-offset: 4
108  * c-file-style: "Stroustrup"
109  * indent-tabs-mode: nil
110  * End:
111  * vim: shiftwidth=4 tabstop=8 expandtab
112  */
113