SHOGUN  v3.0.0
 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 #ifdef HAVE_CXX11
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_SGOBJECT:
100  SG_ERROR("read_scalar_wrapped(): Implementation error during"
101  " reading AsciiFile!");
102  return false;
103  }
104 
105  return true;
106 }
107 
108 bool
109 SerializableAsciiReader00::read_cont_begin_wrapped(
110  const TSGDataType* type, index_t* len_read_y, index_t* len_read_x)
111 {
112  switch (type->m_ctype) {
113  case CT_NDARRAY:
115  case CT_SCALAR:
116  SG_ERROR("read_cont_begin_wrapped(): Implementation error "
117  "during writing AsciiFile!");
118  return false;
119  case CT_VECTOR: case CT_SGVECTOR:
120  if (fscanf(m_file->m_fstream, "%" SCNi32 " ", len_read_y) != 1)
121  return false;
122  *len_read_x = 1;
123  break;
124  case CT_MATRIX: case CT_SGMATRIX:
125  if (fscanf(m_file->m_fstream, "%" SCNi32 " %" SCNi32 " ",
126  len_read_y, len_read_x) != 2)
127  return false;
128  break;
129  }
130 
131  if (fgetc(m_file->m_fstream) != CHAR_CONT_BEGIN) return false;
132 
133  return true;
134 }
135 
136 bool
137 SerializableAsciiReader00::read_cont_end_wrapped(
138  const TSGDataType* type, index_t len_read_y, index_t len_read_x)
139 {
140  if (fgetc(m_file->m_fstream) != CHAR_CONT_END) return false;
141 
142  return true;
143 }
144 
145 bool
146 SerializableAsciiReader00::read_string_begin_wrapped(
147  const TSGDataType* type, index_t* length)
148 {
149  if (fscanf(m_file->m_fstream, "%" PRIi32, length) != 1)
150  return false;
151  if (fgetc(m_file->m_fstream) != ' ') return false;
152  if (fgetc(m_file->m_fstream) != CHAR_STRING_BEGIN) return false;
153 
154  return true;
155 }
156 
157 bool
158 SerializableAsciiReader00::read_string_end_wrapped(
159  const TSGDataType* type, index_t length)
160 {
161  if (fgetc(m_file->m_fstream) != CHAR_STRING_END) return false;
162 
163  return true;
164 }
165 
166 bool
167 SerializableAsciiReader00::read_stringentry_begin_wrapped(
168  const TSGDataType* type, index_t y)
169 {
170  if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
171 
172  return true;
173 }
174 
175 bool
176 SerializableAsciiReader00::read_stringentry_end_wrapped(
177  const TSGDataType* type, index_t y)
178 {
179  if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
180 
181  return true;
182 }
183 
184 bool
185 SerializableAsciiReader00::read_sparse_begin_wrapped(
186  const TSGDataType* type, index_t* length)
187 {
188  if (fscanf(m_file->m_fstream, "%" PRIi32, length) != 1) return false;
189  if (fgetc(m_file->m_fstream) != ' ') return false;
190  if (fgetc(m_file->m_fstream) != CHAR_SPARSE_BEGIN) return false;
191 
192  return true;
193 }
194 
195 bool
196 SerializableAsciiReader00::read_sparse_end_wrapped(
197  const TSGDataType* type, index_t length)
198 {
199  if (fgetc(m_file->m_fstream) != CHAR_SPARSE_END) return false;
200 
201  return true;
202 }
203 
204 bool
205 SerializableAsciiReader00::read_sparseentry_begin_wrapped(
206  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
207  index_t* feat_index, index_t y)
208 {
209  if (fscanf(m_file->m_fstream, "%" PRIi32, feat_index) != 1)
210  return false;
211  if (fgetc(m_file->m_fstream) != ' ') return false;
212  if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
213 
214  return true;
215 }
216 
217 bool
218 SerializableAsciiReader00::read_sparseentry_end_wrapped(
219  const TSGDataType* type, SGSparseVectorEntry<char>* first_entry,
220  index_t* feat_index, index_t y)
221 {
222  if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
223 
224  return true;
225 }
226 
227 bool
228 SerializableAsciiReader00::read_item_begin_wrapped(
229  const TSGDataType* type, index_t y, index_t x)
230 {
231  if (fgetc(m_file->m_fstream) != CHAR_ITEM_BEGIN) return false;
232 
233  return true;
234 }
235 
236 bool
237 SerializableAsciiReader00::read_item_end_wrapped(
238  const TSGDataType* type, index_t y, index_t x)
239 {
240  if (fgetc(m_file->m_fstream) != CHAR_ITEM_END) return false;
241 
242  return true;
243 }
244 
245 bool
246 SerializableAsciiReader00::read_sgserializable_begin_wrapped(
247  const TSGDataType* type, char* sgserializable_name,
248  EPrimitiveType* generic)
249 {
250  if (fscanf(m_file->m_fstream, "%" STRING_LEN_STR "s ",
251  sgserializable_name) != 1) return false;
252 
253  if (strcmp(sgserializable_name, STR_SGSERIAL_NULL) == 0) {
254  if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
255  return false;
256 
257  *sgserializable_name = '\0';
258  } else {
259  string_t buf;
260  if (fscanf(m_file->m_fstream, "%" STRING_LEN_STR "s ", buf)
261  != 1) return false;
262 
263  if (buf[0] != CHAR_SGSERIAL_BEGIN) {
264  if (!TSGDataType::string_to_ptype(generic, buf))
265  return false;
266 
267  if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_BEGIN)
268  return false;
269  if (fgetc(m_file->m_fstream) != CHAR_TYPE_END)
270  return false;
271  }
272  }
273 
274  m_file->m_stack_fpos.push_back(ftell(m_file->m_fstream));
275 
276  return true;
277 }
278 
279 bool
280 SerializableAsciiReader00::read_sgserializable_end_wrapped(
281  const TSGDataType* type, const char* sgserializable_name,
282  EPrimitiveType generic)
283 {
284  if (fgetc(m_file->m_fstream) != CHAR_SGSERIAL_END) return false;
285 
286  m_file->m_stack_fpos.pop_back();
287 
288  return true;
289 }
290 
291 bool
292 SerializableAsciiReader00::read_type_begin_wrapped(
293  const TSGDataType* type, const char* name, const char* prefix)
294 {
295  if (fseek(m_file->m_fstream, m_file->m_stack_fpos.back(), SEEK_SET
296  ) != 0) return false;
297 
299 
300  string_t type_str;
301  type->to_string(type_str, STRING_LEN);
302 
303  string_t r_name, r_type;
304  while (true) {
305  if (fscanf(m_file->m_fstream, "%" STRING_LEN_STR "s %"
306  STRING_LEN_STR "s ", r_name, r_type) != 2)
307  return false;
308 
309  if (strcmp(r_name, name) == 0
310  && strcmp(r_type, type_str) == 0) return true;
311 
312  if (!m_file->ignore()) return false;
313  }
314 
315  return false;
316 }
317 
318 bool
319 SerializableAsciiReader00::read_type_end_wrapped(
320  const TSGDataType* type, const char* name, const char* prefix)
321 {
322  if (fgetc(m_file->m_fstream) != CHAR_TYPE_END) return false;
323 
325 
326  return true;
327 }

SHOGUN Machine Learning Toolbox - Documentation