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

SHOGUN Machine Learning Toolbox - Documentation