SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
BinaryStream.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) 2009 Soeren Sonnenburg
8  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #ifndef __BINARYSTREAM_H__
12 #define __BINARYSTREAM_H__
13 
14 #include <shogun/lib/config.h>
15 
16 #include <shogun/io/SGIO.h>
17 #include <shogun/base/SGObject.h>
18 #include <shogun/lib/memory.h>
19 
20 #include <stdio.h>
21 #include <sys/stat.h>
22 
23 namespace shogun
24 {
30 template <class T> class CBinaryStream : public CSGObject
31 {
32 public:
36  {
37  rw = NULL;
38  m_fname = NULL;
39  fd = NULL;
40  length = 0;
41 
42  set_generic<T>();
43  }
44 
52  CBinaryStream(const char * fname, const char * flag = "r")
53  : CSGObject()
54  {
55  /* open_stream(bs.m_fname, bs.rw); */
57  set_generic<T>();
58  }
59 
60 
66  {
67  open_stream(bs.m_fname, bs.rw);
68  ASSERT(length == bs.length)
69  set_generic<T>();
70  }
71 
72 
74  virtual ~CBinaryStream()
75  {
76  close_stream();
77  }
78 
84  void open_stream(const char * fname, const char * flag = "r")
85  {
86  rw = get_strdup(flag);
87  m_fname = get_strdup(fname);
88 
89  fd = fopen(fname, flag);
90  if (!fd)
91  SG_ERROR("Error opening file '%s'\n", m_fname)
92 
93  struct stat sb;
94  if (stat(fname, &sb) == -1)
95  SG_ERROR("Error determining file size of '%s'\n", m_fname)
96 
97  length = sb.st_size;
98  SG_DEBUG("Opened file '%s' of size %ld byte\n", fname, length)
99  }
100 
103  {
104  SG_FREE(rw);
105  SG_FREE(m_fname);
106  if (fd)
107  {
108  fclose(fd);
109  }
110 
111  rw = NULL;
112  m_fname = NULL;
113  fd = NULL;
114  length = 0;
115  }
116 
121  uint64_t get_length()
122  {
123  return length / sizeof(T);
124  }
125 
130  uint64_t get_size()
131  {
132  return length;
133  }
134 
146  char * get_line(uint64_t &len, uint64_t &offs)
147  {
148  return NULL;
149  }
150 
155  int32_t get_num_lines()
156  {
157  return 0;
158  }
159 
166  void pre_buffer(T * buffer, long index, long num) const
167  {
168  ASSERT(index >= 0)
169  ASSERT(num >= 0)
170 
171  if (num == 0)
172  {
173  return;
174  }
175 
176  if (fseek(fd, ((long) sizeof(T)) * ((long) index), SEEK_SET) != 0)
177  SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T) * ((int64_t) index), m_fname)
178 
179  if (fread(buffer, sizeof(T), num, fd) != num)
180  SG_ERROR("Error calling fread (file '%s')\n", m_fname)
181  }
182 
187  inline T read_next() const
188  {
189  T ptr;
190  if (fread(&ptr, sizeof(T), 1, fd) != 1)
191  {
192  fprintf(stderr, "Error calling fread (file '%s')\n", m_fname);
193  exit(1);
194  }
195  return ptr;
196  }
197 
203  inline T operator[](int32_t index) const
204  {
205 
206  if (fseek(fd, ((long) sizeof(T)) * ((long) index), SEEK_SET) != 0)
207  SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T) * ((int64_t) index), m_fname)
208 
209  T ptr;
210 
211  if (fread(&ptr, sizeof(T), 1, fd) != 1)
212  SG_ERROR("Error calling fread (file '%s')\n", m_fname)
213 
214  return ptr;
215  }
216 
218  virtual const char * get_name() const
219  {
220  return "BinaryStream";
221  }
222 
223 protected:
225  FILE * fd;
227  uint64_t length;
229  char * rw;
231  char * m_fname;
232 };
233 }
234 #endif // BINARY_STREAM

SHOGUN Machine Learning Toolbox - Documentation