Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #ifndef __BINARYSTREAM_H__
00012 #define __BINARYSTREAM_H__
00013
00014 #include <shogun/io/SGIO.h>
00015 #include <shogun/base/SGObject.h>
00016
00017 #include <stdio.h>
00018 #include <string.h>
00019
00020 namespace shogun
00021 {
00027 template <class T> class CBinaryStream : public CSGObject
00028 {
00029 public:
00032 CBinaryStream() : CSGObject()
00033 {
00034 rw=NULL;
00035 m_fname=NULL;
00036 fd = NULL;
00037 length = 0;
00038 }
00039
00047 CBinaryStream(const char* fname, const char* flag="r")
00048 : CSGObject()
00049 {
00050
00051 SG_NOTIMPLEMENTED;
00052 }
00053
00054
00059 CBinaryStream(const CBinaryStream &bs)
00060 {
00061 open_stream(bs.m_fname, bs.rw);
00062 ASSERT(length==bs.length);
00063 }
00064
00065
00067 virtual ~CBinaryStream()
00068 {
00069 close_stream();
00070 }
00071
00077 void open_stream(const char* fname, const char* flag="r")
00078 {
00079 rw=strdup(flag);
00080 m_fname=strdup(fname);
00081
00082 fd = fopen(fname, flag);
00083 if (!fd)
00084 SG_ERROR("Error opening file '%s'\n", m_fname);
00085
00086 struct stat sb;
00087 if (stat(fname, &sb) == -1)
00088 SG_ERROR("Error determining file size of '%s'\n", m_fname);
00089
00090 length = sb.st_size;
00091 SG_DEBUG("Opened file '%s' of size %ld byte\n", fname, length);
00092 }
00093
00095 void close_stream()
00096 {
00097 SG_FREE(rw);
00098 SG_FREE(m_fname);
00099 if (fd)
00100 fclose(fd);
00101
00102 rw=NULL;
00103 m_fname=NULL;
00104 fd = NULL;
00105 length = 0;
00106 }
00107
00112 uint64_t get_length()
00113 {
00114 return length/sizeof(T);
00115 }
00116
00121 uint64_t get_size()
00122 {
00123 return length;
00124 }
00125
00137 char* get_line(uint64_t& len, uint64_t& offs)
00138 {
00139 return NULL;
00140 }
00141
00146 int32_t get_num_lines()
00147 {
00148 return 0;
00149 }
00150
00157 void pre_buffer(T* buffer, long index, long num) const
00158 {
00159 ASSERT(index>=0);
00160 ASSERT(num>=0);
00161
00162 if (num==0)
00163 return;
00164
00165 if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
00166 SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname);
00167
00168 if ( fread(buffer, sizeof(T), num, fd) != num)
00169 SG_ERROR("Error calling fread (file '%s')\n", m_fname);
00170 }
00171
00176 inline T read_next() const
00177 {
00178 T ptr;
00179 if ( fread(&ptr, sizeof(T), 1, fd) != 1)
00180 {
00181 fprintf(stderr, "Error calling fread (file '%s')\n", m_fname);
00182 exit(1);
00183 }
00184 return ptr;
00185 }
00186
00192 inline T operator[](int32_t index) const
00193 {
00194
00195 if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
00196 SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname);
00197
00198 T ptr;
00199
00200 if ( fread(&ptr, sizeof(T), 1, fd) != 1)
00201 SG_ERROR("Error calling fread (file '%s')\n", m_fname);
00202
00203 return ptr;
00204 }
00205
00207 inline virtual const char* get_name() const { return "BinaryStream"; }
00208
00209 protected:
00211 FILE* fd;
00213 uint64_t length;
00215 char* rw;
00217 char* m_fname;
00218 };
00219 }
00220 #endif // BINARY_STREAM