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 
00013 using namespace shogun;
00014 
00015 SerializableAsciiReader00::SerializableAsciiReader00(
00016     CSerializableAsciiFile* file) { m_file = file; }
00017 
00018 SerializableAsciiReader00::~SerializableAsciiReader00() {}
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_NDARRAY:
00101         SG_NOTIMPLEMENTED;
00102     case CT_SCALAR:
00103         SG_ERROR("read_cont_begin_wrapped(): Implementation error "
00104                  "during writing AsciiFile!");
00105         return false;
00106     case CT_VECTOR: case CT_SGVECTOR:
00107         if (fscanf(m_file->m_fstream, "%"SCNi32" ", len_read_y) != 1)
00108             return false;
00109         *len_read_x = 1;
00110         break;
00111     case CT_MATRIX: case CT_SGMATRIX:
00112         if (fscanf(m_file->m_fstream, "%"SCNi32" %"SCNi32" ",
00113                    len_read_y, len_read_x) != 2)
00114             return false;
00115         break;
00116     }
00117 
00118     if (fgetc(m_file->m_fstream) != CHAR_CONT_BEGIN) return false;
00119 
00120     return true;
00121 }
00122 
00123 bool
00124 SerializableAsciiReader00::read_cont_end_wrapped(
00125     const TSGDataType* type, index_t len_read_y, index_t len_read_x)
00126 {
00127     if (fgetc(m_file->m_fstream) != CHAR_CONT_END) return false;
00128 
00129     return true;
00130 }
00131 
00132 bool
00133 SerializableAsciiReader00::read_string_begin_wrapped(
00134     const TSGDataType* type, index_t* length)
00135 {
00136     if (fscanf(m_file->m_fstream, "%"PRIi32, length) != 1)
00137         return false;
00138     if (fgetc(m_file->m_fstream) != ' ') return false;
00139     if (fgetc(m_file->m_fstream) != CHAR_STRING_BEGIN) return false;
00140 
00141     return true;
00142 }
00143 
00144 bool
00145 SerializableAsciiReader00::read_string_end_wrapped(
00146     const TSGDataType* type, index_t length)
00147 {
00148     if (fgetc(m_file->m_fstream) != CHAR_STRING_END) return false;
00149 
00150     return true;
00151 }
00152 
00153 bool
00154 SerializableAsciiReader00::read_stringentry_begin_wrapped(
00155     const TSGDataType* type, index_t y)
00156 {
00157     if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
00158 
00159     return true;
00160 }
00161 
00162 bool
00163 SerializableAsciiReader00::read_stringentry_end_wrapped(
00164     const TSGDataType* type, index_t y)
00165 {
00166     if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
00167 
00168     return true;
00169 }
00170 
00171 bool
00172 SerializableAsciiReader00::read_sparse_begin_wrapped(
00173     const TSGDataType* type, index_t* length)
00174 {
00175     if (fscanf(m_file->m_fstream, "%"PRIi32, 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 length)
00185 {
00186     if (fgetc(m_file->m_fstream) != CHAR_SPARSE_END) return false;
00187 
00188     return true;
00189 }
00190 
00191 bool
00192 SerializableAsciiReader00::read_sparseentry_begin_wrapped(
00193     const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
00194     index_t* feat_index, index_t y)
00195 {
00196     if (fscanf(m_file->m_fstream, "%"PRIi32, feat_index) != 1)
00197         return false;
00198     if (fgetc(m_file->m_fstream) != ' ') return false;
00199     if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
00200 
00201     return true;
00202 }
00203 
00204 bool
00205 SerializableAsciiReader00::read_sparseentry_end_wrapped(
00206     const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
00207     index_t* feat_index, index_t y)
00208 {
00209     if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
00210 
00211     return true;
00212 }
00213 
00214 bool
00215 SerializableAsciiReader00::read_item_begin_wrapped(
00216     const TSGDataType* type, index_t y, index_t x)
00217 {
00218     if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
00219 
00220     return true;
00221 }
00222 
00223 bool
00224 SerializableAsciiReader00::read_item_end_wrapped(
00225     const TSGDataType* type, index_t y, index_t x)
00226 {
00227     if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
00228 
00229     return true;
00230 }
00231 
00232 bool
00233 SerializableAsciiReader00::read_sgserializable_begin_wrapped(
00234     const TSGDataType* type, char* sgserializable_name,
00235     EPrimitiveType* generic)
00236 {
00237     if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s ",
00238                sgserializable_name) != 1) return false;
00239 
00240     if (strcmp(sgserializable_name, STR_SGSERIAL_NULL) == 0) {
00241         if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
00242             return false;
00243 
00244         *sgserializable_name = '\0';
00245     } else {
00246         string_t buf;
00247         if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s ", buf)
00248             != 1) return false;
00249 
00250         if (buf[0] != CHAR_SGSERIAL_BEGIN) {
00251             if (!TSGDataType::string_to_ptype(generic, buf))
00252                 return false;
00253 
00254             if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
00255                 return false;
00256             if (fgetc(m_file->m_fstream) != CHAR_TYPE_END)
00257                 return false;
00258         }
00259     }
00260 
00261     m_file->m_stack_fpos.push_back(ftell(m_file->m_fstream));
00262 
00263     return true;
00264 }
00265 
00266 bool
00267 SerializableAsciiReader00::read_sgserializable_end_wrapped(
00268     const TSGDataType* type, const char* sgserializable_name,
00269     EPrimitiveType generic)
00270 {
00271     if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_END) return false;
00272 
00273     m_file->m_stack_fpos.pop_back();
00274 
00275     return true;
00276 }
00277 
00278 bool
00279 SerializableAsciiReader00::read_type_begin_wrapped(
00280     const TSGDataType* type, const char* name, const char* prefix)
00281 {
00282     if (fseek(m_file->m_fstream, m_file->m_stack_fpos.back(), SEEK_SET
00283             ) != 0) return false;
00284 
00285     SG_SET_LOCALE_C;
00286 
00287     string_t type_str;
00288     type->to_string(type_str, STRING_LEN);
00289 
00290     string_t r_name, r_type;
00291     while (true) {
00292         if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s %"
00293                    STRING_LEN_STR "s ", r_name, r_type) != 2)
00294             return false;
00295 
00296         if (strcmp(r_name, name) == 0
00297             && strcmp(r_type, type_str) == 0) return true;
00298 
00299         if (!m_file->ignore()) return false;
00300     }
00301 
00302     return false;
00303 }
00304 
00305 bool
00306 SerializableAsciiReader00::read_type_end_wrapped(
00307     const TSGDataType* type, const char* name, const char* prefix)
00308 {
00309     if (fgetc(m_file->m_fstream) != CHAR_TYPE_END) return false;
00310 
00311     SG_RESET_LOCALE;
00312 
00313     return true;
00314 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation