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 "lib/io.h"
00015 #include "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             open_stream(bs.m_fname, bs.rw);
00051         }
00052 
00053 
00058         CBinaryStream(const CBinaryStream &bs)
00059         {
00060             open_stream(bs.m_fname, bs.rw);
00061             ASSERT(length==bs.length);
00062         }
00063 
00064 
00066         virtual ~CBinaryStream()
00067         {
00068             close_stream();
00069         }
00070 
00076         void open_stream(const char* fname, const char* flag="r")
00077         {
00078             rw=strdup(flag);
00079             m_fname=strdup(fname);
00080 
00081             fd = fopen(fname, flag);
00082             if (!fd)
00083                 SG_ERROR("Error opening file '%s'\n", m_fname);
00084 
00085             struct stat sb;
00086             if (stat(fname, &sb) == -1)
00087                 SG_ERROR("Error determining file size of '%s'\n", m_fname);
00088 
00089             length = sb.st_size;
00090             SG_DEBUG("Opened file '%s' of size %ld byte\n", fname, length);
00091         }
00092 
00094         void close_stream()
00095         {
00096             free(rw);
00097             free(m_fname);
00098             if (fd)
00099                 fclose(fd);
00100 
00101             rw=NULL;
00102             m_fname=NULL;
00103             fd = NULL:
00104             length = 0;
00105         }
00106 
00111         uint64_t get_length()
00112         {
00113             return length/sizeof(T);
00114         }
00115 
00120         uint64_t get_size()
00121         {
00122             return length;
00123         }
00124 
00136         char* get_line(uint64_t& len, uint64_t& offs)
00137         {
00138             return NULL;
00139         }
00140 
00145         int32_t get_num_lines()
00146         {
00147             return 0;
00148         }
00149 
00156         void pre_buffer(T* buffer, long index, long num) const
00157         {           
00158             ASSERT(index>=0);
00159             ASSERT(num>=0);
00160 
00161             if (num==0)
00162                 return;
00163 
00164             if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
00165                 SG_ERROR(stderr, "Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname);
00166 
00167             if ( fread(buffer, sizeof(T), num, fd) != num)
00168                 SG_ERROR(stderr, "Error calling fread (file '%s')\n", m_fname);
00169         }
00170 
00175         inline T read_next() const
00176         {
00177             T ptr;
00178             if ( fread(&ptr, sizeof(T), 1, fd) != 1)
00179             {
00180                 fprintf(stderr, "Error calling fread (file '%s')\n", m_fname);
00181                 exit(1);
00182             }
00183             return ptr;
00184         }
00185 
00191         inline T operator[](int32_t index) const
00192         {
00193 
00194             if (fseek(fd, ((long) sizeof(T))*((long) index), SEEK_SET) != 0)
00195                 SG_ERROR(stderr, "Error seeking to %ld (file '%s')\n", sizeof(T)*((int64_t) index), m_fname);
00196 
00197             T ptr;
00198 
00199             if ( fread(&ptr, sizeof(T), 1, fd) != 1)
00200                 SG_ERROR(stderr, "Error calling fread (file '%s')\n", m_fname);
00201 
00202             return T;
00203         }
00204 
00206         inline virtual const char* get_name() const { return "BinaryStream"; }
00207 
00208     protected:
00210         FILE* fd;
00212         uint64_t length;
00214         char* rw;
00216         char* m_fname;
00217 };
00218 }
00219 #endif // BINARY_STREAM
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation