SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
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(const 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         new (&matrix[i]) SGSparseVector<sg_type>();                                     \
00146         int32_t len=0;                                                                  \
00147         if (fread(&len, sizeof(int32_t), 1, file)!=1)                                   \
00148             SG_ERROR("Failed to read sparse vector length of vector idx=%d\n", i);      \
00149         matrix[i].num_feat_entries=len;                                                 \
00150         SGSparseVectorEntry<sg_type>* vec = SG_MALLOC(SGSparseVectorEntry<sg_type>, len);                   \
00151         if (fread(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len)     \
00152             SG_ERROR("Failed to read sparse vector %d\n", i);                           \
00153         matrix[i].features=vec;                                                         \
00154     }                                                                                   \
00155 }
00156 GET_SPARSEMATRIX(get_sparse_matrix, bool, TSGDataType(CT_MATRIX, ST_NONE, PT_BOOL))
00157 GET_SPARSEMATRIX(get_sparse_matrix, char, TSGDataType(CT_MATRIX, ST_NONE, PT_CHAR))
00158 GET_SPARSEMATRIX(get_sparse_matrix, uint8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_UINT8))
00159 GET_SPARSEMATRIX(get_int8_sparsematrix, int8_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT8))
00160 GET_SPARSEMATRIX(get_sparse_matrix, int32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
00161 GET_SPARSEMATRIX(get_uint_sparsematrix, uint32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT32))
00162 GET_SPARSEMATRIX(get_long_sparsematrix, int64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
00163 GET_SPARSEMATRIX(get_ulong_sparsematrix, uint64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT64))
00164 GET_SPARSEMATRIX(get_sparse_matrix, int16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
00165 GET_SPARSEMATRIX(get_sparse_matrix, uint16_t, TSGDataType(CT_MATRIX, ST_NONE, PT_INT16))
00166 GET_SPARSEMATRIX(get_sparse_matrix, float32_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT32))
00167 GET_SPARSEMATRIX(get_sparse_matrix, float64_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOAT64))
00168 GET_SPARSEMATRIX(get_longreal_sparsematrix, floatmax_t, TSGDataType(CT_MATRIX, ST_NONE, PT_FLOATMAX))
00169 #undef GET_SPARSEMATRIX
00170 
00171 
00172 #define GET_STRING_LIST(fname, sg_type, datatype)                                               \
00173 void CBinaryFile::fname(SGString<sg_type>*& strings, int32_t& num_str, int32_t& max_string_len) \
00174 {                                                                                               \
00175     strings=NULL;                                                                               \
00176     num_str=0;                                                                                  \
00177     max_string_len=0;                                                                           \
00178                                                                                                 \
00179     if (!file)                                                                                  \
00180         SG_ERROR("File invalid.\n");                                                            \
00181                                                                                                 \
00182     TSGDataType dtype(CT_SCALAR, ST_NONE, PT_BOOL); read_header(&dtype);                                    \
00183     if (dtype!=datatype)                                                                        \
00184         SG_ERROR("Datatype mismatch\n");                                                        \
00185                                                                                                 \
00186     if (fread(&num_str, sizeof(int32_t), 1, file)!=1)                                           \
00187         SG_ERROR("Failed to read number of strings\n");                                         \
00188                                                                                                 \
00189     strings=SG_MALLOC(SGString<sg_type>, num_str);                                                      \
00190                                                                                                 \
00191     for (int32_t i=0; i<num_str; i++)                                                           \
00192     {                                                                                           \
00193         int32_t len=0;                                                                          \
00194         if (fread(&len, sizeof(int32_t), 1, file)!=1)                                           \
00195             SG_ERROR("Failed to read string length of string with idx=%d\n", i);                \
00196         strings[i].slen=len;                                                                    \
00197         sg_type* str = SG_MALLOC(sg_type, len);                                                     \
00198         if (fread(str, sizeof(sg_type), len, file)!= (size_t) len)                              \
00199             SG_ERROR("Failed to read string %d\n", i);                                          \
00200         strings[i].string=str;                                                                  \
00201     }                                                                                           \
00202 }
00203 
00204 GET_STRING_LIST(get_string_list, char, TSGDataType(CT_VECTOR, ST_NONE, PT_CHAR))
00205 GET_STRING_LIST(get_string_list, uint8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_UINT8))
00206 GET_STRING_LIST(get_int8_string_list, int8_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT8))
00207 GET_STRING_LIST(get_string_list, int32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
00208 GET_STRING_LIST(get_uint_string_list, uint32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT32))
00209 GET_STRING_LIST(get_long_string_list, int64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
00210 GET_STRING_LIST(get_ulong_string_list, uint64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT64))
00211 GET_STRING_LIST(get_string_list, int16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
00212 GET_STRING_LIST(get_string_list, uint16_t, TSGDataType(CT_VECTOR, ST_NONE, PT_INT16))
00213 GET_STRING_LIST(get_string_list, float32_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT32))
00214 GET_STRING_LIST(get_string_list, float64_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOAT64))
00215 GET_STRING_LIST(get_longreal_string_list, floatmax_t, TSGDataType(CT_VECTOR, ST_NONE, PT_FLOATMAX))
00216 #undef GET_STRING_LIST
00217 
00220 #define SET_VECTOR(fname, sg_type, dtype)                           \
00221 void CBinaryFile::fname(const sg_type* vec, int32_t len)            \
00222 {                                                                   \
00223     if (!(file && vec))                                             \
00224         SG_ERROR("File or vector invalid.\n");                      \
00225                                                                     \
00226     TSGDataType t dtype; write_header(&t);                          \
00227                                                                     \
00228     if (fwrite(&len, sizeof(int32_t), 1, file)!=1 ||                \
00229             fwrite(vec, sizeof(sg_type), len, file)!=(size_t) len)  \
00230         SG_ERROR("Failed to write vector\n");                       \
00231 }
00232 SET_VECTOR(set_vector, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
00233 SET_VECTOR(set_vector, char, (CT_VECTOR, ST_NONE, PT_CHAR))
00234 SET_VECTOR(set_vector, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00235 SET_VECTOR(set_vector, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
00236 SET_VECTOR(set_vector, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
00237 SET_VECTOR(set_vector, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00238 SET_VECTOR(set_vector, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00239 #undef SET_VECTOR
00240 
00241 #define SET_MATRIX(fname, sg_type, dtype) \
00242 void CBinaryFile::fname(const sg_type* matrix, int32_t num_feat, int32_t num_vec)   \
00243 {                                                                                   \
00244     if (!(file && matrix))                                                          \
00245         SG_ERROR("File or matrix invalid.\n");                                      \
00246                                                                                     \
00247     TSGDataType t dtype; write_header(&t);                                          \
00248                                                                                     \
00249     if (fwrite(&num_feat, sizeof(int32_t), 1, file)!=1 ||                           \
00250             fwrite(&num_vec, sizeof(int32_t), 1, file)!=1 ||                        \
00251             fwrite(matrix, sizeof(sg_type)*num_feat, num_vec, file)!=(size_t) num_vec)  \
00252         SG_ERROR("Failed to write Matrix\n");                                       \
00253 }
00254 SET_MATRIX(set_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
00255 SET_MATRIX(set_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
00256 SET_MATRIX(set_int8_matrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
00257 SET_MATRIX(set_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00258 SET_MATRIX(set_uint_matrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00259 SET_MATRIX(set_long_matrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00260 SET_MATRIX(set_ulong_matrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00261 SET_MATRIX(set_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00262 SET_MATRIX(set_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00263 SET_MATRIX(set_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
00264 SET_MATRIX(set_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
00265 SET_MATRIX(set_longreal_matrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
00266 #undef SET_MATRIX
00267 
00268 #define SET_NDARRAY(fname,sg_type,datatype)                                 \
00269 void CBinaryFile::fname(const sg_type * array, int32_t * dims,int32_t num_dims) \
00270 {                                                                           \
00271     size_t total = 1;                                                       \
00272                                                                             \
00273     if (!file)                                                              \
00274         SG_ERROR("File invalid.\n");                                        \
00275                                                                             \
00276     if (!array)                                                             \
00277         SG_ERROR("Invalid array!\n");                                       \
00278                                                                             \
00279     TSGDataType t datatype;                                                 \
00280     write_header(&t);                                                       \
00281                                                                             \
00282     if (fwrite(&num_dims,sizeof(int32_t),1,file) != 1)                      \
00283         SG_ERROR("Failed to write number of dimensions!\n");                \
00284                                                                             \
00285     if (fwrite(dims,sizeof(int32_t),num_dims,file) != (size_t)num_dims)     \
00286         SG_ERROR("Failed to write sizes of dimensions!\n");                 \
00287                                                                             \
00288     for (int32_t i = 0;i < num_dims;i++)                                        \
00289         total *= dims[i];                                                   \
00290                                                                             \
00291     if (fwrite(array,sizeof(sg_type),total,file) != (size_t)total)          \
00292         SG_ERROR("Failed to write array data!\n");                          \
00293 }
00294 
00295 SET_NDARRAY(set_ndarray,uint8_t,(CT_NDARRAY, ST_NONE, PT_UINT8));
00296 SET_NDARRAY(set_ndarray,char,(CT_NDARRAY, ST_NONE, PT_CHAR));
00297 SET_NDARRAY(set_ndarray,int32_t,(CT_NDARRAY, ST_NONE, PT_INT32));
00298 SET_NDARRAY(set_ndarray,int16_t,(CT_NDARRAY, ST_NONE, PT_INT16));
00299 SET_NDARRAY(set_ndarray,uint16_t,(CT_NDARRAY, ST_NONE, PT_UINT16));
00300 SET_NDARRAY(set_ndarray,float32_t,(CT_NDARRAY, ST_NONE, PT_FLOAT32));
00301 SET_NDARRAY(set_ndarray,float64_t,(CT_NDARRAY, ST_NONE, PT_FLOAT64));
00302 #undef SET_NDARRAY
00303 
00304 #define SET_SPARSEMATRIX(fname, sg_type, dtype)             \
00305 void CBinaryFile::fname(const SGSparseVector<sg_type>* matrix,  \
00306         int32_t num_feat, int32_t num_vec)                  \
00307 {                                                           \
00308     if (!(file && matrix))                                  \
00309         SG_ERROR("File or matrix invalid.\n");              \
00310                                                             \
00311     TSGDataType t dtype; write_header(&t);                  \
00312                                                             \
00313     if (fwrite(&num_vec, sizeof(int32_t), 1, file)!=1)      \
00314         SG_ERROR("Failed to write Sparse Matrix\n");        \
00315                                                             \
00316     for (int32_t i=0; i<num_vec; i++)                       \
00317     {                                                       \
00318         SGSparseVectorEntry<sg_type>* vec = matrix[i].features; \
00319         int32_t len=matrix[i].num_feat_entries;             \
00320         if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) ||  \
00321                 (fwrite(vec, sizeof(SGSparseVectorEntry<sg_type>), len, file)!= (size_t) len))      \
00322             SG_ERROR("Failed to write Sparse Matrix\n");    \
00323     }                                                       \
00324 }
00325 SET_SPARSEMATRIX(set_sparse_matrix, bool, (CT_MATRIX, ST_NONE, PT_BOOL))
00326 SET_SPARSEMATRIX(set_sparse_matrix, char, (CT_MATRIX, ST_NONE, PT_CHAR))
00327 SET_SPARSEMATRIX(set_sparse_matrix, uint8_t, (CT_MATRIX, ST_NONE, PT_UINT8))
00328 SET_SPARSEMATRIX(set_int8_sparsematrix, int8_t, (CT_MATRIX, ST_NONE, PT_INT8))
00329 SET_SPARSEMATRIX(set_sparse_matrix, int32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00330 SET_SPARSEMATRIX(set_uint_sparsematrix, uint32_t, (CT_MATRIX, ST_NONE, PT_INT32))
00331 SET_SPARSEMATRIX(set_long_sparsematrix, int64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00332 SET_SPARSEMATRIX(set_ulong_sparsematrix, uint64_t, (CT_MATRIX, ST_NONE, PT_INT64))
00333 SET_SPARSEMATRIX(set_sparse_matrix, int16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00334 SET_SPARSEMATRIX(set_sparse_matrix, uint16_t, (CT_MATRIX, ST_NONE, PT_INT16))
00335 SET_SPARSEMATRIX(set_sparse_matrix, float32_t, (CT_MATRIX, ST_NONE, PT_FLOAT32))
00336 SET_SPARSEMATRIX(set_sparse_matrix, float64_t, (CT_MATRIX, ST_NONE, PT_FLOAT64))
00337 SET_SPARSEMATRIX(set_longreal_sparsematrix, floatmax_t, (CT_MATRIX, ST_NONE, PT_FLOATMAX))
00338 #undef SET_SPARSEMATRIX
00339 
00340 #define SET_STRING_LIST(fname, sg_type, dtype) \
00341 void CBinaryFile::fname(const SGString<sg_type>* strings, int32_t num_str)  \
00342 {                                                                                       \
00343     if (!(file && strings))                                                             \
00344         SG_ERROR("File or strings invalid.\n");                                         \
00345                                                                                         \
00346     TSGDataType t dtype; write_header(&t);                                              \
00347     for (int32_t i=0; i<num_str; i++)                                                   \
00348     {                                                                                   \
00349         int32_t len = strings[i].slen;                                              \
00350         if ((fwrite(&len, sizeof(int32_t), 1, file)!=1) ||                              \
00351                 (fwrite(strings[i].string, sizeof(sg_type), len, file)!= (size_t) len)) \
00352             SG_ERROR("Failed to write Sparse Matrix\n");                                \
00353     }                                                                                   \
00354 }
00355 SET_STRING_LIST(set_string_list, char, (CT_VECTOR, ST_NONE, PT_CHAR))
00356 SET_STRING_LIST(set_string_list, uint8_t, (CT_VECTOR, ST_NONE, PT_UINT8))
00357 SET_STRING_LIST(set_int8_string_list, int8_t, (CT_VECTOR, ST_NONE, PT_INT8))
00358 SET_STRING_LIST(set_string_list, int32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00359 SET_STRING_LIST(set_uint_string_list, uint32_t, (CT_VECTOR, ST_NONE, PT_INT32))
00360 SET_STRING_LIST(set_long_string_list, int64_t, (CT_VECTOR, ST_NONE, PT_INT64))
00361 SET_STRING_LIST(set_ulong_string_list, uint64_t, (CT_VECTOR, ST_NONE, PT_INT64))
00362 SET_STRING_LIST(set_string_list, int16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00363 SET_STRING_LIST(set_string_list, uint16_t, (CT_VECTOR, ST_NONE, PT_INT16))
00364 SET_STRING_LIST(set_string_list, float32_t, (CT_VECTOR, ST_NONE, PT_FLOAT32))
00365 SET_STRING_LIST(set_string_list, float64_t, (CT_VECTOR, ST_NONE, PT_FLOAT64))
00366 SET_STRING_LIST(set_longreal_string_list, floatmax_t, (CT_VECTOR, ST_NONE, PT_FLOATMAX))
00367 #undef SET_STRING_LIST
00368 
00369 
00370 int32_t CBinaryFile::parse_first_header(TSGDataType& type)
00371 {
00372         return -1;
00373 }
00374 
00375 int32_t CBinaryFile::parse_next_header(TSGDataType& type)
00376 {
00377         return -1;
00378 }
00379 
00380 void
00381 CBinaryFile::read_header(TSGDataType* dest)
00382 {
00383     ASSERT(file);
00384     ASSERT(dest);
00385 
00386     if (fseek(file, 0L, SEEK_SET)!=0)
00387         SG_ERROR("Error seeking file '%s' to the beginning.\n", filename);
00388 
00389     char fourcc[4];
00390     uint16_t endian=0;
00391 
00392     if (fread(&fourcc, sizeof(char), 4, file)!=4)
00393         SG_ERROR("Error reading fourcc header in file '%s'\n", filename);
00394 
00395     if (fread(&endian, sizeof(uint16_t), 1, file)!=1)
00396         SG_ERROR("Error reading endian header in file '%s'\n", filename);
00397 
00398     if ((fread(&dest->m_ctype, sizeof(dest->m_ctype), 1, file)!=1) ||
00399             (fread(&dest->m_ptype, sizeof(dest->m_ptype), 1, file)!=1))
00400         SG_ERROR("Error reading datatype header in file '%s'\n", filename);
00401 
00402     if (strncmp(fourcc, "SG01", 4))
00403         SG_ERROR("Header mismatch, expected SG01 in file '%s'\n", filename);
00404 }
00405 
00406 void
00407 CBinaryFile::write_header(const TSGDataType* datatype)
00408 {
00409     ASSERT(file);
00410 
00411     const char* fourcc="SG01";
00412     uint16_t endian=0x1234;
00413 
00414     if (!((fwrite(fourcc, sizeof(char), 4, file)==4) &&
00415           (fwrite(&endian, sizeof(uint16_t), 1, file)==1) &&
00416           (fwrite(&datatype->m_ctype, sizeof(datatype->m_ctype), 1,
00417                   file)==1)
00418           && (fwrite(&datatype->m_ptype, sizeof(datatype->m_ptype), 1,
00419                      file)==1)
00420             ))
00421         SG_ERROR("Error writing header\n");
00422 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation