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

SHOGUN Machine Learning Toolbox - Documentation