SHOGUN v0.9.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 "lib/config.h" 00012 #ifdef HAVE_XML 00013 00014 #include "lib/SerializableXmlReader00.h" 00015 00016 using namespace shogun; 00017 00018 SerializableXmlReader00::SerializableXmlReader00( 00019 CSerializableXmlFile* file) { m_file = file; } 00020 00021 SerializableXmlReader00::~SerializableXmlReader00(void) {} 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_SCALAR: break; 00112 case CT_VECTOR: 00113 *len_read_y = xmlChildElementCount(m); 00114 break; 00115 case CT_MATRIX: 00116 *len_read_x = xmlChildElementCount(m); 00117 00118 for (xmlNode* cur=m->children; cur != NULL; cur=cur->next) { 00119 if (cur->type != XML_ELEMENT_NODE) continue; 00120 00121 if (*len_read_y == 0) 00122 *len_read_y = xmlChildElementCount(cur); 00123 00124 if (*len_read_y != (index_t) xmlChildElementCount(cur)) 00125 return false; 00126 } 00127 00128 break; 00129 } 00130 00131 return true; 00132 } 00133 00134 bool 00135 SerializableXmlReader00::read_cont_end_wrapped( 00136 const TSGDataType* type, index_t len_read_y, index_t len_read_x) 00137 { 00138 if (len_read_y > 0) m_file->pop_node(); 00139 00140 if (type->m_ctype == CT_MATRIX && len_read_y *len_read_x > 0) 00141 m_file->pop_node(); 00142 00143 return true; 00144 } 00145 00146 bool 00147 SerializableXmlReader00::read_string_begin_wrapped( 00148 const TSGDataType* type, index_t* length) 00149 { 00150 xmlNode* m = m_file->m_stack_stream.back(); 00151 00152 *length = xmlChildElementCount(m); 00153 00154 return true; 00155 } 00156 00157 bool 00158 SerializableXmlReader00::read_string_end_wrapped( 00159 const TSGDataType* type, index_t length) 00160 { 00161 if (length > 0) m_file->pop_node(); 00162 00163 return true; 00164 } 00165 00166 bool 00167 SerializableXmlReader00::read_stringentry_begin_wrapped( 00168 const TSGDataType* type, index_t y) 00169 { 00170 if (y == 0) { 00171 if (!m_file->join_node(BAD_CAST STR_STRING)) return false; 00172 return true; 00173 } 00174 00175 if (!m_file->next_node(BAD_CAST STR_STRING)) return false; 00176 00177 return true; 00178 } 00179 00180 bool 00181 SerializableXmlReader00::read_stringentry_end_wrapped( 00182 const TSGDataType* type, index_t y) 00183 { 00184 return true; 00185 } 00186 00187 bool 00188 SerializableXmlReader00::read_sparse_begin_wrapped( 00189 const TSGDataType* type, index_t* vec_index, 00190 index_t* length) 00191 { 00192 xmlNode* m = m_file->m_stack_stream.back(); 00193 00194 bool result = true; 00195 xmlChar* buf; 00196 00197 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_VECINDEX)) == NULL) 00198 return false; 00199 if (sscanf((const char*) buf, "%"PRIi32, vec_index) != 1) 00200 result = false; 00201 xmlFree(buf); if (!result) return false; 00202 00203 *length = xmlChildElementCount(m); 00204 00205 return true; 00206 } 00207 00208 bool 00209 SerializableXmlReader00::read_sparse_end_wrapped( 00210 const TSGDataType* type, index_t* vec_index, 00211 index_t length) 00212 { 00213 if (length > 0) m_file->pop_node(); 00214 00215 return true; 00216 } 00217 00218 bool 00219 SerializableXmlReader00::read_sparseentry_begin_wrapped( 00220 const TSGDataType* type, TSparseEntry<char>* first_entry, 00221 index_t* feat_index, index_t y) 00222 { 00223 bool result = true; 00224 xmlChar* buf; 00225 00226 if (y == 0) { 00227 if (!m_file->join_node(BAD_CAST STR_SPARSE)) return false; 00228 } else { 00229 if (!m_file->next_node(BAD_CAST STR_SPARSE)) return false; 00230 } 00231 00232 if ((buf = xmlGetProp(m_file->m_stack_stream.back(), BAD_CAST 00233 STR_PROP_FEATINDEX)) == NULL) return false; 00234 if (sscanf((const char*) buf, "%"PRIi32, feat_index) != 1) 00235 result = false; 00236 xmlFree(buf); if (!result) return false; 00237 00238 return true; 00239 } 00240 00241 bool 00242 SerializableXmlReader00::read_sparseentry_end_wrapped( 00243 const TSGDataType* type, TSparseEntry<char>* first_entry, 00244 index_t* feat_index, index_t y) 00245 { 00246 return true; 00247 } 00248 00249 bool 00250 SerializableXmlReader00::read_item_begin_wrapped( 00251 const TSGDataType* type, index_t y, index_t x) 00252 { 00253 switch (type->m_ctype) { 00254 case CT_SCALAR: break; 00255 case CT_VECTOR: 00256 if (y == 0) { 00257 if (!m_file->join_node(BAD_CAST STR_ITEM)) return false; 00258 return true; 00259 } 00260 break; 00261 case CT_MATRIX: 00262 if (type->m_ctype == CT_MATRIX && y == 0) { 00263 if (x != 0) { m_file->pop_node(); m_file->pop_node(); } 00264 00265 string_t buf_x; snprintf(buf_x, STRING_LEN, "x%"PRIi32, x); 00266 if (!m_file->join_node(BAD_CAST buf_x)) return false; 00267 if (!m_file->join_node(BAD_CAST STR_ITEM)) return false; 00268 return true; 00269 } 00270 break; 00271 } 00272 00273 if (!m_file->next_node(BAD_CAST STR_ITEM)) return false; 00274 00275 return true; 00276 } 00277 00278 bool 00279 SerializableXmlReader00::read_item_end_wrapped( 00280 const TSGDataType* type, index_t y, index_t x) 00281 { 00282 return true; 00283 } 00284 00285 bool 00286 SerializableXmlReader00::read_sgserializable_begin_wrapped( 00287 const TSGDataType* type, char* sgserializable_name, 00288 EPrimitiveType* generic) 00289 { 00290 xmlNode* m = m_file->m_stack_stream.back(); 00291 xmlChar* buf; 00292 00293 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_IS_NULL)) != NULL) { 00294 xmlFree(buf); 00295 *sgserializable_name = '\0'; 00296 return true; 00297 } 00298 00299 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_INSTANCE_NAME)) == NULL) 00300 return false; 00301 strncpy(sgserializable_name, (const char*) buf, STRING_LEN); 00302 xmlFree(buf); 00303 00304 if ((buf = xmlGetProp(m, BAD_CAST STR_PROP_GENERIC_NAME)) 00305 != NULL) { 00306 if (!TSGDataType::string_to_ptype(generic, (const char*) buf)) 00307 return false; 00308 xmlFree(buf); 00309 } 00310 00311 return true; 00312 } 00313 00314 bool 00315 SerializableXmlReader00::read_sgserializable_end_wrapped( 00316 const TSGDataType* type, const char* sgserializable_name, 00317 EPrimitiveType generic) 00318 { 00319 return true; 00320 } 00321 00322 bool 00323 SerializableXmlReader00::read_type_begin_wrapped( 00324 const TSGDataType* type, const char* name, const char* prefix) 00325 { 00326 bool result = true; 00327 00328 SG_SET_LOCALE_C; 00329 00330 if (!m_file->join_node(BAD_CAST name)) return false; 00331 00332 string_t buf; type->to_string(buf, STRING_LEN); 00333 xmlChar* t; 00334 if ((t = xmlGetProp(m_file->m_stack_stream.back(), 00335 BAD_CAST STR_PROP_TYPE)) == NULL) return false; 00336 if (xmlStrcmp(BAD_CAST buf, t) != 0) result = false; 00337 xmlFree(t); if (!result) return false; 00338 00339 return true; 00340 } 00341 00342 bool 00343 SerializableXmlReader00::read_type_end_wrapped( 00344 const TSGDataType* type, const char* name, const char* prefix) 00345 { 00346 m_file->pop_node(); 00347 00348 SG_RESET_LOCALE; 00349 00350 return true; 00351 } 00352 00353 #endif /* HAVE_XML */