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