00001
00002
00003
00004
00005
00006
00007
00008
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(void)
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(void)
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(void)
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 vec_index,
00239 index_t length)
00240 {
00241 if (fprintf(m_fstream, "%"PRIi32" %"PRIi32" %c", vec_index, length,
00242 CHAR_SPARSE_BEGIN) <= 0) return false;
00243
00244 return true;
00245 }
00246
00247 bool
00248 CSerializableAsciiFile::write_sparse_end_wrapped(
00249 const TSGDataType* type, index_t vec_index,
00250 index_t length)
00251 {
00252 if (fprintf(m_fstream, "%c", CHAR_SPARSE_END) <= 0) return false;
00253
00254 return true;
00255 }
00256
00257 bool
00258 CSerializableAsciiFile::write_sparseentry_begin_wrapped(
00259 const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00260 index_t feat_index, index_t y)
00261 {
00262 if (fprintf(m_fstream, " %"PRIi32" %c", feat_index, CHAR_ITEM_BEGIN)
00263 <= 0) return false;
00264
00265 return true;
00266 }
00267
00268 bool
00269 CSerializableAsciiFile::write_sparseentry_end_wrapped(
00270 const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00271 index_t feat_index, index_t y)
00272 {
00273 if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
00274
00275 return true;
00276 }
00277
00278 bool
00279 CSerializableAsciiFile::write_item_begin_wrapped(
00280 const TSGDataType* type, index_t y, index_t x)
00281 {
00282 if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
00283
00284 return true;
00285 }
00286
00287 bool
00288 CSerializableAsciiFile::write_item_end_wrapped(
00289 const TSGDataType* type, index_t y, index_t x)
00290 {
00291 if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
00292
00293 return true;
00294 }
00295
00296 bool
00297 CSerializableAsciiFile::write_sgserializable_begin_wrapped(
00298 const TSGDataType* type, const char* sgserializable_name,
00299 EPrimitiveType generic)
00300 {
00301 if (*sgserializable_name == '\0') {
00302 if (fprintf(m_fstream, "%s %c", STR_SGSERIAL_NULL,
00303 CHAR_SGSERIAL_BEGIN) <= 0)
00304 return false;
00305 } else {
00306 if (fprintf(m_fstream, "%s ", sgserializable_name) <= 0)
00307 return false;
00308
00309 if (generic != PT_NOT_GENERIC) {
00310 string_t buf;
00311 TSGDataType::ptype_to_string(buf, generic, STRING_LEN);
00312 if (fprintf(m_fstream, "%s ", buf) <= 0) return false;
00313 }
00314
00315 if (fprintf(m_fstream, "%c%c", CHAR_SGSERIAL_BEGIN,
00316 CHAR_TYPE_END) <= 0)
00317 return false;
00318 }
00319
00320 return true;
00321 }
00322
00323 bool
00324 CSerializableAsciiFile::write_sgserializable_end_wrapped(
00325 const TSGDataType* type, const char* sgserializable_name,
00326 EPrimitiveType generic)
00327 {
00328 if (fprintf(m_fstream, "%c", CHAR_SGSERIAL_END) <= 0)
00329 return false;
00330
00331 return true;
00332 }
00333
00334 bool
00335 CSerializableAsciiFile::write_type_begin_wrapped(
00336 const TSGDataType* type, const char* name, const char* prefix)
00337 {
00338 string_t buf;
00339 type->to_string(buf, STRING_LEN);
00340
00341 SG_SET_LOCALE_C;
00342
00343 if (fprintf(m_fstream, "%s %s ", name, buf) <= 0) return false;
00344
00345 return true;
00346 }
00347
00348 bool
00349 CSerializableAsciiFile::write_type_end_wrapped(
00350 const TSGDataType* type, const char* name, const char* prefix)
00351 {
00352 if (fprintf(m_fstream, "%c", CHAR_TYPE_END) <= 0) return false;
00353
00354 SG_RESET_LOCALE;
00355
00356 return true;
00357 }