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* vec_index,
00174     index_t* length)
00175 {
00176     if (fscanf(m_file->m_fstream, "%"PRIi32" %"PRIi32, vec_index,
00177                length) != 2) return false;
00178     if (fgetc(m_file->m_fstream) != ' ') return false;
00179     if (fgetc(m_file->m_fstream) != CHAR_SPARSE_BEGIN) return false;
00180 
00181     return true;
00182 }
00183 
00184 bool
00185 SerializableAsciiReader00::read_sparse_end_wrapped(
00186     const TSGDataType* type, index_t* vec_index,
00187     index_t length)
00188 {
00189     if (fgetc(m_file->m_fstream) != CHAR_SPARSE_END) return false;
00190 
00191     return true;
00192 }
00193 
00194 bool
00195 SerializableAsciiReader00::read_sparseentry_begin_wrapped(
00196     const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
00197     index_t* feat_index, index_t y)
00198 {
00199     if (fscanf(m_file->m_fstream, "%"PRIi32, feat_index) != 1)
00200         return false;
00201     if (fgetc(m_file->m_fstream) != ' ') return false;
00202     if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
00203 
00204     return true;
00205 }
00206 
00207 bool
00208 SerializableAsciiReader00::read_sparseentry_end_wrapped(
00209     const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
00210     index_t* feat_index, index_t y)
00211 {
00212     if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
00213 
00214     return true;
00215 }
00216 
00217 bool
00218 SerializableAsciiReader00::read_item_begin_wrapped(
00219     const TSGDataType* type, index_t y, index_t x)
00220 {
00221     if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
00222 
00223     return true;
00224 }
00225 
00226 bool
00227 SerializableAsciiReader00::read_item_end_wrapped(
00228     const TSGDataType* type, index_t y, index_t x)
00229 {
00230     if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
00231 
00232     return true;
00233 }
00234 
00235 bool
00236 SerializableAsciiReader00::read_sgserializable_begin_wrapped(
00237     const TSGDataType* type, char* sgserializable_name,
00238     EPrimitiveType* generic)
00239 {
00240     if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s ",
00241                sgserializable_name) != 1) return false;
00242 
00243     if (strcmp(sgserializable_name, STR_SGSERIAL_NULL) == 0) {
00244         if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
00245             return false;
00246 
00247         *sgserializable_name = '\0';
00248     } else {
00249         string_t buf;
00250         if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s ", buf)
00251             != 1) return false;
00252 
00253         if (buf[0] != CHAR_SGSERIAL_BEGIN) {
00254             if (!TSGDataType::string_to_ptype(generic, buf))
00255                 return false;
00256 
00257             if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
00258                 return false;
00259             if (fgetc(m_file->m_fstream) != CHAR_TYPE_END)
00260                 return false;
00261         }
00262     }
00263 
00264     m_file->m_stack_fpos.push_back(ftell(m_file->m_fstream));
00265 
00266     return true;
00267 }
00268 
00269 bool
00270 SerializableAsciiReader00::read_sgserializable_end_wrapped(
00271     const TSGDataType* type, const char* sgserializable_name,
00272     EPrimitiveType generic)
00273 {
00274     if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_END) return false;
00275 
00276     m_file->m_stack_fpos.pop_back();
00277 
00278     return true;
00279 }
00280 
00281 bool
00282 SerializableAsciiReader00::read_type_begin_wrapped(
00283     const TSGDataType* type, const char* name, const char* prefix)
00284 {
00285     if (fseek(m_file->m_fstream, m_file->m_stack_fpos.back(), SEEK_SET
00286             ) != 0) return false;
00287 
00288     SG_SET_LOCALE_C;
00289 
00290     string_t type_str;
00291     type->to_string(type_str, STRING_LEN);
00292 
00293     string_t r_name, r_type;
00294     while (true) {
00295         if (fscanf(m_file->m_fstream, "%"STRING_LEN_STR"s %"
00296                    STRING_LEN_STR "s ", r_name, r_type) != 2)
00297             return false;
00298 
00299         if (strcmp(r_name, name) == 0
00300             && strcmp(r_type, type_str) == 0) return true;
00301 
00302         if (!m_file->ignore()) return false;
00303     }
00304 
00305     return false;
00306 }
00307 
00308 bool
00309 SerializableAsciiReader00::read_type_end_wrapped(
00310     const TSGDataType* type, const char* name, const char* prefix)
00311 {
00312     if (fgetc(m_file->m_fstream) != CHAR_TYPE_END) return false;
00313 
00314     SG_RESET_LOCALE;
00315 
00316     return true;
00317 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation