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

SHOGUN Machine Learning Toolbox - Documentation