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

SHOGUN Machine Learning Toolbox - Documentation