SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
File.h
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) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  * Copyright (C) 2010 Berlin Institute of Technology
10  */
11 
12 #ifndef __FILE_H__
13 #define __FILE_H__
14 
15 #include <shogun/lib/config.h>
16 
17 #include <stdio.h>
18 #include <shogun/base/SGObject.h>
19 
20 namespace shogun
21 {
22 template <class ST> class SGString;
23 template <class ST> class SGSparseVector;
24 template <class ST> struct SGSparseVectorEntry;
25 
35 class CFile : public CSGObject
36 {
37 public:
39  CFile();
40 
46  CFile(FILE* f, const char* name=NULL);
47 
54  CFile(int fd, const char* mode, const char* name=NULL);
55 
62  CFile(const char* fname, char rw='r', const char* name=NULL);
63 
65  virtual ~CFile();
66 
68  void close()
69  {
70  SG_FREE(variable_name);
71  SG_FREE(filename);
72  if (file)
73  fclose(file);
74  variable_name=NULL;
75  filename=NULL;
76  file=NULL;
77  }
78 
84  {
85  return file;
86  }
87 
94  void set_variable_name(const char* name);
95 
102  char* get_variable_name();
103 
105  /*virtual DataType get_data_type()=0;*/
106 
108  /*virtual void get_vector(void*& vector, int32_t& len, DataType& dtype);*/
109 
117  virtual void get_vector(bool*& vector, int32_t& len);
118  virtual void get_vector(int8_t*& vector, int32_t& len)=0;
119  virtual void get_vector(uint8_t*& vector, int32_t& len)=0;
120  virtual void get_vector(char*& vector, int32_t& len)=0;
121  virtual void get_vector(int32_t*& vector, int32_t& len)=0;
122  virtual void get_vector(uint32_t*& vector, int32_t& len)=0;
123  virtual void get_vector(float64_t*& vector, int32_t& len)=0;
124  virtual void get_vector(float32_t*& vector, int32_t& len)=0;
125  virtual void get_vector(floatmax_t*& vector, int32_t& len)=0;
126  virtual void get_vector(int16_t*& vector, int32_t& len)=0;
127  virtual void get_vector(uint16_t*& vector, int32_t& len)=0;
128  virtual void get_vector(int64_t*& vector, int32_t& len)=0;
129  virtual void get_vector(uint64_t*& vector, int32_t& len)=0;
131 
133  /*virtual void get_matrix(
134  void*& matrix, int32_t& num_feat, int32_t& num_vec, DataType& dtype);*/
135 
144  virtual void get_matrix(
145  bool*& matrix, int32_t& num_feat, int32_t& num_vec);
146  virtual void get_matrix(
147  uint8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
148  virtual void get_matrix(
149  int8_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
150  virtual void get_matrix(
151  char*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
152  virtual void get_matrix(
153  int32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
154  virtual void get_matrix(
155  uint32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
156  virtual void get_matrix(
157  int64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
158  virtual void get_matrix(
159  uint64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
160  virtual void get_matrix(
161  float32_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
162  virtual void get_matrix(
163  float64_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
164  virtual void get_matrix(
165  floatmax_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
166  virtual void get_matrix(
167  int16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
168  virtual void get_matrix(
169  uint16_t*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
171 
173  /*virtual void get_ndarray(
174  void*& array, int32_t*& dims, int32_t& num_dims, DataType& dtype);*/
175 
184  virtual void get_ndarray(
185  uint8_t*& array, int32_t*& dims, int32_t& num_dims)=0;
186  virtual void get_ndarray(
187  char*& array, int32_t*& dims, int32_t& num_dims)=0;
188  virtual void get_ndarray(
189  int32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
190  virtual void get_ndarray(
191  float32_t*& array, int32_t*& dims, int32_t& num_dims)=0;
192  virtual void get_ndarray(
193  float64_t*& array, int32_t*& dims, int32_t& num_dims)=0;
194  virtual void get_ndarray(
195  int16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
196  virtual void get_ndarray(
197  uint16_t*& array, int32_t*& dims, int32_t& num_dims)=0;
199  //
207  virtual void get_sparse_vector(
208  SGSparseVectorEntry<bool>*& entries, int32_t& num_feat);
209  virtual void get_sparse_vector(
210  SGSparseVectorEntry<uint8_t>*& entries, int32_t& num_feat);
211  virtual void get_sparse_vector(
212  SGSparseVectorEntry<int8_t>*& entries, int32_t& num_feat);
213  virtual void get_sparse_vector(
214  SGSparseVectorEntry<char>*& entries, int32_t& num_feat);
215  virtual void get_sparse_vector(
216  SGSparseVectorEntry<int32_t>*& entries, int32_t& num_feat);
217  virtual void get_sparse_vector(
218  SGSparseVectorEntry<uint32_t>*& entries, int32_t& num_feat);
219  virtual void get_sparse_vector(
220  SGSparseVectorEntry<int64_t>*& entries, int32_t& num_feat);
221  virtual void get_sparse_vector(
222  SGSparseVectorEntry<uint64_t>*& entries, int32_t& num_feat);
223  virtual void get_sparse_vector(
224  SGSparseVectorEntry<int16_t>*& entries, int32_t& num_feat);
225  virtual void get_sparse_vector(
226  SGSparseVectorEntry<uint16_t>*& entries, int32_t& num_feat);
227  virtual void get_sparse_vector(
228  SGSparseVectorEntry<float32_t>*& entries, int32_t& num_feat);
229  virtual void get_sparse_vector(
230  SGSparseVectorEntry<float64_t>*& entries, int32_t& num_feat);
231  virtual void get_sparse_vector(
232  SGSparseVectorEntry<floatmax_t>*& entries, int32_t& num_feat);
234 
243  virtual void get_sparse_matrix(
244  SGSparseVector<bool>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
245  virtual void get_sparse_matrix(
246  SGSparseVector<uint8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
247  virtual void get_sparse_matrix(
248  SGSparseVector<int8_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
249  virtual void get_sparse_matrix(
250  SGSparseVector<char>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
251  virtual void get_sparse_matrix(
252  SGSparseVector<int32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
253  virtual void get_sparse_matrix(
254  SGSparseVector<uint32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
255  virtual void get_sparse_matrix(
256  SGSparseVector<int64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
257  virtual void get_sparse_matrix(
258  SGSparseVector<uint64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
259  virtual void get_sparse_matrix(
260  SGSparseVector<int16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
261  virtual void get_sparse_matrix(
262  SGSparseVector<uint16_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
263  virtual void get_sparse_matrix(
264  SGSparseVector<float32_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
265  virtual void get_sparse_matrix(
266  SGSparseVector<float64_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
267  virtual void get_sparse_matrix(
268  SGSparseVector<floatmax_t>*& matrix, int32_t& num_feat, int32_t& num_vec)=0;
270 
271 
280  virtual void get_string_list(
281  SGString<bool>*& strings, int32_t& num_str,
282  int32_t& max_string_len);
283  virtual void get_string_list(
284  SGString<uint8_t>*& strings, int32_t& num_str,
285  int32_t& max_string_len)=0;
286  virtual void get_string_list(
287  SGString<int8_t>*& strings, int32_t& num_str,
288  int32_t& max_string_len)=0;
289  virtual void get_string_list(
290  SGString<char>*& strings, int32_t& num_str,
291  int32_t& max_string_len)=0;
292  virtual void get_string_list(
293  SGString<int32_t>*& strings, int32_t& num_str,
294  int32_t& max_string_len)=0;
295  virtual void get_string_list(
296  SGString<uint32_t>*& strings, int32_t& num_str,
297  int32_t& max_string_len)=0;
298  virtual void get_string_list(
299  SGString<int16_t>*& strings, int32_t& num_str,
300  int32_t& max_string_len)=0;
301  virtual void get_string_list(
302  SGString<uint16_t>*& strings, int32_t& num_str,
303  int32_t& max_string_len)=0;
304  virtual void get_string_list(
305  SGString<int64_t>*& strings, int32_t& num_str,
306  int32_t& max_string_len)=0;
307  virtual void get_string_list(
308  SGString<uint64_t>*& strings, int32_t& num_str,
309  int32_t& max_string_len)=0;
310  virtual void get_string_list(
311  SGString<float32_t>*& strings, int32_t& num_str,
312  int32_t& max_string_len)=0;
313  virtual void get_string_list(
314  SGString<float64_t>*& strings, int32_t& num_str,
315  int32_t& max_string_len)=0;
316  virtual void get_string_list(
317  SGString<floatmax_t>*& strings, int32_t& num_str,
318  int32_t& max_string_len)=0;
320 
322  /*virtual void get_vector(void*& vector, int32_t& len, DataType& dtype);*/
323 
331  virtual void set_vector(const bool* vector, int32_t len);
332  virtual void set_vector(const int8_t* vector, int32_t len)=0;
333  virtual void set_vector(const uint8_t* vector, int32_t len)=0;
334  virtual void set_vector(const char* vector, int32_t len)=0;
335  virtual void set_vector(const int32_t* vector, int32_t len)=0;
336  virtual void set_vector(const uint32_t* vector, int32_t len)=0;
337  virtual void set_vector(const float32_t* vector, int32_t len)=0;
338  virtual void set_vector(const float64_t* vector, int32_t len)=0;
339  virtual void set_vector(const floatmax_t* vector, int32_t len)=0;
340  virtual void set_vector(const int16_t* vector, int32_t len)=0;
341  virtual void set_vector(const uint16_t* vector, int32_t len)=0;
342  virtual void set_vector(const int64_t* vector, int32_t len)=0;
343  virtual void set_vector(const uint64_t* vector, int32_t len)=0;
345 
346 
354  virtual void set_matrix(
355  const bool* matrix, int32_t num_feat, int32_t num_vec);
356  virtual void set_matrix(
357  const uint8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
358  virtual void set_matrix(
359  const int8_t* matrix, int32_t num_feat, int32_t num_vec)=0;
360  virtual void set_matrix(
361  const char* matrix, int32_t num_feat, int32_t num_vec)=0;
362  virtual void set_matrix(
363  const int32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
364  virtual void set_matrix(
365  const uint32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
366  virtual void set_matrix(
367  const int64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
368  virtual void set_matrix(
369  const uint64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
370  virtual void set_matrix(
371  const float32_t* matrix, int32_t num_feat, int32_t num_vec)=0;
372  virtual void set_matrix(
373  const float64_t* matrix, int32_t num_feat, int32_t num_vec)=0;
374  virtual void set_matrix(
375  const floatmax_t* matrix, int32_t num_feat, int32_t num_vec)=0;
376  virtual void set_matrix(
377  const int16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
378  virtual void set_matrix(
379  const uint16_t* matrix, int32_t num_feat, int32_t num_vec)=0;
381  //
389  virtual void set_sparse_vector(
390  const SGSparseVectorEntry<bool>* entries, int32_t num_feat);
391  virtual void set_sparse_vector(
392  const SGSparseVectorEntry<uint8_t>* entries, int32_t num_feat);
393  virtual void set_sparse_vector(
394  const SGSparseVectorEntry<int8_t>* entries, int32_t num_feat);
395  virtual void set_sparse_vector(
396  const SGSparseVectorEntry<char>* entries, int32_t num_feat);
397  virtual void set_sparse_vector(
398  const SGSparseVectorEntry<int32_t>* entries, int32_t num_feat);
399  virtual void set_sparse_vector(
400  const SGSparseVectorEntry<uint32_t>* entries, int32_t num_feat);
401  virtual void set_sparse_vector(
402  const SGSparseVectorEntry<int64_t>* entries, int32_t num_feat);
403  virtual void set_sparse_vector(
404  const SGSparseVectorEntry<uint64_t>* entries, int32_t num_feat);
405  virtual void set_sparse_vector(
406  const SGSparseVectorEntry<int16_t>* entries, int32_t num_feat);
407  virtual void set_sparse_vector(
408  const SGSparseVectorEntry<uint16_t>* entries, int32_t num_feat);
409  virtual void set_sparse_vector(
410  const SGSparseVectorEntry<float32_t>* entries, int32_t num_feat);
411  virtual void set_sparse_vector(
412  const SGSparseVectorEntry<float64_t>* entries, int32_t num_feat);
413  virtual void set_sparse_vector(
414  const SGSparseVectorEntry<floatmax_t>* entries, int32_t num_feat);
416 
424  virtual void set_sparse_matrix(
425  const SGSparseVector<bool>* matrix, int32_t num_feat, int32_t num_vec)=0;
426  virtual void set_sparse_matrix(
427  const SGSparseVector<uint8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
428  virtual void set_sparse_matrix(
429  const SGSparseVector<int8_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
430  virtual void set_sparse_matrix(
431  const SGSparseVector<char>* matrix, int32_t num_feat, int32_t num_vec)=0;
432  virtual void set_sparse_matrix(
433  const SGSparseVector<int32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
434  virtual void set_sparse_matrix(
435  const SGSparseVector<uint32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
436  virtual void set_sparse_matrix(
437  const SGSparseVector<int64_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
438  virtual void set_sparse_matrix(
439  const SGSparseVector<uint64_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
440  virtual void set_sparse_matrix(
441  const SGSparseVector<int16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
442  virtual void set_sparse_matrix(
443  const SGSparseVector<uint16_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
444  virtual void set_sparse_matrix(
445  const SGSparseVector<float32_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
446  virtual void set_sparse_matrix(
447  const SGSparseVector<float64_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
448  virtual void set_sparse_matrix(
449  const SGSparseVector<floatmax_t>* matrix, int32_t num_feat, int32_t num_vec)=0;
451 
452 
461  virtual void set_string_list(
462  const SGString<bool>* strings, int32_t num_str);
463  virtual void set_string_list(
464  const SGString<uint8_t>* strings, int32_t num_str)=0;
465  virtual void set_string_list(
466  const SGString<int8_t>* strings, int32_t num_str)=0;
467  virtual void set_string_list(
468  const SGString<char>* strings, int32_t num_str)=0;
469  virtual void set_string_list(
470  const SGString<int32_t>* strings, int32_t num_str)=0;
471  virtual void set_string_list(
472  const SGString<uint32_t>* strings, int32_t num_str)=0;
473  virtual void set_string_list(
474  const SGString<int16_t>* strings, int32_t num_str)=0;
475  virtual void set_string_list(
476  const SGString<uint16_t>* strings, int32_t num_str)=0;
477  virtual void set_string_list(
478  const SGString<int64_t>* strings, int32_t num_str)=0;
479  virtual void set_string_list(
480  const SGString<uint64_t>* strings, int32_t num_str)=0;
481  virtual void set_string_list(
482  const SGString<float32_t>* strings, int32_t num_str)=0;
483  virtual void set_string_list(
484  const SGString<float64_t>* strings, int32_t num_str)=0;
485  virtual void set_string_list(
486  const SGString<floatmax_t>* strings, int32_t num_str)=0;
488 
490  virtual const char* get_name() const { return "File"; }
491 
498  static char* read_whole_file(char* fname, size_t& len);
499 
500 protected:
502  FILE* file;
504  char task;
506  char* filename;
509 };
510 }
511 #endif // __FILE_H__

SHOGUN Machine Learning Toolbox - Documentation