SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinaryFile.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 
11 #include <shogun/io/File.h>
12 #include <shogun/io/BinaryFile.h>
13 #include <shogun/io/SGIO.h>
14 #include <shogun/lib/DataType.h>
16 #include <shogun/lib/SGString.h>
18 
19 using namespace shogun;
20 
22 {
23  SG_UNSTABLE("CBinaryFile::CBinaryFile()", "\n")
24 }
25 
26 CBinaryFile::CBinaryFile(FILE* f, const char* name) : CFile(f, name)
27 {
28 }
29 
30 CBinaryFile::CBinaryFile(const char* fname, char rw, const char* name) : CFile(fname, rw, name)
31 {
32 }
33 
35 {
36 }
37 
38 #define GET_VECTOR(fname, sg_type, datatype) \
39 void CBinaryFile::fname(sg_type*& vec, int32_t& len) \
40 { \
41  if (!file) \
42  SG_ERROR("File invalid.\n") \
43  TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
44  if (dtype!=datatype) \
45  SG_ERROR("Datatype mismatch\n") \
46  \
47  if (fread(&len, sizeof(int32_t), 1, file)!=1) \
48  SG_ERROR("Failed to read vector length\n") \
49  vec=SG_MALLOC(sg_type, len); \
50  if (fread(vec, sizeof(sg_type), len, file)!=(size_t) len) \
51  SG_ERROR("Failed to read Matrix\n") \
52 }
53 
54 GET_VECTOR(get_vector, int8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT8))
55 GET_VECTOR(get_vector, uint8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT8))
56 GET_VECTOR(get_vector, char, TSGDataType(CT_VECTOR, ST_NONE, PT_CHAR))
57 GET_VECTOR(get_vector, int32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
58 GET_VECTOR(get_vector, uint32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT32))
59 GET_VECTOR(get_vector, float32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT32))
60 GET_VECTOR(get_vector, float64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT64))
61 GET_VECTOR(get_vector, floatmax_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOATMAX))
62 GET_VECTOR(get_vector, int16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
63 GET_VECTOR(get_vector, uint16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
64 GET_VECTOR(get_vector, int64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
65 GET_VECTOR(get_vector, uint64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT64))
66 #undef GET_VECTOR
67 
68 #define GET_MATRIX(fname, sg_type, datatype) \
69 void CBinaryFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec) \
70 { \
71  if (!file) \
72  SG_ERROR("File invalid.\n") \
73  TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
74  if (dtype!=datatype) \
75  SG_ERROR("Datatype mismatch\n") \
76  \
77  if (fread(&num_feat, sizeof(int32_t), 1, file)!=1 || \
78  fread(&num_vec, sizeof(int32_t), 1, file)!=1) \
79  SG_ERROR("Failed to read Matrix dimensions\n") \
80  matrix=SG_MALLOC(sg_type, int64_t(num_feat)*num_vec); \
81  if (fread(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
82  SG_ERROR("Failed to read Matrix\n") \
83 }
84 
85 GET_MATRIX(get_matrix, char, TSGDataType(CT_MATRIX, ST_NONE, PT_CHAR))
86 GET_MATRIX(get_matrix, uint8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_UINT8))
87 GET_MATRIX(get_matrix, int8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT8))
88 GET_MATRIX(get_matrix, int32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
89 GET_MATRIX(get_matrix, uint32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
90 GET_MATRIX(get_matrix, int64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
91 GET_MATRIX(get_matrix, uint64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
92 GET_MATRIX(get_matrix, int16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
93 GET_MATRIX(get_matrix, uint16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
94 GET_MATRIX(get_matrix, float32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT32))
95 GET_MATRIX(get_matrix, float64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT64))
96 GET_MATRIX(get_matrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOATMAX))
97 #undef GET_MATRIX
98 
99 #define GET_NDARRAY(fname,sg_type,datatype) \
100 void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\
101 { \
102  size_t total = 1; \
103  \
104  if (!file) \
105  SG_ERROR("File invalid.\n") \
106  \
107  TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); \
108  read_header(&dtype); \
109  \
110  if (dtype!=datatype) \
111  SG_ERROR("Datatype mismatch\n") \
112  \
113  if (fread(&num_dims,sizeof(int32_t),1,file) != 1) \
114  SG_ERROR("Failed to read number of dimensions") \
115  \
116  dims = SG_MALLOC(int32_t, num_dims); \
117  if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
118  SG_ERROR("Failed to read sizes of dimensions!") \
119  \
120  for (int32_t i = 0;i < num_dims;i++) \
121  total *= dims[i]; \
122  \
123  array = SG_MALLOC(sg_type, total); \
124  if (fread(array,sizeof(sg_type),total,file) != (size_t)total) \
125  SG_ERROR("Failed to read array data!") \
126 }
127 
128 GET_NDARRAY(get_ndarray,uint8_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_UINT8));
129 GET_NDARRAY(get_ndarray,char,TSGDataType(CT_NDARRAY, ST_NONE, PT_CHAR));
130 GET_NDARRAY(get_ndarray,int32_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_INT32));
131 GET_NDARRAY(get_ndarray,int16_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_INT16));
132 GET_NDARRAY(get_ndarray,uint16_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_UINT16));
133 GET_NDARRAY(get_ndarray,float32_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_FLOAT32));
134 GET_NDARRAY(get_ndarray,float64_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_FLOAT64));
135 #undef GET_NDARRAY
136 
137 #define GET_SPARSEMATRIX(fname, sg_type, datatype) \
138 void CBinaryFile::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec) \
139 { \
140  if (!(file)) \
141  SG_ERROR("File invalid.\n") \
142  \
143  TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
144  if (dtype!=datatype) \
145  SG_ERROR("Datatype mismatch\n") \
146  \
147  if (fread(&num_vec, sizeof(int32_t), 1, file)!=1) \
148  SG_ERROR("Failed to read number of vectors\n") \
149  \
150  matrix=SG_MALLOC(SGSparseVector<sg_type>, num_vec); \
151  \
152  for (int32_t i=0; i<num_vec; i++) \
153  { \
154  new (&matrix[i]) SGSparseVector<sg_type>(); \
155  int32_t len=0; \
156  if (fread(&len, sizeof(int32_t), 1, file)!=1) \
157  SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i) \
158  matrix[i].num_feat_entries=len; \
159  SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len); \
160  if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len) \
161  SG_ERROR("Failed to read sparse vector %d\n", i) \
162  matrix[i].features=vec; \
163  num_feat = CMath::max(num_feat, matrix[i].get_num_dimensions()); \
164  } \
165 }
166 GET_SPARSEMATRIX(get_sparse_matrix, bool, TSGDataType(CT_MATRIX, ST_NONE, PT_BOOL))
167 GET_SPARSEMATRIX(get_sparse_matrix, char, TSGDataType(CT_MATRIX, ST_NONE, PT_CHAR))
168 GET_SPARSEMATRIX(get_sparse_matrix, uint8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_UINT8))
169 GET_SPARSEMATRIX(get_sparse_matrix, int8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT8))
170 GET_SPARSEMATRIX(get_sparse_matrix, int32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
171 GET_SPARSEMATRIX(get_sparse_matrix, uint32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
172 GET_SPARSEMATRIX(get_sparse_matrix, int64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
173 GET_SPARSEMATRIX(get_sparse_matrix, uint64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
174 GET_SPARSEMATRIX(get_sparse_matrix, int16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
175 GET_SPARSEMATRIX(get_sparse_matrix, uint16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
176 GET_SPARSEMATRIX(get_sparse_matrix, float32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT32))
177 GET_SPARSEMATRIX(get_sparse_matrix, float64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT64))
178 GET_SPARSEMATRIX(get_sparse_matrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOATMAX))
179 #undef GET_SPARSEMATRIX
180 
181 
182 #define GET_STRING_LIST(fname, sg_type, datatype) \
183 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
184 { \
185  strings=NULL; \
186  num_str=0; \
187  max_string_len=0; \
188  \
189  if (!file) \
190  SG_ERROR("File invalid.\n") \
191  \
192  TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype); \
193  if (dtype!=datatype) \
194  SG_ERROR("Datatype mismatch\n") \
195  \
196  if (fread(&num_str, sizeof(int32_t), 1, file)!=1) \
197  SG_ERROR("Failed to read number of strings\n") \
198  \
199  strings=SG_MALLOC(SGString<sg_type>, num_str); \
200  \
201  for (int32_t i=0; i<num_str; i++) \
202  { \
203  int32_t len=0; \
204  if (fread(&len, sizeof(int32_t), 1, file)!=1) \
205  SG_ERROR("Failed to read string length of string with idx=%d\n", i) \
206  strings[i].slen=len; \
207  sg_type* str = SG_MALLOC(sg_type, len); \
208  if (fread(str, sizeof(sg_type), len, file)!= (size_t) len) \
209  SG_ERROR("Failed to read string %d\n", i) \
210  strings[i].string=str; \
211  } \
212 }
213 
214 GET_STRING_LIST(get_string_list, char, TSGDataType(CT_VECTOR, ST_NONE, PT_CHAR))
215 GET_STRING_LIST(get_string_list, uint8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT8))
216 GET_STRING_LIST(get_string_list, int8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT8))
217 GET_STRING_LIST(get_string_list, int32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
218 GET_STRING_LIST(get_string_list, uint32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
219 GET_STRING_LIST(get_string_list, int64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
220 GET_STRING_LIST(get_string_list, uint64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
221 GET_STRING_LIST(get_string_list, int16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
222 GET_STRING_LIST(get_string_list, uint16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
223 GET_STRING_LIST(get_string_list, float32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT32))
224 GET_STRING_LIST(get_string_list, float64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT64))
225 GET_STRING_LIST(get_string_list, floatmax_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOATMAX))
226 #undef GET_STRING_LIST
227 
230 #define SET_VECTOR(fname, sg_type, dtype) \
231 void CBinaryFile::fname(const sg_type* vec, int32_t len) \
232 { \
233  if (!(file && vec)) \
234  SG_ERROR("File or vector invalid.\n") \
235  \
236  TSGDataType t dtype; write_header(&t); \
237  \
238  if (fwrite(&len, sizeof(int32_t), 1, file)!=1 || \
239  fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len) \
240  SG_ERROR("Failed to write vector\n") \
241 }
242 SET_VECTOR(set_vector, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
243 SET_VECTOR(set_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
244 SET_VECTOR(set_vector, char, (CT_VECTOR, ST_NONE, PT_CHAR))
245 SET_VECTOR(set_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
246 SET_VECTOR(set_vector, uint32_t, (CT_VECTOR, ST_NONE, PT_UINT32))
247 SET_VECTOR(set_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
248 SET_VECTOR(set_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
249 SET_VECTOR(set_vector, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
250 SET_VECTOR(set_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
251 SET_VECTOR(set_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
252 SET_VECTOR(set_vector, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
253 SET_VECTOR(set_vector, uint64_t, (CT_VECTOR, ST_NONE, PT_UINT64))
254 #undef SET_VECTOR
255 
256 #define SET_MATRIX(fname, sg_type, dtype) \
257 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec) \
258 { \
259  if (!(file && matrix)) \
260  SG_ERROR("File or matrix invalid.\n") \
261  \
262  TSGDataType t dtype; write_header(&t); \
263  \
264  if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 || \
265  fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 || \
266  fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec) \
267  SG_ERROR("Failed to write Matrix\n") \
268 }
269 SET_MATRIX(set_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
270 SET_MATRIX(set_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
271 SET_MATRIX(set_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
272 SET_MATRIX(set_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
273 SET_MATRIX(set_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
274 SET_MATRIX(set_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
275 SET_MATRIX(set_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
276 SET_MATRIX(set_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
277 SET_MATRIX(set_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
278 SET_MATRIX(set_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
279 SET_MATRIX(set_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
280 SET_MATRIX(set_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
281 #undef SET_MATRIX
282 
283 #define SET_NDARRAY(fname,sg_type,datatype) \
284 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \
285 { \
286  size_t total = 1; \
287  \
288  if (!file) \
289  SG_ERROR("File invalid.\n") \
290  \
291  if (!array) \
292  SG_ERROR("Invalid array!\n") \
293  \
294  TSGDataType t datatype; \
295  write_header(&t); \
296  \
297  if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1) \
298  SG_ERROR("Failed to write number of dimensions!\n") \
299  \
300  if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims) \
301  SG_ERROR("Failed to write sizes of dimensions!\n") \
302  \
303  for (int32_t i = 0;i < num_dims;i++) \
304  total *= dims[i]; \
305  \
306  if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total) \
307  SG_ERROR("Failed to write array data!\n") \
308 }
309 
310 SET_NDARRAY(set_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
311 SET_NDARRAY(set_ndarray,char,(CT_NDARRAY, ST_NONE, PT_CHAR));
312 SET_NDARRAY(set_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
313 SET_NDARRAY(set_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
314 SET_NDARRAY(set_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
315 SET_NDARRAY(set_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
316 SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
317 #undef SET_NDARRAY
318 
319 #define SET_SPARSEMATRIX(fname, sg_type, dtype) \
320 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix, \
321  int32_t num_feat, int32_t num_vec) \
322 { \
323  if (!(file && matrix)) \
324  SG_ERROR("File or matrix invalid.\n") \
325  \
326  TSGDataType t dtype; write_header(&t); \
327  \
328  if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1) \
329  SG_ERROR("Failed to write Sparse Matrix\n") \
330  \
331  for (int32_t i=0; i<num_vec; i++) \
332  { \
333  SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \
334  int32_t len=matrix[i].num_feat_entries; \
335  if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
336  (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)) \
337  SG_ERROR("Failed to write Sparse Matrix\n") \
338  } \
339 }
340 SET_SPARSEMATRIX(set_sparse_matrix, bool, (CT_MATRIX, ST_NONE, PT_BOOL))
341 SET_SPARSEMATRIX(set_sparse_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
342 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
343 SET_SPARSEMATRIX(set_sparse_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
344 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
345 SET_SPARSEMATRIX(set_sparse_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
346 SET_SPARSEMATRIX(set_sparse_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
347 SET_SPARSEMATRIX(set_sparse_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
348 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
349 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
350 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
351 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
352 SET_SPARSEMATRIX(set_sparse_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
353 #undef SET_SPARSEMATRIX
354 
355 #define SET_STRING_LIST(fname, sg_type, dtype) \
356 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str) \
357 { \
358  if (!(file && strings)) \
359  SG_ERROR("File or strings invalid.\n") \
360  \
361  TSGDataType t dtype; write_header(&t); \
362  for (int32_t i=0; i<num_str; i++) \
363  { \
364  int32_t len = strings[i].slen; \
365  if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) || \
366  (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \
367  SG_ERROR("Failed to write Sparse Matrix\n") \
368  } \
369 }
370 SET_STRING_LIST(set_string_list, char, (CT_VECTOR, ST_NONE, PT_CHAR))
371 SET_STRING_LIST(set_string_list, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
372 SET_STRING_LIST(set_string_list, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
373 SET_STRING_LIST(set_string_list, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
374 SET_STRING_LIST(set_string_list, uint32_t, (CT_VECTOR, ST_NONE, PT_INT32))
375 SET_STRING_LIST(set_string_list, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
376 SET_STRING_LIST(set_string_list, uint64_t, (CT_VECTOR, ST_NONE, PT_INT64))
377 SET_STRING_LIST(set_string_list, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
378 SET_STRING_LIST(set_string_list, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
379 SET_STRING_LIST(set_string_list, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
380 SET_STRING_LIST(set_string_list, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
381 SET_STRING_LIST(set_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
382 #undef SET_STRING_LIST
383 
384 
386 {
387  return -1;
388 }
389 
391 {
392  return -1;
393 }
394 
395 void
397 {
398  ASSERT(file)
399  ASSERT(dest)
400 
401  if (fseek(file, 0L, SEEK_SET)!=0)
402  SG_ERROR("Error seeking file '%s' to the beginning.\n", filename)
403 
404  char fourcc[4];
405  uint16_t endian=0;
406 
407  if (fread(&fourcc, sizeof(char), 4, file)!=4)
408  SG_ERROR("Error reading fourcc header in file '%s'\n", filename)
409 
410  if (fread(&endian, sizeof(uint16_t), 1, file)!=1)
411  SG_ERROR("Error reading endian header in file '%s'\n", filename)
412 
413  if ((fread(&dest->m_ctype, sizeof(dest->m_ctype), 1, file)!=1) ||
414  (fread(&dest->m_ptype, sizeof(dest->m_ptype), 1, file)!=1))
415  SG_ERROR("Error reading datatype header in file '%s'\n", filename)
416 
417  if (strncmp(fourcc, "SG01", 4))
418  SG_ERROR("Header mismatch, expected SG01 in file '%s'\n", filename)
419 }
420 
421 void
423 {
424  ASSERT(file)
425 
426  const char* fourcc="SG01";
427  uint16_t endian=0x1234;
428 
429  if (!((fwrite(fourcc, sizeof(char), 4, file)==4) &&
430  (fwrite(&endian, sizeof(uint16_t), 1, file)==1) &&
431  (fwrite(&datatype->m_ctype, sizeof(datatype->m_ctype), 1,
432  file)==1)
433  && (fwrite(&datatype->m_ptype, sizeof(datatype->m_ptype), 1,
434  file)==1)
435  ))
436  SG_ERROR("Error writing header\n")
437 }

SHOGUN Machine Learning Toolbox - Documentation