SHOGUN  v2.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 #include <string.h>
15 
16 #include <shogun/io/File.h>
17 
20 
21 using namespace shogun;
22 
24 {
25  file=NULL;
26  filename=NULL;
27  variable_name=NULL;
28 }
29 
30 CFile::CFile(FILE* f, const char* name) : CSGObject()
31 {
32  file=f;
33  filename=NULL;
34  variable_name=NULL;
35 
36  if (name)
37  set_variable_name(name);
38 }
39 
40 CFile::CFile(const char* fname, char rw, const char* name) : CSGObject()
41 {
42  variable_name=NULL;
43  task=rw;
44  filename=strdup(fname);
45  char mode[2];
46  mode[0]=rw;
47  mode[1]='\0';
48 
49  if (rw=='r' || rw == 'w')
50  {
51  if (filename)
52  {
53  if (!(file=fopen((const char*) filename, (const char*) mode)))
54  SG_ERROR("Error opening file '%s'\n", filename);
55  }
56  }
57  else
58  SG_ERROR("unknown mode '%c'\n", mode[0]);
59 
60  if (name)
61  set_variable_name(name);
62 }
63 
64 void CFile::get_vector(bool*& vector, int32_t& len)
65 {
66  int32_t* int_vector;
67  get_vector(int_vector, len);
68 
69  ASSERT(len>0);
70  vector= SG_MALLOC(bool, len);
71 
72  for (int32_t i=0; i<len; i++)
73  vector[i]= (int_vector[i]!=0);
74 
75  SG_FREE(int_vector);
76 }
77 
78 void CFile::set_vector(const bool* vector, int32_t len)
79 {
80  int32_t* int_vector = SG_MALLOC(int32_t, len);
81  for (int32_t i=0;i<len;i++)
82  {
83  if (vector[i])
84  int_vector[i]=1;
85  else
86  int_vector[i]=0;
87  }
88  set_vector(int_vector,len);
89  SG_FREE(int_vector);
90 }
91 
92 void CFile::get_matrix(bool*& matrix, int32_t& num_feat, int32_t& num_vec)
93 {
94  uint8_t * byte_matrix;
95  get_matrix(byte_matrix,num_feat,num_vec);
96 
97  ASSERT(num_feat > 0 && num_vec > 0)
98  matrix = SG_MALLOC(bool, num_feat*num_vec);
99 
100  for(int32_t i = 0;i < num_vec;i++)
101  {
102  for(int32_t j = 0;j < num_feat;j++)
103  matrix[i*num_feat+j] = byte_matrix[i*num_feat+j] != 0 ? 1 : 0;
104  }
105 
106  SG_FREE(byte_matrix);
107 }
108 
109 void CFile::set_matrix(const bool* matrix, int32_t num_feat, int32_t num_vec)
110 {
111  uint8_t * byte_matrix = SG_MALLOC(uint8_t, num_feat*num_vec);
112  for(int32_t i = 0;i < num_vec;i++)
113  {
114  for(int32_t j = 0;j < num_feat;j++)
115  byte_matrix[i*num_feat+j] = matrix[i*num_feat+j] != 0 ? 1 : 0;
116  }
117 
118  set_matrix(byte_matrix,num_feat,num_vec);
119 
120  SG_FREE(byte_matrix);
121 }
122 
124  SGString<bool>*& strings, int32_t& num_str,
125  int32_t& max_string_len)
126 {
127  SGString<int8_t>* strs;
128  get_int8_string_list(strs, num_str, max_string_len);
129 
130  ASSERT(num_str>0 && max_string_len>0);
131  strings=SG_MALLOC(SGString<bool>, num_str);
132 
133  for(int32_t i = 0;i < num_str;i++)
134  {
135  strings[i].slen = strs[i].slen;
136  strings[i].string = SG_MALLOC(bool, strs[i].slen);
137  for(int32_t j = 0;j < strs[i].slen;j++)
138  strings[i].string[j] = strs[i].string[j] != 0 ? 1 : 0;
139  }
140 
141  for(int32_t i = 0;i < num_str;i++)
142  SG_FREE(strs[i].string);
143  SG_FREE(strs);
144 }
145 
146 void CFile::set_string_list(const SGString<bool>* strings, int32_t num_str)
147 {
148  SGString<int8_t> * strs = SG_MALLOC(SGString<int8_t>, num_str);
149 
150  for(int32_t i = 0;i < num_str;i++)
151  {
152  strs[i].slen = strings[i].slen;
153  strs[i].string = SG_MALLOC(int8_t, strings[i].slen);
154  for(int32_t j = 0;j < strings[i].slen;j++)
155  strs[i].string[j] = strings[i].string[j] != 0 ? 1 : 0;
156  }
157 
158  set_int8_string_list(strs,num_str);
159 
160  for(int32_t i = 0;i < num_str;i++)
161  SG_FREE(strs[i].string);
162  SG_FREE(strs);
163 }
164 
166 {
167  close();
168 }
169 
170 void CFile::set_variable_name(const char* name)
171 {
173  variable_name=strdup(name);
174 }
175 
177 {
178  return strdup(variable_name);
179 }
180 
181 char* CFile::read_whole_file(char* fname, size_t& len)
182 {
183  FILE* tmpf=fopen(fname, "r");
184  ASSERT(tmpf);
185  fseek(tmpf,0,SEEK_END);
186  len=ftell(tmpf);
187  ASSERT(len>0);
188  rewind(tmpf);
189  char* result = SG_MALLOC(char, len);
190  size_t total=fread(result,1,len,tmpf);
191  ASSERT(total==len);
192  fclose(tmpf);
193  return result;
194 }

SHOGUN Machine Learning Toolbox - Documentation