SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
BinaryStream.h
Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2009 Soeren Sonnenburg
00008  * Copyright (C) 2009 Fraunhofer Institute FIRST and Max-Planck-Society
00009  */
00010 
00011 #ifndef __BINARYSTREAM_H__
00012 #define __BINARYSTREAM_H__
00013 
00014 #include <shogun/io/SGIO.h>
00015 #include <shogun/base/SGObject.h>
00016 #include <shogun/lib/memory.h>
00017 
00018 #include <stdio.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             set_generic<T>();
00040         }
00041 
00049         CBinaryStream(const char* fname, const char* flag="r")
00050         : CSGObject()
00051         {
00052             /* open_stream(bs.m_fname, bs.rw); */
00053             SG_NOTIMPLEMENTED
00054             set_generic<T>();
00055         }
00056 
00057 
00062         CBinaryStream(const CBinaryStream &bs)
00063         {
00064             open_stream(bs.m_fname, bs.rw);
00065             ASSERT(length==bs.length)
00066             set_generic<T>();
00067         }
00068 
00069 
00071         virtual ~CBinaryStream()
00072         {
00073             close_stream();
00074         }
00075 
00081         void open_stream(const char* fname, const char* flag="r")
00082         {
00083             rw=get_strdup(flag);
00084             m_fname=get_strdup(fname);
00085 
00086             fd = fopen(fname, flag);
00087             if (!fd)
00088                 SG_ERROR("Error opening file '%s'\n", m_fname)
00089 
00090             struct stat sb;
00091             if (stat(fname, &sb) == -1)
00092                 SG_ERROR("Error determining file size of '%s'\n", m_fname)
00093 
00094             length = sb.st_size;
00095             SG_DEBUG("Opened file '%s' of size %ld byte\n", fname, length)
00096         }
00097 
00099         void close_stream()
00100         {
00101             SG_FREE(rw);
00102             SG_FREE(m_fname);
00103             if (fd)
00104                 fclose(fd);
00105 
00106             rw=NULL;
00107             m_fname=NULL;
00108             fd = NULL;
00109             length = 0;
00110         }
00111 
00116         uint64_t get_length()
00117         {
00118             return length/sizeof(T);
00119         }
00120 
00125         uint64_t get_size()
00126         {
00127             return length;
00128         }
00129 
00141         char* get_line(uint64_t& len, uint64_t& offs)
00142         {
00143             return NULL;
00144         }
00145 
00150         int32_t get_num_lines()
00151         {
00152             return 0;
00153         }
00154 
00161         void pre_buffer(T* buffer, long index, long num) const
00162         {
00163             ASSERT(index>=0)
00164             ASSERT(num>=0)
00165 
00166             if (num==0)
00167                 return;
00168 
00169             if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
00170                 SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname)
00171 
00172             if ( fread(buffer, sizeof(T), num, fd) != num)
00173                 SG_ERROR("Error calling fread (file '%s')\n", m_fname)
00174         }
00175 
00180         inline T read_next() const
00181         {
00182             T ptr;
00183             if ( fread(&ptr, sizeof(T), 1, fd) != 1)
00184             {
00185                 fprintf(stderr, "Error calling fread (file '%s')\n", m_fname);
00186                 exit(1);
00187             }
00188             return ptr;
00189         }
00190 
00196         inline T operator[](int32_t index) const
00197         {
00198 
00199             if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
00200                 SG_ERROR("Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname)
00201 
00202             T ptr;
00203 
00204             if ( fread(&ptr, sizeof(T), 1, fd) != 1)
00205                 SG_ERROR("Error calling fread (file '%s')\n", m_fname)
00206 
00207             return ptr;
00208         }
00209 
00211         virtual const char* get_name() const { return "BinaryStream"; }
00212 
00213     protected:
00215         FILE* fd;
00217         uint64_t length;
00219         char* rw;
00221         char* m_fname;
00222 };
00223 }
00224 #endif // BINARY_STREAM
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation