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

SHOGUN Machine Learning Toolbox - Documentation