SHOGUN  v2.0.0
 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/io/SGIO.h>
15 #include <shogun/base/SGObject.h>
16 
17 #include <stdio.h>
18 #include <string.h>
19 #include <sys/mman.h>
20 
21 namespace shogun
22 {
27 template <class T> class CSimpleFile : public CSGObject
28 {
29  public:
32  {
33  SG_UNSTABLE("CSimpleFile::CSimpleFile()", "\n");
34 
35  file=NULL;
36  filename=strdup("");
37  status = false;
38  }
39 
46  CSimpleFile(char* fname, FILE* f)
47  : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
48  {
49  file=f;
50  filename=strdup(fname);
51  status = (file!=NULL && filename!=NULL);
52  }
53 
54  virtual ~CSimpleFile()
55  {
58  }
59 
66  T* load(T* target, int64_t& num)
67  {
68  if (status)
69  {
70  status=false;
71 
72  if (num==0)
73  {
74  bool seek_status=true;
75  int64_t cur_pos=ftell(file);
76 
77  if (cur_pos!=-1)
78  {
79  if (!fseek(file, 0, SEEK_END))
80  {
81  if ((num=(int64_t) ftell(file)) != -1)
82  {
83  SG_INFO( "file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T));
84  num/=sizeof(T);
85  }
86  else
87  seek_status=false;
88  }
89  else
90  seek_status=false;
91  }
92 
93  if ((fseek(file,cur_pos, SEEK_SET)) == -1)
94  seek_status=false;
95 
96  if (!seek_status)
97  {
98  SG_ERROR( "filesize autodetection failed\n");
99  num=0;
100  return NULL;
101  }
102  }
103 
104  if (num>0)
105  {
106  if (!target)
107  target=SG_MALLOC(T, num);
108 
109  if (target)
110  {
111  size_t num_read=fread((void*) target, sizeof(T), num, file);
112  status=((int64_t) num_read == num);
113 
114  if (!status)
115  SG_ERROR( "only %ld of %ld entries read. io error\n", (int64_t) num_read, num);
116  }
117  else
118  SG_ERROR( "failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename);
119  }
120  return target;
121  }
122  else
123  {
124  num=-1;
125  return NULL;
126  }
127  }
128 
135  bool save(T* target, int64_t num)
136  {
137  if (status)
138  {
139  status=false;
140  if (num>0)
141  {
142  if (!target)
143  target=SG_MALLOC(T, num);
144 
145  if (target)
146  {
147  status=(fwrite((void*) target, sizeof(T), num, file)==
148  (size_t) num);
149  }
150  }
151  }
152  return status;
153  }
154 
160  void get_buffered_line(char* line, uint64_t len)
161  {
162 
163  /*
164  if (!line_buffer)
165  {
166  line_buffer=SG_MALLOC(char, line_buffer_size);
167  size_t num_read=fread((void*) target, sizeof(T), num, file);
168 
169  if (target)
170  {
171  size_t num_read=fread((void*) target, sizeof(T), num, file);
172  status=((int64_t) num_read == num);
173 
174  if (!status)
175  SG_ERROR( "only %ld of %ld entries read. io error\n", (int64_t) num_read, num);
176  }
177  else
178  SG_ERROR( "failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename);
179 
180  */
181  }
182 
185  {
187  line_buffer=NULL;
188  }
189 
194  inline void set_line_buffer_size(int32_t bufsize)
195  {
196  if (bufsize<=0)
197  bufsize=1024*1024;
198 
200  line_buffer_size=bufsize;
201  }
202 
207  inline bool is_ok() { return status; }
208 
210  inline virtual const char* get_name() const { return "SimpleFile"; }
211 
212  protected:
214  FILE* file;
216  bool status;
218  char task;
220  char* filename;
221 
225  char* line_buffer;
226 };
227 }
228 #endif

SHOGUN Machine Learning Toolbox - Documentation