SDIFFile.hxx

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
00003  *                         UNIVERSITAT POMPEU FABRA
00004  *
00005  *
00006  * This program is free software; you can redistribute it and/or modify
00007  * it under the terms of the GNU General Public License as published by
00008  * the Free Software Foundation; either version 2 of the License, or
00009  * (at your option) any later version.
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  *
00020  */
00021 
00022 #ifndef __SDIFFile__
00023 #define __SDIFFile__
00024 
00025 #include "SDIFType.hxx"
00026 #include "SDIFHeader.hxx"
00027 #include "SDIFFrame.hxx"
00028 #include "SDIFMatrix.hxx"
00029 #include "SDIFStorage.hxx"
00030 #include "ByteOrder.hxx"
00031 
00032 #ifndef WIN32
00033         #ifdef macintosh
00034                 #include <types.h>
00035                 #include <unistd.h>
00036         #else
00037                 #include <sys/types.h>
00038                 #include <unistd.h>
00039         #endif
00040 #else
00041   #include <stdio.h>
00042   #include <io.h>
00043 #endif
00044 
00058 namespace SDIF
00059 {
00060 
00061 class File
00062 {
00063 public:
00064         enum Mode
00065         {
00066                 eInput = 1, eOutput = 2, eFullDuplex = 3
00067         };
00068 
00069         File(const char* filename, Mode mode);
00070         ~File();
00071 
00072         void Open(void);                                                                                //open data file
00073         void Close(void);                                                                               //close data file
00074 
00075         bool Done(void);
00076 
00077         CLAM::TIndex Pos(void);
00078         CLAM::TIndex Pos(CLAM::TIndex pos);
00079 
00080 private:
00081         bool mSkipData;
00082         bool mFirstAccess;
00083         char* mpName;
00084         Mode mMode;
00085         int mFile;
00086         CLAM::TSize mSize;
00087 
00088 
00089 /*
00090   template <class T> void ReadArray(Array<T>& data)                                     //read from data file
00091   {
00092         if (mFile==-1)                                                                                                  // check if file is opened
00093                 throw Err("DataFileIO not opened");
00094 
00095         // added (char*) typecast to compile in MCW
00096         int size = read(mFile,(char*)data.GetPtr(),data.AllocatedSizeInBytes());
00097         data.SetSize(size/sizeof(T));
00098   }
00099 
00100   template <class T> void WriteArray(const Array<T>& data)                        //write to data file
00101   {
00102         if (mFile==-1)                                                                                                  // check if file is open
00103                 throw Err("DataFileIO not opened");
00104 
00105         // added (char*) typecast to compile in MCW
00106         write(mFile,(char*)data.GetPtr(),data.SizeInBytes());
00107   }
00108 */
00109 
00110         void Read(CLAM::TByte* ptr,int n);
00111 
00112         template <class T> void TRead(T& t)
00113         {
00114                 Read((CLAM::TByte*) &t,sizeof(T));
00115                 FixByteOrder((CLAM::TByte*) &t,1,sizeof(T));
00116         }
00117 
00118         void Write(const CLAM::TByte* ptr,int n);
00119 
00120         template <class T> void TWrite(const T& t)
00121         {
00122                 T tmp(t);
00123                 FixByteOrder((CLAM::TByte*) &tmp,1,sizeof(T));
00124                 Write((CLAM::TByte*) &tmp,sizeof(T));
00125         }
00126 
00127         inline void FixByteOrder(CLAM::TByte* ptr,
00128                 CLAM::TUInt32 nElems,CLAM::TUInt32 elemSize);
00129 
00130         void Read(DataFrameHeader& header);
00131         void Write(const DataFrameHeader& header);
00132 
00133         void Read(FrameHeader& header);
00134         void Write(const FrameHeader& header);
00135 
00136         void Read(MatrixHeader& header);
00137         void Write(const MatrixHeader& header);
00138 
00139         void Read(OpeningsFrame& frame);
00140         void Write(const OpeningsFrame& frame);
00141 
00142         void Read(Matrix& matrix);
00143         void Write(const Matrix& matrix);
00144 
00145         void Read(TypeId& header);
00146         void Write(const TypeId& header);
00147 
00148         void SkipMatrixData(const Matrix& matrix);
00149         void ReadMatrixData(Matrix& matrix);
00150         void WriteMatrixData(const Matrix& matrix);
00151 
00152 public:
00153         void Read(Storage& storage);
00154         void Write(const Storage& storage);
00155 
00156         void Read(Frame& frame);
00157         void Write(const Frame& frame);
00158 
00159 private:
00160         void _FixByteOrder(
00161                 CLAM::TByte* ptr,CLAM::TUInt32 nElems,CLAM::TUInt32 elemSize);
00162 };
00163 
00164 inline int File::Pos(void)
00165 {
00166         int pos = lseek(mFile,0,SEEK_CUR);
00167         return pos;
00168 }
00169 
00170 inline int File::Pos(int pos)
00171 {
00172         return lseek(mFile,pos,SEEK_SET);
00173 }
00174 
00175 inline void File::Read(CLAM::TByte* ptr,int n)
00176 {
00177         if (read(mFile,(char*)ptr,n)!=n) {
00178                 throw CLAM::Err("DataFileIO read error");
00179         }
00180 }
00181 
00182 inline void File::Write(const CLAM::TByte* ptr,int n)
00183 {
00184         if (write(mFile,(const char*)ptr,n)!=n) {
00185                 throw CLAM::Err("DataFileIO read error");
00186         }
00187 }
00188 
00189 inline bool File::Done(void)
00190 {
00191         return Pos()>=mSize;
00192 }
00193 
00194 inline void File::FixByteOrder(CLAM::TByte* ptr,
00195         CLAM::TUInt32 nElems,CLAM::TUInt32 elemSize)
00196 {
00197 #ifdef CLAM_LITTLE_ENDIAN
00198         _FixByteOrder(ptr,nElems,elemSize);
00199 #else
00200 #ifndef CLAM_BIG_ENDIAN
00201 #pragma message ("BYTE ORDER NOT DEFINED!")
00202 #endif
00203 #endif
00204 }
00205 
00206 }
00207 
00208 #endif
00209 
Generated by  doxygen 1.6.3