SHOGUN  v3.0.0
 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_SGOBJECT:
161  SG_ERROR("write_scalar_wrapped(): Implementation error during"
162  " writing AsciiFile!");
163  return false;
164  }
165 
166  return true;
167 }
168 
169 bool
170 CSerializableAsciiFile::write_cont_begin_wrapped(
171  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
172 {
173  switch (type->m_ctype) {
174  case CT_NDARRAY:
176  break;
177  case CT_SCALAR:
178  SG_ERROR("write_cont_begin_wrapped(): Implementation error "
179  "during writing AsciiFile!");
180  return false;
181  case CT_VECTOR: case CT_SGVECTOR:
182  if (fprintf(m_fstream, "%" PRIi32 " %c", len_real_y,
183  CHAR_CONT_BEGIN) <= 0)
184  return false;
185  break;
186  case CT_MATRIX: case CT_SGMATRIX:
187  if (fprintf(m_fstream, "%" PRIi32" %" PRIi32 " %c",
188  len_real_y, len_real_x, CHAR_CONT_BEGIN) <= 0)
189  return false;
190  break;
191  }
192 
193  return true;
194 }
195 
196 bool
197 CSerializableAsciiFile::write_cont_end_wrapped(
198  const TSGDataType* type, index_t len_real_y, index_t len_real_x)
199 {
200  if (fprintf(m_fstream, "%c", CHAR_CONT_END) <= 0) return false;
201 
202  return true;
203 }
204 
205 bool
206 CSerializableAsciiFile::write_string_begin_wrapped(
207  const TSGDataType* type, index_t length)
208 {
209  if (fprintf(m_fstream, "%" PRIi32 " %c", length,
210  CHAR_STRING_BEGIN) <= 0) return false;
211 
212  return true;
213 }
214 
215 bool
216 CSerializableAsciiFile::write_string_end_wrapped(
217  const TSGDataType* type, index_t length)
218 {
219  if (fprintf(m_fstream, "%c", CHAR_STRING_END) <= 0) return false;
220 
221  return true;
222 }
223 
224 bool
225 CSerializableAsciiFile::write_stringentry_begin_wrapped(
226  const TSGDataType* type, index_t y)
227 {
228  if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
229 
230  return true;
231 }
232 
233 bool
234 CSerializableAsciiFile::write_stringentry_end_wrapped(
235  const TSGDataType* type, index_t y)
236 {
237  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
238 
239  return true;
240 }
241 
242 bool
243 CSerializableAsciiFile::write_sparse_begin_wrapped(
244  const TSGDataType* type, index_t length)
245 {
246  if (fprintf(m_fstream, "%" PRIi32" %c", length,
247  CHAR_SPARSE_BEGIN) <= 0) return false;
248 
249  return true;
250 }
251 
252 bool
253 CSerializableAsciiFile::write_sparse_end_wrapped(
254  const TSGDataType* type, index_t length)
255 {
256  if (fprintf(m_fstream, "%c", CHAR_SPARSE_END) <= 0) return false;
257 
258  return true;
259 }
260 
261 bool
262 CSerializableAsciiFile::write_sparseentry_begin_wrapped(
263  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
264  index_t feat_index, index_t y)
265 {
266  if (fprintf(m_fstream, " %" PRIi32 " %c", feat_index, CHAR_ITEM_BEGIN)
267  <= 0) return false;
268 
269  return true;
270 }
271 
272 bool
273 CSerializableAsciiFile::write_sparseentry_end_wrapped(
274  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
275  index_t feat_index, index_t y)
276 {
277  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
278 
279  return true;
280 }
281 
282 bool
283 CSerializableAsciiFile::write_item_begin_wrapped(
284  const TSGDataType* type, index_t y, index_t x)
285 {
286  if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
287 
288  return true;
289 }
290 
291 bool
292 CSerializableAsciiFile::write_item_end_wrapped(
293  const TSGDataType* type, index_t y, index_t x)
294 {
295  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
296 
297  return true;
298 }
299 
300 bool
301 CSerializableAsciiFile::write_sgserializable_begin_wrapped(
302  const TSGDataType* type, const char* sgserializable_name,
303  EPrimitiveType generic)
304 {
305  if (*sgserializable_name == '\0') {
306  if (fprintf(m_fstream, "%s %c", STR_SGSERIAL_NULL,
307  CHAR_SGSERIAL_BEGIN) <= 0)
308  return false;
309  } else {
310  if (fprintf(m_fstream, "%s ", sgserializable_name) <= 0)
311  return false;
312 
313  if (generic != PT_NOT_GENERIC) {
314  string_t buf;
316  if (fprintf(m_fstream, "%s ", buf) <= 0) return false;
317  }
318 
319  if (fprintf(m_fstream, "%c%c", CHAR_SGSERIAL_BEGIN,
320  CHAR_TYPE_END) <= 0)
321  return false;
322  }
323 
324  return true;
325 }
326 
327 bool
328 CSerializableAsciiFile::write_sgserializable_end_wrapped(
329  const TSGDataType* type, const char* sgserializable_name,
330  EPrimitiveType generic)
331 {
332  if (fprintf(m_fstream, "%c", CHAR_SGSERIAL_END) <= 0)
333  return false;
334 
335  return true;
336 }
337 
338 bool
339 CSerializableAsciiFile::write_type_begin_wrapped(
340  const TSGDataType* type, const char* name, const char* prefix)
341 {
342  string_t buf;
343  type->to_string(buf, STRING_LEN);
344 
346 
347  if (fprintf(m_fstream, "%s %s ", name, buf) <= 0) return false;
348 
349  return true;
350 }
351 
352 bool
353 CSerializableAsciiFile::write_type_end_wrapped(
354  const TSGDataType* type, const char* name, const char* prefix)
355 {
356  if (fprintf(m_fstream, "%c", CHAR_TYPE_END) <= 0) return false;
357 
359 
360  return true;
361 }

SHOGUN Machine Learning Toolbox - Documentation