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