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

SHOGUN Machine Learning Toolbox - Documentation