SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 CFile::CFile(int fd, const char* mode, const char* name) : CSGObject()
45 {
46  file=fdopen(fd, mode);
47  filename=NULL;
48  variable_name=NULL;
49  task=mode[0];
50 
51  if (name)
52  set_variable_name(name);
53 }
54 
55 CFile::CFile(const char* fname, char rw, const char* name) : CSGObject()
56 {
57  variable_name=NULL;
58  task=rw;
59  filename=get_strdup(fname);
60  char mode[2];
61  mode[0]=rw;
62  mode[1]='\0';
63 
64  if (rw=='r' || rw == 'w')
65  {
66  if (filename)
67  {
68  if (!(file=fopen((const char*) filename, (const char*) mode)))
69  SG_ERROR("Error opening file '%s'\n", filename)
70  }
71  }
72  else
73  SG_ERROR("unknown mode '%c'\n", mode[0])
74 
75  if (name)
76  set_variable_name(name);
77 }
78 
79 void CFile::get_vector(bool*& vector, int32_t& len)
80 {
81  int32_t* int_vector;
82  get_vector(int_vector, len);
83 
84  ASSERT(len>0)
85  vector= SG_MALLOC(bool, len);
86 
87  for (int32_t i=0; i<len; i++)
88  vector[i]= (int_vector[i]!=0);
89 
90  SG_FREE(int_vector);
91 }
92 
93 void CFile::set_vector(const bool* vector, int32_t len)
94 {
95  int32_t* int_vector = SG_MALLOC(int32_t, len);
96  for (int32_t i=0;i<len;i++)
97  {
98  if (vector[i])
99  int_vector[i]=1;
100  else
101  int_vector[i]=0;
102  }
103  set_vector(int_vector,len);
104  SG_FREE(int_vector);
105 }
106 
107 void CFile::get_matrix(bool*& matrix, int32_t& num_feat, int32_t& num_vec)
108 {
109  uint8_t * byte_matrix;
110  get_matrix(byte_matrix,num_feat,num_vec);
111 
112  ASSERT(num_feat > 0 && num_vec > 0)
113  matrix = SG_MALLOC(bool, num_feat*num_vec);
114 
115  for(int32_t i = 0;i < num_vec;i++)
116  {
117  for(int32_t j = 0;j < num_feat;j++)
118  matrix[i*num_feat+j] = byte_matrix[i*num_feat+j] != 0 ? 1 : 0;
119  }
120 
121  SG_FREE(byte_matrix);
122 }
123 
124 void CFile::set_matrix(const bool* matrix, int32_t num_feat, int32_t num_vec)
125 {
126  uint8_t * byte_matrix = SG_MALLOC(uint8_t, num_feat*num_vec);
127  for(int32_t i = 0;i < num_vec;i++)
128  {
129  for(int32_t j = 0;j < num_feat;j++)
130  byte_matrix[i*num_feat+j] = matrix[i*num_feat+j] != 0 ? 1 : 0;
131  }
132 
133  set_matrix(byte_matrix,num_feat,num_vec);
134 
135  SG_FREE(byte_matrix);
136 }
137 
139  SGString<bool>*& strings, int32_t& num_str,
140  int32_t& max_string_len)
141 {
142  SGString<int8_t>* strs;
143  get_string_list(strs, num_str, max_string_len);
144 
145  ASSERT(num_str>0 && max_string_len>0)
146  strings=SG_MALLOC(SGString<bool>, num_str);
147 
148  for(int32_t i = 0;i < num_str;i++)
149  {
150  strings[i].slen = strs[i].slen;
151  strings[i].string = SG_MALLOC(bool, strs[i].slen);
152  for(int32_t j = 0;j < strs[i].slen;j++)
153  strings[i].string[j] = strs[i].string[j] != 0 ? 1 : 0;
154  }
155 
156  for(int32_t i = 0;i < num_str;i++)
157  SG_FREE(strs[i].string);
158  SG_FREE(strs);
159 }
160 
161 void CFile::set_string_list(const SGString<bool>* strings, int32_t num_str)
162 {
163  SGString<int8_t> * strs = SG_MALLOC(SGString<int8_t>, num_str);
164 
165  for(int32_t i = 0;i < num_str;i++)
166  {
167  strs[i].slen = strings[i].slen;
168  strs[i].string = SG_MALLOC(int8_t, strings[i].slen);
169  for(int32_t j = 0;j < strings[i].slen;j++)
170  strs[i].string[j] = strings[i].string[j] != 0 ? 1 : 0;
171  }
172 
173  set_string_list(strs,num_str);
174 
175  for(int32_t i = 0;i < num_str;i++)
176  SG_FREE(strs[i].string);
177  SG_FREE(strs);
178 }
179 
181 {
182  close();
183 }
184 
185 void CFile::set_variable_name(const char* name)
186 {
187  SG_FREE(variable_name);
188  variable_name=strdup(name);
189 }
190 
192 {
193  return strdup(variable_name);
194 }
195 
196 #define SPARSE_VECTOR_GETTER(type) \
197 void CFile::set_sparse_vector( \
198  const SGSparseVectorEntry<type>* entries, int32_t num_feat) \
199 { \
200  SGSparseVector<type> v((SGSparseVectorEntry<type>*) entries, num_feat, false); \
201  set_sparse_matrix(&v, 0, 1); \
202 } \
203  \
204 void CFile::get_sparse_vector( \
205  SGSparseVectorEntry<type>*& entries, int32_t& num_feat) \
206 { \
207  SGSparseVector<type>* v; \
208  int32_t dummy; \
209  int32_t nvec; \
210  get_sparse_matrix(v, dummy, nvec); \
211  ASSERT(nvec==1) \
212  entries=v->features; \
213  num_feat=v->num_feat_entries; \
214 }
216 SPARSE_VECTOR_GETTER(int8_t)
217 SPARSE_VECTOR_GETTER(uint8_t)
219 SPARSE_VECTOR_GETTER(int32_t)
220 SPARSE_VECTOR_GETTER(uint32_t)
224 SPARSE_VECTOR_GETTER(int16_t)
225 SPARSE_VECTOR_GETTER(uint16_t)
226 SPARSE_VECTOR_GETTER(int64_t)
227 SPARSE_VECTOR_GETTER(uint64_t)
228 
229 #undef SPARSE_VECTOR_GETTER
230 
231 
232 char* CFile::read_whole_file(char* fname, size_t& len)
233 {
234  FILE* tmpf=fopen(fname, "r");
235  ASSERT(tmpf)
236  fseek(tmpf,0,SEEK_END);
237  len=ftell(tmpf);
238  ASSERT(len>0)
239  rewind(tmpf);
240  char* result = SG_MALLOC(char, len);
241  size_t total=fread(result,1,len,tmpf);
242  ASSERT(total==len)
243  fclose(tmpf);
244  return result;
245 }

SHOGUN Machine Learning Toolbox - Documentation