SHOGUN  v1.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 vec_index,
239  index_t length)
240 {
241  if (fprintf(m_fstream, "%"PRIi32" %"PRIi32" %c", vec_index, length,
242  CHAR_SPARSE_BEGIN) <= 0) return false;
243 
244  return true;
245 }
246 
247 bool
248 CSerializableAsciiFile::write_sparse_end_wrapped(
249  const TSGDataType* type, index_t vec_index,
250  index_t length)
251 {
252  if (fprintf(m_fstream, "%c", CHAR_SPARSE_END) <= 0) return false;
253 
254  return true;
255 }
256 
257 bool
258 CSerializableAsciiFile::write_sparseentry_begin_wrapped(
259  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
260  index_t feat_index, index_t y)
261 {
262  if (fprintf(m_fstream, " %"PRIi32" %c", feat_index, CHAR_ITEM_BEGIN)
263  <= 0) return false;
264 
265  return true;
266 }
267 
268 bool
269 CSerializableAsciiFile::write_sparseentry_end_wrapped(
270  const TSGDataType* type, const SGSparseVectorEntry<char>* first_entry,
271  index_t feat_index, index_t y)
272 {
273  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
274 
275  return true;
276 }
277 
278 bool
279 CSerializableAsciiFile::write_item_begin_wrapped(
280  const TSGDataType* type, index_t y, index_t x)
281 {
282  if (fprintf(m_fstream, "%c", CHAR_ITEM_BEGIN) <= 0) return false;
283 
284  return true;
285 }
286 
287 bool
288 CSerializableAsciiFile::write_item_end_wrapped(
289  const TSGDataType* type, index_t y, index_t x)
290 {
291  if (fprintf(m_fstream, "%c", CHAR_ITEM_END) <= 0) return false;
292 
293  return true;
294 }
295 
296 bool
297 CSerializableAsciiFile::write_sgserializable_begin_wrapped(
298  const TSGDataType* type, const char* sgserializable_name,
299  EPrimitiveType generic)
300 {
301  if (*sgserializable_name == '\0') {
302  if (fprintf(m_fstream, "%s %c", STR_SGSERIAL_NULL,
303  CHAR_SGSERIAL_BEGIN) <= 0)
304  return false;
305  } else {
306  if (fprintf(m_fstream, "%s ", sgserializable_name) <= 0)
307  return false;
308 
309  if (generic != PT_NOT_GENERIC) {
310  string_t buf;
312  if (fprintf(m_fstream, "%s ", buf) <= 0) return false;
313  }
314 
315  if (fprintf(m_fstream, "%c%c", CHAR_SGSERIAL_BEGIN,
316  CHAR_TYPE_END) <= 0)
317  return false;
318  }
319 
320  return true;
321 }
322 
323 bool
324 CSerializableAsciiFile::write_sgserializable_end_wrapped(
325  const TSGDataType* type, const char* sgserializable_name,
326  EPrimitiveType generic)
327 {
328  if (fprintf(m_fstream, "%c", CHAR_SGSERIAL_END) <= 0)
329  return false;
330 
331  return true;
332 }
333 
334 bool
335 CSerializableAsciiFile::write_type_begin_wrapped(
336  const TSGDataType* type, const char* name, const char* prefix)
337 {
338  string_t buf;
339  type->to_string(buf, STRING_LEN);
340 
342 
343  if (fprintf(m_fstream, "%s %s ", name, buf) <= 0) return false;
344 
345  return true;
346 }
347 
348 bool
349 CSerializableAsciiFile::write_type_end_wrapped(
350  const TSGDataType* type, const char* name, const char* prefix)
351 {
352  if (fprintf(m_fstream, "%c", CHAR_TYPE_END) <= 0) return false;
353 
355 
356  return true;
357 }

SHOGUN Machine Learning Toolbox - Documentation