StreamingFile.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) 2011 Shashwat Lal Das
00008  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
00009  */
00010 
00011 #include <shogun/io/streaming/StreamingFile.h>
00012 
00013 #include <ctype.h>
00014 
00015 namespace shogun
00016 {
00028 /* For dense vectors */
00029 #define GET_VECTOR(fname, conv, sg_type)                \
00030     void CStreamingFile::get_vector                 \
00031     (sg_type*& vector, int32_t& num_feat)               \
00032     {                               \
00033         vector=NULL;                        \
00034         num_feat=-1;                        \
00035         SG_ERROR("Read function not supported by the feature type!"); \
00036     }
00037 
00038 GET_VECTOR(get_bool_vector, atoi, bool)
00039 GET_VECTOR(get_byte_vector, atoi, uint8_t)
00040 GET_VECTOR(get_char_vector, atoi, char)
00041 GET_VECTOR(get_int_vector, atoi, int32_t)
00042 GET_VECTOR(get_shortreal_vector, atof, float32_t)
00043 GET_VECTOR(get_real_vector, atof, float64_t)
00044 GET_VECTOR(get_short_vector, atoi, int16_t)
00045 GET_VECTOR(get_word_vector, atoi, uint16_t)
00046 GET_VECTOR(get_int8_vector, atoi, int8_t)
00047 GET_VECTOR(get_uint_vector, atoi, uint32_t)
00048 GET_VECTOR(get_long_vector, atoi, int64_t)
00049 GET_VECTOR(get_ulong_vector, atoi, uint64_t)
00050 GET_VECTOR(get_longreal_vector, atoi, floatmax_t)
00051 #undef GET_VECTOR
00052 
00053 /* For dense vectors with labels */
00054 #define GET_VECTOR_AND_LABEL(fname, conv, sg_type)          \
00055     void CStreamingFile::get_vector_and_label           \
00056     (sg_type*& vector, int32_t& num_feat, float64_t& label)     \
00057     {                               \
00058         vector=NULL;                        \
00059         num_feat=-1;                        \
00060         SG_ERROR("Read function not supported by the feature type!"); \
00061     }
00062 
00063 GET_VECTOR_AND_LABEL(get_bool_vector_and_label, str_to_bool, bool)
00064 GET_VECTOR_AND_LABEL(get_byte_vector_and_label, atoi, uint8_t)
00065 GET_VECTOR_AND_LABEL(get_char_vector_and_label, atoi, char)
00066 GET_VECTOR_AND_LABEL(get_int_vector_and_label, atoi, int32_t)
00067 GET_VECTOR_AND_LABEL(get_shortreal_vector_and_label, atof, float32_t)
00068 GET_VECTOR_AND_LABEL(get_real_vector_and_label, atof, float64_t)
00069 GET_VECTOR_AND_LABEL(get_short_vector_and_label, atoi, int16_t)
00070 GET_VECTOR_AND_LABEL(get_word_vector_and_label, atoi, uint16_t)
00071 GET_VECTOR_AND_LABEL(get_int8_vector_and_label, atoi, int8_t)
00072 GET_VECTOR_AND_LABEL(get_uint_vector_and_label, atoi, uint32_t)
00073 GET_VECTOR_AND_LABEL(get_long_vector_and_label, atoi, int64_t)
00074 GET_VECTOR_AND_LABEL(get_ulong_vector_and_label, atoi, uint64_t)
00075 GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t)
00076 #undef GET_VECTOR_AND_LABEL
00077 
00078 /* For string vectors */
00079 #define GET_STRING(fname, conv, sg_type)                \
00080     void CStreamingFile::get_string                 \
00081     (sg_type*& vector, int32_t& num_feat)               \
00082     {                               \
00083         vector=NULL;                        \
00084         num_feat=-1;                        \
00085         SG_ERROR("Read function not supported by the feature type!"); \
00086     }
00087 
00088 GET_STRING(get_bool_string, str_to_bool, bool)
00089 GET_STRING(get_byte_string, atoi, uint8_t)
00090 GET_STRING(get_char_string, atoi, char)
00091 GET_STRING(get_int_string, atoi, int32_t)
00092 GET_STRING(get_shortreal_string, atof, float32_t)
00093 GET_STRING(get_real_string, atof, float64_t)
00094 GET_STRING(get_short_string, atoi, int16_t)
00095 GET_STRING(get_word_string, atoi, uint16_t)
00096 GET_STRING(get_int8_string, atoi, int8_t)
00097 GET_STRING(get_uint_string, atoi, uint32_t)
00098 GET_STRING(get_long_string, atoi, int64_t)
00099 GET_STRING(get_ulong_string, atoi, uint64_t)
00100 GET_STRING(get_longreal_string, atoi, floatmax_t)
00101 #undef GET_STRING
00102 
00103 /* For string vectors with labels */
00104 #define GET_STRING_AND_LABEL(fname, conv, sg_type)          \
00105     void CStreamingFile::get_string_and_label           \
00106     (sg_type*& vector, int32_t& num_feat, float64_t& label)     \
00107     {                               \
00108         vector=NULL;                        \
00109         num_feat=-1;                            \
00110         SG_ERROR("Read function not supported by the feature type!"); \
00111     }
00112 
00113 GET_STRING_AND_LABEL(get_bool_string_and_label, str_to_bool, bool)
00114 GET_STRING_AND_LABEL(get_byte_string_and_label, atoi, uint8_t)
00115 GET_STRING_AND_LABEL(get_char_string_and_label, atoi, char)
00116 GET_STRING_AND_LABEL(get_int_string_and_label, atoi, int32_t)
00117 GET_STRING_AND_LABEL(get_shortreal_string_and_label, atof, float32_t)
00118 GET_STRING_AND_LABEL(get_real_string_and_label, atof, float64_t)
00119 GET_STRING_AND_LABEL(get_short_string_and_label, atoi, int16_t)
00120 GET_STRING_AND_LABEL(get_word_string_and_label, atoi, uint16_t)
00121 GET_STRING_AND_LABEL(get_int8_string_and_label, atoi, int8_t)
00122 GET_STRING_AND_LABEL(get_uint_string_and_label, atoi, uint32_t)
00123 GET_STRING_AND_LABEL(get_long_string_and_label, atoi, int64_t)
00124 GET_STRING_AND_LABEL(get_ulong_string_and_label, atoi, uint64_t)
00125 GET_STRING_AND_LABEL(get_longreal_string_and_label, atoi, floatmax_t)
00126 #undef GET_STRING_AND_LABEL
00127 
00128 /* For sparse vectors */
00129 #define GET_SPARSE_VECTOR(fname, conv, sg_type)             \
00130                                     \
00131     void CStreamingFile::get_sparse_vector              \
00132     (SGSparseVectorEntry<sg_type>*& vector, int32_t& num_feat)  \
00133     {                               \
00134         vector=NULL;                        \
00135         num_feat=-1;                        \
00136         SG_ERROR("Read function not supported by the feature type!"); \
00137     }
00138 
00139 GET_SPARSE_VECTOR(get_bool_sparse_vector, str_to_bool, bool)
00140 GET_SPARSE_VECTOR(get_byte_sparse_vector, atoi, uint8_t)
00141 GET_SPARSE_VECTOR(get_char_sparse_vector, atoi, char)
00142 GET_SPARSE_VECTOR(get_int_sparse_vector, atoi, int32_t)
00143 GET_SPARSE_VECTOR(get_shortreal_sparse_vector, atof, float32_t)
00144 GET_SPARSE_VECTOR(get_real_sparse_vector, atof, float64_t)
00145 GET_SPARSE_VECTOR(get_short_sparse_vector, atoi, int16_t)
00146 GET_SPARSE_VECTOR(get_word_sparse_vector, atoi, uint16_t)
00147 GET_SPARSE_VECTOR(get_int8_sparse_vector, atoi, int8_t)
00148 GET_SPARSE_VECTOR(get_uint_sparse_vector, atoi, uint32_t)
00149 GET_SPARSE_VECTOR(get_long_sparse_vector, atoi, int64_t)
00150 GET_SPARSE_VECTOR(get_ulong_sparse_vector, atoi, uint64_t)
00151 GET_SPARSE_VECTOR(get_longreal_sparse_vector, atoi, floatmax_t)
00152 #undef GET_SPARSE_VECTOR
00153 
00154 /* For sparse vectors with labels */
00155 #define GET_SPARSE_VECTOR_AND_LABEL(fname, conv, sg_type)       \
00156                                     \
00157     void CStreamingFile::get_sparse_vector_and_label        \
00158     (SGSparseVectorEntry<sg_type>*& vector,             \
00159      int32_t& num_feat,                     \
00160      float64_t& label)                      \
00161     {                               \
00162         vector=NULL;                        \
00163         num_feat=-1;                        \
00164         SG_ERROR("Read function not supported by the feature type!"); \
00165     }
00166 
00167 GET_SPARSE_VECTOR_AND_LABEL(get_bool_sparse_vector_and_label, str_to_bool, bool)
00168 GET_SPARSE_VECTOR_AND_LABEL(get_byte_sparse_vector_and_label, atoi, uint8_t)
00169 GET_SPARSE_VECTOR_AND_LABEL(get_char_sparse_vector_and_label, atoi, char)
00170 GET_SPARSE_VECTOR_AND_LABEL(get_int_sparse_vector_and_label, atoi, int32_t)
00171 GET_SPARSE_VECTOR_AND_LABEL(get_shortreal_sparse_vector_and_label, atof, float32_t)
00172 GET_SPARSE_VECTOR_AND_LABEL(get_real_sparse_vector_and_label, atof, float64_t)
00173 GET_SPARSE_VECTOR_AND_LABEL(get_short_sparse_vector_and_label, atoi, int16_t)
00174 GET_SPARSE_VECTOR_AND_LABEL(get_word_sparse_vector_and_label, atoi, uint16_t)
00175 GET_SPARSE_VECTOR_AND_LABEL(get_int8_sparse_vector_and_label, atoi, int8_t)
00176 GET_SPARSE_VECTOR_AND_LABEL(get_uint_sparse_vector_and_label, atoi, uint32_t)
00177 GET_SPARSE_VECTOR_AND_LABEL(get_long_sparse_vector_and_label, atoi, int64_t)
00178 GET_SPARSE_VECTOR_AND_LABEL(get_ulong_sparse_vector_and_label, atoi, uint64_t)
00179 GET_SPARSE_VECTOR_AND_LABEL(get_longreal_sparse_vector_and_label, atoi, floatmax_t)
00180 #undef GET_SPARSE_VECTOR_AND_LABEL
00181 
00182 void CStreamingFile::get_vector(VwExample*& ex, int32_t &len)
00183 {
00184     SG_ERROR("Read function not supported by the feature type!\n");
00185 }
00186 
00187 void CStreamingFile::get_vector_and_label(VwExample*& ex, int32_t& len, float64_t& label)
00188 {
00189     SG_ERROR("Read function not supported by the feature type!\n");
00190 }
00191 
00192 }
00193 using namespace shogun;
00194 
00195 CStreamingFile::CStreamingFile() : CSGObject()
00196 {
00197     buf=NULL;
00198     filename=NULL;
00199 }
00200 
00201 CStreamingFile::CStreamingFile(const char* fname, char rw) : CSGObject()
00202 {
00203     task=rw;
00204     filename=strdup(fname);
00205     int mode = O_LARGEFILE;
00206 
00207     switch (rw)
00208     {
00209     case 'r':
00210         mode |= O_RDONLY;
00211         break;
00212     case 'w':
00213         mode |= O_WRONLY;
00214         break;
00215     default:
00216         SG_ERROR("Unknown mode '%c'\n", task);
00217     }
00218 
00219     if (filename)
00220     {
00221         int file = open((const char*) filename, mode);
00222         if (file < 0)
00223             SG_ERROR("Error opening file '%s'\n", filename);
00224 
00225         buf = new CIOBuffer(file);
00226         SG_REF(buf);
00227     }
00228     else
00229         SG_ERROR("Error getting the file name!\n");
00230 }
00231 
00232 CStreamingFile::~CStreamingFile()
00233 {
00234     SG_FREE(filename);
00235     SG_UNREF(buf);
00236 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation