SHOGUN  v2.0.0
 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/io/SGIO.h>
15 #include <shogun/base/SGObject.h>
16 
17 #include <stdio.h>
18 #include <string.h>
19 
20 namespace shogun
21 {
27 template <class T> class CBinaryStream : public CSGObject
28 {
29  public:
33  {
34  rw=NULL;
35  m_fname=NULL;
36  fd = NULL;
37  length = 0;
38  }
39 
47  CBinaryStream(const char* fname, const char* flag="r")
48  : CSGObject()
49  {
50  /* open_stream(bs.m_fname, bs.rw); */
52  }
53 
54 
60  {
61  open_stream(bs.m_fname, bs.rw);
62  ASSERT(length==bs.length);
63  }
64 
65 
67  virtual ~CBinaryStream()
68  {
69  close_stream();
70  }
71 
77  void open_stream(const char* fname, const char* flag="r")
78  {
79  rw=strdup(flag);
80  m_fname=strdup(fname);
81 
82  fd = fopen(fname, flag);
83  if (!fd)
84  SG_ERROR("Error opening file '%s'\n", m_fname);
85 
86  struct stat sb;
87  if (stat(fname, &sb) == -1)
88  SG_ERROR("Error determining file size of '%s'\n", m_fname);
89 
90  length = sb.st_size;
91  SG_DEBUG("Opened file '%s' of size %ld byte\n", fname, length);
92  }
93 
95  void close_stream()
96  {
97  SG_FREE(rw);
99  if (fd)
100  fclose(fd);
101 
102  rw=NULL;
103  m_fname=NULL;
104  fd = NULL;
105  length = 0;
106  }
107 
112  uint64_t get_length()
113  {
114  return length/sizeof(T);
115  }
116 
121  uint64_t get_size()
122  {
123  return length;
124  }
125 
137  char* get_line(uint64_t& len, uint64_t& offs)
138  {
139  return NULL;
140  }
141 
146  int32_t get_num_lines()
147  {
148  return 0;
149  }
150 
157  void pre_buffer(T* buffer, long index, long num) const
158  {
159  ASSERT(index>=0);
160  ASSERT(num>=0);
161 
162  if (num==0)
163  return;
164 
165  if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
166  SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname);
167 
168  if ( fread(buffer, sizeof(T), num, fd) != num)
169  SG_ERROR("Error calling fread (file '%s')\n", m_fname);
170  }
171 
176  inline T read_next() const
177  {
178  T ptr;
179  if ( fread(&ptr, sizeof(T), 1, fd) != 1)
180  {
181  fprintf(stderr, "Error calling fread (file '%s')\n", m_fname);
182  exit(1);
183  }
184  return ptr;
185  }
186 
192  inline T operator[](int32_t index) const
193  {
194 
195  if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
196  SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname);
197 
198  T ptr;
199 
200  if ( fread(&ptr, sizeof(T), 1, fd) != 1)
201  SG_ERROR("Error calling fread (file '%s')\n", m_fname);
202 
203  return ptr;
204  }
205 
207  inline virtual const char* get_name() const { return "BinaryStream"; }
208 
209  protected:
211  FILE* fd;
213  uint64_t length;
215  char* rw;
217  char* m_fname;
218 };
219 }
220 #endif // BINARY_STREAM

SHOGUN Machine Learning Toolbox - Documentation