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

SHOGUN Machine Learning Toolbox - Documentation