SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableXmlReader00.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 
11 #include <shogun/lib/config.h>
12 #ifdef HAVE_XML
13 
15 
16 using namespace shogun;
17 
18 SerializableXmlReader00::SerializableXmlReader00(
19  CSerializableXmlFile* file) { m_file = file; }
20 
21 SerializableXmlReader00::~SerializableXmlReader00() {}
22 
23 bool
24 SerializableXmlReader00::read_scalar_wrapped(
25  const TSGDataType* type, void* param)
26 {
27  xmlNode* m = m_file->m_stack_stream.back();
28 
29  bool result = true;
30  xmlChar* xml_buf;
31  if ((xml_buf = xmlNodeGetContent(m)) == NULL) return false;
32  const char* buf = (const char*) xml_buf;
33 
34  switch (type->m_ptype) {
35  case PT_BOOL:
36  string_t bool_buf;
37 
38  if (sscanf(buf, "%"STRING_LEN_STR"s", bool_buf) != 1)
39  result = false;
40 
41  if (strcmp(buf, STR_TRUE) == 0) *(bool*) param = true;
42  else if (strcmp(buf, STR_FALSE) == 0) *(bool*) param = false;
43  else result = false;
44 
45  break;
46  case PT_CHAR:
47  if (sscanf(buf, "%c", (char*) param) != 1)
48  result = false;
49  break;
50  case PT_INT8:
51  if (sscanf(buf, "%"SCNi8, (int8_t*) param) != 1)
52  result = false;
53  break;
54  case PT_UINT8:
55  if (sscanf(buf, "%"SCNu8, (uint8_t*) param) != 1)
56  result = false;
57  break;
58  case PT_INT16:
59  if (sscanf(buf, "%"SCNi16, (int16_t*) param) != 1)
60  result = false;
61  break;
62  case PT_UINT16:
63  if (sscanf(buf, "%"SCNu16, (uint16_t*) param) != 1)
64  result = false;
65  break;
66  case PT_INT32:
67  if (sscanf(buf, "%"SCNi32, (int32_t*) param) != 1)
68  result = false;
69  break;
70  case PT_UINT32:
71  if (sscanf(buf, "%"SCNu32, (uint32_t*) param) != 1)
72  result = false;
73  break;
74  case PT_INT64:
75  if (sscanf(buf, "%"SCNi64, (int64_t*) param) != 1)
76  result = false;
77  break;
78  case PT_UINT64:
79  if (sscanf(buf, "%"SCNu64, (uint64_t*) param) != 1)
80  result = false;
81  break;
82  case PT_FLOAT32:
83  if (sscanf(buf, "%g", (float32_t*) param) != 1)
84  result = false;
85  break;
86  case PT_FLOAT64:
87  if (sscanf(buf, "%lg", (float64_t*) param) != 1)
88  result = false;
89  break;
90  case PT_FLOATMAX:
91  if (sscanf(buf, "%Lg", (floatmax_t*) param) != 1)
92  result = false;
93  break;
94  case PT_SGOBJECT:
95  SG_ERROR("read_scalar_wrapped(): Implementation error during"
96  " reading XmlFile!");
97  result = false;
98  }
99 
100  xmlFree(xml_buf);
101  return result;
102 }
103 
104 bool
105 SerializableXmlReader00::read_cont_begin_wrapped(
106  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
107 {
108  xmlNode* m = m_file->m_stack_stream.back();
109 
110  switch (type->m_ctype) {
111  case CT_NDARRAY:
113  case CT_SCALAR: break;
114  case CT_VECTOR: case CT_SGVECTOR:
115  *len_read_y = xmlChildElementCount(m);
116  break;
117  case CT_MATRIX: case CT_SGMATRIX:
118  *len_read_x = xmlChildElementCount(m);
119 
120  for (xmlNode* cur=m->children; cur != NULL; cur=cur->next) {
121  if (cur->type != XML_ELEMENT_NODE) continue;
122 
123  if (*len_read_y == 0)
124  *len_read_y = xmlChildElementCount(cur);
125 
126  if (*len_read_y != (index_t) xmlChildElementCount(cur))
127  return false;
128  }
129 
130  break;
131  }
132 
133  return true;
134 }
135 
136 bool
137 SerializableXmlReader00::read_cont_end_wrapped(
138  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
139 {
140  if (len_read_y > 0) m_file->pop_node();
141 
142  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
143  {
144  if (len_read_y*len_read_x>0)
145  m_file->pop_node();
146  }
147 
148  return true;
149 }
150 
151 bool
152 SerializableXmlReader00::read_string_begin_wrapped(
153  const TSGDataType* type, index_t* length)
154 {
155  xmlNode* m = m_file->m_stack_stream.back();
156 
157  *length = xmlChildElementCount(m);
158 
159  return true;
160 }
161 
162 bool
163 SerializableXmlReader00::read_string_end_wrapped(
164  const TSGDataType* type, index_t length)
165 {
166  if (length > 0) m_file->pop_node();
167 
168  return true;
169 }
170 
171 bool
172 SerializableXmlReader00::read_stringentry_begin_wrapped(
173  const TSGDataType* type, index_t y)
174 {
175  if (y == 0) {
176  if (!m_file->join_node(BAD_CAST STR_STRING)) return false;
177  return true;
178  }
179 
180  if (!m_file->next_node(BAD_CAST STR_STRING)) return false;
181 
182  return true;
183 }
184 
185 bool
186 SerializableXmlReader00::read_stringentry_end_wrapped(
187  const TSGDataType* type, index_t y)
188 {
189  return true;
190 }
191 
192 bool
193 SerializableXmlReader00::read_sparse_begin_wrapped(
194  const TSGDataType* type, index_t* length)
195 {
196  return true;
197 }
198 
199 bool
200 SerializableXmlReader00::read_sparse_end_wrapped(
201  const TSGDataType* type, index_t length)
202 {
203  if (length > 0) m_file->pop_node();
204 
205  return true;
206 }
207 
208 bool
209 SerializableXmlReader00::read_sparseentry_begin_wrapped(
210  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
211  index_t* feat_index, index_t y)
212 {
213  bool result = true;
214  xmlChar* buf;
215 
216  if (y == 0) {
217  if (!m_file->join_node(BAD_CAST STR_SPARSE)) return false;
218  } else {
219  if (!m_file->next_node(BAD_CAST STR_SPARSE)) return false;
220  }
221 
222  if ((buf = xmlGetProp(m_file->m_stack_stream.back(), BAD_CAST
223  STR_PROP_FEATINDEX)) == NULL) return false;
224  if (sscanf((const char*) buf, "%"PRIi32, feat_index) != 1)
225  result = false;
226  xmlFree(buf); if (!result) return false;
227 
228  return true;
229 }
230 
231 bool
232 SerializableXmlReader00::read_sparseentry_end_wrapped(
233  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
234  index_t* feat_index, index_t y)
235 {
236  return true;
237 }
238 
239 bool
240 SerializableXmlReader00::read_item_begin_wrapped(
241  const TSGDataType* type, index_t y, index_t x)
242 {
243  switch (type->m_ctype) {
244  case CT_NDARRAY:
246  case CT_SCALAR: break;
247  case CT_VECTOR: case CT_SGVECTOR:
248  if (y == 0) {
249  if (!m_file->join_node(BAD_CAST STR_ITEM)) return false;
250  return true;
251  }
252  break;
253  case CT_MATRIX: case CT_SGMATRIX:
254  if (y==0)
255  {
256  if (x != 0) { m_file->pop_node(); m_file->pop_node(); }
257 
258  string_t buf_x; snprintf(buf_x, STRING_LEN, "x%"PRIi32, x);
259  if (!m_file->join_node(BAD_CAST buf_x)) return false;
260  if (!m_file->join_node(BAD_CAST STR_ITEM)) return false;
261  return true;
262  }
263  break;
264  }
265 
266  if (!m_file->next_node(BAD_CAST STR_ITEM)) return false;
267 
268  return true;
269 }
270 
271 bool
272 SerializableXmlReader00::read_item_end_wrapped(
273  const TSGDataType* type, index_t y, index_t x)
274 {
275  return true;
276 }
277 
278 bool
279 SerializableXmlReader00::read_sgserializable_begin_wrapped(
280  const TSGDataType* type, char* sgserializable_name,
281  EPrimitiveType* generic)
282 {
283  xmlNode* m = m_file->m_stack_stream.back();
284  xmlChar* buf;
285 
286  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) {
287  xmlFree(buf);
288  *sgserializable_name = '\0';
289  return true;
290  }
291 
292  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL)
293  return false;
294  strncpy(sgserializable_name, (const char*) buf, STRING_LEN);
295  xmlFree(buf);
296 
297  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME))
298  != NULL) {
299  if (!TSGDataType::string_to_ptype(generic, (const char*) buf))
300  return false;
301  xmlFree(buf);
302  }
303 
304  return true;
305 }
306 
307 bool
308 SerializableXmlReader00::read_sgserializable_end_wrapped(
309  const TSGDataType* type, const char* sgserializable_name,
310  EPrimitiveType generic)
311 {
312  return true;
313 }
314 
315 bool
316 SerializableXmlReader00::read_type_begin_wrapped(
317  const TSGDataType* type, const char* name, const char* prefix)
318 {
319  bool result = true;
320 
322 
323  if (!m_file->join_node(BAD_CAST name)) return false;
324 
325  string_t buf; type->to_string(buf, STRING_LEN);
326  xmlChar* t;
327  if ((t = xmlGetProp(m_file->m_stack_stream.back(),
328  BAD_CAST STR_PROP_TYPE)) == NULL) return false;
329  if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false;
330  xmlFree(t); if (!result) return false;
331 
332  return true;
333 }
334 
335 bool
336 SerializableXmlReader00::read_type_end_wrapped(
337  const TSGDataType* type, const char* name, const char* prefix)
338 {
339  m_file->pop_node();
340 
342 
343  return true;
344 }
345 
346 #endif /* HAVE_XML */

SHOGUN Machine Learning Toolbox - Documentation