1 /******************************************************************
2  *  $Id: tr.c,v 1.5 2005/05/27 19:28:16 snowdrop Exp $
3  *
4  * CSOAP Project:  A SOAP client/server library in C
5  * Copyright (C) 2003  Ferhat Ayaz
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA  02111-1307, USA.
21  *
22  * Email: ayaz@jprogrammet.net
23  ******************************************************************/
24 
25 #include "tr.h"
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 
30 #define XSD2C_MAP(xsdtype, ctype, builtin) \
31   trRegisterType(xsdtype, ctype, builtin);
32 
33 #define XSD2C_MAPNS(xsdtype, ctype, builtin) \
34   trRegisterTypeNS(trXSDNS, xsdtype, ctype, builtin);
35 
36 #define XSD2C_MAPNS_LIST(xsdtype, ctype) \
37   trRegisterListTypeNS(trXSDNS, xsdtype, ctype);
38 
39 struct XSD2C_TypeRegistry
40 {
41   char *xsd_type;
42   char *c_type;
43   int isbuildin;
44   struct XSD2C_TypeRegistry* next;
45 };
46 
47 static struct XSD2C_TypeRegistry* tr_head;
48 static struct XSD2C_TypeRegistry* tr_tail;
49 static struct XSD2C_TypeRegistry* trl_head;
50 static struct XSD2C_TypeRegistry* trl_tail;
51 
52 
53 static char trXSDNS[15];
54 static int _trInitialized = 0;
55 static char* _trC2XSD(const char* cType, struct XSD2C_TypeRegistry* head);
56 static char* _trXSD2C(const char* xsdType, struct XSD2C_TypeRegistry* head);
57 
58 
59 /*void trInitModule(const char* ns)*/
trInitModule()60 void trInitModule()
61 {
62   if (_trInitialized)
63    return;
64 /*  struct XSD2C_TypeRegistry* cur;*/
65   tr_head = NULL;
66   tr_tail = NULL;
67   trl_head = NULL;
68   trl_tail = NULL;
69 /*
70   strcpy(trXSDNS, ns);
71 
72   #include "types.map"
73 
74   cur = tr_head;
75 
76   while (cur != NULL)
77   {
78     cur->isbuildin = 1;
79     cur = cur->next;
80   }
81 */
82   _trInitialized  = 1;
83 };
84 
85 
trFreeModule()86 void trFreeModule()
87 {
88   struct XSD2C_TypeRegistry* cur, *tmp;
89 
90   cur = tr_head;
91 
92   while (cur != NULL)
93   {
94     if (cur->xsd_type) free(cur->xsd_type);
95     if (cur->c_type) free(cur->c_type);
96     tmp = cur->next;
97     free(cur);
98     cur = tmp;
99   }
100   cur = trl_head;
101 
102   while (cur != NULL)
103   {
104     if (cur->xsd_type) free(cur->xsd_type);
105     if (cur->c_type) free(cur->c_type);
106     tmp = cur->next;
107     free(cur);
108     cur = tmp;
109   }
110 }
111 
112 
trGetBuildInFlag(const char * xsdType)113 int trGetBuildInFlag(const char* xsdType)
114 {
115   struct XSD2C_TypeRegistry* cur;
116 
117   cur = tr_head;
118 
119   while (cur != NULL)
120   {
121     if (!strcmp(cur->xsd_type, xsdType))
122     {
123       return cur->isbuildin;
124     }
125     cur = cur->next;
126   }
127   return -1;
128 }
129 
130 
trC2XSD(const char * cType)131 char* trC2XSD(const char* cType)
132 {
133   return _trC2XSD(cType, tr_head);
134 }
135 
trXSD2C(const char * xsdType)136 char* trXSD2C(const char* xsdType)
137 {
138   return _trXSD2C(xsdType, tr_head);
139 }
140 
trC2XSDList(const char * cType)141 char* trC2XSDList(const char* cType)
142 {
143   return _trC2XSD(cType, trl_head);
144 }
145 
trXSD2CList(const char * xsdType)146 char* trXSD2CList(const char* xsdType)
147 {
148   return _trXSD2C(xsdType, trl_head);
149 }
150 
trRegisterType(const char * xsdType,const char * cType,int builtin)151 void trRegisterType(const char* xsdType, const char* cType, int builtin)
152 {
153   trRegisterTypeNS(NULL, xsdType, cType, builtin);
154 }
155 
trRegisterTypeNS(const char * ns,const char * xsdType,const char * cType,int builtin)156 void trRegisterTypeNS(const char* ns, const char* xsdType, const char* cType, int builtin)
157 {
158   struct XSD2C_TypeRegistry* reg;
159   if (xsdType == NULL || cType == NULL)
160   {
161     fprintf(stderr, "WARNING: Can not register type\n");
162     return;
163   }
164 
165   reg = (struct XSD2C_TypeRegistry*)malloc(sizeof(struct XSD2C_TypeRegistry));
166   reg->xsd_type = (char*)malloc((ns?strlen(ns):0)+strlen(xsdType)+2);
167   reg->c_type = (char*)malloc(strlen(cType)+1);
168   reg->next = NULL;
169   reg->isbuildin = builtin;
170 
171   if (ns)
172     sprintf(reg->xsd_type, "%s:%s",ns,xsdType);
173   else
174     strcpy(reg->xsd_type, xsdType);
175 
176   strcpy(reg->c_type, cType);
177   printf("[TYPE] Registered '%s'->'%s'\n", reg->xsd_type, reg->c_type);
178 
179   if (tr_tail)
180   {
181     tr_tail->next = reg;
182   }
183 
184   if (tr_head == NULL)
185   {
186     tr_head = reg;
187   }
188 
189   tr_tail = reg;
190 }
191 
trXSDParseNs(char * xsdType)192 char* trXSDParseNs(char* xsdType)
193 {
194 	int c = 0;
195 	while (xsdType[c] != '\0' ) {
196 		if (xsdType[c] == ':') return &xsdType[++c];
197 		c++;
198 	}
199 	return xsdType;
200 }
201 
trRegisterListType(const char * xsdType,const char * cType)202 void trRegisterListType(const char* xsdType, const char* cType)
203 {
204   trRegisterListTypeNS(NULL, xsdType, cType);
205 }
206 
trRegisterListTypeNS(const char * ns,const char * xsdType,const char * cType)207 void trRegisterListTypeNS(const char* ns, const char* xsdType, const char* cType)
208 {
209   struct XSD2C_TypeRegistry* reg;
210   if (xsdType == NULL || cType == NULL)
211   {
212     fprintf(stderr, "WARNING: Can not register type\n");
213     return;
214   }
215 
216   reg = (struct XSD2C_TypeRegistry*)malloc(sizeof(struct XSD2C_TypeRegistry));
217   reg->xsd_type = (char*)malloc((ns?strlen(ns):0)+strlen(xsdType)+2);
218   reg->c_type = (char*)malloc(strlen(cType)+1);
219   reg->next = NULL;
220   reg->isbuildin = 0;
221 
222   if (ns)
223     sprintf(reg->xsd_type, "%s:%s",ns,xsdType);
224   else
225     strcpy(reg->xsd_type, xsdType);
226 
227   strcpy(reg->c_type, cType);
228 
229   if (trl_tail)
230   {
231     trl_tail->next = reg;
232   }
233 
234   if (trl_head == NULL)
235   {
236     trl_head = reg;
237   }
238 
239   trl_tail = reg;
240 }
241 
242 
243 static
_trC2XSD(const char * cType,struct XSD2C_TypeRegistry * head)244 char* _trC2XSD(const char* cType, struct XSD2C_TypeRegistry* head)
245 {
246   struct XSD2C_TypeRegistry* cur;
247 
248   printf("[TYPE] Search: '%s'   ", cType?cType:"null");
249   cur = head;
250 
251   while (cur != NULL)
252   {
253     if (!strcmp(cur->c_type, cType))
254     {
255       printf("FOUND\n");
256       return cur->xsd_type;
257     }
258     cur = cur->next;
259   }
260 
261       printf("NOT FOUND\n");
262   return NULL;
263 }
264 
_trXSD2C(const char * xsdType,struct XSD2C_TypeRegistry * head)265 static char* _trXSD2C(const char* xsdType, struct XSD2C_TypeRegistry* head)
266 {
267   struct XSD2C_TypeRegistry* cur;
268 
269   cur = head;
270 
271   while (cur != NULL)
272   {
273   /*  printf("%s\n", cur->xsd_type);*/
274     if (!strcmp(cur->xsd_type, xsdType))
275     {
276       return cur->c_type;
277     }
278     cur = cur->next;
279   }
280 
281   return NULL;
282 }
283