SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
SerializableAsciiFile.cpp
Go to the documentation of this file.
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_COMPLEX128:
00156         if (fprintf(m_fstream, "(%.16lg,%.16lg)",
00157             ((complex128_t*) param)->real(),((complex128_t*) param)->imag()) <= 0)
00158             return false;
00159         break;
00160     case PT_UNDEFINED:
00161     case PT_SGOBJECT:
00162         SG_ERROR("write_scalar_wrapped(): Implementation error during"
00163                  " writing AsciiFile!");
00164         return false;
00165     }
00166 
00167     return true;
00168 }
00169 
00170 bool
00171 CSerializableAsciiFile::write_cont_begin_wrapped(
00172     const TSGDataType* type, index_t len_real_y, index_t len_real_x)
00173 {
00174     switch (type->m_ctype) {
00175     case CT_NDARRAY:
00176         SG_NOTIMPLEMENTED
00177         break;
00178     case CT_VECTOR: case CT_SGVECTOR:
00179         if (fprintf(m_fstream, "%" PRIi32 " %c", len_real_y,
00180                     CHAR_CONT_BEGIN) <= 0)
00181             return false;
00182         break;
00183     case CT_MATRIX: case CT_SGMATRIX:
00184         if (fprintf(m_fstream, "%" PRIi32" %" PRIi32 " %c",
00185                     len_real_y, len_real_x, CHAR_CONT_BEGIN) <= 0)
00186             return false;
00187         break;
00188     case CT_UNDEFINED:
00189     case CT_SCALAR:
00190         SG_ERROR("write_cont_begin_wrapped(): Implementation error "
00191                  "during writing AsciiFile!");
00192         return false;
00193     }
00194 
00195     return true;
00196 }
00197 
00198 bool
00199 CSerializableAsciiFile::write_cont_end_wrapped(
00200     const TSGDataType* type, index_t len_real_y, index_t len_real_x)
00201 {
00202     if (fprintf(m_fstream, "%c", CHAR_CONT_END) <= 0) return false;
00203 
00204     return true;
00205 }
00206 
00207 bool
00208 CSerializableAsciiFile::write_string_begin_wrapped(
00209     const TSGDataType* type, index_t length)
00210 {
00211     if (fprintf(m_fstream, "%" PRIi32 " %c", length,
00212                 CHAR_STRING_BEGIN) <= 0) return false;
00213 
00214     return true;
00215 }
00216 
00217 bool
00218 CSerializableAsciiFile::write_string_end_wrapped(
00219     const TSGDataType* type, index_t length)
00220 {
00221     if (fprintf(m_fstream, "%c", CHAR_STRING_END) <= 0) return false;
00222 
00223     return true;
00224 }
00225 
00226 bool
00227 CSerializableAsciiFile::write_stringentry_begin_wrapped(
00228     const TSGDataType* type, index_t y)
00229 {
00230     if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
00231 
00232     return true;
00233 }
00234 
00235 bool
00236 CSerializableAsciiFile::write_stringentry_end_wrapped(
00237     const TSGDataType* type, index_t y)
00238 {
00239     if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
00240 
00241     return true;
00242 }
00243 
00244 bool
00245 CSerializableAsciiFile::write_sparse_begin_wrapped(
00246     const TSGDataType* type, index_t length)
00247 {
00248     if (fprintf(m_fstream, "%" PRIi32" %c", length,
00249                 CHAR_SPARSE_BEGIN) <= 0) return false;
00250 
00251     return true;
00252 }
00253 
00254 bool
00255 CSerializableAsciiFile::write_sparse_end_wrapped(
00256     const TSGDataType* type, index_t length)
00257 {
00258     if (fprintf(m_fstream, "%c", CHAR_SPARSE_END) <= 0) return false;
00259 
00260     return true;
00261 }
00262 
00263 bool
00264 CSerializableAsciiFile::write_sparseentry_begin_wrapped(
00265     const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00266     index_t feat_index, index_t y)
00267 {
00268     if (fprintf(m_fstream, " %" PRIi32 " %c", feat_index, CHAR_ITEM_BEGIN)
00269         <= 0) return false;
00270 
00271     return true;
00272 }
00273 
00274 bool
00275 CSerializableAsciiFile::write_sparseentry_end_wrapped(
00276     const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
00277     index_t feat_index, index_t y)
00278 {
00279     if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
00280 
00281     return true;
00282 }
00283 
00284 bool
00285 CSerializableAsciiFile::write_item_begin_wrapped(
00286     const TSGDataType* type, index_t y, index_t x)
00287 {
00288     if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
00289 
00290     return true;
00291 }
00292 
00293 bool
00294 CSerializableAsciiFile::write_item_end_wrapped(
00295     const TSGDataType* type, index_t y, index_t x)
00296 {
00297     if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
00298 
00299     return true;
00300 }
00301 
00302 bool
00303 CSerializableAsciiFile::write_sgserializable_begin_wrapped(
00304     const TSGDataType* type, const char* sgserializable_name,
00305     EPrimitiveType generic)
00306 {
00307     if (*sgserializable_name == '\0') {
00308         if (fprintf(m_fstream, "%s %c", STR_SGSERIAL_NULL,
00309                     CHAR_SGSERIAL_BEGIN) <= 0)
00310             return false;
00311     } else {
00312         if (fprintf(m_fstream, "%s ", sgserializable_name) <= 0)
00313             return false;
00314 
00315         if (generic != PT_NOT_GENERIC) {
00316             string_t buf;
00317             TSGDataType::ptype_to_string(buf, generic, STRING_LEN);
00318             if (fprintf(m_fstream, "%s ", buf) <= 0) return false;
00319         }
00320 
00321         if (fprintf(m_fstream, "%c%c", CHAR_SGSERIAL_BEGIN,
00322                     CHAR_TYPE_END) <= 0)
00323             return false;
00324     }
00325 
00326     return true;
00327 }
00328 
00329 bool
00330 CSerializableAsciiFile::write_sgserializable_end_wrapped(
00331     const TSGDataType* type, const char* sgserializable_name,
00332     EPrimitiveType generic)
00333 {
00334     if (fprintf(m_fstream, "%c", CHAR_SGSERIAL_END) <= 0)
00335         return false;
00336 
00337     return true;
00338 }
00339 
00340 bool
00341 CSerializableAsciiFile::write_type_begin_wrapped(
00342     const TSGDataType* type, const char* name, const char* prefix)
00343 {
00344     string_t buf;
00345     type->to_string(buf, STRING_LEN);
00346 
00347     SG_SET_LOCALE_C;
00348 
00349     if (fprintf(m_fstream, "%s %s ", name, buf) <= 0) return false;
00350 
00351     return true;
00352 }
00353 
00354 bool
00355 CSerializableAsciiFile::write_type_end_wrapped(
00356     const TSGDataType* type, const char* name, const char* prefix)
00357 {
00358     if (fprintf(m_fstream, "%c", CHAR_TYPE_END) <= 0) return false;
00359 
00360     SG_RESET_LOCALE;
00361 
00362     return true;
00363 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation