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