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

SHOGUN Machine Learning Toolbox - Documentation