SHOGUN
v3.2.0
|
00001 /* 00002 * This program is free software; you can redistribute it and/or modify 00003 * it under the terms of the GNU General Public License as published by 00004 * the Free Software Foundation; either version 3 of the License, or 00005 * (at your option) any later version. 00006 * 00007 * Written (W) 2010 Soeren Sonnenburg 00008 * Copyright (C) 2010 Berlin Institute of Technology 00009 */ 00010 00011 #include <shogun/lib/config.h> 00012 #ifdef HAVE_XML 00013 00014 #include <shogun/lib/common.h> 00015 #include <shogun/io/SerializableXmlReader00.h> 00016 00017 using namespace shogun; 00018 00019 SerializableXmlReader00::SerializableXmlReader00( 00020 CSerializableXmlFile* file) { m_file = file; } 00021 00022 SerializableXmlReader00::~SerializableXmlReader00() {} 00023 00024 bool 00025 SerializableXmlReader00::read_scalar_wrapped( 00026 const TSGDataType* type, void* param) 00027 { 00028 xmlNode* m = m_file->m_stack_stream.back(); 00029 00030 bool result = true; 00031 xmlChar* xml_buf; 00032 if ((xml_buf = xmlNodeGetContent(m)) == NULL) return false; 00033 const char* buf = (const char*) xml_buf; 00034 00035 switch (type->m_ptype) { 00036 case PT_BOOL: 00037 string_t bool_buf; 00038 00039 if (sscanf(buf, "%" STRING_LEN_STR "s", bool_buf) != 1) 00040 result = false; 00041 00042 if (strcmp(buf, STR_TRUE) == 0) *(bool*) param = true; 00043 else if (strcmp(buf, STR_FALSE) == 0) *(bool*) param = false; 00044 else result = false; 00045 00046 break; 00047 case PT_CHAR: 00048 if (sscanf(buf, "%c", (char*) param) != 1) 00049 result = false; 00050 break; 00051 case PT_INT8: 00052 if (sscanf(buf, "%" SCNi8, (int8_t*) param) != 1) 00053 result = false; 00054 break; 00055 case PT_UINT8: 00056 if (sscanf(buf, "%" SCNu8, (uint8_t*) param) != 1) 00057 result = false; 00058 break; 00059 case PT_INT16: 00060 if (sscanf(buf, "%" SCNi16, (int16_t*) param) != 1) 00061 result = false; 00062 break; 00063 case PT_UINT16: 00064 if (sscanf(buf, "%" SCNu16, (uint16_t*) param) != 1) 00065 result = false; 00066 break; 00067 case PT_INT32: 00068 if (sscanf(buf, "%" SCNi32, (int32_t*) param) != 1) 00069 result = false; 00070 break; 00071 case PT_UINT32: 00072 if (sscanf(buf, "%" SCNu32, (uint32_t*) param) != 1) 00073 result = false; 00074 break; 00075 case PT_INT64: 00076 if (sscanf(buf, "%" SCNi64, (int64_t*) param) != 1) 00077 result = false; 00078 break; 00079 case PT_UINT64: 00080 if (sscanf(buf, "%" SCNu64, (uint64_t*) param) != 1) 00081 result = false; 00082 break; 00083 case PT_FLOAT32: 00084 result = CMath::strtof(buf, (float32_t*) param); 00085 break; 00086 case PT_FLOAT64: 00087 result = CMath::strtod(buf, (float64_t*) param); 00088 break; 00089 case PT_FLOATMAX: 00090 result = CMath::strtold(buf, (floatmax_t*) param); 00091 break; 00092 case PT_COMPLEX128: 00093 float64_t c_real, c_imag; 00094 if (sscanf(buf, "(%lg,%lg)", &c_real, &c_imag) != 2) 00095 result = false; 00096 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11) || defined(_LIBCPP_VERSION) 00097 ((complex128_t*) param)->real(c_real); 00098 ((complex128_t*) param)->imag(c_imag); 00099 #else 00100 ((complex128_t*) param)->real()=c_real; 00101 ((complex128_t*) param)->imag()=c_imag; 00102 #endif 00103 break; 00104 case PT_UNDEFINED: 00105 case PT_SGOBJECT: 00106 SG_ERROR("read_scalar_wrapped(): Implementation error during" 00107 " reading XmlFile!"); 00108 result = false; 00109 } 00110 00111 xmlFree(xml_buf); 00112 return result; 00113 } 00114 00115 bool 00116 SerializableXmlReader00::read_cont_begin_wrapped( 00117 const TSGDataType* type, index_t* len_read_y, index_t* len_read_x) 00118 { 00119 xmlNode* m = m_file->m_stack_stream.back(); 00120 00121 switch (type->m_ctype) { 00122 case CT_NDARRAY: 00123 SG_NOTIMPLEMENTED 00124 case CT_SCALAR: break; 00125 case CT_VECTOR: case CT_SGVECTOR: 00126 *len_read_y = xmlChildElementCount(m); 00127 break; 00128 case CT_MATRIX: case CT_SGMATRIX: 00129 *len_read_x = xmlChildElementCount(m); 00130 00131 for (xmlNode* cur=m->children; cur != NULL; cur=cur->next) { 00132 if (cur->type != XML_ELEMENT_NODE) continue; 00133 00134 if (*len_read_y == 0) 00135 *len_read_y = xmlChildElementCount(cur); 00136 00137 if (*len_read_y != (index_t) xmlChildElementCount(cur)) 00138 return false; 00139 } 00140 break; 00141 case CT_UNDEFINED: 00142 SG_ERROR("type undefined\n"); 00143 } 00144 00145 return true; 00146 } 00147 00148 bool 00149 SerializableXmlReader00::read_cont_end_wrapped( 00150 const TSGDataType* type, index_t len_read_y, index_t len_read_x) 00151 { 00152 if (len_read_y > 0) m_file->pop_node(); 00153 00154 if (type->m_ctype==CT_MATRIX || type->m_ctype==CT_SGMATRIX) 00155 { 00156 if (len_read_y*len_read_x>0) 00157 m_file->pop_node(); 00158 } 00159 00160 return true; 00161 } 00162 00163 bool 00164 SerializableXmlReader00::read_string_begin_wrapped( 00165 const TSGDataType* type, index_t* length) 00166 { 00167 xmlNode* m = m_file->m_stack_stream.back(); 00168 00169 *length = xmlChildElementCount(m); 00170 00171 return true; 00172 } 00173 00174 bool 00175 SerializableXmlReader00::read_string_end_wrapped( 00176 const TSGDataType* type, index_t length) 00177 { 00178 if (length > 0) m_file->pop_node(); 00179 00180 return true; 00181 } 00182 00183 bool 00184 SerializableXmlReader00::read_stringentry_begin_wrapped( 00185 const TSGDataType* type, index_t y) 00186 { 00187 if (y == 0) { 00188 if (!m_file->join_node(BAD_CAST STR_STRING)) return false; 00189 return true; 00190 } 00191 00192 if (!m_file->next_node(BAD_CAST STR_STRING)) return false; 00193 00194 return true; 00195 } 00196 00197 bool 00198 SerializableXmlReader00::read_stringentry_end_wrapped( 00199 const TSGDataType* type, index_t y) 00200 { 00201 return true; 00202 } 00203 00204 bool 00205 SerializableXmlReader00::read_sparse_begin_wrapped( 00206 const TSGDataType* type, index_t* length) 00207 { 00208 return true; 00209 } 00210 00211 bool 00212 SerializableXmlReader00::read_sparse_end_wrapped( 00213 const TSGDataType* type, index_t length) 00214 { 00215 if (length > 0) m_file->pop_node(); 00216 00217 return true; 00218 } 00219 00220 bool 00221 SerializableXmlReader00::read_sparseentry_begin_wrapped( 00222 const TSGDataType* type, SGSparseVectorEntry<char>* first_entry, 00223 index_t* feat_index, index_t y) 00224 { 00225 bool result = true; 00226 xmlChar* buf; 00227 00228 if (y == 0) { 00229 if (!m_file->join_node(BAD_CAST STR_SPARSE)) return false; 00230 } else { 00231 if (!m_file->next_node(BAD_CAST STR_SPARSE)) return false; 00232 } 00233 00234 if ((buf = xmlGetProp(m_file->m_stack_stream.back(), BAD_CAST 00235 STR_PROP_FEATINDEX)) == NULL) return false; 00236 if (sscanf((const char*) buf, "%" PRIi32, feat_index) != 1) 00237 result = false; 00238 xmlFree(buf); if (!result) return false; 00239 00240 return true; 00241 } 00242 00243 bool 00244 SerializableXmlReader00::read_sparseentry_end_wrapped( 00245 const TSGDataType* type, SGSparseVectorEntry<char>* first_entry, 00246 index_t* feat_index, index_t y) 00247 { 00248 return true; 00249 } 00250 00251 bool 00252 SerializableXmlReader00::read_item_begin_wrapped( 00253 const TSGDataType* type, index_t y, index_t x) 00254 { 00255 switch (type->m_ctype) { 00256 case CT_NDARRAY: 00257 SG_NOTIMPLEMENTED 00258 case CT_SCALAR: break; 00259 case CT_VECTOR: case CT_SGVECTOR: 00260 if (y == 0) { 00261 if (!m_file->join_node(BAD_CAST STR_ITEM)) return false; 00262 return true; 00263 } 00264 break; 00265 case CT_MATRIX: case CT_SGMATRIX: 00266 if (y==0) 00267 { 00268 if (x != 0) { m_file->pop_node(); m_file->pop_node(); } 00269 00270 string_t buf_x; snprintf(buf_x, STRING_LEN, "x%" PRIi32, x); 00271 if (!m_file->join_node(BAD_CAST buf_x)) return false; 00272 if (!m_file->join_node(BAD_CAST STR_ITEM)) return false; 00273 return true; 00274 } 00275 break; 00276 case CT_UNDEFINED: 00277 SG_ERROR("type undefined\n"); 00278 } 00279 00280 if (!m_file->next_node(BAD_CAST STR_ITEM)) return false; 00281 00282 return true; 00283 } 00284 00285 bool 00286 SerializableXmlReader00::read_item_end_wrapped( 00287 const TSGDataType* type, index_t y, index_t x) 00288 { 00289 return true; 00290 } 00291 00292 bool 00293 SerializableXmlReader00::read_sgserializable_begin_wrapped( 00294 const TSGDataType* type, char* sgserializable_name, 00295 EPrimitiveType* generic) 00296 { 00297 xmlNode* m = m_file->m_stack_stream.back(); 00298 xmlChar* buf; 00299 00300 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) { 00301 xmlFree(buf); 00302 *sgserializable_name = '\0'; 00303 return true; 00304 } 00305 00306 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL) 00307 return false; 00308 strncpy(sgserializable_name, (const char*) buf, STRING_LEN); 00309 xmlFree(buf); 00310 00311 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME)) 00312 != NULL) { 00313 if (!TSGDataType::string_to_ptype(generic, (const char*) buf)) 00314 return false; 00315 xmlFree(buf); 00316 } 00317 00318 return true; 00319 } 00320 00321 bool 00322 SerializableXmlReader00::read_sgserializable_end_wrapped( 00323 const TSGDataType* type, const char* sgserializable_name, 00324 EPrimitiveType generic) 00325 { 00326 return true; 00327 } 00328 00329 bool 00330 SerializableXmlReader00::read_type_begin_wrapped( 00331 const TSGDataType* type, const char* name, const char* prefix) 00332 { 00333 bool result = true; 00334 00335 SG_SET_LOCALE_C; 00336 00337 if (!m_file->join_node(BAD_CAST name)) return false; 00338 00339 string_t buf; type->to_string(buf, STRING_LEN); 00340 xmlChar* t; 00341 if ((t = xmlGetProp(m_file->m_stack_stream.back(), 00342 BAD_CAST STR_PROP_TYPE)) == NULL) return false; 00343 if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false; 00344 xmlFree(t); if (!result) return false; 00345 00346 return true; 00347 } 00348 00349 bool 00350 SerializableXmlReader00::read_type_end_wrapped( 00351 const TSGDataType* type, const char* name, const char* prefix) 00352 { 00353 m_file->pop_node(); 00354 00355 SG_RESET_LOCALE; 00356 00357 return true; 00358 } 00359 00360 #endif /* HAVE_XML */