SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
StreamingFile.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) 2011 Shashwat Lal Das
8  * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/memory.h>
14 #include <fcntl.h>
15 #ifdef _WIN32
16 #include <io.h>
17 #endif
18 
19 namespace shogun
20 {
32 /* For dense vectors */
33 #define GET_VECTOR(fname, conv, sg_type) \
34  void CStreamingFile::get_vector \
35  (sg_type*& vector, int32_t& num_feat) \
36  { \
37  vector=NULL; \
38  num_feat=-1; \
39  SG_ERROR("Read function not supported by the feature type!") \
40  }
41 
42 GET_VECTOR(get_bool_vector, atoi, bool)
43 GET_VECTOR(get_byte_vector, atoi, uint8_t)
44 GET_VECTOR(get_char_vector, atoi, char)
45 GET_VECTOR(get_int_vector, atoi, int32_t)
46 GET_VECTOR(get_shortreal_vector, atof, float32_t)
47 GET_VECTOR(get_real_vector, atof, float64_t)
48 GET_VECTOR(get_short_vector, atoi, int16_t)
49 GET_VECTOR(get_word_vector, atoi, uint16_t)
50 GET_VECTOR(get_int8_vector, atoi, int8_t)
51 GET_VECTOR(get_uint_vector, atoi, uint32_t)
52 GET_VECTOR(get_long_vector, atoi, int64_t)
53 GET_VECTOR(get_ulong_vector, atoi, uint64_t)
54 GET_VECTOR(get_longreal_vector, atoi, floatmax_t)
55 #undef GET_VECTOR
56 
57 /* For dense vectors with labels */
58 #define GET_VECTOR_AND_LABEL(fname, conv, sg_type) \
59  void CStreamingFile::get_vector_and_label \
60  (sg_type*& vector, int32_t& num_feat, float64_t& label) \
61  { \
62  vector=NULL; \
63  num_feat=-1; \
64  SG_ERROR("Read function not supported by the feature type!") \
65  }
66 
67 GET_VECTOR_AND_LABEL(get_bool_vector_and_label, str_to_bool, bool)
68 GET_VECTOR_AND_LABEL(get_byte_vector_and_label, atoi, uint8_t)
69 GET_VECTOR_AND_LABEL(get_char_vector_and_label, atoi, char)
70 GET_VECTOR_AND_LABEL(get_int_vector_and_label, atoi, int32_t)
71 GET_VECTOR_AND_LABEL(get_shortreal_vector_and_label, atof, float32_t)
72 GET_VECTOR_AND_LABEL(get_real_vector_and_label, atof, float64_t)
73 GET_VECTOR_AND_LABEL(get_short_vector_and_label, atoi, int16_t)
74 GET_VECTOR_AND_LABEL(get_word_vector_and_label, atoi, uint16_t)
75 GET_VECTOR_AND_LABEL(get_int8_vector_and_label, atoi, int8_t)
76 GET_VECTOR_AND_LABEL(get_uint_vector_and_label, atoi, uint32_t)
77 GET_VECTOR_AND_LABEL(get_long_vector_and_label, atoi, int64_t)
78 GET_VECTOR_AND_LABEL(get_ulong_vector_and_label, atoi, uint64_t)
79 GET_VECTOR_AND_LABEL(get_longreal_vector_and_label, atoi, floatmax_t)
80 #undef GET_VECTOR_AND_LABEL
81 
82 /* For string vectors */
83 #define GET_STRING(fname, conv, sg_type) \
84  void CStreamingFile::get_string \
85  (sg_type*& vector, int32_t& num_feat) \
86  { \
87  vector=NULL; \
88  num_feat=-1; \
89  SG_ERROR("Read function not supported by the feature type!") \
90  }
91 
92 GET_STRING(get_bool_string, str_to_bool, bool)
93 GET_STRING(get_byte_string, atoi, uint8_t)
94 GET_STRING(get_char_string, atoi, char)
95 GET_STRING(get_int_string, atoi, int32_t)
96 GET_STRING(get_shortreal_string, atof, float32_t)
97 GET_STRING(get_real_string, atof, float64_t)
98 GET_STRING(get_short_string, atoi, int16_t)
99 GET_STRING(get_word_string, atoi, uint16_t)
100 GET_STRING(get_int8_string, atoi, int8_t)
101 GET_STRING(get_uint_string, atoi, uint32_t)
102 GET_STRING(get_long_string, atoi, int64_t)
103 GET_STRING(get_ulong_string, atoi, uint64_t)
104 GET_STRING(get_longreal_string, atoi, floatmax_t)
105 #undef GET_STRING
106 
107 /* For string vectors with labels */
108 #define GET_STRING_AND_LABEL(fname, conv, sg_type) \
109  void CStreamingFile::get_string_and_label \
110  (sg_type*& vector, int32_t& num_feat, float64_t& label) \
111  { \
112  vector=NULL; \
113  num_feat=-1; \
114  SG_ERROR("Read function not supported by the feature type!") \
115  }
116 
117 GET_STRING_AND_LABEL(get_bool_string_and_label, str_to_bool, bool)
118 GET_STRING_AND_LABEL(get_byte_string_and_label, atoi, uint8_t)
119 GET_STRING_AND_LABEL(get_char_string_and_label, atoi, char)
120 GET_STRING_AND_LABEL(get_int_string_and_label, atoi, int32_t)
121 GET_STRING_AND_LABEL(get_shortreal_string_and_label, atof, float32_t)
122 GET_STRING_AND_LABEL(get_real_string_and_label, atof, float64_t)
123 GET_STRING_AND_LABEL(get_short_string_and_label, atoi, int16_t)
124 GET_STRING_AND_LABEL(get_word_string_and_label, atoi, uint16_t)
125 GET_STRING_AND_LABEL(get_int8_string_and_label, atoi, int8_t)
126 GET_STRING_AND_LABEL(get_uint_string_and_label, atoi, uint32_t)
127 GET_STRING_AND_LABEL(get_long_string_and_label, atoi, int64_t)
128 GET_STRING_AND_LABEL(get_ulong_string_and_label, atoi, uint64_t)
129 GET_STRING_AND_LABEL(get_longreal_string_and_label, atoi, floatmax_t)
130 #undef GET_STRING_AND_LABEL
131 
132 /* For sparse vectors */
133 #define GET_SPARSE_VECTOR(fname, conv, sg_type) \
134  \
135  void CStreamingFile::get_sparse_vector \
136  (SGSparseVectorEntry<sg_type>*& vector, int32_t& num_feat) \
137  { \
138  vector=NULL; \
139  num_feat=-1; \
140  SG_ERROR("Read function not supported by the feature type!") \
141  }
142 
143 GET_SPARSE_VECTOR(get_bool_sparse_vector, str_to_bool, bool)
144 GET_SPARSE_VECTOR(get_byte_sparse_vector, atoi, uint8_t)
145 GET_SPARSE_VECTOR(get_char_sparse_vector, atoi, char)
146 GET_SPARSE_VECTOR(get_int_sparse_vector, atoi, int32_t)
147 GET_SPARSE_VECTOR(get_shortreal_sparse_vector, atof, float32_t)
148 GET_SPARSE_VECTOR(get_real_sparse_vector, atof, float64_t)
149 GET_SPARSE_VECTOR(get_short_sparse_vector, atoi, int16_t)
150 GET_SPARSE_VECTOR(get_word_sparse_vector, atoi, uint16_t)
151 GET_SPARSE_VECTOR(get_int8_sparse_vector, atoi, int8_t)
152 GET_SPARSE_VECTOR(get_uint_sparse_vector, atoi, uint32_t)
153 GET_SPARSE_VECTOR(get_long_sparse_vector, atoi, int64_t)
154 GET_SPARSE_VECTOR(get_ulong_sparse_vector, atoi, uint64_t)
155 GET_SPARSE_VECTOR(get_longreal_sparse_vector, atoi, floatmax_t)
156 #undef GET_SPARSE_VECTOR
157 
158 /* For sparse vectors with labels */
159 #define GET_SPARSE_VECTOR_AND_LABEL(fname, conv, sg_type) \
160  \
161  void CStreamingFile::get_sparse_vector_and_label \
162  (SGSparseVectorEntry<sg_type>*& vector, \
163  int32_t& num_feat, \
164  float64_t& label) \
165  { \
166  vector=NULL; \
167  num_feat=-1; \
168  SG_ERROR("Read function not supported by the feature type!") \
169  }
170 
171 GET_SPARSE_VECTOR_AND_LABEL(get_bool_sparse_vector_and_label, str_to_bool, bool)
172 GET_SPARSE_VECTOR_AND_LABEL(get_byte_sparse_vector_and_label, atoi, uint8_t)
173 GET_SPARSE_VECTOR_AND_LABEL(get_char_sparse_vector_and_label, atoi, char)
174 GET_SPARSE_VECTOR_AND_LABEL(get_int_sparse_vector_and_label, atoi, int32_t)
175 GET_SPARSE_VECTOR_AND_LABEL(get_shortreal_sparse_vector_and_label, atof, float32_t)
176 GET_SPARSE_VECTOR_AND_LABEL(get_real_sparse_vector_and_label, atof, float64_t)
177 GET_SPARSE_VECTOR_AND_LABEL(get_short_sparse_vector_and_label, atoi, int16_t)
178 GET_SPARSE_VECTOR_AND_LABEL(get_word_sparse_vector_and_label, atoi, uint16_t)
179 GET_SPARSE_VECTOR_AND_LABEL(get_int8_sparse_vector_and_label, atoi, int8_t)
180 GET_SPARSE_VECTOR_AND_LABEL(get_uint_sparse_vector_and_label, atoi, uint32_t)
181 GET_SPARSE_VECTOR_AND_LABEL(get_long_sparse_vector_and_label, atoi, int64_t)
182 GET_SPARSE_VECTOR_AND_LABEL(get_ulong_sparse_vector_and_label, atoi, uint64_t)
183 GET_SPARSE_VECTOR_AND_LABEL(get_longreal_sparse_vector_and_label, atoi, floatmax_t)
184 #undef GET_SPARSE_VECTOR_AND_LABEL
185 
186 void CStreamingFile::get_vector(VwExample*& ex, int32_t &len)
187 {
188  SG_ERROR("Read function not supported by the feature type!\n")
189 }
190 
192 {
193  SG_ERROR("Read function not supported by the feature type!\n")
194 }
195 
196 }
197 using namespace shogun;
198 
200 {
201  buf=NULL;
202  filename=NULL;
203 }
204 
205 CStreamingFile::CStreamingFile(const char* fname, char rw) : CSGObject()
206 {
207  task=rw;
208  filename=get_strdup(fname);
209  int mode = O_LARGEFILE;
210 
211  switch (rw)
212  {
213  case 'r':
214  mode |= O_RDONLY;
215  break;
216  case 'w':
217  mode |= O_WRONLY;
218  break;
219  default:
220  SG_ERROR("Unknown mode '%c'\n", task)
221  }
222 
223  if (filename)
224  {
225  int file = open((const char*) filename, mode);
226  if (file < 0)
227  SG_ERROR("Error opening file '%s'\n", filename)
228 
229  buf = new CIOBuffer(file);
230  SG_REF(buf);
231  }
232  else
233  SG_ERROR("Error getting the file name!\n")
234 }
235 
237 {
238  SG_FREE(filename);
239  SG_UNREF(buf);
240 }
An I/O buffer class.
Definition: IOBuffer.h:41
#define GET_STRING_AND_LABEL(fname, conv, sg_type)
virtual void get_vector(bool *&vector, int32_t &len)
#define SG_ERROR(...)
Definition: SGIO.h:128
#define SG_REF(x)
Definition: SGObject.h:52
#define O_LARGEFILE
Definition: IOBuffer.h:26
#define GET_VECTOR_AND_LABEL(fname, conv, sg_type)
char * filename
Name of the handled file.
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
#define GET_SPARSE_VECTOR_AND_LABEL(fname, conv, sg_type)
CIOBuffer * buf
Buffer to hold stuff in memory.
double float64_t
Definition: common.h:60
long double floatmax_t
Definition: common.h:61
Example class for VW.
Definition: vw_example.h:58
#define GET_STRING(fname, conv, sg_type)
#define GET_SPARSE_VECTOR(fname, conv, sg_type)
float float32_t
Definition: common.h:59
#define GET_VECTOR(fname, conv, sg_type)
virtual void get_vector_and_label(bool *&vector, int32_t &len, float64_t &label)
#define SG_UNREF(x)
Definition: SGObject.h:53
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18

SHOGUN Machine Learning Toolbox - Documentation