Bug fix.
[yaz-moved-to-github.git] / ill / ill-get.c
1 /*
2  * Copyright (c) 1999-2000, Index Data.
3  * See the file LICENSE for details.
4  *
5  * $Log: ill-get.c,v $
6  * Revision 1.5  2000-02-24 08:52:01  adam
7  * Bug fix.
8  *
9  * Revision 1.4  2000/02/04 11:01:15  adam
10  * Added more elements.
11  *
12  * Revision 1.3  2000/01/31 13:15:21  adam
13  * Removed uses of assert(3). Cleanup of ODR. CCL parser update so
14  * that some characters are not surrounded by spaces in resulting term.
15  * ILL-code updates.
16  *
17  * Revision 1.2  2000/01/15 09:38:51  adam
18  * Implemented ill_get_ILLRequest. Added some type mappings for ILL protocol.
19  *
20  * Revision 1.1  1999/12/16 23:36:19  adam
21  * Implemented ILL protocol. Minor updates ASN.1 compiler.
22  *
23  */
24
25 #include <yaz/ill.h>
26
27 bool_t *ill_get_bool (struct ill_get_ctl *gc, const char *name,
28                       const char *sub, int val)
29 {
30     ODR o = gc->odr;
31     char element[128];
32     const char *v;
33     bool_t *r = odr_malloc (o, sizeof(*r));
34     
35     strcpy(element, name);
36     if (sub)
37     {
38         strcat (element, ",");
39         strcat (element, sub);
40     }    
41
42     v = (gc->f)(gc->clientData, element);
43     if (v)
44         val = atoi(v);
45     else if (val < 0)
46         return 0;
47     *r = val;
48     return r;
49 }
50
51 int *ill_get_int (struct ill_get_ctl *gc, const char *name,
52                   const char *sub, int val)
53 {
54     ODR o = gc->odr;
55     char element[128];
56     const char *v;
57     int *r = odr_malloc (o, sizeof(*r));
58     
59     strcpy(element, name);
60     if (sub)
61     {
62         strcat (element, ",");
63         strcat (element, sub);
64     }    
65     v = (gc->f)(gc->clientData, element);
66     if (v)
67         val = atoi(v);
68     *r = val;
69     return r;
70 }
71
72 int *ill_get_enumerated (struct ill_get_ctl *gc, const char *name,
73                          const char *sub, int val)
74 {
75     return ill_get_int(gc, name, sub, val);
76 }
77
78 ILL_String *ill_get_ILL_String (struct ill_get_ctl *gc, const char *name,
79                                 const char *sub)
80 {
81     ILL_String *r = (ILL_String *) odr_malloc (gc->odr, sizeof(*r));
82     char element[128];
83     const char *v;
84
85     strcpy(element, name);
86     if (sub)
87     {
88         strcat (element, ",");
89         strcat (element, sub);
90     }
91     v = (gc->f)(gc->clientData, element);
92     if (!v)
93         return 0;
94     r->which = ILL_String_GeneralString;
95     r->u.GeneralString = odr_strdup (gc->odr, v);
96     return r;
97 }
98
99
100 ILL_ISO_Date *ill_get_ILL_ISO_Date (struct ill_get_ctl *gc, const char *name,
101                                     const char *sub, const char *val)
102 {
103     char element[128];
104     const char *v;
105
106     strcpy(element, name);
107     if (sub)
108     {
109         strcat (element, ",");
110         strcat (element, sub);
111     }
112     v = (gc->f)(gc->clientData, element);
113     if (!v)
114         v = val;
115     if (!v)
116         return 0;
117     return odr_strdup (gc->odr, v);
118 }
119
120 ILL_ISO_Time *ill_get_ILL_ISO_Time (struct ill_get_ctl *gc, const char *name,
121                                     const char *sub, const char *val)
122 {
123     char element[128];
124     const char *v;
125
126     strcpy(element, name);
127     if (sub)
128     {
129         strcat (element, ",");
130         strcat (element, sub);
131     }
132     v = (gc->f)(gc->clientData, element);
133     if (!v)
134         v = val;
135     if (!v)
136         return 0;
137     return odr_strdup (gc->odr, v);
138 }
139
140 ILL_Person_Or_Institution_Symbol *ill_get_Person_Or_Insitution_Symbol (
141     struct ill_get_ctl *gc, const char *name, const char *sub)
142 {
143     char element[128];
144     ODR o = gc->odr;
145     ILL_Person_Or_Institution_Symbol *p = odr_malloc (o, sizeof(*p));
146     
147     strcpy(element, name);
148     if (sub)
149     {
150         strcat (element, ",");
151         strcat (element, sub);
152     }
153     p->which = ILL_Person_Or_Institution_Symbol_person_symbol;
154     if ((p->u.person_symbol = ill_get_ILL_String (gc, element, "person")))
155         return p;
156
157     p->which = ILL_Person_Or_Institution_Symbol_institution_symbol;
158     if ((p->u.institution_symbol =
159          ill_get_ILL_String (gc, element, "institution")))
160         return p;
161     return 0;
162 }
163
164 static ILL_Name_Of_Person_Or_Institution *ill_get_Name_Of_Person_Or_Institution(
165     struct ill_get_ctl *gc, const char *name, const char *sub)
166 {
167     char element[128];
168     ODR o = gc->odr;
169     ILL_Name_Of_Person_Or_Institution *p = odr_malloc (o, sizeof(*p));
170     
171     strcpy(element, name);
172     if (sub)
173     {
174         strcat (element, ",");
175         strcat (element, sub);
176     }
177     p->which = ILL_Name_Of_Person_Or_Institution_name_of_person;
178     if ((p->u.name_of_person =
179          ill_get_ILL_String (gc, element, "name-of-person")))
180         return p;
181
182     p->which = ILL_Name_Of_Person_Or_Institution_name_of_institution;
183     if ((p->u.name_of_institution =
184          ill_get_ILL_String (gc, element, "name-of-institution")))
185         return p;
186     return 0;
187 }
188     
189 ILL_System_Id *ill_get_System_Id(struct ill_get_ctl *gc,
190                                  const char *name, const char *sub)
191 {
192     ODR o = gc->odr;
193     char element[128];
194     ILL_System_Id *p;
195     
196     strcpy(element, name);
197     if (sub)
198     {
199         strcat (element, ",");
200         strcat (element, sub);
201     }
202     p = (ILL_System_Id *) odr_malloc (o, sizeof(*p));
203     p->person_or_institution_symbol = ill_get_Person_Or_Insitution_Symbol (
204         gc, element, "person-or-institution-symbol");
205     p->name_of_person_or_institution = ill_get_Name_Of_Person_Or_Institution (
206         gc, element, "name-of-person-or-institution");
207     return p;
208 }
209
210 ILL_Transaction_Id *ill_get_Transaction_Id (struct ill_get_ctl *gc,
211                                             const char *name, const char *sub)
212 {
213     ODR o = gc->odr;
214     ILL_Transaction_Id *r = (ILL_Transaction_Id *) odr_malloc (o, sizeof(*r));
215     char element[128];
216     
217     strcpy(element, name);
218     if (sub)
219     {
220         strcat (element, ",");
221         strcat (element, sub);
222     }    
223     r->initial_requester_id =
224         ill_get_System_Id (gc, element, "initial-requester-id");
225     r->transaction_group_qualifier =
226         ill_get_ILL_String (gc, element, "transaction-group-qualifier");
227     r->transaction_qualifier =
228         ill_get_ILL_String (gc, element, "transaction-qualifier");
229     r->sub_transaction_qualifier =
230         ill_get_ILL_String (gc, element, "sub-transaction-qualifier");
231     return r;
232 }
233
234
235 ILL_Service_Date_this *ill_get_Service_Date_this (
236     struct ill_get_ctl *gc, const char *name, const char *sub)
237 {
238     ODR o = gc->odr;
239     ILL_Service_Date_this *r =
240         (ILL_Service_Date_this *) odr_malloc (o, sizeof(*r));
241     char element[128];
242     
243     strcpy(element, name);
244     if (sub)
245     {
246         strcat (element, ",");
247         strcat (element, sub);
248     }
249     r->date = ill_get_ILL_ISO_Date (gc, element, "date", "20000101");
250     r->time = ill_get_ILL_ISO_Time (gc, element, "time", 0);
251     return r;
252 }
253
254 ILL_Service_Date_original *ill_get_Service_Date_original (
255     struct ill_get_ctl *gc, const char *name, const char *sub)
256 {
257     ODR o = gc->odr;
258     ILL_Service_Date_original *r =
259         (ILL_Service_Date_original *) odr_malloc (o, sizeof(*r));
260     char element[128];
261     
262     strcpy(element, name);
263     if (sub)
264     {
265         strcat (element, ",");
266         strcat (element, sub);
267     }
268     r->date = ill_get_ILL_ISO_Date (gc, element, "date", 0);
269     r->time = ill_get_ILL_ISO_Time (gc, element, "time", 0);
270     if (!r->date && !r->time)
271         return 0;
272     return r;
273 }
274
275 ILL_Service_Date_Time *ill_get_Service_Date_Time (
276     struct ill_get_ctl *gc, const char *name, const char *sub)
277 {
278     ODR o = gc->odr;
279     ILL_Service_Date_Time *r =
280         (ILL_Service_Date_Time *) odr_malloc (o, sizeof(*r));
281     char element[128];
282     
283     strcpy(element, name);
284     if (sub)
285     {
286         strcat (element, ",");
287         strcat (element, sub);
288     }    
289     r->date_time_of_this_service = ill_get_Service_Date_this (
290         gc, element, "this");
291     r->date_time_of_original_service = ill_get_Service_Date_original (
292         gc, element, "original");
293     return r;
294 }
295
296 ILL_Requester_Optional_Messages_Type *ill_get_Requester_Optional_Messages_Type (
297     struct ill_get_ctl *gc, const char *name, const char *sub)
298 {
299     ODR o = gc->odr;
300     ILL_Requester_Optional_Messages_Type *r =
301         (ILL_Requester_Optional_Messages_Type *) odr_malloc (o, sizeof(*r));
302     char element[128];
303     
304     strcpy(element, name);
305     if (sub)
306     {
307         strcat (element, ",");
308         strcat (element, sub);
309     }
310     r->can_send_RECEIVED = ill_get_bool (gc, element, "can-send-RECEIVED", 0);
311     r->can_send_RETURNED = ill_get_bool (gc, element, "can-send-RETURNED", 0);
312     r->requester_SHIPPED =
313         ill_get_enumerated (gc, element, "requester-SHIPPED", 1);
314     r->requester_CHECKED_IN =
315         ill_get_enumerated (gc, element, "requester-CHECKED-IN", 1);
316     return r;
317 }
318
319 ILL_Item_Id *ill_get_Item_Id (
320     struct ill_get_ctl *gc, const char *name, const char *sub)   
321 {
322     ODR o = gc->odr;
323     ILL_Item_Id *r = (ILL_Item_Id *) odr_malloc (o, sizeof(*r));
324     char element[128];
325     
326     strcpy(element, name);
327     if (sub)
328     {
329         strcat (element, ",");
330         strcat (element, sub);
331     }
332     r->item_type = ill_get_enumerated (gc, element, "item-type",
333                                        ILL_Item_Id_monograph);
334     r->held_medium_type = 0;
335     r->call_number = ill_get_ILL_String(gc, element, "call-number");
336     r->author = ill_get_ILL_String(gc, element, "author");
337     r->title = ill_get_ILL_String(gc, element, "title");
338     r->sub_title = ill_get_ILL_String(gc, element, "sub-title");
339     r->sponsoring_body = ill_get_ILL_String(gc, element, "sponsoring-body");
340     r->place_of_publication =
341         ill_get_ILL_String(gc, element, "place-of-publication");
342     r->publisher = ill_get_ILL_String(gc, element, "publisher");
343     r->series_title_number =
344         ill_get_ILL_String(gc, element, "series-title-number");
345     r->volume_issue = ill_get_ILL_String(gc, element, "volume-issue");
346     r->edition = ill_get_ILL_String(gc, element, "edition");
347     r->publication_date = ill_get_ILL_String(gc, element, "publication-date");
348     r->publication_date_of_component =
349         ill_get_ILL_String(gc, element, "publication-date-of-component");
350     r->author_of_article = ill_get_ILL_String(gc, element,
351                                               "author-of-article");
352     r->title_of_article = ill_get_ILL_String(gc, element, "title-or-article");
353     r->pagination = ill_get_ILL_String(gc, element, "pagination");
354     r->national_bibliography_no = 0;
355     r->iSBN = ill_get_ILL_String(gc, element, "ISBN");
356     r->iSSN = ill_get_ILL_String(gc, element, "ISSN");
357     r->system_no = 0;
358     r->additional_no_letters =
359         ill_get_ILL_String(gc, element, "additional-no-letters");
360     r->verification_reference_source = 
361         ill_get_ILL_String(gc, element, "verification-reference-source");
362     return r;
363 }
364
365 ILL_ItemRequest *ill_get_ItemRequest (
366     struct ill_get_ctl *gc, const char *name, const char *sub)
367 {
368     ODR o = gc->odr;
369     ILL_ItemRequest *r = (ILL_ItemRequest *)odr_malloc(o, sizeof(*r));
370     return 0;
371 }
372
373
374 ILL_Client_Id *ill_get_Client_Id (
375     struct ill_get_ctl *gc, const char *name, const char *sub)
376 {
377     char element[128];
378     ODR o = gc->odr;
379     ILL_Client_Id *r = (ILL_Client_Id *) odr_malloc(o, sizeof(*r));
380
381     strcpy(element, name);
382     if (sub)
383     {
384         strcat (element, ",");
385         strcat (element, sub);
386     }
387     r->client_name = ill_get_ILL_String (gc, element, "client-name");
388     r->client_status = ill_get_ILL_String (gc, element, "client-status");
389     r->client_identifier = ill_get_ILL_String (gc, element,
390                                                "client-identifier");
391     return r;
392 }
393
394 ILL_Postal_Address *ill_get_Postal_Address (
395     struct ill_get_ctl *gc, const char *name, const char *sub)
396 {
397     ODR o = gc->odr;
398     ILL_Postal_Address *r =
399         (ILL_Postal_Address *) odr_malloc(o, sizeof(*r));
400     char element[128];
401
402     strcpy(element, name);
403     if (sub)
404     {
405         strcat (element, ",");
406         strcat (element, sub);
407     }
408     r->name_of_person_or_institution = 
409         ill_get_Name_Of_Person_Or_Institution (
410             gc, element, "name-of-person-or-institution");
411     r->extended_postal_delivery_address =
412         ill_get_ILL_String (
413             gc, element, "extended-postal-delivery-address");
414     r->street_and_number =
415         ill_get_ILL_String (gc, element, "street-and-number");
416     r->post_office_box =
417         ill_get_ILL_String (gc, element, "post-office-box");
418     r->city = ill_get_ILL_String (gc, element, "city");
419     r->region = ill_get_ILL_String (gc, element, "region");
420     r->country = ill_get_ILL_String (gc, element, "country");
421     r->postal_code = ill_get_ILL_String (gc, element, "postal-code");
422     return r;
423 }
424
425 ILL_System_Address *ill_get_System_Address (
426     struct ill_get_ctl *gc, const char *name, const char *sub)
427 {
428     ODR o = gc->odr;
429     ILL_System_Address *r =
430         (ILL_System_Address *) odr_malloc(o, sizeof(*r));
431     char element[128];
432     
433     strcpy(element, name);
434     if (sub)
435     {
436         strcat (element, ",");
437         strcat (element, sub);
438     }
439     r->telecom_service_identifier =
440         ill_get_ILL_String (gc, element, "telecom-service-identifier");
441     r->telecom_service_address =
442         ill_get_ILL_String (gc, element, "telecom-service-addreess");
443     return r;
444 }
445
446 ILL_Delivery_Address *ill_get_Delivery_Address (
447     struct ill_get_ctl *gc, const char *name, const char *sub)
448 {
449     ODR o = gc->odr;
450     ILL_Delivery_Address *r =
451         (ILL_Delivery_Address *) odr_malloc(o, sizeof(*r));
452     char element[128];
453     
454     strcpy(element, name);
455     if (sub)
456     {
457         strcat (element, ",");
458         strcat (element, sub);
459     }
460     r->postal_address =
461         ill_get_Postal_Address (gc, element, "postal-address");
462     r->electronic_address =
463         ill_get_System_Address (gc, element, "electronic-address");
464     return r;
465 }
466
467 ILL_Search_Type *ill_get_Search_Type (
468     struct ill_get_ctl *gc, const char *name, const char *sub)
469 {
470     ODR o = gc->odr;
471     ILL_Search_Type *r = (ILL_Search_Type *) odr_malloc(o, sizeof(*r));
472     char element[128];
473     
474     strcpy(element, name);
475     if (sub)
476     {
477         strcat (element, ",");
478         strcat (element, sub);
479     }
480     r->level_of_service = ill_get_ILL_String (gc, element, "level-of-service");
481     r->need_before_date = ill_get_ILL_ISO_Date (gc, element,
482                                                 "need-before-date", 0);
483     r->expiry_date = ill_get_ILL_ISO_Date (gc, element, "expiry-date", 0);
484     r->expiry_flag = ill_get_enumerated (gc, element, "expiry-flag", 3);
485                                          
486     return r;
487 }
488
489 ILL_Request *ill_get_ILLRequest (
490     struct ill_get_ctl *gc, const char *name, const char *sub)
491 {
492     ODR o = gc->odr;
493     ILL_Request *r = (ILL_Request *) odr_malloc(o, sizeof(*r));
494     char element[128];
495     
496     strcpy(element, name);
497     if (sub)
498     {
499         strcat (element, ",");
500         strcat (element, sub);
501     }
502     r->protocol_version_num =
503         ill_get_enumerated (gc, element, "protocol-version-num", 
504                             ILL_Request_version_2);
505     
506     r->transaction_id = ill_get_Transaction_Id (gc, element, "transaction-id");
507     r->service_date_time =
508         ill_get_Service_Date_Time (gc, element, "service-date-time");
509     r->requester_id = ill_get_System_Id (gc, element, "requester-id");
510     r->responder_id = ill_get_System_Id (gc, element, "responder-id");
511     r->transaction_type =
512         ill_get_enumerated(gc, element, "transaction-type", 1);
513
514     r->delivery_address =
515         ill_get_Delivery_Address (gc, element, "delivery-address");
516     r->delivery_service = 0; /* TODO */
517     /* ill_get_Delivery_Service (gc, element, "delivery-service"); */
518     r->billing_address =
519         ill_get_Delivery_Address (gc, element, "billing-address");
520
521     r->num_iLL_service_type = 1;
522     r->iLL_service_type = (ILL_Service_Type **)
523         odr_malloc (o, sizeof(*r->iLL_service_type));
524     *r->iLL_service_type =
525         ill_get_enumerated (gc, element, "ill-service-type",
526                             ILL_Service_Type_copy_non_returnable);
527
528     r->responder_specific_service = 0;
529     r->requester_optional_messages =
530         ill_get_Requester_Optional_Messages_Type (
531             gc, element,"requester-optional-messages");
532     r->search_type = 0;            /* TODO */
533     r->num_supply_medium_info_type = 0;
534     r->supply_medium_info_type = 0;
535
536     r->place_on_hold = ill_get_enumerated (
537         gc, element, "place-on-hold", 
538         ILL_Place_On_Hold_Type_according_to_responder_policy);
539     r->client_id = ill_get_Client_Id (gc, element, "client-id");
540                            
541     r->item_id = ill_get_Item_Id (gc, element, "item-id");
542     r->supplemental_item_description = 0;
543     r->cost_info_type = 0;
544     r->copyright_compliance =
545         ill_get_ILL_String(gc, element, "copyright-complicance");
546     r->third_party_info_type = 0;
547     r->retry_flag = ill_get_bool (gc, element, "retry-flag", 0);
548     r->forward_flag = ill_get_bool (gc, element, "forward-flag", 0);
549     r->requester_note = ill_get_ILL_String(gc, element, "requester-note");
550     r->forward_note = ill_get_ILL_String(gc, element, "forward-note");
551     r->num_iLL_request_extensions = 0;
552     r->iLL_request_extensions = 0;
553     return r;
554 }