SHOGUN
v2.0.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* length) 00195 { 00196 return true; 00197 } 00198 00199 bool 00200 SerializableXmlReader00::read_sparse_end_wrapped( 00201 const TSGDataType* type, index_t length) 00202 { 00203 if (length > 0) m_file->pop_node(); 00204 00205 return true; 00206 } 00207 00208 bool 00209 SerializableXmlReader00::read_sparseentry_begin_wrapped( 00210 const TSGDataType* type, SGSparseVectorEntry<char>* first_entry, 00211 index_t* feat_index, index_t y) 00212 { 00213 bool result = true; 00214 xmlChar* buf; 00215 00216 if (y == 0) { 00217 if (!m_file->join_node(BAD_CAST STR_SPARSE)) return false; 00218 } else { 00219 if (!m_file->next_node(BAD_CAST STR_SPARSE)) return false; 00220 } 00221 00222 if ((buf = xmlGetProp(m_file->m_stack_stream.back(), BAD_CAST 00223 STR_PROP_FEATINDEX)) == NULL) return false; 00224 if (sscanf((const char*) buf, "%"PRIi32, feat_index) != 1) 00225 result = false; 00226 xmlFree(buf); if (!result) return false; 00227 00228 return true; 00229 } 00230 00231 bool 00232 SerializableXmlReader00::read_sparseentry_end_wrapped( 00233 const TSGDataType* type, SGSparseVectorEntry<char>* first_entry, 00234 index_t* feat_index, index_t y) 00235 { 00236 return true; 00237 } 00238 00239 bool 00240 SerializableXmlReader00::read_item_begin_wrapped( 00241 const TSGDataType* type, index_t y, index_t x) 00242 { 00243 switch (type->m_ctype) { 00244 case CT_NDARRAY: 00245 SG_NOTIMPLEMENTED; 00246 case CT_SCALAR: break; 00247 case CT_VECTOR: case CT_SGVECTOR: 00248 if (y == 0) { 00249 if (!m_file->join_node(BAD_CAST STR_ITEM)) return false; 00250 return true; 00251 } 00252 break; 00253 case CT_MATRIX: case CT_SGMATRIX: 00254 if (y==0) 00255 { 00256 if (x != 0) { m_file->pop_node(); m_file->pop_node(); } 00257 00258 string_t buf_x; snprintf(buf_x, STRING_LEN, "x%"PRIi32, x); 00259 if (!m_file->join_node(BAD_CAST buf_x)) return false; 00260 if (!m_file->join_node(BAD_CAST STR_ITEM)) return false; 00261 return true; 00262 } 00263 break; 00264 } 00265 00266 if (!m_file->next_node(BAD_CAST STR_ITEM)) return false; 00267 00268 return true; 00269 } 00270 00271 bool 00272 SerializableXmlReader00::read_item_end_wrapped( 00273 const TSGDataType* type, index_t y, index_t x) 00274 { 00275 return true; 00276 } 00277 00278 bool 00279 SerializableXmlReader00::read_sgserializable_begin_wrapped( 00280 const TSGDataType* type, char* sgserializable_name, 00281 EPrimitiveType* generic) 00282 { 00283 xmlNode* m = m_file->m_stack_stream.back(); 00284 xmlChar* buf; 00285 00286 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) { 00287 xmlFree(buf); 00288 *sgserializable_name = '\0'; 00289 return true; 00290 } 00291 00292 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL) 00293 return false; 00294 strncpy(sgserializable_name, (const char*) buf, STRING_LEN); 00295 xmlFree(buf); 00296 00297 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME)) 00298 != NULL) { 00299 if (!TSGDataType::string_to_ptype(generic, (const char*) buf)) 00300 return false; 00301 xmlFree(buf); 00302 } 00303 00304 return true; 00305 } 00306 00307 bool 00308 SerializableXmlReader00::read_sgserializable_end_wrapped( 00309 const TSGDataType* type, const char* sgserializable_name, 00310 EPrimitiveType generic) 00311 { 00312 return true; 00313 } 00314 00315 bool 00316 SerializableXmlReader00::read_type_begin_wrapped( 00317 const TSGDataType* type, const char* name, const char* prefix) 00318 { 00319 bool result = true; 00320 00321 SG_SET_LOCALE_C; 00322 00323 if (!m_file->join_node(BAD_CAST name)) return false; 00324 00325 string_t buf; type->to_string(buf, STRING_LEN); 00326 xmlChar* t; 00327 if ((t = xmlGetProp(m_file->m_stack_stream.back(), 00328 BAD_CAST STR_PROP_TYPE)) == NULL) return false; 00329 if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false; 00330 xmlFree(t); if (!result) return false; 00331 00332 return true; 00333 } 00334 00335 bool 00336 SerializableXmlReader00::read_type_end_wrapped( 00337 const TSGDataType* type, const char* name, const char* prefix) 00338 { 00339 m_file->pop_node(); 00340 00341 SG_RESET_LOCALE; 00342 00343 return true; 00344 } 00345 00346 #endif /* HAVE_XML */