SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableAsciiReader00.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2010 Soeren Sonnenburg
8  * Copyright (C) 2010 Berlin Institute of Technology
9  */
10 
13 #include <shogun/lib/common.h>
14 
15 using namespace shogun;
16 
18  CSerializableAsciiFile* file) { m_file = file; }
19 
21 
22 bool
23 SerializableAsciiReader00::read_scalar_wrapped(
24  const TSGDataType* type, void* param)
25 {
26  switch (type->m_ptype) {
27  case PT_BOOL:
28  char bool_buf;
29 
30  if (fscanf(m_file->m_fstream, "%c", &bool_buf) != 1)
31  return false;
32 
33  switch (bool_buf) {
34  case 't': *(bool*) param = true; break;
35  case 'f': *(bool*) param = false; break;
36  default: return false;
37  }
38 
39  break;
40  case PT_CHAR:
41  if (fscanf(m_file->m_fstream, "%" SCNu8, (uint8_t*) param)
42  != 1) return false;
43  break;
44  case PT_INT8:
45  if (fscanf(m_file->m_fstream, "%" SCNi8, (int8_t*) param)
46  != 1) return false;
47  break;
48  case PT_UINT8:
49  if (fscanf(m_file->m_fstream, "%" SCNu8, (uint8_t*) param)
50  != 1) return false;
51  break;
52  case PT_INT16:
53  if (fscanf(m_file->m_fstream, "%" SCNi16, (int16_t*) param)
54  != 1) return false;
55  break;
56  case PT_UINT16:
57  if (fscanf(m_file->m_fstream, "%" SCNu16, (uint16_t*) param)
58  != 1) return false;
59  break;
60  case PT_INT32:
61  if (fscanf(m_file->m_fstream, "%" SCNi32, (int32_t*) param)
62  != 1) return false;
63  break;
64  case PT_UINT32:
65  if (fscanf(m_file->m_fstream, "%" SCNu32, (uint32_t*) param)
66  != 1) return false;
67  break;
68  case PT_INT64:
69  if (fscanf(m_file->m_fstream, "%" SCNi64, (int64_t*) param)
70  != 1) return false;
71  break;
72  case PT_UINT64:
73  if (fscanf(m_file->m_fstream, "%" SCNu64, (uint64_t*) param)
74  != 1) return false;
75  break;
76  case PT_FLOAT32:
77  if (fscanf(m_file->m_fstream, "%g", (float32_t*) param)
78  != 1) return false;
79  break;
80  case PT_FLOAT64:
81  if (fscanf(m_file->m_fstream, "%lg", (float64_t*) param)
82  != 1) return false;
83  break;
84  case PT_FLOATMAX:
85  if (fscanf(m_file->m_fstream, "%Lg", (floatmax_t*) param)
86  != 1) return false;
87  break;
88  case PT_COMPLEX128:
89  float64_t c_real, c_imag;
90  if (fscanf(m_file->m_fstream, "(%lg,%lg)", &c_real, &c_imag)
91  != 2) return false;
92 #if defined(HAVE_CXX0X) || defined(HAVE_CXX11) || defined(_LIBCPP_VERSION)
93  ((complex128_t*) param)->real(c_real);
94  ((complex128_t*) param)->imag(c_imag);
95 #else
96  ((complex128_t*) param)->real()=c_real;
97  ((complex128_t*) param)->imag()=c_imag;
98 #endif
99  break;
100  case PT_UNDEFINED:
101  case PT_SGOBJECT:
102  SG_ERROR("read_scalar_wrapped(): Implementation error during"
103  " reading AsciiFile!");
104  return false;
105  }
106 
107  return true;
108 }
109 
110 bool
111 SerializableAsciiReader00::read_cont_begin_wrapped(
112  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
113 {
114  switch (type->m_ctype) {
115  case CT_NDARRAY:
117  case CT_VECTOR: case CT_SGVECTOR:
118  if (fscanf(m_file->m_fstream, "%" SCNi32 " ", len_read_y) != 1)
119  return false;
120  *len_read_x = 1;
121  break;
122  case CT_MATRIX: case CT_SGMATRIX:
123  if (fscanf(m_file->m_fstream, "%" SCNi32 " %" SCNi32 " ",
124  len_read_y, len_read_x) != 2)
125  return false;
126  break;
127  case CT_UNDEFINED:
128  case CT_SCALAR:
129  SG_ERROR("read_cont_begin_wrapped(): Implementation error "
130  "during writing AsciiFile!");
131  return false;
132  }
133 
134  if (fgetc(m_file->m_fstream) != CHAR_CONT_BEGIN) return false;
135 
136  return true;
137 }
138 
139 bool
140 SerializableAsciiReader00::read_cont_end_wrapped(
141  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
142 {
143  if (fgetc(m_file->m_fstream) != CHAR_CONT_END) return false;
144 
145  return true;
146 }
147 
148 bool
149 SerializableAsciiReader00::read_string_begin_wrapped(
150  const TSGDataType* type, index_t* length)
151 {
152  if (fscanf(m_file->m_fstream, "%" PRIi32, length) != 1)
153  return false;
154  if (fgetc(m_file->m_fstream) != ' ') return false;
155  if (fgetc(m_file->m_fstream) != CHAR_STRING_BEGIN) return false;
156 
157  return true;
158 }
159 
160 bool
161 SerializableAsciiReader00::read_string_end_wrapped(
162  const TSGDataType* type, index_t length)
163 {
164  if (fgetc(m_file->m_fstream) != CHAR_STRING_END) return false;
165 
166  return true;
167 }
168 
169 bool
170 SerializableAsciiReader00::read_stringentry_begin_wrapped(
171  const TSGDataType* type, index_t y)
172 {
173  if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
174 
175  return true;
176 }
177 
178 bool
179 SerializableAsciiReader00::read_stringentry_end_wrapped(
180  const TSGDataType* type, index_t y)
181 {
182  if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
183 
184  return true;
185 }
186 
187 bool
188 SerializableAsciiReader00::read_sparse_begin_wrapped(
189  const TSGDataType* type, index_t* length)
190 {
191  if (fscanf(m_file->m_fstream, "%" PRIi32, length) != 1) return false;
192  if (fgetc(m_file->m_fstream) != ' ') return false;
193  if (fgetc(m_file->m_fstream) != CHAR_SPARSE_BEGIN) return false;
194 
195  return true;
196 }
197 
198 bool
199 SerializableAsciiReader00::read_sparse_end_wrapped(
200  const TSGDataType* type, index_t length)
201 {
202  if (fgetc(m_file->m_fstream) != CHAR_SPARSE_END) return false;
203 
204  return true;
205 }
206 
207 bool
208 SerializableAsciiReader00::read_sparseentry_begin_wrapped(
209  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
210  index_t* feat_index, index_t y)
211 {
212  if (fscanf(m_file->m_fstream, "%" PRIi32, feat_index) != 1)
213  return false;
214  if (fgetc(m_file->m_fstream) != ' ') return false;
215  if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
216 
217  return true;
218 }
219 
220 bool
221 SerializableAsciiReader00::read_sparseentry_end_wrapped(
222  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
223  index_t* feat_index, index_t y)
224 {
225  if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
226 
227  return true;
228 }
229 
230 bool
231 SerializableAsciiReader00::read_item_begin_wrapped(
232  const TSGDataType* type, index_t y, index_t x)
233 {
234  if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
235 
236  return true;
237 }
238 
239 bool
240 SerializableAsciiReader00::read_item_end_wrapped(
241  const TSGDataType* type, index_t y, index_t x)
242 {
243  if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
244 
245  return true;
246 }
247 
248 bool
249 SerializableAsciiReader00::read_sgserializable_begin_wrapped(
250  const TSGDataType* type, char* sgserializable_name,
251  EPrimitiveType* generic)
252 {
253  if (fscanf(m_file->m_fstream, "%" STRING_LEN_STR "s ",
254  sgserializable_name) != 1) return false;
255 
256  if (strcmp(sgserializable_name, STR_SGSERIAL_NULL) == 0) {
257  if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
258  return false;
259 
260  *sgserializable_name = '\0';
261  } else {
262  string_t buf;
263  if (fscanf(m_file->m_fstream, "%" STRING_LEN_STR "s ", buf)
264  != 1) return false;
265 
266  if (buf[0] != CHAR_SGSERIAL_BEGIN) {
267  if (!TSGDataType::string_to_ptype(generic, buf))
268  return false;
269 
270  if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
271  return false;
272  if (fgetc(m_file->m_fstream) != CHAR_TYPE_END)
273  return false;
274  }
275  }
276 
277  m_file->m_stack_fpos.push_back(ftell(m_file->m_fstream));
278 
279  return true;
280 }
281 
282 bool
283 SerializableAsciiReader00::read_sgserializable_end_wrapped(
284  const TSGDataType* type, const char* sgserializable_name,
285  EPrimitiveType generic)
286 {
287  if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_END) return false;
288 
289  m_file->m_stack_fpos.pop_back();
290 
291  return true;
292 }
293 
294 bool
295 SerializableAsciiReader00::read_type_begin_wrapped(
296  const TSGDataType* type, const char* name, const char* prefix)
297 {
298  if (fseek(m_file->m_fstream, m_file->m_stack_fpos.back(), SEEK_SET
299  ) != 0) return false;
300 
302 
303  string_t type_str;
304  type->to_string(type_str, STRING_LEN);
305 
306  string_t r_name, r_type;
307  while (true) {
308  if (fscanf(m_file->m_fstream, "%" STRING_LEN_STR "s %"
309  STRING_LEN_STR "s ", r_name, r_type) != 2)
310  return false;
311 
312  if (strcmp(r_name, name) == 0
313  && strcmp(r_type, type_str) == 0) return true;
314 
315  if (!m_file->ignore()) return false;
316  }
317 
318  return false;
319 }
320 
321 bool
322 SerializableAsciiReader00::read_type_end_wrapped(
323  const TSGDataType* type, const char* name, const char* prefix)
324 {
325  if (fgetc(m_file->m_fstream) != CHAR_TYPE_END) return false;
326 
328 
329  return true;
330 }

SHOGUN Machine Learning Toolbox - Documentation