SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
File.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) 1999-2010 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 #include <stdio.h>
13 #include <string.h>
14 
15 #include <shogun/io/File.h>
16 #include <shogun/io/SGIO.h>
17 #include <shogun/base/SGObject.h>
18 
19 #include <shogun/lib/memory.h>
21 #include <shogun/lib/SGString.h>
22 
23 using namespace shogun;
24 
26 {
27  file=NULL;
28  filename=NULL;
29  variable_name=NULL;
30  task='\0';
31 }
32 
33 CFile::CFile(FILE* f, const char* name) : CSGObject()
34 {
35  file=f;
36  filename=NULL;
37  variable_name=NULL;
38  task='\0';
39 
40  if (name)
41  set_variable_name(name);
42 }
43 
44 #ifdef HAVE_FDOPEN
45 CFile::CFile(int fd, const char* mode, const char* name) : CSGObject()
46 {
47  file=fdopen(fd, mode);
48  filename=NULL;
49  variable_name=NULL;
50  task=mode[0];
51 
52  if (name)
53  set_variable_name(name);
54 }
55 #endif
56 
57 CFile::CFile(const char* fname, char rw, const char* name) : CSGObject()
58 {
59  variable_name=NULL;
60  task=rw;
61  filename=get_strdup(fname);
62  char mode[2];
63  mode[0]=rw;
64  mode[1]='\0';
65 
66  if (rw=='r' || rw == 'w')
67  {
68  if (filename)
69  {
70  if (!(file=fopen((const char*) filename, (const char*) mode)))
71  SG_ERROR("Error opening file '%s'\n", filename)
72  }
73  }
74  else
75  SG_ERROR("unknown mode '%c'\n", mode[0])
76 
77  if (name)
78  set_variable_name(name);
79 }
80 
81 void CFile::get_vector(bool*& vector, int32_t& len)
82 {
83  int32_t* int_vector;
84  get_vector(int_vector, len);
85 
86  ASSERT(len>0)
87  vector= SG_MALLOC(bool, len);
88 
89  for (int32_t i=0; i<len; i++)
90  vector[i]= (int_vector[i]!=0);
91 
92  SG_FREE(int_vector);
93 }
94 
95 void CFile::set_vector(const bool* vector, int32_t len)
96 {
97  int32_t* int_vector = SG_MALLOC(int32_t, len);
98  for (int32_t i=0;i<len;i++)
99  {
100  if (vector[i])
101  int_vector[i]=1;
102  else
103  int_vector[i]=0;
104  }
105  set_vector(int_vector,len);
106  SG_FREE(int_vector);
107 }
108 
109 void CFile::get_matrix(bool*& matrix, int32_t& num_feat, int32_t& num_vec)
110 {
111  uint8_t * byte_matrix;
112  get_matrix(byte_matrix,num_feat,num_vec);
113 
114  ASSERT(num_feat > 0 && num_vec > 0)
115  matrix = SG_MALLOC(bool, num_feat*num_vec);
116 
117  for(int32_t i = 0;i < num_vec;i++)
118  {
119  for(int32_t j = 0;j < num_feat;j++)
120  matrix[i*num_feat+j] = byte_matrix[i*num_feat+j] != 0 ? 1 : 0;
121  }
122 
123  SG_FREE(byte_matrix);
124 }
125 
126 void CFile::set_matrix(const bool* matrix, int32_t num_feat, int32_t num_vec)
127 {
128  uint8_t * byte_matrix = SG_MALLOC(uint8_t, num_feat*num_vec);
129  for(int32_t i = 0;i < num_vec;i++)
130  {
131  for(int32_t j = 0;j < num_feat;j++)
132  byte_matrix[i*num_feat+j] = matrix[i*num_feat+j] != 0 ? 1 : 0;
133  }
134 
135  set_matrix(byte_matrix,num_feat,num_vec);
136 
137  SG_FREE(byte_matrix);
138 }
139 
141  SGString<bool>*& strings, int32_t& num_str,
142  int32_t& max_string_len)
143 {
144  SGString<int8_t>* strs;
145  get_string_list(strs, num_str, max_string_len);
146 
147  ASSERT(num_str>0 && max_string_len>0)
148  strings=SG_MALLOC(SGString<bool>, num_str);
149 
150  for(int32_t i = 0;i < num_str;i++)
151  {
152  strings[i].slen = strs[i].slen;
153  strings[i].string = SG_MALLOC(bool, strs[i].slen);
154  for(int32_t j = 0;j < strs[i].slen;j++)
155  strings[i].string[j] = strs[i].string[j] != 0 ? 1 : 0;
156  }
157 
158  for(int32_t i = 0;i < num_str;i++)
159  SG_FREE(strs[i].string);
160  SG_FREE(strs);
161 }
162 
163 void CFile::set_string_list(const SGString<bool>* strings, int32_t num_str)
164 {
165  SGString<int8_t> * strs = SG_MALLOC(SGString<int8_t>, num_str);
166 
167  for(int32_t i = 0;i < num_str;i++)
168  {
169  strs[i].slen = strings[i].slen;
170  strs[i].string = SG_MALLOC(int8_t, strings[i].slen);
171  for(int32_t j = 0;j < strings[i].slen;j++)
172  strs[i].string[j] = strings[i].string[j] != 0 ? 1 : 0;
173  }
174 
175  set_string_list(strs,num_str);
176 
177  for(int32_t i = 0;i < num_str;i++)
178  SG_FREE(strs[i].string);
179  SG_FREE(strs);
180 }
181 
183 {
184  close();
185 }
186 
187 void CFile::set_variable_name(const char* name)
188 {
189  SG_FREE(variable_name);
190  variable_name=get_strdup(name);
191 }
192 
194 {
195  return get_strdup(variable_name);
196 }
197 
198 #define SPARSE_VECTOR_GETTER(type) \
199 void CFile::set_sparse_vector( \
200  const SGSparseVectorEntry<type>* entries, int32_t num_feat) \
201 { \
202  SGSparseVector<type> v((SGSparseVectorEntry<type>*) entries, num_feat, false); \
203  set_sparse_matrix(&v, 0, 1); \
204 } \
205  \
206 void CFile::get_sparse_vector( \
207  SGSparseVectorEntry<type>*& entries, int32_t& num_feat) \
208 { \
209  SGSparseVector<type>* v; \
210  int32_t dummy; \
211  int32_t nvec; \
212  get_sparse_matrix(v, dummy, nvec); \
213  ASSERT(nvec==1) \
214  entries=v->features; \
215  num_feat=v->num_feat_entries; \
216 }
218 SPARSE_VECTOR_GETTER(int8_t)
219 SPARSE_VECTOR_GETTER(uint8_t)
221 SPARSE_VECTOR_GETTER(int32_t)
222 SPARSE_VECTOR_GETTER(uint32_t)
226 SPARSE_VECTOR_GETTER(int16_t)
227 SPARSE_VECTOR_GETTER(uint16_t)
228 SPARSE_VECTOR_GETTER(int64_t)
229 SPARSE_VECTOR_GETTER(uint64_t)
230 
231 #undef SPARSE_VECTOR_GETTER
232 
233 
234 char* CFile::read_whole_file(char* fname, size_t& len)
235 {
236  FILE* tmpf=fopen(fname, "r");
237  ASSERT(tmpf)
238  fseek(tmpf,0,SEEK_END);
239  len=ftell(tmpf);
240  ASSERT(len>0)
241  rewind(tmpf);
242  char* result = SG_MALLOC(char, len);
243  size_t total=fread(result,1,len,tmpf);
244  ASSERT(total==len)
245  fclose(tmpf);
246  return result;
247 }
virtual void set_matrix(const bool *matrix, int32_t num_feat, int32_t num_vec)
Definition: File.cpp:126
#define SPARSE_VECTOR_GETTER(type)
Definition: File.cpp:198
virtual void set_string_list(const SGString< bool > *strings, int32_t num_str)
Definition: File.cpp:163
#define SG_ERROR(...)
Definition: SGIO.h:129
FILE * file
Definition: File.h:505
void close()
Definition: File.h:69
shogun string
#define ASSERT(x)
Definition: SGIO.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
virtual ~CFile()
Definition: File.cpp:182
virtual void get_vector(bool *&vector, int32_t &len)
Definition: File.cpp:81
char * variable_name
Definition: File.h:511
virtual void get_matrix(bool *&matrix, int32_t &num_feat, int32_t &num_vec)
Definition: File.cpp:109
double float64_t
Definition: common.h:50
long double floatmax_t
Definition: common.h:51
char * filename
Definition: File.h:509
float float32_t
Definition: common.h:49
char task
Definition: File.h:507
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
char * get_variable_name()
Definition: File.cpp:193
static char * read_whole_file(char *fname, size_t &len)
Definition: File.cpp:234
index_t slen
Definition: SGString.h:79
virtual void get_string_list(SGString< bool > *&strings, int32_t &num_str, int32_t &max_string_len)
Definition: File.cpp:140
virtual void set_vector(const bool *vector, int32_t len)
Definition: File.cpp:95
void set_variable_name(const char *name)
Definition: File.cpp:187

SHOGUN Machine Learning Toolbox - Documentation