SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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* vec_index,
195  index_t* length)
196 {
197  xmlNode* m = m_file->m_stack_stream.back();
198 
199  bool result = true;
200  xmlChar* buf;
201 
202  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_VECINDEX)) == NULL)
203  return false;
204  if (sscanf((const char*) buf, "%"PRIi32, vec_index) != 1)
205  result = false;
206  xmlFree(buf); if (!result) return false;
207 
208  *length = xmlChildElementCount(m);
209 
210  return true;
211 }
212 
213 bool
214 SerializableXmlReader00::read_sparse_end_wrapped(
215  const TSGDataType* type, index_t* vec_index,
216  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  }
280 
281  if (!m_file->next_node(BAD_CAST STR_ITEM)) return false;
282 
283  return true;
284 }
285 
286 bool
287 SerializableXmlReader00::read_item_end_wrapped(
288  const TSGDataType* type, index_t y, index_t x)
289 {
290  return true;
291 }
292 
293 bool
294 SerializableXmlReader00::read_sgserializable_begin_wrapped(
295  const TSGDataType* type, char* sgserializable_name,
296  EPrimitiveType* generic)
297 {
298  xmlNode* m = m_file->m_stack_stream.back();
299  xmlChar* buf;
300 
301  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) {
302  xmlFree(buf);
303  *sgserializable_name = '\0';
304  return true;
305  }
306 
307  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL)
308  return false;
309  strncpy(sgserializable_name, (const char*) buf, STRING_LEN);
310  xmlFree(buf);
311 
312  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME))
313  != NULL) {
314  if (!TSGDataType::string_to_ptype(generic, (const char*) buf))
315  return false;
316  xmlFree(buf);
317  }
318 
319  return true;
320 }
321 
322 bool
323 SerializableXmlReader00::read_sgserializable_end_wrapped(
324  const TSGDataType* type, const char* sgserializable_name,
325  EPrimitiveType generic)
326 {
327  return true;
328 }
329 
330 bool
331 SerializableXmlReader00::read_type_begin_wrapped(
332  const TSGDataType* type, const char* name, const char* prefix)
333 {
334  bool result = true;
335 
337 
338  if (!m_file->join_node(BAD_CAST name)) return false;
339 
340  string_t buf; type->to_string(buf, STRING_LEN);
341  xmlChar* t;
342  if ((t = xmlGetProp(m_file->m_stack_stream.back(),
343  BAD_CAST STR_PROP_TYPE)) == NULL) return false;
344  if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false;
345  xmlFree(t); if (!result) return false;
346 
347  return true;
348 }
349 
350 bool
351 SerializableXmlReader00::read_type_end_wrapped(
352  const TSGDataType* type, const char* name, const char* prefix)
353 {
354  m_file->pop_node();
355 
357 
358  return true;
359 }
360 
361 #endif /* HAVE_XML */

SHOGUN Machine Learning Toolbox - Documentation