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

SHOGUN Machine Learning Toolbox - Documentation