4c3a60544bf477b65233b8dc4a170975c78ccde0
[idzebra-moved-to-github.git] / recctrl / recctrl.c
1 /* $Id: recctrl.c,v 1.6 2002-08-02 19:26:56 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23
24 #include <stdio.h>
25 #include <assert.h>
26 #include <string.h>
27
28 #include <zebrautl.h>
29 #include "rectext.h"
30 #include "recgrs.h"
31
32 struct recTypeEntry {
33     RecType recType;
34     struct recTypeEntry *next;
35     int init_flag;
36     void *clientData;
37 };
38
39 struct recTypes {
40     data1_handle dh;
41     struct recTypeEntry *entries;
42 };
43
44 RecTypes recTypes_init (data1_handle dh)
45 {
46     RecTypes p = (RecTypes) nmem_malloc (data1_nmem_get (dh), sizeof(*p));
47
48     p->dh = dh;
49     p->entries = 0;
50     return p;
51 }
52
53 void recTypes_destroy (RecTypes rts)
54 {
55     struct recTypeEntry *rte;
56
57     for (rte = rts->entries; rte; rte = rte->next)
58         if (rte->init_flag)
59             (*(rte->recType)->destroy)(rte->clientData);
60 }
61
62 void recTypes_add_handler (RecTypes rts, RecType rt)
63 {
64     struct recTypeEntry *rte;
65
66     rte = (struct recTypeEntry *)
67         nmem_malloc (data1_nmem_get (rts->dh), sizeof(*rte));
68
69     rte->recType = rt;
70     rte->init_flag = 0;
71     rte->clientData = 0;
72     rte->next = rts->entries;
73     rts->entries = rte;
74 }
75
76 RecType recType_byName (RecTypes rts, const char *name, char *subType,
77                         void **clientDataP)
78 {
79     struct recTypeEntry *rte;
80     char *p;
81     char tmpname[256];
82
83     strcpy (tmpname, name);
84     if ((p = strchr (tmpname, '.')))
85     {
86         *p = '\0';
87         strcpy (subType, p+1);
88     }
89     else
90         *subType = '\0';
91     for (rte = rts->entries; rte; rte = rte->next)
92         if (!strcmp (rte->recType->name, tmpname))
93         {
94             if (!rte->init_flag)
95             {
96                 rte->init_flag = 1;
97                 rte->clientData =
98                     (*(rte->recType)->init)(rte->recType);
99             }
100             *clientDataP = rte->clientData;
101             return rte->recType;
102         }
103     return 0;
104 }
105
106 void recTypes_default_handlers (RecTypes rts)
107 {
108     recTypes_add_handler (rts, recTypeGrs);
109     recTypes_add_handler (rts, recTypeText);
110 }