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

SHOGUN Machine Learning Toolbox - Documentation