SHOGUN  6.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules
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 #ifndef _WIN32
23 #include <sys/mman.h>
24 #endif
25 
26 namespace shogun
27 {
32 template <class T> class CSimpleFile : public CSGObject
33 {
34  public:
37  {
38  SG_UNSTABLE("CSimpleFile::CSimpleFile()", "\n")
39 
40  file=NULL;
41  filename=get_strdup("");
42  status = false;
43 
44  set_generic<T>();
45  }
46 
53  CSimpleFile(char* fname, FILE* f)
54  : CSGObject(), line_buffer_size(1024*1024), line_buffer(NULL)
55  {
56  file=f;
57  filename=get_strdup(fname);
58  status = (file!=NULL && filename!=NULL);
59  }
60 
61  virtual ~CSimpleFile()
62  {
63  SG_FREE(filename);
65  }
66 
73  T* load(T* target, int64_t& num)
74  {
75  if (status)
76  {
77  status=false;
78 
79  if (num==0)
80  {
81  bool seek_status=true;
82  int64_t cur_pos=ftell(file);
83 
84  if (cur_pos!=-1)
85  {
86  if (!fseek(file, 0, SEEK_END))
87  {
88  if ((num=(int64_t) ftell(file)) != -1)
89  {
90  SG_INFO("file of size %ld bytes == %ld entries detected\n", num,num/sizeof(T))
91  num/=sizeof(T);
92  }
93  else
94  seek_status=false;
95  }
96  else
97  seek_status=false;
98  }
99 
100  if ((fseek(file,cur_pos, SEEK_SET)) == -1)
101  seek_status=false;
102 
103  if (!seek_status)
104  {
105  SG_ERROR("filesize autodetection failed\n")
106  num=0;
107  return NULL;
108  }
109  }
110 
111  if (num>0)
112  {
113  if (!target)
114  target=SG_MALLOC(T, num);
115 
116  if (target)
117  {
118  size_t num_read=fread((void*) target, sizeof(T), num, file);
119  status=((int64_t) num_read == num);
120 
121  if (!status)
122  SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num)
123  }
124  else
125  SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename)
126  }
127  return target;
128  }
129  else
130  {
131  num=-1;
132  return NULL;
133  }
134  }
135 
142  bool save(T* target, int64_t num)
143  {
144  if (status)
145  {
146  status=false;
147  if (num>0)
148  {
149  if (!target)
150  target=SG_MALLOC(T, num);
151 
152  if (target)
153  {
154  status=(fwrite((void*) target, sizeof(T), num, file)==
155  (size_t) num);
156  }
157  }
158  }
159  return status;
160  }
161 
167  void get_buffered_line(char* line, uint64_t len)
168  {
169 
170  /*
171  if (!line_buffer)
172  {
173  line_buffer=SG_MALLOC(char, line_buffer_size);
174  size_t num_read=fread((void*) target, sizeof(T), num, file);
175 
176  if (target)
177  {
178  size_t num_read=fread((void*) target, sizeof(T), num, file);
179  status=((int64_t) num_read == num);
180 
181  if (!status)
182  SG_ERROR("only %ld of %ld entries read. io error\n", (int64_t) num_read, num)
183  }
184  else
185  SG_ERROR("failed to allocate memory while trying to read %ld entries from file \"s\"\n", (int64_t) num, filename)
186 
187  */
188  }
189 
192  {
193  SG_FREE(line_buffer);
194  line_buffer=NULL;
195  }
196 
201  inline void set_line_buffer_size(int32_t bufsize)
202  {
203  if (bufsize<=0)
204  bufsize=1024*1024;
205 
207  line_buffer_size=bufsize;
208  }
209 
214  inline bool is_ok() { return status; }
215 
217  virtual const char* get_name() const { return "SimpleFile"; }
218 
219  protected:
221  FILE* file;
223  bool status;
225  char task;
227  char* filename;
228 
232  char* line_buffer;
233 };
234 }
235 #endif
virtual const char * get_name() const
Definition: SimpleFile.h:217
#define SG_INFO(...)
Definition: SGIO.h:117
Template class SimpleFile to read and write from files.
Definition: SimpleFile.h:32
virtual ~CSimpleFile()
Definition: SimpleFile.h:61
#define SG_ERROR(...)
Definition: SGIO.h:128
void set_line_buffer_size(int32_t bufsize)
Definition: SimpleFile.h:201
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:125
bool save(T *target, int64_t num)
Definition: SimpleFile.h:142
void get_buffered_line(char *line, uint64_t len)
Definition: SimpleFile.h:167
CSimpleFile(char *fname, FILE *f)
Definition: SimpleFile.h:53
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
int32_t line_buffer_size
Definition: SimpleFile.h:230
T * load(T *target, int64_t &num)
Definition: SimpleFile.h:73
#define SG_UNSTABLE(func,...)
Definition: SGIO.h:131

SHOGUN Machine Learning Toolbox - Documentation