BinaryFile.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2010 Soeren Sonnenburg
00008  * Copyright (C) 2010 Berlin Institute of Technology
00009  */
00010 
00011 #include <shogun/io/File.h>
00012 #include <shogun/features/SparseFeatures.h>
00013 #include <shogun/io/BinaryFile.h>
00014 
00015 using namespace shogun;
00016 
00017 CBinaryFile::CBinaryFile()
00018 {
00019     SG_UNSTABLE("CBinaryFile::CBinaryFile()", "\n");
00020 }
00021 
00022 CBinaryFile::CBinaryFile(FILE* f, const char* name) : CFile(f, name)
00023 {
00024 }
00025 
00026 CBinaryFile::CBinaryFile(char* fname, char rw, const char* name) : CFile(fname, rw, name)
00027 {
00028 }
00029 
00030 CBinaryFile::~CBinaryFile()
00031 {
00032 }
00033 
00034 #define GET_VECTOR(fname, sg_type, datatype)                                        \
00035 void CBinaryFile::fname(sg_type*& vec, int32_t& len)                                \
00036 {                                                                                   \
00037     if (!file)                                                                      \
00038         SG_ERROR("File invalid.\n");                                                \
00039     TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype);                        \
00040     if (dtype!=datatype)                                                            \
00041         SG_ERROR("Datatype mismatch\n");                                            \
00042                                                                                     \
00043     if (fread(&len, sizeof(int32_t), 1, file)!=1)                                   \
00044         SG_ERROR("Failed to read vector length\n");                                 \
00045     vec=SG_MALLOC(sg_type, len);                                                            \
00046     if (fread(vec, sizeof(sg_type), len, file)!=(size_t) len)                       \
00047         SG_ERROR("Failed to read Matrix\n");                                        \
00048 }
00049 
00050 GET_VECTOR(get_vector, uint8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT8))
00051 GET_VECTOR(get_vector, char, TSGDataType(CT_VECTOR, ST_NONE, PT_CHAR))
00052 GET_VECTOR(get_vector, int32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
00053 GET_VECTOR(get_vector, float32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT32))
00054 GET_VECTOR(get_vector, float64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT64))
00055 GET_VECTOR(get_vector, int16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
00056 GET_VECTOR(get_vector, uint16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
00057 #undef GET_VECTOR
00058 
00059 #define GET_MATRIX(fname, sg_type, datatype)                                        \
00060 void CBinaryFile::fname(sg_type*& matrix, int32_t& num_feat, int32_t& num_vec)      \
00061 {                                                                                   \
00062     if (!file)                                                                      \
00063         SG_ERROR("File invalid.\n");                                                \
00064     TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype);                        \
00065     if (dtype!=datatype)                                                            \
00066         SG_ERROR("Datatype mismatch\n");                                            \
00067                                                                                     \
00068     if (fread(&num_feat, sizeof(int32_t), 1, file)!=1 ||                            \
00069             fread(&num_vec, sizeof(int32_t), 1, file)!=1)                           \
00070         SG_ERROR("Failed to read Matrix dimensions\n");                             \
00071     matrix=SG_MALLOC(sg_type, int64_t(num_feat)*num_vec);                                   \
00072     if (fread(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec)   \
00073         SG_ERROR("Failed to read Matrix\n");                                        \
00074 }
00075 
00076 GET_MATRIX(get_matrix, char, TSGDataType(CT_MATRIX, ST_NONE, PT_CHAR))
00077 GET_MATRIX(get_matrix, uint8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_UINT8))
00078 GET_MATRIX(get_int8_matrix, int8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT8))
00079 GET_MATRIX(get_matrix, int32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
00080 GET_MATRIX(get_uint_matrix, uint32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
00081 GET_MATRIX(get_long_matrix, int64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
00082 GET_MATRIX(get_ulong_matrix, uint64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
00083 GET_MATRIX(get_matrix, int16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
00084 GET_MATRIX(get_matrix, uint16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
00085 GET_MATRIX(get_matrix, float32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT32))
00086 GET_MATRIX(get_matrix, float64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT64))
00087 GET_MATRIX(get_longreal_matrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOATMAX))
00088 #undef GET_MATRIX
00089 
00090 #define GET_NDARRAY(fname,sg_type,datatype)                                 \
00091 void CBinaryFile::fname(sg_type *& array, int32_t *& dims,int32_t & num_dims)\
00092 {                                                                           \
00093     size_t total = 1;                                                       \
00094                                                                             \
00095     if (!file)                                                              \
00096         SG_ERROR("File invalid.\n");                                        \
00097                                                                             \
00098     TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL);                         \
00099     read_header(&dtype);                                                    \
00100                                                                             \
00101     if (dtype!=datatype)                                                    \
00102         SG_ERROR("Datatype mismatch\n");                                    \
00103                                                                             \
00104     if (fread(&num_dims,sizeof(int32_t),1,file) != 1)                       \
00105         SG_ERROR("Failed to read number of dimensions");                    \
00106                                                                             \
00107     dims = SG_MALLOC(int32_t, num_dims);                                            \
00108     if (fread(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims)      \
00109         SG_ERROR("Failed to read sizes of dimensions!");                    \
00110                                                                             \
00111     for (int32_t i = 0;i < num_dims;i++)                                    \
00112         total *= dims[i];                                                   \
00113                                                                             \
00114     array = SG_MALLOC(sg_type, total);                                              \
00115     if (fread(array,sizeof(sg_type),total,file) != (size_t)total)           \
00116         SG_ERROR("Failed to read array data!");                             \
00117 }
00118 
00119 GET_NDARRAY(get_ndarray,uint8_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_UINT8));
00120 GET_NDARRAY(get_ndarray,char,TSGDataType(CT_NDARRAY, ST_NONE, PT_CHAR));
00121 GET_NDARRAY(get_ndarray,int32_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_INT32));
00122 GET_NDARRAY(get_ndarray,int16_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_INT16));
00123 GET_NDARRAY(get_ndarray,uint16_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_UINT16));
00124 GET_NDARRAY(get_ndarray,float32_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_FLOAT32));
00125 GET_NDARRAY(get_ndarray,float64_t,TSGDataType(CT_NDARRAY, ST_NONE, PT_FLOAT64));
00126 #undef GET_NDARRAY
00127 
00128 #define GET_SPARSEMATRIX(fname, sg_type, datatype)                                      \
00129 void CBinaryFile::fname(SGSparseVector<sg_type>*& matrix, int32_t& num_feat, int32_t& num_vec)  \
00130 {                                                                                       \
00131     if (!(file))                                                                        \
00132         SG_ERROR("File invalid.\n");                                                    \
00133                                                                                         \
00134     TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype);                            \
00135     if (dtype!=datatype)                                                                \
00136         SG_ERROR("Datatype mismatch\n");                                                \
00137                                                                                         \
00138     if (fread(&num_vec, sizeof(int32_t), 1, file)!=1)                                   \
00139         SG_ERROR("Failed to read number of vectors\n");                                 \
00140                                                                                         \
00141     matrix=SG_MALLOC(SGSparseVector<sg_type>, num_vec);                                             \
00142                                                                                         \
00143     for (int32_t i=0; i<num_vec; i++)                                                   \
00144     {                                                                                   \
00145         int32_t len=0;                                                                  \
00146         if (fread(&len, sizeof(int32_t), 1, file)!=1)                                   \
00147             SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i);      \
00148         matrix[i].num_feat_entries=len;                                                 \
00149         SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len);                   \
00150         if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)     \
00151             SG_ERROR("Failed to read sparse vector %d\n", i);                           \
00152         matrix[i].features=vec;                                                         \
00153     }                                                                                   \
00154 }
00155 GET_SPARSEMATRIX(get_sparse_matrix, bool, TSGDataType(CT_MATRIX, ST_NONE, PT_BOOL))
00156 GET_SPARSEMATRIX(get_sparse_matrix, char, TSGDataType(CT_MATRIX, ST_NONE, PT_CHAR))
00157 GET_SPARSEMATRIX(get_sparse_matrix, uint8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_UINT8))
00158 GET_SPARSEMATRIX(get_int8_sparsematrix, int8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT8))
00159 GET_SPARSEMATRIX(get_sparse_matrix, int32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
00160 GET_SPARSEMATRIX(get_uint_sparsematrix, uint32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
00161 GET_SPARSEMATRIX(get_long_sparsematrix, int64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
00162 GET_SPARSEMATRIX(get_ulong_sparsematrix, uint64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
00163 GET_SPARSEMATRIX(get_sparse_matrix, int16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
00164 GET_SPARSEMATRIX(get_sparse_matrix, uint16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
00165 GET_SPARSEMATRIX(get_sparse_matrix, float32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT32))
00166 GET_SPARSEMATRIX(get_sparse_matrix, float64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT64))
00167 GET_SPARSEMATRIX(get_longreal_sparsematrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOATMAX))
00168 #undef GET_SPARSEMATRIX
00169 
00170 
00171 #define GET_STRING_LIST(fname, sg_type, datatype)                                               \
00172 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
00173 {                                                                                               \
00174     strings=NULL;                                                                               \
00175     num_str=0;                                                                                  \
00176     max_string_len=0;                                                                           \
00177                                                                                                 \
00178     if (!file)                                                                                  \
00179         SG_ERROR("File invalid.\n");                                                            \
00180                                                                                                 \
00181     TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype);                                    \
00182     if (dtype!=datatype)                                                                        \
00183         SG_ERROR("Datatype mismatch\n");                                                        \
00184                                                                                                 \
00185     if (fread(&num_str, sizeof(int32_t), 1, file)!=1)                                           \
00186         SG_ERROR("Failed to read number of strings\n");                                         \
00187                                                                                                 \
00188     strings=SG_MALLOC(SGString<sg_type>, num_str);                                                      \
00189                                                                                                 \
00190     for (int32_t i=0; i<num_str; i++)                                                           \
00191     {                                                                                           \
00192         int32_t len=0;                                                                          \
00193         if (fread(&len, sizeof(int32_t), 1, file)!=1)                                           \
00194             SG_ERROR("Failed to read string length of string with idx=%d\n", i);                \
00195         strings[i].slen=len;                                                                    \
00196         sg_type* str = SG_MALLOC(sg_type, len);                                                     \
00197         if (fread(str, sizeof(sg_type), len, file)!= (size_t) len)                              \
00198             SG_ERROR("Failed to read string %d\n", i);                                          \
00199         strings[i].string=str;                                                                  \
00200     }                                                                                           \
00201 }
00202 
00203 GET_STRING_LIST(get_string_list, char, TSGDataType(CT_VECTOR, ST_NONE, PT_CHAR))
00204 GET_STRING_LIST(get_string_list, uint8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT8))
00205 GET_STRING_LIST(get_int8_string_list, int8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT8))
00206 GET_STRING_LIST(get_string_list, int32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
00207 GET_STRING_LIST(get_uint_string_list, uint32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
00208 GET_STRING_LIST(get_long_string_list, int64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
00209 GET_STRING_LIST(get_ulong_string_list, uint64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
00210 GET_STRING_LIST(get_string_list, int16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
00211 GET_STRING_LIST(get_string_list, uint16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
00212 GET_STRING_LIST(get_string_list, float32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT32))
00213 GET_STRING_LIST(get_string_list, float64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT64))
00214 GET_STRING_LIST(get_longreal_string_list, floatmax_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOATMAX))
00215 #undef GET_STRING_LIST
00216 
00219 #define SET_VECTOR(fname, sg_type, dtype)                           \
00220 void CBinaryFile::fname(const sg_type* vec, int32_t len)            \
00221 {                                                                   \
00222     if (!(file && vec))                                             \
00223         SG_ERROR("File or vector invalid.\n");                      \
00224                                                                     \
00225     TSGDataType t dtype; write_header(&t);                          \
00226                                                                     \
00227     if (fwrite(&len, sizeof(int32_t), 1, file)!=1 ||                \
00228             fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len)  \
00229         SG_ERROR("Failed to write vector\n");                       \
00230 }
00231 SET_VECTOR(set_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
00232 SET_VECTOR(set_vector, char, (CT_VECTOR, ST_NONE, PT_CHAR))
00233 SET_VECTOR(set_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00234 SET_VECTOR(set_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
00235 SET_VECTOR(set_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
00236 SET_VECTOR(set_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00237 SET_VECTOR(set_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00238 #undef SET_VECTOR
00239 
00240 #define SET_MATRIX(fname, sg_type, dtype) \
00241 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec)   \
00242 {                                                                                   \
00243     if (!(file && matrix))                                                          \
00244         SG_ERROR("File or matrix invalid.\n");                                      \
00245                                                                                     \
00246     TSGDataType t dtype; write_header(&t);                                          \
00247                                                                                     \
00248     if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 ||                           \
00249             fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 ||                        \
00250             fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec)  \
00251         SG_ERROR("Failed to write Matrix\n");                                       \
00252 }
00253 SET_MATRIX(set_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
00254 SET_MATRIX(set_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
00255 SET_MATRIX(set_int8_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
00256 SET_MATRIX(set_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00257 SET_MATRIX(set_uint_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00258 SET_MATRIX(set_long_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00259 SET_MATRIX(set_ulong_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00260 SET_MATRIX(set_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00261 SET_MATRIX(set_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00262 SET_MATRIX(set_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
00263 SET_MATRIX(set_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
00264 SET_MATRIX(set_longreal_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
00265 #undef SET_MATRIX
00266 
00267 #define SET_NDARRAY(fname,sg_type,datatype)                                 \
00268 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \
00269 {                                                                           \
00270     size_t total = 1;                                                       \
00271                                                                             \
00272     if (!file)                                                              \
00273         SG_ERROR("File invalid.\n");                                        \
00274                                                                             \
00275     if (!array)                                                             \
00276         SG_ERROR("Invalid array!\n");                                       \
00277                                                                             \
00278     TSGDataType t datatype;                                                 \
00279     write_header(&t);                                                       \
00280                                                                             \
00281     if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1)                      \
00282         SG_ERROR("Failed to write number of dimensions!\n");                \
00283                                                                             \
00284     if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims)     \
00285         SG_ERROR("Failed to write sizes of dimensions!\n");                 \
00286                                                                             \
00287     for (int32_t i = 0;i < num_dims;i++)                                        \
00288         total *= dims[i];                                                   \
00289                                                                             \
00290     if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total)          \
00291         SG_ERROR("Failed to write array data!\n");                          \
00292 }
00293 
00294 SET_NDARRAY(set_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
00295 SET_NDARRAY(set_ndarray,char,(CT_NDARRAY, ST_NONE, PT_CHAR));
00296 SET_NDARRAY(set_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
00297 SET_NDARRAY(set_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
00298 SET_NDARRAY(set_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
00299 SET_NDARRAY(set_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
00300 SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
00301 #undef SET_NDARRAY
00302 
00303 #define SET_SPARSEMATRIX(fname, sg_type, dtype)             \
00304 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix,  \
00305         int32_t num_feat, int32_t num_vec)                  \
00306 {                                                           \
00307     if (!(file && matrix))                                  \
00308         SG_ERROR("File or matrix invalid.\n");              \
00309                                                             \
00310     TSGDataType t dtype; write_header(&t);                  \
00311                                                             \
00312     if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1)      \
00313         SG_ERROR("Failed to write Sparse Matrix\n");        \
00314                                                             \
00315     for (int32_t i=0; i<num_vec; i++)                       \
00316     {                                                       \
00317         SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \
00318         int32_t len=matrix[i].num_feat_entries;             \
00319         if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) ||  \
00320                 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len))      \
00321             SG_ERROR("Failed to write Sparse Matrix\n");    \
00322     }                                                       \
00323 }
00324 SET_SPARSEMATRIX(set_sparse_matrix, bool, (CT_MATRIX, ST_NONE, PT_BOOL))
00325 SET_SPARSEMATRIX(set_sparse_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
00326 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
00327 SET_SPARSEMATRIX(set_int8_sparsematrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
00328 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00329 SET_SPARSEMATRIX(set_uint_sparsematrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00330 SET_SPARSEMATRIX(set_long_sparsematrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00331 SET_SPARSEMATRIX(set_ulong_sparsematrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00332 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00333 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00334 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
00335 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
00336 SET_SPARSEMATRIX(set_longreal_sparsematrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
00337 #undef SET_SPARSEMATRIX
00338 
00339 #define SET_STRING_LIST(fname, sg_type, dtype) \
00340 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str)  \
00341 {                                                                                       \
00342     if (!(file && strings))                                                             \
00343         SG_ERROR("File or strings invalid.\n");                                         \
00344                                                                                         \
00345     TSGDataType t dtype; write_header(&t);                                              \
00346     for (int32_t i=0; i<num_str; i++)                                                   \
00347     {                                                                                   \
00348         int32_t len = strings[i].slen;                                              \
00349         if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) ||                              \
00350                 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \
00351             SG_ERROR("Failed to write Sparse Matrix\n");                                \
00352     }                                                                                   \
00353 }
00354 SET_STRING_LIST(set_string_list, char, (CT_VECTOR, ST_NONE, PT_CHAR))
00355 SET_STRING_LIST(set_string_list, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
00356 SET_STRING_LIST(set_int8_string_list, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
00357 SET_STRING_LIST(set_string_list, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00358 SET_STRING_LIST(set_uint_string_list, uint32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00359 SET_STRING_LIST(set_long_string_list, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
00360 SET_STRING_LIST(set_ulong_string_list, uint64_t, (CT_VECTOR, ST_NONE, PT_INT64))
00361 SET_STRING_LIST(set_string_list, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00362 SET_STRING_LIST(set_string_list, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00363 SET_STRING_LIST(set_string_list, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
00364 SET_STRING_LIST(set_string_list, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
00365 SET_STRING_LIST(set_longreal_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
00366 #undef SET_STRING_LIST
00367 
00368 
00369 int32_t CBinaryFile::parse_first_header(TSGDataType& type)
00370 {
00371         return -1;
00372 }
00373 
00374 int32_t CBinaryFile::parse_next_header(TSGDataType& type)
00375 {
00376         return -1;
00377 }
00378 
00379 void
00380 CBinaryFile::read_header(TSGDataType* dest)
00381 {
00382     ASSERT(file);
00383     ASSERT(dest);
00384 
00385     if (fseek(file, 0L, SEEK_SET)!=0)
00386         SG_ERROR("Error seeking file '%s' to the beginning.\n", filename);
00387 
00388     char fourcc[4];
00389     uint16_t endian=0;
00390 
00391     if (fread(&fourcc, sizeof(char), 4, file)!=4)
00392         SG_ERROR("Error reading fourcc header in file '%s'\n", filename);
00393 
00394     if (fread(&endian, sizeof(uint16_t), 1, file)!=1)
00395         SG_ERROR("Error reading endian header in file '%s'\n", filename);
00396 
00397     if ((fread(&dest->m_ctype, sizeof(dest->m_ctype), 1, file)!=1) ||
00398             (fread(&dest->m_ptype, sizeof(dest->m_ptype), 1, file)!=1))
00399         SG_ERROR("Error reading datatype header in file '%s'\n", filename);
00400 
00401     if (strncmp(fourcc, "SG01", 4))
00402         SG_ERROR("Header mismatch, expected SG01 in file '%s'\n", filename);
00403 }
00404 
00405 void
00406 CBinaryFile::write_header(const TSGDataType* datatype)
00407 {
00408     ASSERT(file);
00409 
00410     const char* fourcc="SG01";
00411     uint16_t endian=0x1234;
00412 
00413     if (!((fwrite(fourcc, sizeof(char), 4, file)==4) &&
00414           (fwrite(&endian, sizeof(uint16_t), 1, file)==1) &&
00415           (fwrite(&datatype->m_ctype, sizeof(datatype->m_ctype), 1,
00416                   file)==1)
00417           && (fwrite(&datatype->m_ptype, sizeof(datatype->m_ptype), 1,
00418                      file)==1)
00419             ))
00420         SG_ERROR("Error writing header\n");
00421 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation