SHOGUN  v3.1.1
 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 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11) || defined(_LIBCPP_VERSION)
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_UNDEFINED:
108  case PT_SGOBJECT:
109  SG_ERROR("read_scalar_wrapped(): Implementation error during"
110  " reading XmlFile!");
111  result = false;
112  }
113 
114  xmlFree(xml_buf);
115  return result;
116 }
117 
118 bool
119 SerializableXmlReader00::read_cont_begin_wrapped(
120  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
121 {
122  xmlNode* m = m_file->m_stack_stream.back();
123 
124  switch (type->m_ctype) {
125  case CT_NDARRAY:
127  case CT_SCALAR: break;
128  case CT_VECTOR: case CT_SGVECTOR:
129  *len_read_y = xmlChildElementCount(m);
130  break;
131  case CT_MATRIX: case CT_SGMATRIX:
132  *len_read_x = xmlChildElementCount(m);
133 
134  for (xmlNode* cur=m->children; cur != NULL; cur=cur->next) {
135  if (cur->type != XML_ELEMENT_NODE) continue;
136 
137  if (*len_read_y == 0)
138  *len_read_y = xmlChildElementCount(cur);
139 
140  if (*len_read_y != (index_t) xmlChildElementCount(cur))
141  return false;
142  }
143  break;
144  case CT_UNDEFINED:
145  SG_ERROR("type undefined\n");
146  }
147 
148  return true;
149 }
150 
151 bool
152 SerializableXmlReader00::read_cont_end_wrapped(
153  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
154 {
155  if (len_read_y > 0) m_file->pop_node();
156 
157  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
158  {
159  if (len_read_y*len_read_x>0)
160  m_file->pop_node();
161  }
162 
163  return true;
164 }
165 
166 bool
167 SerializableXmlReader00::read_string_begin_wrapped(
168  const TSGDataType* type, index_t* length)
169 {
170  xmlNode* m = m_file->m_stack_stream.back();
171 
172  *length = xmlChildElementCount(m);
173 
174  return true;
175 }
176 
177 bool
178 SerializableXmlReader00::read_string_end_wrapped(
179  const TSGDataType* type, index_t length)
180 {
181  if (length > 0) m_file->pop_node();
182 
183  return true;
184 }
185 
186 bool
187 SerializableXmlReader00::read_stringentry_begin_wrapped(
188  const TSGDataType* type, index_t y)
189 {
190  if (y == 0) {
191  if (!m_file->join_node(BAD_CAST STR_STRING)) return false;
192  return true;
193  }
194 
195  if (!m_file->next_node(BAD_CAST STR_STRING)) return false;
196 
197  return true;
198 }
199 
200 bool
201 SerializableXmlReader00::read_stringentry_end_wrapped(
202  const TSGDataType* type, index_t y)
203 {
204  return true;
205 }
206 
207 bool
208 SerializableXmlReader00::read_sparse_begin_wrapped(
209  const TSGDataType* type, index_t* length)
210 {
211  return true;
212 }
213 
214 bool
215 SerializableXmlReader00::read_sparse_end_wrapped(
216  const TSGDataType* type, index_t length)
217 {
218  if (length > 0) m_file->pop_node();
219 
220  return true;
221 }
222 
223 bool
224 SerializableXmlReader00::read_sparseentry_begin_wrapped(
225  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
226  index_t* feat_index, index_t y)
227 {
228  bool result = true;
229  xmlChar* buf;
230 
231  if (y == 0) {
232  if (!m_file->join_node(BAD_CAST STR_SPARSE)) return false;
233  } else {
234  if (!m_file->next_node(BAD_CAST STR_SPARSE)) return false;
235  }
236 
237  if ((buf = xmlGetProp(m_file->m_stack_stream.back(), BAD_CAST
238  STR_PROP_FEATINDEX)) == NULL) return false;
239  if (sscanf((const char*) buf, "%" PRIi32, feat_index) != 1)
240  result = false;
241  xmlFree(buf); if (!result) return false;
242 
243  return true;
244 }
245 
246 bool
247 SerializableXmlReader00::read_sparseentry_end_wrapped(
248  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
249  index_t* feat_index, index_t y)
250 {
251  return true;
252 }
253 
254 bool
255 SerializableXmlReader00::read_item_begin_wrapped(
256  const TSGDataType* type, index_t y, index_t x)
257 {
258  switch (type->m_ctype) {
259  case CT_NDARRAY:
261  case CT_SCALAR: break;
262  case CT_VECTOR: case CT_SGVECTOR:
263  if (y == 0) {
264  if (!m_file->join_node(BAD_CAST STR_ITEM)) return false;
265  return true;
266  }
267  break;
268  case CT_MATRIX: case CT_SGMATRIX:
269  if (y==0)
270  {
271  if (x != 0) { m_file->pop_node(); m_file->pop_node(); }
272 
273  string_t buf_x; snprintf(buf_x, STRING_LEN, "x%" PRIi32, x);
274  if (!m_file->join_node(BAD_CAST buf_x)) return false;
275  if (!m_file->join_node(BAD_CAST STR_ITEM)) return false;
276  return true;
277  }
278  break;
279  case CT_UNDEFINED:
280  SG_ERROR("type undefined\n");
281  }
282 
283  if (!m_file->next_node(BAD_CAST STR_ITEM)) return false;
284 
285  return true;
286 }
287 
288 bool
289 SerializableXmlReader00::read_item_end_wrapped(
290  const TSGDataType* type, index_t y, index_t x)
291 {
292  return true;
293 }
294 
295 bool
296 SerializableXmlReader00::read_sgserializable_begin_wrapped(
297  const TSGDataType* type, char* sgserializable_name,
298  EPrimitiveType* generic)
299 {
300  xmlNode* m = m_file->m_stack_stream.back();
301  xmlChar* buf;
302 
303  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) {
304  xmlFree(buf);
305  *sgserializable_name = '\0';
306  return true;
307  }
308 
309  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL)
310  return false;
311  strncpy(sgserializable_name, (const char*) buf, STRING_LEN);
312  xmlFree(buf);
313 
314  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME))
315  != NULL) {
316  if (!TSGDataType::string_to_ptype(generic, (const char*) buf))
317  return false;
318  xmlFree(buf);
319  }
320 
321  return true;
322 }
323 
324 bool
325 SerializableXmlReader00::read_sgserializable_end_wrapped(
326  const TSGDataType* type, const char* sgserializable_name,
327  EPrimitiveType generic)
328 {
329  return true;
330 }
331 
332 bool
333 SerializableXmlReader00::read_type_begin_wrapped(
334  const TSGDataType* type, const char* name, const char* prefix)
335 {
336  bool result = true;
337 
339 
340  if (!m_file->join_node(BAD_CAST name)) return false;
341 
342  string_t buf; type->to_string(buf, STRING_LEN);
343  xmlChar* t;
344  if ((t = xmlGetProp(m_file->m_stack_stream.back(),
345  BAD_CAST STR_PROP_TYPE)) == NULL) return false;
346  if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false;
347  xmlFree(t); if (!result) return false;
348 
349  return true;
350 }
351 
352 bool
353 SerializableXmlReader00::read_type_end_wrapped(
354  const TSGDataType* type, const char* name, const char* prefix)
355 {
356  m_file->pop_node();
357 
359 
360  return true;
361 }
362 
363 #endif /* HAVE_XML */
#define SG_RESET_LOCALE
Definition: SGIO.h:88
std::complex< float64_t > complex128_t
Definition: common.h:65
#define STRING_LEN_STR
Definition: common.h:54
int32_t index_t
Definition: common.h:60
static bool string_to_ptype(EPrimitiveType *ptype, const char *str)
Definition: DataType.cpp:392
#define SG_ERROR(...)
Definition: SGIO.h:131
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:141
#define SG_SET_LOCALE_C
Definition: SGIO.h:87
Datatypes that shogun supports.
Definition: DataType.h:67
double float64_t
Definition: common.h:48
long double floatmax_t
Definition: common.h:49
#define STRING_LEN
Definition: common.h:53
float float32_t
Definition: common.h:47
void to_string(char *dest, size_t n) const
Definition: DataType.cpp:144
EContainerType m_ctype
Definition: DataType.h:70
template class SGSparseVectorEntry
Definition: File.h:23
char string_t[STRING_LEN]
Definition: common.h:55
EPrimitiveType m_ptype
Definition: DataType.h:74

SHOGUN Machine Learning Toolbox - Documentation