SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SerializableAsciiFile.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 
14 #define STR_HEADER_00 \
15  "<<_SHOGUN_SERIALIZABLE_ASCII_FILE_V_00_>>"
16 
17 using namespace shogun;
18 
20  :CSerializableFile() { init(); }
21 
23  :CSerializableFile(fstream, rw) { init(); }
24 
26  const char* fname, char rw)
27  :CSerializableFile(fname, rw) { init(); }
28 
30 
31 bool
32 CSerializableAsciiFile::ignore()
33 {
34  for (uint32_t cont_count = 0, item_count = 0,
35  sgserial_count = 0; ;) {
36  switch (fgetc(m_fstream)) {
37  case CHAR_ITEM_BEGIN: item_count++; break;
38  case CHAR_CONT_BEGIN: cont_count++; break;
39  case CHAR_SGSERIAL_BEGIN: sgserial_count++; break;
40  case CHAR_CONT_END:
41  if (cont_count-- == 0) return false;
42  break;
43  case CHAR_ITEM_END:
44  if (item_count-- == 0) return false;
45  break;
46  case CHAR_SGSERIAL_END:
47  if (sgserial_count-- == 0) return false;
48  break;
49  case CHAR_TYPE_END:
50  if (cont_count == 0 && item_count == 0
51  && sgserial_count == 0)
52  return true;
53  break;
54  case EOF: return false;
55  default: break;
56  }
57  }
58 
59  return false;
60 }
61 
63 CSerializableAsciiFile::new_reader(char* dest_version, size_t n)
64 {
65  string_t buf;
66  if (fscanf(m_fstream, "%" STRING_LEN_STR"s\n", buf) != 1)
67  return NULL;
68 
69  strncpy(dest_version, buf, n < STRING_LEN? n: STRING_LEN);
70  m_stack_fpos.push_back(ftell(m_fstream));
71 
72  if (strcmp(STR_HEADER_00, dest_version) == 0)
73  return new SerializableAsciiReader00(this);
74 
75  return NULL;
76 }
77 
78 void
79 CSerializableAsciiFile::init()
80 {
81  if (m_fstream == NULL) return;
82 
83  switch (m_task) {
84  case 'w':
85  if (fprintf(m_fstream, STR_HEADER_00"\n") <= 0) {
86  close(); return;
87  }
88  m_stack_fpos.push_back(ftell(m_fstream));
89  break;
90  case 'r': break;
91  default:
92  SG_WARNING("Could not open file `%s', unknown mode!\n",
93  m_filename);
94  close(); return;
95  }
96 }
97 
98 bool
99 CSerializableAsciiFile::write_scalar_wrapped(
100  const TSGDataType* type, const void* param)
101 {
102  switch (type->m_ptype) {
103  case PT_BOOL:
104  if (fprintf(m_fstream, "%c", *(bool*) param? 't': 'f') <= 0)
105  return false;
106  break;
107  case PT_CHAR:
108  if (fprintf(m_fstream, "%" PRIu8, *(uint8_t*) param) <= 0)
109  return false;
110  break;
111  case PT_INT8:
112  if (fprintf(m_fstream, "%" PRIi8, *(int8_t*) param) <= 0)
113  return false;
114  break;
115  case PT_UINT8:
116  if (fprintf(m_fstream, "%" PRIu8, *(uint8_t*) param) <= 0)
117  return false;
118  break;
119  case PT_INT16:
120  if (fprintf(m_fstream, "%" PRIi16, *(int16_t*) param) <= 0)
121  return false;
122  break;
123  case PT_UINT16:
124  if (fprintf(m_fstream, "%" PRIu16, *(uint16_t*) param) <= 0)
125  return false;
126  break;
127  case PT_INT32:
128  if (fprintf(m_fstream, "%" PRIi32, *(int32_t*) param) <= 0)
129  return false;
130  break;
131  case PT_UINT32:
132  if (fprintf(m_fstream, "%" PRIu32, *(uint32_t*) param) <= 0)
133  return false;
134  break;
135  case PT_INT64:
136  if (fprintf(m_fstream, "%" PRIi64, *(int64_t*) param) <= 0)
137  return false;
138  break;
139  case PT_UINT64:
140  if (fprintf(m_fstream, "%" PRIu64, *(uint64_t*) param) <= 0)
141  return false;
142  break;
143  case PT_FLOAT32:
144  if (fprintf(m_fstream, "%.16g", *(float32_t*) param) <= 0)
145  return false;
146  break;
147  case PT_FLOAT64:
148  if (fprintf(m_fstream, "%.16lg", *(float64_t*) param) <= 0)
149  return false;
150  break;
151  case PT_FLOATMAX:
152  if (fprintf(m_fstream, "%.16Lg", *(floatmax_t*) param) <= 0)
153  return false;
154  break;
155  case PT_COMPLEX128:
156  if (fprintf(m_fstream, "(%.16lg,%.16lg)",
157  ((complex128_t*) param)->real(),((complex128_t*) param)->imag()) <= 0)
158  return false;
159  break;
160  case PT_UNDEFINED:
161  case PT_SGOBJECT:
162  SG_ERROR("write_scalar_wrapped(): Implementation error during"
163  " writing AsciiFile!");
164  return false;
165  }
166 
167  return true;
168 }
169 
170 bool
171 CSerializableAsciiFile::write_cont_begin_wrapped(
172  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
173 {
174  switch (type->m_ctype) {
175  case CT_NDARRAY:
177  break;
178  case CT_VECTOR: case CT_SGVECTOR:
179  if (fprintf(m_fstream, "%" PRIi32 " %c", len_real_y,
180  CHAR_CONT_BEGIN) <= 0)
181  return false;
182  break;
183  case CT_MATRIX: case CT_SGMATRIX:
184  if (fprintf(m_fstream, "%" PRIi32" %" PRIi32 " %c",
185  len_real_y, len_real_x, CHAR_CONT_BEGIN) <= 0)
186  return false;
187  break;
188  case CT_UNDEFINED:
189  case CT_SCALAR:
190  SG_ERROR("write_cont_begin_wrapped(): Implementation error "
191  "during writing AsciiFile!");
192  return false;
193  }
194 
195  return true;
196 }
197 
198 bool
199 CSerializableAsciiFile::write_cont_end_wrapped(
200  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
201 {
202  if (fprintf(m_fstream, "%c", CHAR_CONT_END) <= 0) return false;
203 
204  return true;
205 }
206 
207 bool
208 CSerializableAsciiFile::write_string_begin_wrapped(
209  const TSGDataType* type, index_t length)
210 {
211  if (fprintf(m_fstream, "%" PRIi32 " %c", length,
212  CHAR_STRING_BEGIN) <= 0) return false;
213 
214  return true;
215 }
216 
217 bool
218 CSerializableAsciiFile::write_string_end_wrapped(
219  const TSGDataType* type, index_t length)
220 {
221  if (fprintf(m_fstream, "%c", CHAR_STRING_END) <= 0) return false;
222 
223  return true;
224 }
225 
226 bool
227 CSerializableAsciiFile::write_stringentry_begin_wrapped(
228  const TSGDataType* type, index_t y)
229 {
230  if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
231 
232  return true;
233 }
234 
235 bool
236 CSerializableAsciiFile::write_stringentry_end_wrapped(
237  const TSGDataType* type, index_t y)
238 {
239  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
240 
241  return true;
242 }
243 
244 bool
245 CSerializableAsciiFile::write_sparse_begin_wrapped(
246  const TSGDataType* type, index_t length)
247 {
248  if (fprintf(m_fstream, "%" PRIi32" %c", length,
249  CHAR_SPARSE_BEGIN) <= 0) return false;
250 
251  return true;
252 }
253 
254 bool
255 CSerializableAsciiFile::write_sparse_end_wrapped(
256  const TSGDataType* type, index_t length)
257 {
258  if (fprintf(m_fstream, "%c", CHAR_SPARSE_END) <= 0) return false;
259 
260  return true;
261 }
262 
263 bool
264 CSerializableAsciiFile::write_sparseentry_begin_wrapped(
265  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
266  index_t feat_index, index_t y)
267 {
268  if (fprintf(m_fstream, " %" PRIi32 " %c", feat_index, CHAR_ITEM_BEGIN)
269  <= 0) return false;
270 
271  return true;
272 }
273 
274 bool
275 CSerializableAsciiFile::write_sparseentry_end_wrapped(
276  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
277  index_t feat_index, index_t y)
278 {
279  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
280 
281  return true;
282 }
283 
284 bool
285 CSerializableAsciiFile::write_item_begin_wrapped(
286  const TSGDataType* type, index_t y, index_t x)
287 {
288  if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
289 
290  return true;
291 }
292 
293 bool
294 CSerializableAsciiFile::write_item_end_wrapped(
295  const TSGDataType* type, index_t y, index_t x)
296 {
297  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
298 
299  return true;
300 }
301 
302 bool
303 CSerializableAsciiFile::write_sgserializable_begin_wrapped(
304  const TSGDataType* type, const char* sgserializable_name,
305  EPrimitiveType generic)
306 {
307  if (*sgserializable_name == '\0') {
308  if (fprintf(m_fstream, "%s %c", STR_SGSERIAL_NULL,
309  CHAR_SGSERIAL_BEGIN) <= 0)
310  return false;
311  } else {
312  if (fprintf(m_fstream, "%s ", sgserializable_name) <= 0)
313  return false;
314 
315  if (generic != PT_NOT_GENERIC) {
316  string_t buf;
318  if (fprintf(m_fstream, "%s ", buf) <= 0) return false;
319  }
320 
321  if (fprintf(m_fstream, "%c%c", CHAR_SGSERIAL_BEGIN,
322  CHAR_TYPE_END) <= 0)
323  return false;
324  }
325 
326  return true;
327 }
328 
329 bool
330 CSerializableAsciiFile::write_sgserializable_end_wrapped(
331  const TSGDataType* type, const char* sgserializable_name,
332  EPrimitiveType generic)
333 {
334  if (fprintf(m_fstream, "%c", CHAR_SGSERIAL_END) <= 0)
335  return false;
336 
337  return true;
338 }
339 
340 bool
341 CSerializableAsciiFile::write_type_begin_wrapped(
342  const TSGDataType* type, const char* name, const char* prefix)
343 {
344  string_t buf;
345  type->to_string(buf, STRING_LEN);
346 
348 
349  if (fprintf(m_fstream, "%s %s ", name, buf) <= 0) return false;
350 
351  return true;
352 }
353 
354 bool
355 CSerializableAsciiFile::write_type_end_wrapped(
356  const TSGDataType* type, const char* name, const char* prefix)
357 {
358  if (fprintf(m_fstream, "%c", CHAR_TYPE_END) <= 0) return false;
359 
361 
362  return true;
363 }

SHOGUN Machine Learning Toolbox - Documentation