SHOGUN  4.1.0
 全部  命名空间 文件 函数 变量 类型定义 枚举 枚举值 友元 宏定义  
SerializableXmlReader00.cpp
浏览该文件的文档.
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  result = CMath::strtof(buf, (float32_t*) param);
85  break;
86  case PT_FLOAT64:
87  result = CMath::strtod(buf, (float64_t*) param);
88  break;
89  case PT_FLOATMAX:
90  result = CMath::strtold(buf, (floatmax_t*) param);
91  break;
92  case PT_COMPLEX128:
93  float64_t c_real, c_imag;
94  if (sscanf(buf, "(%lg,%lg)", &c_real, &c_imag) != 2)
95  result = false;
96 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11) || defined(_LIBCPP_VERSION)
97  ((complex128_t*) param)->real(c_real);
98  ((complex128_t*) param)->imag(c_imag);
99 #else
100  ((complex128_t*) param)->real()=c_real;
101  ((complex128_t*) param)->imag()=c_imag;
102 #endif
103  break;
104  case PT_UNDEFINED:
105  case PT_SGOBJECT:
106  SG_ERROR("read_scalar_wrapped(): Implementation error during"
107  " reading XmlFile!");
108  result = false;
109  }
110 
111  xmlFree(xml_buf);
112  return result;
113 }
114 
115 bool
116 SerializableXmlReader00::read_cont_begin_wrapped(
117  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
118 {
119  xmlNode* m = m_file->m_stack_stream.back();
120 
121  switch (type->m_ctype) {
122  case CT_NDARRAY:
124  case CT_SCALAR: break;
125  case CT_VECTOR: case CT_SGVECTOR:
126  *len_read_y = xmlChildElementCount(m);
127  break;
128  case CT_MATRIX: case CT_SGMATRIX:
129  *len_read_x = xmlChildElementCount(m);
130 
131  for (xmlNode* cur=m->children; cur != NULL; cur=cur->next) {
132  if (cur->type != XML_ELEMENT_NODE) continue;
133 
134  if (*len_read_y == 0)
135  *len_read_y = xmlChildElementCount(cur);
136 
137  if (*len_read_y != (index_t) xmlChildElementCount(cur))
138  return false;
139  }
140  break;
141  case CT_UNDEFINED:
142  SG_ERROR("type undefined\n");
143  }
144 
145  return true;
146 }
147 
148 bool
149 SerializableXmlReader00::read_cont_end_wrapped(
150  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
151 {
152  if (len_read_y > 0) m_file->pop_node();
153 
154  if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX)
155  {
156  if (len_read_y*len_read_x>0)
157  m_file->pop_node();
158  }
159 
160  return true;
161 }
162 
163 bool
164 SerializableXmlReader00::read_string_begin_wrapped(
165  const TSGDataType* type, index_t* length)
166 {
167  xmlNode* m = m_file->m_stack_stream.back();
168 
169  *length = xmlChildElementCount(m);
170 
171  return true;
172 }
173 
174 bool
175 SerializableXmlReader00::read_string_end_wrapped(
176  const TSGDataType* type, index_t length)
177 {
178  if (length > 0) m_file->pop_node();
179 
180  return true;
181 }
182 
183 bool
184 SerializableXmlReader00::read_stringentry_begin_wrapped(
185  const TSGDataType* type, index_t y)
186 {
187  if (y == 0) {
188  if (!m_file->join_node(BAD_CAST STR_STRING)) return false;
189  return true;
190  }
191 
192  if (!m_file->next_node(BAD_CAST STR_STRING)) return false;
193 
194  return true;
195 }
196 
197 bool
198 SerializableXmlReader00::read_stringentry_end_wrapped(
199  const TSGDataType* type, index_t y)
200 {
201  return true;
202 }
203 
204 bool
205 SerializableXmlReader00::read_sparse_begin_wrapped(
206  const TSGDataType* type, index_t* length)
207 {
208  return true;
209 }
210 
211 bool
212 SerializableXmlReader00::read_sparse_end_wrapped(
213  const TSGDataType* type, index_t length)
214 {
215  if (length > 0) m_file->pop_node();
216 
217  return true;
218 }
219 
220 bool
221 SerializableXmlReader00::read_sparseentry_begin_wrapped(
222  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
223  index_t* feat_index, index_t y)
224 {
225  bool result = true;
226  xmlChar* buf;
227 
228  if (y == 0) {
229  if (!m_file->join_node(BAD_CAST STR_SPARSE)) return false;
230  } else {
231  if (!m_file->next_node(BAD_CAST STR_SPARSE)) return false;
232  }
233 
234  if ((buf = xmlGetProp(m_file->m_stack_stream.back(), BAD_CAST
235  STR_PROP_FEATINDEX)) == NULL) return false;
236  if (sscanf((const char*) buf, "%" PRIi32, feat_index) != 1)
237  result = false;
238  xmlFree(buf); if (!result) return false;
239 
240  return true;
241 }
242 
243 bool
244 SerializableXmlReader00::read_sparseentry_end_wrapped(
245  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
246  index_t* feat_index, index_t y)
247 {
248  return true;
249 }
250 
251 bool
252 SerializableXmlReader00::read_item_begin_wrapped(
253  const TSGDataType* type, index_t y, index_t x)
254 {
255  switch (type->m_ctype) {
256  case CT_NDARRAY:
258  case CT_SCALAR: break;
259  case CT_VECTOR: case CT_SGVECTOR:
260  if (y == 0) {
261  if (!m_file->join_node(BAD_CAST STR_ITEM)) return false;
262  return true;
263  }
264  break;
265  case CT_MATRIX: case CT_SGMATRIX:
266  if (y==0)
267  {
268  if (x != 0) { m_file->pop_node(); m_file->pop_node(); }
269 
270  string_t buf_x; snprintf(buf_x, STRING_LEN, "x%" PRIi32, x);
271  if (!m_file->join_node(BAD_CAST buf_x)) return false;
272  if (!m_file->join_node(BAD_CAST STR_ITEM)) return false;
273  return true;
274  }
275  break;
276  case CT_UNDEFINED:
277  SG_ERROR("type undefined\n");
278  }
279 
280  if (!m_file->next_node(BAD_CAST STR_ITEM)) return false;
281 
282  return true;
283 }
284 
285 bool
286 SerializableXmlReader00::read_item_end_wrapped(
287  const TSGDataType* type, index_t y, index_t x)
288 {
289  return true;
290 }
291 
292 bool
293 SerializableXmlReader00::read_sgserializable_begin_wrapped(
294  const TSGDataType* type, char* sgserializable_name,
295  EPrimitiveType* generic)
296 {
297  xmlNode* m = m_file->m_stack_stream.back();
298  xmlChar* buf;
299 
300  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) {
301  xmlFree(buf);
302  *sgserializable_name = '\0';
303  return true;
304  }
305 
306  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL)
307  return false;
308  strncpy(sgserializable_name, (const char*) buf, STRING_LEN);
309  xmlFree(buf);
310 
311  if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME))
312  != NULL) {
313  if (!TSGDataType::string_to_ptype(generic, (const char*) buf))
314  return false;
315  xmlFree(buf);
316  }
317 
318  return true;
319 }
320 
321 bool
322 SerializableXmlReader00::read_sgserializable_end_wrapped(
323  const TSGDataType* type, const char* sgserializable_name,
324  EPrimitiveType generic)
325 {
326  return true;
327 }
328 
329 bool
330 SerializableXmlReader00::read_type_begin_wrapped(
331  const TSGDataType* type, const char* name, const char* prefix)
332 {
333  bool result = true;
334 
336 
337  if (!m_file->join_node(BAD_CAST name)) return false;
338 
339  string_t buf; type->to_string(buf, STRING_LEN);
340  xmlChar* t;
341  if ((t = xmlGetProp(m_file->m_stack_stream.back(),
342  BAD_CAST STR_PROP_TYPE)) == NULL) return false;
343  if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false;
344  xmlFree(t); if (!result) return false;
345 
346  return true;
347 }
348 
349 bool
350 SerializableXmlReader00::read_type_end_wrapped(
351  const TSGDataType* type, const char* name, const char* prefix)
352 {
353  m_file->pop_node();
354 
356 
357  return true;
358 }
359 
360 #endif /* HAVE_XML */
static bool strtof(const char *str, float32_t *float_result)
Definition: Math.cpp:297
#define SG_RESET_LOCALE
Definition: SGIO.h:86
std::complex< float64_t > complex128_t
Definition: common.h:67
#define STRING_LEN_STR
Definition: common.h:56
int32_t index_t
Definition: common.h:62
static bool string_to_ptype(EPrimitiveType *ptype, const char *str)
Definition: DataType.cpp:393
static bool strtod(const char *str, float64_t *double_result)
Definition: Math.cpp:328
#define SG_ERROR(...)
Definition: SGIO.h:129
#define SG_NOTIMPLEMENTED
Definition: SGIO.h:139
#define SG_SET_LOCALE_C
Definition: SGIO.h:85
Datatypes that shogun supports.
Definition: DataType.h:68
double float64_t
Definition: common.h:50
long double floatmax_t
Definition: common.h:51
#define STRING_LEN
Definition: common.h:55
float float32_t
Definition: common.h:49
void to_string(char *dest, size_t n) const
Definition: DataType.cpp:145
EContainerType m_ctype
Definition: DataType.h:71
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
template class SGSparseVectorEntry
Definition: File.h:23
char string_t[STRING_LEN]
Definition: common.h:57
EPrimitiveType m_ptype
Definition: DataType.h:75
static bool strtold(const char *str, floatmax_t *long_double_result)
Definition: Math.cpp:359

SHOGUN 机器学习工具包 - 项目文档