SHOGUN  v3.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/lib/memory.h>
15 #include <shogun/io/SGIO.h>
16 #include <shogun/base/SGObject.h>
17 
18 #include <stdio.h>
19 #include <string.h>
20 #include <sys/mman.h>
21 
22 namespace shogun
23 {
28 template <class T> class CSimpleFile : public CSGObject
29 {
30  public:
33  {
34  SG_UNSTABLE("CSimpleFile::CSimpleFile()", "\n")
35 
36  file=NULL;
37  filename=get_strdup("");
38  status = false;
39 
40  set_generic<T>();
41  }
42 
49  CSimpleFile(char* fname, FILE* f)
50  : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
51  {
52  file=f;
53  filename=get_strdup(fname);
54  status = (file!=NULL && filename!=NULL);
55  }
56 
57  virtual ~CSimpleFile()
58  {
59  SG_FREE(filename);
61  }
62 
69  T* load(T* target, int64_t& num)
70  {
71  if (status)
72  {
73  status=false;
74 
75  if (num==0)
76  {
77  bool seek_status=true;
78  int64_t cur_pos=ftell(file);
79 
80  if (cur_pos!=-1)
81  {
82  if (!fseek(file, 0, SEEK_END))
83  {
84  if ((num=(int64_t) ftell(file)) != -1)
85  {
86  SG_INFO("file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T))
87  num/=sizeof(T);
88  }
89  else
90  seek_status=false;
91  }
92  else
93  seek_status=false;
94  }
95 
96  if ((fseek(file,cur_pos, SEEK_SET)) == -1)
97  seek_status=false;
98 
99  if (!seek_status)
100  {
101  SG_ERROR("filesize autodetection failed\n")
102  num=0;
103  return NULL;
104  }
105  }
106 
107  if (num>0)
108  {
109  if (!target)
110  target=SG_MALLOC(T, num);
111 
112  if (target)
113  {
114  size_t num_read=fread((void*) target, sizeof(T), num, file);
115  status=((int64_t) num_read == num);
116 
117  if (!status)
118  SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num)
119  }
120  else
121  SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename)
122  }
123  return target;
124  }
125  else
126  {
127  num=-1;
128  return NULL;
129  }
130  }
131 
138  bool save(T* target, int64_t num)
139  {
140  if (status)
141  {
142  status=false;
143  if (num>0)
144  {
145  if (!target)
146  target=SG_MALLOC(T, num);
147 
148  if (target)
149  {
150  status=(fwrite((void*) target, sizeof(T), num, file)==
151  (size_t) num);
152  }
153  }
154  }
155  return status;
156  }
157 
163  void get_buffered_line(char* line, uint64_t len)
164  {
165 
166  /*
167  if (!line_buffer)
168  {
169  line_buffer=SG_MALLOC(char, line_buffer_size);
170  size_t num_read=fread((void*) target, sizeof(T), num, file);
171 
172  if (target)
173  {
174  size_t num_read=fread((void*) target, sizeof(T), num, file);
175  status=((int64_t) num_read == num);
176 
177  if (!status)
178  SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num)
179  }
180  else
181  SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename)
182 
183  */
184  }
185 
188  {
189  SG_FREE(line_buffer);
190  line_buffer=NULL;
191  }
192 
197  inline void set_line_buffer_size(int32_t bufsize)
198  {
199  if (bufsize<=0)
200  bufsize=1024*1024;
201 
203  line_buffer_size=bufsize;
204  }
205 
210  inline bool is_ok() { return status; }
211 
213  virtual const char* get_name() const { return "SimpleFile"; }
214 
215  protected:
217  FILE* file;
219  bool status;
221  char task;
223  char* filename;
224 
228  char* line_buffer;
229 };
230 }
231 #endif

SHOGUN Machine Learning Toolbox - Documentation