SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
SerializableFile.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) 1999-2010 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
11 
13 #include <shogun/io/SGIO.h>
14 
15 using namespace shogun;
16 
18  : CSGObject(), m_fstream(NULL), m_filename(NULL)
19 {
20  init(NULL, 0, "(file)");
21 }
22 
23 CSerializableFile::CSerializableFile(FILE* fstream, char rw)
24  :CSGObject(), m_fstream(NULL), m_filename(NULL)
25 {
26  REQUIRE(fstream != NULL, "Provided fstream should be != NULL\n");
27  init(fstream, rw, "(file)");
28 }
29 
30 CSerializableFile::CSerializableFile(const char* fname, char rw)
31  :CSGObject(), m_fstream(NULL), m_filename(NULL)
32 {
33  const char mode[3] = {rw, 'b', '\0'};
34 
35  if (fname == NULL || *fname == '\0') {
36  close();
37  SG_ERROR("Filename not given for opening file!\n")
38  }
39 
40  if (rw != 'r' && rw != 'w') {
41  close();
42  SG_ERROR("Unknown mode '%c'!\n", mode[0])
43  }
44 
45  FILE* fstream = fopen(fname, mode);
46  if (!fstream) {
47  close();
48  SG_ERROR("Error opening file '%s'\n", fname)
49  }
50 
51  init(fstream, rw, fname);
52 }
53 
55 {
56  close();
57  SG_FREE(m_filename);
58  delete m_reader;
59  m_task = 0;
60 }
61 
62 void
63 CSerializableFile::init(FILE* fstream, char task, const char* filename)
64 {
65  m_fstream = fstream;
66  m_task = task;
67  SG_FREE(m_filename);
68  m_filename = SG_MALLOC(char, strlen(filename)+1);
69  strcpy(m_filename, filename);
70  m_reader = NULL;
71 }
72 
73 void
75 {
76  if (is_opened()) { fclose(m_fstream); m_fstream = NULL; }
77 }
78 
79 bool
81 {
82  return m_fstream != NULL;
83 }
84 
85 bool
86 CSerializableFile::is_task_warn(char rw, const char* name,
87  const char* prefix)
88 {
89  if (m_task == 'r' && m_reader == NULL) {
90  string_t dest_version;
91  strncpy(dest_version, "(unkown)", STRING_LEN);
92  m_reader = new_reader(dest_version, STRING_LEN);
93  if (m_reader == NULL) {
94  SG_WARNING("`%s' has file-version `%s', which is not "
95  "supported!\n", m_filename, dest_version);
96  close(); return false;
97  }
98  }
99 
100  if (rw == 'w' && (m_task != 'w' || !is_opened())) {
101  SG_WARNING("`%s' not opened (for writing) during writing "
102  "`%s%s'!\n", m_filename, prefix, name);
103  return false;
104  }
105  if (rw == 'r' && (m_task != 'r' || !is_opened())) {
106  SG_WARNING("`%s' not opened (for reading) during reading "
107  "`%s%s'!\n", m_filename, prefix, name);
108  return false;
109  }
110 
111  return true;
112 }
113 
114 bool
115 CSerializableFile::false_warn(const char* prefix, const char* name)
116 {
117  if (m_task == 'w')
118  SG_WARNING("Could not write `%s%s' to `%s'!\n", prefix,
119  name, m_filename);
120  if (m_task == 'r')
121  SG_WARNING("Could not read `%s%s' from `%s'!\n", prefix,
122  name, m_filename);
123  if (m_task != 'w' && m_task != 'r')
124  SG_WARNING("Could not read/write `%s%s' from `%s'!\n",
125  prefix, name, m_filename);
126 
127  return false;
128 }
129 
130 bool
131 CSerializableFile::write_scalar(
132  const TSGDataType* type, const char* name, const char* prefix,
133  const void* param)
134 {
135  if (!is_task_warn('w', name, prefix)) return false;
136 
137  if (!write_scalar_wrapped(type, param))
138  return false_warn(prefix, name);
139 
140  return true;
141 }
142 
143 bool
144 CSerializableFile::read_scalar(
145  const TSGDataType* type, const char* name, const char* prefix,
146  void* param)
147 {
148  if (!is_task_warn('r', name, prefix)) return false;
149 
150  if (!m_reader->read_scalar_wrapped(type, param))
151  return false_warn(prefix, name);
152 
153  return true;
154 }
155 
156 bool
157 CSerializableFile::write_cont_begin(
158  const TSGDataType* type, const char* name, const char* prefix,
159  index_t len_real_y, index_t len_real_x)
160 {
161  if (!is_task_warn('w', name, prefix)) return false;
162 
163  if (!write_cont_begin_wrapped(type, len_real_y, len_real_x))
164  return false_warn(prefix, name);
165 
166  return true;
167 }
168 
169 bool
170 CSerializableFile::read_cont_begin(
171  const TSGDataType* type, const char* name, const char* prefix,
172  index_t* len_read_y, index_t* len_read_x)
173 {
174  if (!is_task_warn('r', name, prefix)) return false;
175 
176  if (!m_reader->read_cont_begin_wrapped(type, len_read_y,
177  len_read_x))
178  return false_warn(prefix, name);
179 
180  return true;
181 }
182 
183 bool
184 CSerializableFile::write_cont_end(
185  const TSGDataType* type, const char* name, const char* prefix,
186  index_t len_real_y, index_t len_real_x)
187 {
188  if (!is_task_warn('w', name, prefix)) return false;
189 
190  if (!write_cont_end_wrapped(type, len_real_y, len_real_x))
191  return false_warn(prefix, name);
192 
193  return true;
194 }
195 
196 bool
197 CSerializableFile::read_cont_end(
198  const TSGDataType* type, const char* name, const char* prefix,
199  index_t len_read_y, index_t len_read_x)
200 {
201  if (!is_task_warn('r', name, prefix)) return false;
202 
203  if (!m_reader->read_cont_end_wrapped(type, len_read_y, len_read_x))
204  return false_warn(prefix, name);
205 
206  return true;
207 }
208 
209 bool
210 CSerializableFile::write_string_begin(
211  const TSGDataType* type, const char* name, const char* prefix,
212  index_t length)
213 {
214  if (!is_task_warn('w', name, prefix)) return false;
215 
216  if (!write_string_begin_wrapped(type, length))
217  return false_warn(prefix, name);
218 
219  return true;
220 }
221 
222 bool
223 CSerializableFile::read_string_begin(
224  const TSGDataType* type, const char* name, const char* prefix,
225  index_t* length)
226 {
227  if (!is_task_warn('r', name, prefix)) return false;
228 
229  if (!m_reader->read_string_begin_wrapped(type, length))
230  return false_warn(prefix, name);
231 
232  return true;
233 }
234 
235 bool
236 CSerializableFile::write_string_end(
237  const TSGDataType* type, const char* name, const char* prefix,
238  index_t length)
239 {
240  if (!is_task_warn('w', name, prefix)) return false;
241 
242  if (!write_string_end_wrapped(type, length))
243  return false_warn(prefix, name);
244 
245  return true;
246 }
247 
248 bool
249 CSerializableFile::read_string_end(
250  const TSGDataType* type, const char* name, const char* prefix,
251  index_t length)
252 {
253  if (!is_task_warn('r', name, prefix)) return false;
254 
255  if (!m_reader->read_string_end_wrapped(type, length))
256  return false_warn(prefix, name);
257 
258  return true;
259 }
260 
261 bool
262 CSerializableFile::write_stringentry_begin(
263  const TSGDataType* type, const char* name, const char* prefix,
264  index_t y)
265 {
266  if (!is_task_warn('w', name, prefix)) return false;
267 
268  if (!write_stringentry_begin_wrapped(type, y))
269  return false_warn(prefix, name);
270 
271  return true;
272 }
273 
274 bool
275 CSerializableFile::read_stringentry_begin(
276  const TSGDataType* type, const char* name, const char* prefix,
277  index_t y)
278 {
279  if (!is_task_warn('r', name, prefix)) return false;
280 
281  if (!m_reader->read_stringentry_begin_wrapped(type, y))
282  return false_warn(prefix, name);
283 
284  return true;
285 }
286 
287 bool
288 CSerializableFile::write_stringentry_end(
289  const TSGDataType* type, const char* name, const char* prefix,
290  index_t y)
291 {
292  if (!is_task_warn('w', name, prefix)) return false;
293 
294  if (!write_stringentry_end_wrapped(type, y))
295  return false_warn(prefix, name);
296 
297  return true;
298 }
299 
300 bool
301 CSerializableFile::read_stringentry_end(
302  const TSGDataType* type, const char* name, const char* prefix,
303  index_t y)
304 {
305  if (!is_task_warn('r', name, prefix)) return false;
306 
307  if (!m_reader->read_stringentry_end_wrapped(type, y))
308  return false_warn(prefix, name);
309 
310  return true;
311 }
312 
313 bool
314 CSerializableFile::write_sparse_begin(
315  const TSGDataType* type, const char* name, const char* prefix,
316  index_t length)
317 {
318  if (!is_task_warn('w', name, prefix)) return false;
319 
320  if (!write_sparse_begin_wrapped(type, length))
321  return false_warn(prefix, name);
322 
323  return true;
324 }
325 
326 bool
327 CSerializableFile::read_sparse_begin(
328  const TSGDataType* type, const char* name, const char* prefix,
329  index_t* length)
330 {
331  if (!is_task_warn('r', name, prefix)) return false;
332 
333  if (!m_reader->read_sparse_begin_wrapped(type, length))
334  return false_warn(prefix, name);
335 
336  return true;
337 }
338 
339 bool
340 CSerializableFile::write_sparse_end(
341  const TSGDataType* type, const char* name, const char* prefix,
342  index_t length)
343 {
344  if (!is_task_warn('w', name, prefix)) return false;
345 
346  if (!write_sparse_end_wrapped(type, length))
347  return false_warn(prefix, name);
348 
349  return true;
350 }
351 
352 bool
353 CSerializableFile::read_sparse_end(
354  const TSGDataType* type, const char* name, const char* prefix,
355  index_t length)
356 {
357  if (!is_task_warn('r', name, prefix)) return false;
358 
359  if (!m_reader->read_sparse_end_wrapped(type, length))
360  return false_warn(prefix, name);
361 
362  return true;
363 }
364 
365 bool
366 CSerializableFile::write_sparseentry_begin(
367  const TSGDataType* type, const char* name, const char* prefix,
368  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
369  index_t y)
370 {
371  if (!is_task_warn('w', name, prefix)) return false;
372 
373  if (!write_sparseentry_begin_wrapped(type, first_entry,
374  feat_index, y))
375  return false_warn(prefix, name);
376 
377  return true;
378 }
379 
380 bool
381 CSerializableFile::read_sparseentry_begin(
382  const TSGDataType* type, const char* name, const char* prefix,
383  SGSparseVectorEntry<char>* first_entry, index_t* feat_index, index_t y)
384 {
385  if (!is_task_warn('r', name, prefix)) return false;
386 
387  if (!m_reader->read_sparseentry_begin_wrapped(type, first_entry,
388  feat_index, y))
389  return false_warn(prefix, name);
390 
391  return true;
392 }
393 
394 bool
395 CSerializableFile::write_sparseentry_end(
396  const TSGDataType* type, const char* name, const char* prefix,
397  const SGSparseVectorEntry<char>* first_entry, index_t feat_index,
398  index_t y)
399 {
400  if (!is_task_warn('w', name, prefix)) return false;
401 
402  if (!write_sparseentry_end_wrapped(type, first_entry, feat_index,
403  y))
404  return false_warn(prefix, name);
405 
406  return true;
407 }
408 
409 bool
410 CSerializableFile::read_sparseentry_end(
411  const TSGDataType* type, const char* name, const char* prefix,
412  SGSparseVectorEntry<char>* first_entry, index_t* feat_index,
413  index_t y)
414 {
415  if (!is_task_warn('r', name, prefix)) return false;
416 
417  if (!m_reader->read_sparseentry_end_wrapped(type, first_entry,
418  feat_index, y))
419  return false_warn(prefix, name);
420 
421  return true;
422 }
423 
424 bool
425 CSerializableFile::write_item_begin(
426  const TSGDataType* type, const char* name, const char* prefix,
427  index_t y, index_t x)
428 {
429  if (!is_task_warn('w', name, prefix)) return false;
430 
431  if (!write_item_begin_wrapped(type, y, x))
432  return false_warn(prefix, name);
433 
434  return true;
435 }
436 
437 bool
438 CSerializableFile::read_item_begin(
439  const TSGDataType* type, const char* name, const char* prefix,
440  index_t y, index_t x)
441 {
442  if (!is_task_warn('r', name, prefix)) return false;
443 
444  if (!m_reader->read_item_begin_wrapped(type, y, x))
445  return false_warn(prefix, name);
446 
447  return true;
448 }
449 
450 bool
451 CSerializableFile::write_item_end(
452  const TSGDataType* type, const char* name, const char* prefix,
453  index_t y, index_t x)
454 {
455  if (!is_task_warn('w', name, prefix)) return false;
456 
457  if (!write_item_end_wrapped(type, y, x))
458  return false_warn(prefix, name);
459 
460  return true;
461 }
462 
463 bool
464 CSerializableFile::read_item_end(
465  const TSGDataType* type, const char* name, const char* prefix,
466  index_t y, index_t x)
467 {
468  if (!is_task_warn('r', name, prefix)) return false;
469 
470  if (!m_reader->read_item_end_wrapped(type, y, x))
471  return false_warn(prefix, name);
472 
473  return true;
474 }
475 
476 bool
477 CSerializableFile::write_sgserializable_begin(
478  const TSGDataType* type, const char* name, const char* prefix,
479  const char* sgserializable_name, EPrimitiveType generic)
480 {
481  if (!is_task_warn('w', name, prefix)) return false;
482 
483  if (!write_sgserializable_begin_wrapped(type, sgserializable_name,
484  generic))
485  return false_warn(prefix, name);
486 
487  return true;
488 }
489 
490 bool
491 CSerializableFile::read_sgserializable_begin(
492  const TSGDataType* type, const char* name, const char* prefix,
493  char* sgserializable_name, EPrimitiveType* generic)
494 {
495  if (!is_task_warn('r', name, prefix)) return false;
496 
497  if (!m_reader->read_sgserializable_begin_wrapped(
498  type, sgserializable_name, generic))
499  return false_warn(prefix, name);
500 
501  return true;
502 }
503 
504 bool
505 CSerializableFile::write_sgserializable_end(
506  const TSGDataType* type, const char* name, const char* prefix,
507  const char* sgserializable_name, EPrimitiveType generic)
508 {
509  if (!is_task_warn('w', name, prefix)) return false;
510 
511  if (!write_sgserializable_end_wrapped(type, sgserializable_name,
512  generic))
513  return false_warn(prefix, name);
514 
515  return true;
516 }
517 
518 bool
519 CSerializableFile::read_sgserializable_end(
520  const TSGDataType* type, const char* name, const char* prefix,
521  const char* sgserializable_name, EPrimitiveType generic)
522 {
523  if (!is_task_warn('r', name, prefix)) return false;
524 
525  if (!m_reader->read_sgserializable_end_wrapped(
526  type, sgserializable_name, generic))
527  return false_warn(prefix, name);
528 
529  return true;
530 }
531 
532 bool
533 CSerializableFile::write_type_begin(
534  const TSGDataType* type, const char* name, const char* prefix)
535 {
536  if (!is_task_warn('w', name, prefix)) return false;
537 
538  if (!write_type_begin_wrapped(type, name, prefix))
539  return false_warn(prefix, name);
540 
541  return true;
542 }
543 
544 bool
545 CSerializableFile::read_type_begin(
546  const TSGDataType* type, const char* name, const char* prefix)
547 {
548  if (!is_task_warn('r', name, prefix)) return false;
549 
550  if (!m_reader->read_type_begin_wrapped(type, name, prefix))
551  return false_warn(prefix, name);
552 
553  return true;
554 }
555 
556 bool
557 CSerializableFile::write_type_end(
558  const TSGDataType* type, const char* name, const char* prefix)
559 {
560  if (!is_task_warn('w', name, prefix)) return false;
561 
562  if (!write_type_end_wrapped(type, name, prefix))
563  return false_warn(prefix, name);
564 
565  return true;
566 }
567 
568 bool
569 CSerializableFile::read_type_end(
570  const TSGDataType* type, const char* name, const char* prefix)
571 {
572  if (!is_task_warn('r', name, prefix)) return false;
573 
574  if (!m_reader->read_type_end_wrapped(type, name, prefix))
575  return false_warn(prefix, name);
576 
577  return true;
578 }
int32_t index_t
Definition: common.h:62
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
Datatypes that shogun supports.
Definition: DataType.h:68
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
#define STRING_LEN
Definition: common.h:55
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
template class SGSparseVectorEntry
Definition: File.h:23
char string_t[STRING_LEN]
Definition: common.h:57
#define SG_WARNING(...)
Definition: SGIO.h:128
void init(FILE *fstream, char task, const char *filename)

SHOGUN Machine Learning Toolbox - Documentation