summaryrefslogtreecommitdiffstats
path: root/xsd2c/obj.c
blob: 804cf4f3c074ed04620a895fd39bdaf489236be2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/******************************************************************
 *  $Id: obj.c,v 1.2 2004/06/02 14:57:23 snowdrop Exp $
 *
 * CSOAP Project:  A SOAP client/server library in C
 * Copyright (C) 2003  Ferhat Ayaz
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA  02111-1307, USA.
 * 
 * Email: ayaz@jprogrammer.net
 ******************************************************************/
#include "obj.h"
#include "tr.h"

#include <stdio.h>
#include <string.h>

struct tagObjRegistry
{
  HCOMPLEXTYPE head;
  HCOMPLEXTYPE tail;
}objRegistry;


static HFIELD fieldCreate(const char* name, const char* type, int flag, int mino, int maxo, HCOMPLEXTYPE parentObj);
static void fieldFree(HFIELD field);
static void objAddField(HCOMPLEXTYPE obj, HFIELD field);
static void objFreeComplexType(HCOMPLEXTYPE obj);
static void objRegistryAddComplexType(HCOMPLEXTYPE obj);

static char *targetNS = NULL;

/* ------------------------------------------------------------------ */

void objInitModule(const char* tns)
{
  objRegistry.head = NULL;
  objRegistry.tail = NULL;

  if (tns != NULL) {
    targetNS = (char*)malloc(strlen(tns)+1);
    strcpy(targetNS, tns);
  } 
}

void objFreeModule()
{
  HCOMPLEXTYPE cur;

  cur = objRegistry.head;
  while (cur != NULL)
  {
    objFreeComplexType(cur);
    cur = cur->next;
  }

  free(targetNS);
}


HCOMPLEXTYPE objCreateComplexType(const char* typename)
{
  HCOMPLEXTYPE ct;
  char buf[1054];

  ct = (HCOMPLEXTYPE)malloc(sizeof(struct COMPLEXTYPE));
  ct->type = (char*)malloc(strlen(typename)+1);
  strcpy(ct->type, typename);
  ct->head = ct->tail = NULL;
  ct->next = NULL;
  ct->base_type = NULL;

  objRegistryAddComplexType(ct);
  
  sprintf(buf, "struct %s*", typename);
  trRegisterTypeNS(targetNS, typename, buf);
  sprintf(buf, "struct %s_List*", typename);
  trRegisterListTypeNS(targetNS, typename, buf);
  return ct;
}

HFIELD objAddElement(HCOMPLEXTYPE obj, const char* name, const char* type, int flag, int mino, int maxo)
{
  HFIELD field;

  field = fieldCreate(name, type, flag, mino, maxo, obj);
  objAddField(obj, field);

  return field;
}

HFIELD objAddAttribute(HCOMPLEXTYPE obj, const char* name, const char* type, int flag)
{
  char buffer[1054];
  HFIELD field;

  sprintf(buffer, "attr_%s", name);

  field = fieldCreate(buffer, type, flag, 1, 1, obj);
  field->attrName = (char*)malloc(strlen(name)+1);
  strcpy(field->attrName, name);

  objAddField(obj, field);

  return field;
}

void objSetBaseType(HCOMPLEXTYPE obj, const char* typename)
{
  if (obj->base_type != NULL) free(obj->base_type);
  obj->base_type = (char*)malloc(strlen(typename)+1);
  strcpy(obj->base_type, typename);
}

HCOMPLEXTYPE objRegistryGetComplexType(const char* typename)
{
  return NULL;
}


void objRegistryEnumComplexType(CT_ENUM callback)
{
  HCOMPLEXTYPE cur;

  cur = objRegistry.head;
  while (cur != NULL)
  {
    if (!callback(cur)) return;
    cur = cur->next;
  }
}


/* ========================================================================= */

static
HFIELD fieldCreate(const char* name, const char* type, int flag, int mino, int maxo, HCOMPLEXTYPE parentObj)
{
  HFIELD field;

  field = (HFIELD)malloc(sizeof(struct FIELD));
  field->name = (char*)malloc(strlen(name)+1);
  field->type = (char*)malloc(strlen(type)+1);
  field->flag = flag;
  field->next = NULL;
  field->flag = 0;
  field->minOccurs = mino;
  field->maxOccurs = maxo;
  field->parentObj = parentObj;
  field->attrName = NULL;

  strcpy(field->name, name);
  strcpy(field->type, type);
  
  return field;
}


static 
void fieldFree(HFIELD field)
{
  if (field)
  {
    if (field->name) free(field->name);
    if (field->type) free(field->type);
    if (field->attrName) free(field->attrName);
    if (field->next) fieldFree(field->next);
    
    free(field);
  }
}


static 
void objAddField(HCOMPLEXTYPE obj, HFIELD field)
{
  if (obj->tail)
  {
    obj->tail->next = field;
  }

  if (!obj->head)
  {
    obj->head = field;
  }

  obj->tail = field;
}


static 
void objFreeComplexType(HCOMPLEXTYPE obj)
{
  if (obj)
  {
    if (obj->type) free(obj->type);
    if (obj->base_type) free(obj->base_type);
    if (obj->head) fieldFree(obj->head);
    
    free(obj);
  }
}


static void objRegistryAddComplexType(HCOMPLEXTYPE obj)
{
  if (objRegistry.tail)
  {
    objRegistry.tail->next = obj;
  }

  if (!objRegistry.head)
  {
    objRegistry.head = obj;
  }

  objRegistry.tail = obj;
}