You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

268 lines
5.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /******************************************************************
  2. * $Id: obj.c,v 1.4 2004/10/15 13:35:39 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@jprogrammer.net
  23. ******************************************************************/
  24. #include "obj.h"
  25. #include "tr.h"
  26. #include "util.h"
  27. #include <stdio.h>
  28. #include <string.h>
  29. struct tagObjRegistry
  30. {
  31. HCOMPLEXTYPE head;
  32. HCOMPLEXTYPE tail;
  33. }objRegistry;
  34. static HFIELD fieldCreate(const char* name, const char* type, int flag, int mino, int maxo, HCOMPLEXTYPE parentObj);
  35. static void fieldFree(HFIELD field);
  36. static void objAddField(HCOMPLEXTYPE obj, HFIELD field);
  37. static void objFreeComplexType(HCOMPLEXTYPE obj);
  38. static void objRegistryAddComplexType(HCOMPLEXTYPE obj);
  39. static char *targetNS = NULL;
  40. static int _objInitialized = 0;
  41. /* ------------------------------------------------------------------ */
  42. void objInitModule(const char* tns)
  43. {
  44. if (tns != NULL) {
  45. targetNS = (char*)malloc(strlen(tns)+1);
  46. strcpy(targetNS, tns);
  47. }
  48. if (_objInitialized) return;
  49. objRegistry.head = NULL;
  50. objRegistry.tail = NULL;
  51. _objInitialized = 1;
  52. }
  53. void objFreeModule()
  54. {
  55. HCOMPLEXTYPE cur;
  56. cur = objRegistry.head;
  57. while (cur != NULL)
  58. {
  59. objFreeComplexType(cur);
  60. cur = cur->next;
  61. }
  62. free(targetNS);
  63. }
  64. HCOMPLEXTYPE objCreateComplexType(const char* typename)
  65. {
  66. HCOMPLEXTYPE ct;
  67. char buf[1054];
  68. ct = (HCOMPLEXTYPE)malloc(sizeof(struct COMPLEXTYPE));
  69. ct->type = (char*)malloc(strlen(typename)+1);
  70. strcpy(ct->type, typename);
  71. ct->head = ct->tail = NULL;
  72. ct->next = NULL;
  73. ct->base_type = NULL;
  74. ct->restriction = NULL;
  75. ct->isSimpleContent = 0;
  76. strcpy(ct->targetNS, targetNS);
  77. sprintf(ct->fullName, "%s:%s", targetNS, typename);
  78. objRegistryAddComplexType(ct);
  79. sprintf(buf, "struct %s*", typename);
  80. trRegisterTypeNS(targetNS, typename, buf, 0);
  81. sprintf(buf, "struct %s_List*", typename);
  82. trRegisterListTypeNS(targetNS, typename, buf);
  83. return ct;
  84. }
  85. HFIELD objAddElement(HCOMPLEXTYPE obj, const char* name, const char* type, int flag, int mino, int maxo)
  86. {
  87. HFIELD field;
  88. field = fieldCreate(name, type, flag, mino, maxo, obj);
  89. objAddField(obj, field);
  90. return field;
  91. }
  92. HFIELD objAddAttribute(HCOMPLEXTYPE obj, const char* name, const char* type, int flag)
  93. {
  94. char buffer[1054];
  95. HFIELD field;
  96. sprintf(buffer, "attr_%s", name);
  97. field = fieldCreate(buffer, type, flag, 1, 1, obj);
  98. field->attrName = (char*)malloc(strlen(name)+1);
  99. strcpy(field->attrName, name);
  100. objAddField(obj, field);
  101. return field;
  102. }
  103. void objSetBaseType(HCOMPLEXTYPE obj, const char* typename)
  104. {
  105. if (obj->base_type != NULL) free(obj->base_type);
  106. obj->base_type = (char*)malloc(strlen(typename)+1);
  107. strcpy(obj->base_type, typename);
  108. }
  109. HCOMPLEXTYPE objRegistryGetComplexType(const char* typename)
  110. {
  111. HCOMPLEXTYPE cur;
  112. cur = objRegistry.head;
  113. while (cur != NULL)
  114. {
  115. if (!strcmp(cur->fullName, typename))
  116. return cur;
  117. cur = cur->next;
  118. }
  119. return NULL;
  120. }
  121. void objRegistryEnumComplexType(CT_ENUM callback)
  122. {
  123. HCOMPLEXTYPE cur;
  124. cur = objRegistry.head;
  125. while (cur != NULL)
  126. {
  127. if (!callback(cur)) return;
  128. cur = cur->next;
  129. }
  130. }
  131. /* ========================================================================= */
  132. static
  133. HFIELD fieldCreate(const char* name, const char* type, int flag, int mino, int maxo, HCOMPLEXTYPE parentObj)
  134. {
  135. HFIELD field;
  136. field = (HFIELD)malloc(sizeof(struct FIELD));
  137. field->name = (char*)malloc(strlen(name)+1);
  138. field->type = (char*)malloc(strlen(type)+1);
  139. field->flag = flag;
  140. field->next = NULL;
  141. field->flag = 0;
  142. field->minOccurs = mino;
  143. field->maxOccurs = maxo;
  144. field->parentObj = parentObj;
  145. field->attrName = NULL;
  146. strcpy(field->name, name);
  147. strcpy(field->type, type);
  148. return field;
  149. }
  150. static
  151. void fieldFree(HFIELD field)
  152. {
  153. if (field)
  154. {
  155. if (field->name) free(field->name);
  156. if (field->type) free(field->type);
  157. if (field->attrName) free(field->attrName);
  158. if (field->next) fieldFree(field->next);
  159. free(field);
  160. }
  161. }
  162. static
  163. void objAddField(HCOMPLEXTYPE obj, HFIELD field)
  164. {
  165. printf("ADD FIELD %s %s\n", obj->type, field->type);
  166. if (obj->tail)
  167. {
  168. obj->tail->next = field;
  169. }
  170. if (!obj->head)
  171. {
  172. obj->head = field;
  173. }
  174. obj->tail = field;
  175. }
  176. static
  177. void objFreeComplexType(HCOMPLEXTYPE obj)
  178. {
  179. if (obj)
  180. {
  181. if (obj->type) free(obj->type);
  182. if (obj->base_type) free(obj->base_type);
  183. if (obj->head) fieldFree(obj->head);
  184. free(obj);
  185. }
  186. }
  187. static void objRegistryAddComplexType(HCOMPLEXTYPE obj)
  188. {
  189. if (objRegistry.tail)
  190. {
  191. objRegistry.tail->next = obj;
  192. }
  193. if (!objRegistry.head)
  194. {
  195. objRegistry.head = obj;
  196. }
  197. objRegistry.tail = obj;
  198. }
  199. HRESTRICTION resCreate(const char *type)
  200. {
  201. HRESTRICTION res = (HRESTRICTION)malloc(sizeof(struct RESTRICTION));
  202. res->mode = RES_MODE_EMPTY;
  203. res->type = (char*)malloc(strlen(type)+1);
  204. strcpy(res->type, type);
  205. res->minInclusiveSet = 0;
  206. res->maxInclusiveSet = 0;
  207. res->minExclusiveSet = 0;
  208. res->maxExclusiveSet = 0;
  209. res->enumeration = Enumeration_Create();
  210. return res;
  211. }