Stream.h

Go to the documentation of this file.
00001 #ifndef __STREAM_H__
00002 #define __STREAM_H__
00003 
00004 #include <string.h>
00005 #include "libRefCount.h"
00006 #include <stdarg.h>
00007 
00008 #include <fcntl.h> // for O_RDONLY etc.
00009 #include <sys/stat.h> // for fstat()
00010 #include <unistd.h>
00011 
00012 #undef DEBUG
00013 #define DEBUG -1
00014 #include "libDebug.h"
00015 
00016 #include "Vector.h"
00017 typedef VectorOf<char> VecCh;
00018 
00019 template<class T>
00020 class StreamOf: public VectorOf<T>
00021 {
00022 protected:
00023   int m_nStart;
00024 
00025   // private constructor to use wrap a
00026   StreamOf(T* vt, int ct, bool bPrivate) :
00027     VectorOf<T> (vt, ct, bPrivate), m_nStart(0)
00028   {
00029   }
00030 
00031 public:
00032   StreamOf()__attribute__((always_inline))
00033   : VectorOf<T>()
00034   , m_nStart(0)
00035     {}
00036 
00037   StreamOf(const StreamOf<T>& st) __attribute__((always_inline))
00038   : VectorOf<T>(st)
00039   , m_nStart(st.m_nStart)
00040     {}
00041 
00042   StreamOf(const T* vt, int c) __attribute__((always_inline))
00043   : VectorOf<T>(vt,c)
00044   , m_nStart(0)
00045     {}
00046 
00047   StreamOf<T>& operator = (const StreamOf<T>& st) __attribute__((always_inline))
00048     {
00049     VectorOf<T>::operator=(st);
00050     m_nStart=st.m_nStart;
00051     return *this;
00052     }
00053 
00054   bool operator ==(const StreamOf<T>& st) const
00055     {
00056     return C()==st.C() && memcmp(V(),st,C())==0;
00057     }
00058   bool operator !=(const StreamOf<T>& st) const
00059     {
00060     return C()!=st.C() || memcmp(V(),st,C())!=0;
00061     }
00062   bool operator >(const StreamOf<T>& st) const
00063     {
00064     return C()>st.C() || memcmp(V(),st,C())>0;
00065     }
00066   bool operator <(const StreamOf<T>& st) const
00067     {
00068     return C()<st.C() || memcmp(V(),st,C())<0;
00069     }
00070   bool operator >=(const StreamOf<T>& st) const
00071     {
00072     return C()>=st.C() || memcmp(V(),st,C())>=0;
00073     }
00074   bool operator <=(const StreamOf<T>& st) const
00075     {
00076     return C()<=st.C() || memcmp(V(),st,C())<=0;
00077     }
00078 
00079   T* V()
00080     {
00081     return &VectorOf<T>::V()[m_nStart];
00082     }
00083 
00084   const T* V() const
00085     {
00086     return &VectorOf<T>::V()[m_nStart];
00087     }
00088 
00089   operator T*()
00090     {
00091     return V();
00092     }
00093 
00094   operator const T *() const
00095     {
00096     return V();
00097     }
00098 
00099   int C() const
00100     {
00101     return VectorOf<T>::C()-m_nStart;
00102     }
00103 
00104   StreamOf operator + (const StreamOf<T>& stIn) const
00105     {
00106     int c=C();
00107     int cIn=stIn.C();
00108     int cNew=c+cIn;
00109 
00110     StreamOf<T> st;
00111     st.EnsureSpace(m_nStart+cNew+1);
00112 
00113     if (c > 0)
00114       {
00115       memcpy(st.V(),V(),c);
00116       }
00117     if (cIn > 0)
00118       {
00119       memcpy(&st[c],stIn.V(),cIn);
00120       }
00121     st[cNew]=0;
00122     st.SetSizeUnsafe(m_nStart+cNew);
00123 
00124     return st;
00125     }
00126 
00127   T& Read()
00128     {
00129     ASSERT(C()>0);
00130     T& t=V()[0];
00131     m_nStart++;
00132     return t;
00133     }
00134 
00135   void Skip(int d)
00136     {
00137     ASSERT(C()>=d);
00138     m_nStart+=d;
00139     }
00140 
00141   void Skip(const VectorOf<T> &v)
00142     {
00143     ASSERT(C()>=v.C());
00144     ASSERT(memcmp(V(),v.V(),v.C())==0);
00145     m_nStart+=v.C();
00146     }
00147 
00148   void Read(T* vt, int c)
00149     {
00150     ASSERT(C()>=c);
00151     memcpy(vt,V(),c);
00152     m_nStart+=c;
00153     }
00154 
00155   VectorOf<T> Read(int c)
00156     {
00157     ASSERT(C()>=c);
00158     VectorOf<T> vect;
00159     vect.Allocate(c);
00160     memcpy(vect.V(),V(),c);
00161     m_nStart+=c;
00162     return vect;
00163     }
00164 
00165   StreamOf<T>& operator += (const StreamOf<T>& st)
00166     {
00167     Append(st.V(),st.C());
00168     return *this;
00169     }
00170   };
00171 
00172 typedef StreamOf<byte> Stream;
00173 
00174 //============================================================================
00175 //============================================================================
00176 inline void WriteStream(const Stream& st, const char* sfl)
00177 {
00178   int fd = open(sfl, O_WRONLY | O_CREAT, 0666);
00179   ASSERT(fd >= 0);
00180   write(fd, st.V(), st.C());
00181   close(fd);
00182 }
00183 
00184 //============================================================================
00185 //============================================================================
00186 inline bool ReadStream(Stream& st, const char* sfl)
00187 {
00188   int fd = open(sfl, O_RDONLY);
00189   if (fd < 0)
00190     {
00191     return false;
00192     }
00193   else
00194     {
00195     struct stat statbuf;
00196     fstat(fd, &statbuf);
00197     int c = statbuf.st_size;
00198 
00199     st.EnsureSpace(st.C() + c);
00200 
00201     read(fd, &st.V()[st.C()], c);
00202     st.SetSize(st.C() + c);
00203     close(fd);
00204     return true;
00205     }
00206 }
00207 #endif

Generated on 6 Apr 2011 for Slicer3 by  doxygen 1.6.1