cd4359b4b39fd59755b08b4aa550a991a10ba7da
[idzebra-moved-to-github.git] / recctrl / recctrl.c
1 /*
2  * Copyright (C) 1994-1998, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: recctrl.c,v $
7  * Revision 1.3  1998-10-16 08:14:36  adam
8  * Updated record control system.
9  *
10  * Revision 1.2  1996/10/29 14:03:16  adam
11  * Include zebrautl.h instead of alexutil.h.
12  *
13  * Revision 1.1  1996/10/11 10:57:24  adam
14  * New module recctrl. Used to manage records (extract/retrieval).
15  *
16  * Revision 1.5  1996/06/04 10:18:59  adam
17  * Minor changes - removed include of ctype.h.
18  *
19  * Revision 1.4  1995/12/04  17:59:24  adam
20  * More work on regular expression conversion.
21  *
22  * Revision 1.3  1995/12/04  14:22:30  adam
23  * Extra arg to recType_byName.
24  * Started work on new regular expression parsed input to
25  * structured records.
26  *
27  * Revision 1.2  1995/11/15  14:46:19  adam
28  * Started work on better record management system.
29  *
30  * Revision 1.1  1995/09/27  12:22:28  adam
31  * More work on extract in record control.
32  * Field name is not in isam keys but in prefix in dictionary words.
33  *
34  */
35 #include <stdio.h>
36 #include <assert.h>
37 #include <string.h>
38
39 #include <zebrautl.h>
40 #include "rectext.h"
41 #include "recgrs.h"
42
43 struct recTypeEntry {
44     RecType recType;
45     struct recTypeEntry *next;
46     int init_flag;
47 };
48
49 struct recTypes {
50     data1_handle dh;
51     struct recTypeEntry *entries;
52 };
53
54 RecTypes recTypes_init (data1_handle dh)
55 {
56     RecTypes p = (RecTypes) nmem_malloc (data1_nmem_get (dh), sizeof(*p));
57
58     p->dh = dh;
59     p->entries = 0;
60     return p;
61 }
62
63 void recTypes_destroy (RecTypes rts)
64 {
65     struct recTypeEntry *rte;
66
67     for (rte = rts->entries; rte; rte = rte->next)
68         if (rte->init_flag)
69             (*(rte->recType)->destroy)(rte->recType);
70 }
71
72 void recTypes_add_handler (RecTypes rts, RecType rt)
73 {
74     struct recTypeEntry *rte;
75
76     rte = nmem_malloc (data1_nmem_get (rts->dh), sizeof(*rte));
77
78     rte->recType = rt;
79     rte->init_flag = 0;
80     rte->next = rts->entries;
81     rts->entries = rte;
82 }
83
84 RecType recType_byName (RecTypes rts, const char *name, char *subType)
85 {
86     struct recTypeEntry *rte;
87     char *p;
88     char tmpname[256];
89
90     strcpy (tmpname, name);
91     if ((p = strchr (tmpname, '.')))
92     {
93         *p = '\0';
94         strcpy (subType, p+1);
95     }
96     else
97         *subType = '\0';
98     for (rte = rts->entries; rte; rte = rte->next)
99         if (!strcmp (rte->recType->name, tmpname))
100         {
101             if (!rte->init_flag)
102             {
103                 rte->init_flag = 1;
104                 (*(rte->recType)->init)(rte->recType);
105             }
106             return rte->recType;
107         }
108     return 0;
109 }
110
111 void recTypes_default_handlers (RecTypes rts)
112 {
113     recTypes_add_handler (rts, recTypeGrs);
114     recTypes_add_handler (rts, recTypeText);
115 }