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/SerializableAsciiReader00.h" 00012 00013 using namespace shogun; 00014 00015 SerializableAsciiReader00::SerializableAsciiReader00( 00016 CSerializableAsciiFile* file) { m_file = file; } 00017 00018 SerializableAsciiReader00::~SerializableAsciiReader00(void) {} 00019 00020 bool 00021 SerializableAsciiReader00::read_scalar_wrapped( 00022 const TSGDataType* type, void* param) 00023 { 00024 switch (type->m_ptype) { 00025 case PT_BOOL: 00026 char bool_buf; 00027 00028 if (fscanf(m_file->m_fstream, "%c", &bool_buf) != 1) 00029 return false; 00030 00031 switch (bool_buf) { 00032 case 't': *(bool*) param = true; break; 00033 case 'f': *(bool*) param = false; break; 00034 default: return false; 00035 } 00036 00037 break; 00038 case PT_CHAR: 00039 if (fscanf(m_file->m_fstream, "%"SCNu8, (uint8_t*) param) 00040 != 1) return false; 00041 break; 00042 case PT_INT8: 00043 if (fscanf(m_file->m_fstream, "%"SCNi8, (int8_t*) param) 00044 != 1) return false; 00045 break; 00046 case PT_UINT8: 00047 if (fscanf(m_file->m_fstream, "%"SCNu8, (uint8_t*) param) 00048 != 1) return false; 00049 break; 00050 case PT_INT16: 00051 if (fscanf(m_file->m_fstream, "%"SCNi16, (int16_t*) param) 00052 != 1) return false; 00053 break; 00054 case PT_UINT16: 00055 if (fscanf(m_file->m_fstream, "%"SCNu16, (uint16_t*) param) 00056 != 1) return false; 00057 break; 00058 case PT_INT32: 00059 if (fscanf(m_file->m_fstream, "%"SCNi32, (int32_t*) param) 00060 != 1) return false; 00061 break; 00062 case PT_UINT32: 00063 if (fscanf(m_file->m_fstream, "%"SCNu32, (uint32_t*) param) 00064 != 1) return false; 00065 break; 00066 case PT_INT64: 00067 if (fscanf(m_file->m_fstream, "%"SCNi64, (int64_t*) param) 00068 != 1) return false; 00069 break; 00070 case PT_UINT64: 00071 if (fscanf(m_file->m_fstream, "%"SCNu64, (uint64_t*) param) 00072 != 1) return false; 00073 break; 00074 case PT_FLOAT32: 00075 if (fscanf(m_file->m_fstream, "%g", (float32_t*) param) 00076 != 1) return false; 00077 break; 00078 case PT_FLOAT64: 00079 if (fscanf(m_file->m_fstream, "%lg", (float64_t*) param) 00080 != 1) return false; 00081 break; 00082 case PT_FLOATMAX: 00083 if (fscanf(m_file->m_fstream, "%Lg", (floatmax_t*) param) 00084 != 1) return false; 00085 break; 00086 case PT_SGOBJECT: 00087 SG_ERROR("read_scalar_wrapped(): Implementation error during" 00088 " reading AsciiFile!"); 00089 return false; 00090 } 00091 00092 return true; 00093 } 00094 00095 bool 00096 SerializableAsciiReader00::read_cont_begin_wrapped( 00097 const TSGDataType* type, index_t* len_read_y, index_t* len_read_x) 00098 { 00099 switch (type->m_ctype) { 00100 case CT_SCALAR: 00101 SG_ERROR("read_cont_begin_wrapped(): Implementation error " 00102 "during writing AsciiFile!"); 00103 return false; 00104 case CT_VECTOR: 00105 if (fscanf(m_file->m_fstream, "%"SCNi32" ", len_read_y) != 1) 00106 return false; 00107 *len_read_x = 1; 00108 break; 00109 case CT_MATRIX: 00110 if (fscanf(m_file->m_fstream, "%"SCNi32" %"SCNi32" ", 00111 len_read_y, len_read_x) != 2) 00112 return false; 00113 break; 00114 } 00115 00116 if (fgetc(m_file->m_fstream) != CHAR_CONT_BEGIN) return false; 00117 00118 return true; 00119 } 00120 00121 bool 00122 SerializableAsciiReader00::read_cont_end_wrapped( 00123 const TSGDataType* type, index_t len_read_y, index_t len_read_x) 00124 { 00125 if (fgetc(m_file->m_fstream) != CHAR_CONT_END) return false; 00126 00127 return true; 00128 } 00129 00130 bool 00131 SerializableAsciiReader00::read_string_begin_wrapped( 00132 const TSGDataType* type, index_t* length) 00133 { 00134 if (fscanf(m_file->m_fstream, "%"PRIi32, length) != 1) 00135 return false; 00136 if (fgetc(m_file->m_fstream) != ' ') return false; 00137 if (fgetc(m_file->m_fstream) != CHAR_STRING_BEGIN) return false; 00138 00139 return true; 00140 } 00141 00142 bool 00143 SerializableAsciiReader00::read_string_end_wrapped( 00144 const TSGDataType* type, index_t length) 00145 { 00146 if (fgetc(m_file->m_fstream) != CHAR_STRING_END) return false; 00147 00148 return true; 00149 } 00150 00151 bool 00152 SerializableAsciiReader00::read_stringentry_begin_wrapped( 00153 const TSGDataType* type, index_t y) 00154 { 00155 if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false; 00156 00157 return true; 00158 } 00159 00160 bool 00161 SerializableAsciiReader00::read_stringentry_end_wrapped( 00162 const TSGDataType* type, index_t y) 00163 { 00164 if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false; 00165 00166 return true; 00167 } 00168 00169 bool 00170 SerializableAsciiReader00::read_sparse_begin_wrapped( 00171 const TSGDataType* type, index_t* vec_index, 00172 index_t* length) 00173 { 00174 if (fscanf(m_file->m_fstream, "%"PRIi32" %"PRIi32, vec_index, 00175 length) != 2) return false; 00176 if (fgetc(m_file->m_fstream) != ' ') return false; 00177 if (fgetc(m_file->m_fstream) != CHAR_SPARSE_BEGIN) return false; 00178 00179 return true; 00180 } 00181 00182 bool 00183 SerializableAsciiReader00::read_sparse_end_wrapped( 00184 const TSGDataType* type, index_t* vec_index, 00185 index_t length) 00186 { 00187 if (fgetc(m_file->m_fstream) != CHAR_SPARSE_END) return false; 00188 00189 return true; 00190 } 00191 00192 bool 00193 SerializableAsciiReader00::read_sparseentry_begin_wrapped( 00194 const TSGDataType* type, TSparseEntry<char>* first_entry, 00195 index_t* feat_index, index_t y) 00196 { 00197 if (fscanf(m_file->m_fstream, "%"PRIi32, feat_index) != 1) 00198 return false; 00199 if (fgetc(m_file->m_fstream) != ' ') return false; 00200 if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false; 00201 00202 return true; 00203 } 00204 00205 bool 00206 SerializableAsciiReader00::read_sparseentry_end_wrapped( 00207 const TSGDataType* type, TSparseEntry<char>* first_entry, 00208 index_t* feat_index, index_t y) 00209 { 00210 if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false; 00211 00212 return true; 00213 } 00214 00215 bool 00216 SerializableAsciiReader00::read_item_begin_wrapped( 00217 const TSGDataType* type, index_t y, index_t x) 00218 { 00219 if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false; 00220 00221 return true; 00222 } 00223 00224 bool 00225 SerializableAsciiReader00::read_item_end_wrapped( 00226 const TSGDataType* type, index_t y, index_t x) 00227 { 00228 if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false; 00229 00230 return true; 00231 } 00232 00233 bool 00234 SerializableAsciiReader00::read_sgserializable_begin_wrapped( 00235 const TSGDataType* type, char* sgserializable_name, 00236 EPrimitiveType* generic) 00237 { 00238 if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s ", 00239 sgserializable_name) != 1) return false; 00240 00241 if (strcmp(sgserializable_name, STR_SGSERIAL_NULL) == 0) { 00242 if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN) 00243 return false; 00244 00245 *sgserializable_name = '\0'; 00246 } else { 00247 string_t buf; 00248 if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s ", buf) 00249 != 1) return false; 00250 00251 if (buf[0] != CHAR_SGSERIAL_BEGIN) { 00252 if (!TSGDataType::string_to_ptype(generic, buf)) 00253 return false; 00254 00255 if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN) 00256 return false; 00257 if (fgetc(m_file->m_fstream) != CHAR_TYPE_END) 00258 return false; 00259 } 00260 } 00261 00262 m_file->m_stack_fpos.push_back(ftell(m_file->m_fstream)); 00263 00264 return true; 00265 } 00266 00267 bool 00268 SerializableAsciiReader00::read_sgserializable_end_wrapped( 00269 const TSGDataType* type, const char* sgserializable_name, 00270 EPrimitiveType generic) 00271 { 00272 if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_END) return false; 00273 00274 m_file->m_stack_fpos.pop_back(); 00275 00276 return true; 00277 } 00278 00279 bool 00280 SerializableAsciiReader00::read_type_begin_wrapped( 00281 const TSGDataType* type, const char* name, const char* prefix) 00282 { 00283 if (fseek(m_file->m_fstream, m_file->m_stack_fpos.back(), SEEK_SET 00284 ) != 0) return false; 00285 00286 SG_SET_LOCALE_C; 00287 00288 string_t type_str; 00289 type->to_string(type_str, STRING_LEN); 00290 00291 string_t r_name, r_type; 00292 while (true) { 00293 if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s %" 00294 STRING_LEN_STR "s ", r_name, r_type) != 2) 00295 return false; 00296 00297 if (strcmp(r_name, name) == 0 00298 && strcmp(r_type, type_str) == 0) return true; 00299 00300 if (!m_file->ignore()) return false; 00301 } 00302 00303 return false; 00304 } 00305 00306 bool 00307 SerializableAsciiReader00::read_type_end_wrapped( 00308 const TSGDataType* type, const char* name, const char* prefix) 00309 { 00310 if (fgetc(m_file->m_fstream) != CHAR_TYPE_END) return false; 00311 00312 SG_RESET_LOCALE; 00313 00314 return true; 00315 }