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/io/SerializableAsciiFile.h> 00012 #include <shogun/io/SerializableAsciiReader00.h> 00013 00014 #define STR_HEADER_00 \ 00015 "<<_SHOGUN_SERIALIZABLE_ASCII_FILE_V_00_>>" 00016 00017 using namespace shogun; 00018 00019 CSerializableAsciiFile::CSerializableAsciiFile() 00020 :CSerializableFile() { init(); } 00021 00022 CSerializableAsciiFile::CSerializableAsciiFile(FILE* fstream, char rw) 00023 :CSerializableFile(fstream, rw) { init(); } 00024 00025 CSerializableAsciiFile::CSerializableAsciiFile( 00026 const char* fname, char rw) 00027 :CSerializableFile(fname, rw) { init(); } 00028 00029 CSerializableAsciiFile::~CSerializableAsciiFile() {} 00030 00031 bool 00032 CSerializableAsciiFile::ignore() 00033 { 00034 for (uint32_t cont_count = 0, item_count = 0, 00035 sgserial_count = 0; ;) { 00036 switch (fgetc(m_fstream)) { 00037 case CHAR_ITEM_BEGIN: item_count++; break; 00038 case CHAR_CONT_BEGIN: cont_count++; break; 00039 case CHAR_SGSERIAL_BEGIN: sgserial_count++; break; 00040 case CHAR_CONT_END: 00041 if (cont_count-- == 0) return false; 00042 break; 00043 case CHAR_ITEM_END: 00044 if (item_count-- == 0) return false; 00045 break; 00046 case CHAR_SGSERIAL_END: 00047 if (sgserial_count-- == 0) return false; 00048 break; 00049 case CHAR_TYPE_END: 00050 if (cont_count == 0 && item_count == 0 00051 && sgserial_count == 0) 00052 return true; 00053 break; 00054 case EOF: return false; 00055 default: break; 00056 } 00057 } 00058 00059 return false; 00060 } 00061 00062 CSerializableFile::TSerializableReader* 00063 CSerializableAsciiFile::new_reader(char* dest_version, size_t n) 00064 { 00065 string_t buf; 00066 if (fscanf(m_fstream, "%"STRING_LEN_STR"s\n", buf) != 1) 00067 return NULL; 00068 00069 strncpy(dest_version, buf, n < STRING_LEN? n: STRING_LEN); 00070 m_stack_fpos.push_back(ftell(m_fstream)); 00071 00072 if (strcmp(STR_HEADER_00, dest_version) == 0) 00073 return new SerializableAsciiReader00(this); 00074 00075 return NULL; 00076 } 00077 00078 void 00079 CSerializableAsciiFile::init() 00080 { 00081 if (m_fstream == NULL) return; 00082 00083 switch (m_task) { 00084 case 'w': 00085 if (fprintf(m_fstream, STR_HEADER_00"\n") <= 0) { 00086 close(); return; 00087 } 00088 m_stack_fpos.push_back(ftell(m_fstream)); 00089 break; 00090 case 'r': break; 00091 default: 00092 SG_WARNING("Could not open file `%s', unknown mode!\n", 00093 m_filename); 00094 close(); return; 00095 } 00096 } 00097 00098 bool 00099 CSerializableAsciiFile::write_scalar_wrapped( 00100 const TSGDataType* type, const void* param) 00101 { 00102 switch (type->m_ptype) { 00103 case PT_BOOL: 00104 if (fprintf(m_fstream, "%c", *(bool*) param? 't': 'f') <= 0) 00105 return false; 00106 break; 00107 case PT_CHAR: 00108 if (fprintf(m_fstream, "%"PRIu8, *(uint8_t*) param) <= 0) 00109 return false; 00110 break; 00111 case PT_INT8: 00112 if (fprintf(m_fstream, "%"PRIi8, *(int8_t*) param) <= 0) 00113 return false; 00114 break; 00115 case PT_UINT8: 00116 if (fprintf(m_fstream, "%"PRIu8, *(uint8_t*) param) <= 0) 00117 return false; 00118 break; 00119 case PT_INT16: 00120 if (fprintf(m_fstream, "%"PRIi16, *(int16_t*) param) <= 0) 00121 return false; 00122 break; 00123 case PT_UINT16: 00124 if (fprintf(m_fstream, "%"PRIu16, *(uint16_t*) param) <= 0) 00125 return false; 00126 break; 00127 case PT_INT32: 00128 if (fprintf(m_fstream, "%"PRIi32, *(int32_t*) param) <= 0) 00129 return false; 00130 break; 00131 case PT_UINT32: 00132 if (fprintf(m_fstream, "%"PRIu32, *(uint32_t*) param) <= 0) 00133 return false; 00134 break; 00135 case PT_INT64: 00136 if (fprintf(m_fstream, "%"PRIi64, *(int64_t*) param) <= 0) 00137 return false; 00138 break; 00139 case PT_UINT64: 00140 if (fprintf(m_fstream, "%"PRIu64, *(uint64_t*) param) <= 0) 00141 return false; 00142 break; 00143 case PT_FLOAT32: 00144 if (fprintf(m_fstream, "%.16g", *(float32_t*) param) <= 0) 00145 return false; 00146 break; 00147 case PT_FLOAT64: 00148 if (fprintf(m_fstream, "%.16lg", *(float64_t*) param) <= 0) 00149 return false; 00150 break; 00151 case PT_FLOATMAX: 00152 if (fprintf(m_fstream, "%.16Lg", *(floatmax_t*) param) <= 0) 00153 return false; 00154 break; 00155 case PT_SGOBJECT: 00156 SG_ERROR("write_scalar_wrapped(): Implementation error during" 00157 " writing AsciiFile!"); 00158 return false; 00159 } 00160 00161 return true; 00162 } 00163 00164 bool 00165 CSerializableAsciiFile::write_cont_begin_wrapped( 00166 const TSGDataType* type, index_t len_real_y, index_t len_real_x) 00167 { 00168 switch (type->m_ctype) { 00169 case CT_NDARRAY: 00170 SG_NOTIMPLEMENTED; 00171 case CT_SCALAR: 00172 SG_ERROR("write_cont_begin_wrapped(): Implementation error " 00173 "during writing AsciiFile!"); 00174 return false; 00175 case CT_VECTOR: case CT_SGVECTOR: 00176 if (fprintf(m_fstream, "%"PRIi32" %c", len_real_y, 00177 CHAR_CONT_BEGIN) <= 0) 00178 return false; 00179 break; 00180 case CT_MATRIX: case CT_SGMATRIX: 00181 if (fprintf(m_fstream, "%"PRIi32" %"PRIi32" %c", 00182 len_real_y, len_real_x, CHAR_CONT_BEGIN) <= 0) 00183 return false; 00184 break; 00185 } 00186 00187 return true; 00188 } 00189 00190 bool 00191 CSerializableAsciiFile::write_cont_end_wrapped( 00192 const TSGDataType* type, index_t len_real_y, index_t len_real_x) 00193 { 00194 if (fprintf(m_fstream, "%c", CHAR_CONT_END) <= 0) return false; 00195 00196 return true; 00197 } 00198 00199 bool 00200 CSerializableAsciiFile::write_string_begin_wrapped( 00201 const TSGDataType* type, index_t length) 00202 { 00203 if (fprintf(m_fstream, "%"PRIi32" %c", length, 00204 CHAR_STRING_BEGIN) <= 0) return false; 00205 00206 return true; 00207 } 00208 00209 bool 00210 CSerializableAsciiFile::write_string_end_wrapped( 00211 const TSGDataType* type, index_t length) 00212 { 00213 if (fprintf(m_fstream, "%c", CHAR_STRING_END) <= 0) return false; 00214 00215 return true; 00216 } 00217 00218 bool 00219 CSerializableAsciiFile::write_stringentry_begin_wrapped( 00220 const TSGDataType* type, index_t y) 00221 { 00222 if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false; 00223 00224 return true; 00225 } 00226 00227 bool 00228 CSerializableAsciiFile::write_stringentry_end_wrapped( 00229 const TSGDataType* type, index_t y) 00230 { 00231 if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false; 00232 00233 return true; 00234 } 00235 00236 bool 00237 CSerializableAsciiFile::write_sparse_begin_wrapped( 00238 const TSGDataType* type, index_t length) 00239 { 00240 if (fprintf(m_fstream, "%"PRIi32" %c", length, 00241 CHAR_SPARSE_BEGIN) <= 0) return false; 00242 00243 return true; 00244 } 00245 00246 bool 00247 CSerializableAsciiFile::write_sparse_end_wrapped( 00248 const TSGDataType* type, index_t length) 00249 { 00250 if (fprintf(m_fstream, "%c", CHAR_SPARSE_END) <= 0) return false; 00251 00252 return true; 00253 } 00254 00255 bool 00256 CSerializableAsciiFile::write_sparseentry_begin_wrapped( 00257 const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry, 00258 index_t feat_index, index_t y) 00259 { 00260 if (fprintf(m_fstream, " %"PRIi32" %c", feat_index, CHAR_ITEM_BEGIN) 00261 <= 0) return false; 00262 00263 return true; 00264 } 00265 00266 bool 00267 CSerializableAsciiFile::write_sparseentry_end_wrapped( 00268 const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry, 00269 index_t feat_index, index_t y) 00270 { 00271 if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false; 00272 00273 return true; 00274 } 00275 00276 bool 00277 CSerializableAsciiFile::write_item_begin_wrapped( 00278 const TSGDataType* type, index_t y, index_t x) 00279 { 00280 if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false; 00281 00282 return true; 00283 } 00284 00285 bool 00286 CSerializableAsciiFile::write_item_end_wrapped( 00287 const TSGDataType* type, index_t y, index_t x) 00288 { 00289 if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false; 00290 00291 return true; 00292 } 00293 00294 bool 00295 CSerializableAsciiFile::write_sgserializable_begin_wrapped( 00296 const TSGDataType* type, const char* sgserializable_name, 00297 EPrimitiveType generic) 00298 { 00299 if (*sgserializable_name == '\0') { 00300 if (fprintf(m_fstream, "%s %c", STR_SGSERIAL_NULL, 00301 CHAR_SGSERIAL_BEGIN) <= 0) 00302 return false; 00303 } else { 00304 if (fprintf(m_fstream, "%s ", sgserializable_name) <= 0) 00305 return false; 00306 00307 if (generic != PT_NOT_GENERIC) { 00308 string_t buf; 00309 TSGDataType::ptype_to_string(buf, generic, STRING_LEN); 00310 if (fprintf(m_fstream, "%s ", buf) <= 0) return false; 00311 } 00312 00313 if (fprintf(m_fstream, "%c%c", CHAR_SGSERIAL_BEGIN, 00314 CHAR_TYPE_END) <= 0) 00315 return false; 00316 } 00317 00318 return true; 00319 } 00320 00321 bool 00322 CSerializableAsciiFile::write_sgserializable_end_wrapped( 00323 const TSGDataType* type, const char* sgserializable_name, 00324 EPrimitiveType generic) 00325 { 00326 if (fprintf(m_fstream, "%c", CHAR_SGSERIAL_END) <= 0) 00327 return false; 00328 00329 return true; 00330 } 00331 00332 bool 00333 CSerializableAsciiFile::write_type_begin_wrapped( 00334 const TSGDataType* type, const char* name, const char* prefix) 00335 { 00336 string_t buf; 00337 type->to_string(buf, STRING_LEN); 00338 00339 SG_SET_LOCALE_C; 00340 00341 if (fprintf(m_fstream, "%s %s ", name, buf) <= 0) return false; 00342 00343 return true; 00344 } 00345 00346 bool 00347 CSerializableAsciiFile::write_type_end_wrapped( 00348 const TSGDataType* type, const char* name, const char* prefix) 00349 { 00350 if (fprintf(m_fstream, "%c", CHAR_TYPE_END) <= 0) return false; 00351 00352 SG_RESET_LOCALE; 00353 00354 return true; 00355 }