SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
SimpleFile.h
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-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef __SIMPLEFILE_H__
12 #define __SIMPLEFILE_H__
13 
14 #include <shogun/lib/config.h>
15 
16 #include <shogun/lib/memory.h>
17 #include <shogun/io/SGIO.h>
18 #include <shogun/base/SGObject.h>
19 
20 #include <stdio.h>
21 #include <string.h>
22 #include <sys/mman.h>
23 
24 namespace shogun
25 {
30 template <class T> class CSimpleFile : public CSGObject
31 {
32  public:
35  {
36  SG_UNSTABLE("CSimpleFile::CSimpleFile()", "\n")
37 
38  file=NULL;
39  filename=get_strdup("");
40  status = false;
41 
42  set_generic<T>();
43  }
44 
51  CSimpleFile(char* fname, FILE* f)
52  : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
53  {
54  file=f;
55  filename=get_strdup(fname);
56  status = (file!=NULL && filename!=NULL);
57  }
58 
59  virtual ~CSimpleFile()
60  {
61  SG_FREE(filename);
63  }
64 
71  T* load(T* target, int64_t& num)
72  {
73  if (status)
74  {
75  status=false;
76 
77  if (num==0)
78  {
79  bool seek_status=true;
80  int64_t cur_pos=ftell(file);
81 
82  if (cur_pos!=-1)
83  {
84  if (!fseek(file, 0, SEEK_END))
85  {
86  if ((num=(int64_t) ftell(file)) != -1)
87  {
88  SG_INFO("file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T))
89  num/=sizeof(T);
90  }
91  else
92  seek_status=false;
93  }
94  else
95  seek_status=false;
96  }
97 
98  if ((fseek(file,cur_pos, SEEK_SET)) == -1)
99  seek_status=false;
100 
101  if (!seek_status)
102  {
103  SG_ERROR("filesize autodetection failed\n")
104  num=0;
105  return NULL;
106  }
107  }
108 
109  if (num>0)
110  {
111  if (!target)
112  target=SG_MALLOC(T, num);
113 
114  if (target)
115  {
116  size_t num_read=fread((void*) target, sizeof(T), num, file);
117  status=((int64_t) num_read == num);
118 
119  if (!status)
120  SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num)
121  }
122  else
123  SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename)
124  }
125  return target;
126  }
127  else
128  {
129  num=-1;
130  return NULL;
131  }
132  }
133 
140  bool save(T* target, int64_t num)
141  {
142  if (status)
143  {
144  status=false;
145  if (num>0)
146  {
147  if (!target)
148  target=SG_MALLOC(T, num);
149 
150  if (target)
151  {
152  status=(fwrite((void*) target, sizeof(T), num, file)==
153  (size_t) num);
154  }
155  }
156  }
157  return status;
158  }
159 
165  void get_buffered_line(char* line, uint64_t len)
166  {
167 
168  /*
169  if (!line_buffer)
170  {
171  line_buffer=SG_MALLOC(char, line_buffer_size);
172  size_t num_read=fread((void*) target, sizeof(T), num, file);
173 
174  if (target)
175  {
176  size_t num_read=fread((void*) target, sizeof(T), num, file);
177  status=((int64_t) num_read == num);
178 
179  if (!status)
180  SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num)
181  }
182  else
183  SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename)
184 
185  */
186  }
187 
190  {
191  SG_FREE(line_buffer);
192  line_buffer=NULL;
193  }
194 
199  inline void set_line_buffer_size(int32_t bufsize)
200  {
201  if (bufsize<=0)
202  bufsize=1024*1024;
203 
205  line_buffer_size=bufsize;
206  }
207 
212  inline bool is_ok() { return status; }
213 
215  virtual const char* get_name() const { return "SimpleFile"; }
216 
217  protected:
219  FILE* file;
221  bool status;
223  char task;
225  char* filename;
226 
230  char* line_buffer;
231 };
232 }
233 #endif

SHOGUN Machine Learning Toolbox - Documentation