CLAM-Development  1.4.0
SDIFFile.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 MUSIC TECHNOLOGY GROUP (MTG)
3  * UNIVERSITAT POMPEU FABRA
4  *
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  */
21 
22 #ifndef __SDIFFile__
23 #define __SDIFFile__
24 
25 #include "SDIFType.hxx"
26 #include "SDIFHeader.hxx"
27 #include "SDIFFrame.hxx"
28 #include "SDIFMatrix.hxx"
29 #include "SDIFStorage.hxx"
30 #include "ByteOrder.hxx"
31 
32 #ifndef WIN32
33  #ifdef macintosh
34  #include <types.h>
35  #include <unistd.h>
36  #else
37  #include <sys/types.h>
38  #include <unistd.h>
39  #endif
40 #else
41  #include <stdio.h>
42  #include <io.h>
43 #endif
44 
58 namespace SDIF
59 {
60 
61 class File
62 {
63 public:
64  enum Mode
65  {
66  eInput = 1, eOutput = 2, eFullDuplex = 3
67  };
68 
69  File(const char* filename, Mode mode);
70  ~File();
71 
72  void Open(void); //open data file
73  void Close(void); //close data file
74 
75  bool Done(void);
76 
77  CLAM::TIndex Pos(void);
79 
80 private:
81  bool mSkipData;
82  bool mFirstAccess;
83  char* mpName;
84  Mode mMode;
85  int mFile;
86  CLAM::TSize mSize;
87 
88 
89 /*
90  template <class T> void ReadArray(Array<T>& data) //read from data file
91  {
92  if (mFile==-1) // check if file is opened
93  throw Err("DataFileIO not opened");
94 
95  // added (char*) typecast to compile in MCW
96  int size = read(mFile,(char*)data.GetPtr(),data.AllocatedSizeInBytes());
97  data.SetSize(size/sizeof(T));
98  }
99 
100  template <class T> void WriteArray(const Array<T>& data) //write to data file
101  {
102  if (mFile==-1) // check if file is open
103  throw Err("DataFileIO not opened");
104 
105  // added (char*) typecast to compile in MCW
106  write(mFile,(char*)data.GetPtr(),data.SizeInBytes());
107  }
108 */
109 
110  void Read(CLAM::TByte* ptr,int n);
111 
112  template <class T> void TRead(T& t)
113  {
114  Read((CLAM::TByte*) &t,sizeof(T));
115  FixByteOrder((CLAM::TByte*) &t,1,sizeof(T));
116  }
117 
118  void Write(const CLAM::TByte* ptr,int n);
119 
120  template <class T> void TWrite(const T& t)
121  {
122  T tmp(t);
123  FixByteOrder((CLAM::TByte*) &tmp,1,sizeof(T));
124  Write((CLAM::TByte*) &tmp,sizeof(T));
125  }
126 
127  inline void FixByteOrder(CLAM::TByte* ptr,
128  CLAM::TUInt32 nElems,CLAM::TUInt32 elemSize);
129 
130  void Read(DataFrameHeader& header);
131  void Write(const DataFrameHeader& header);
132 
133  void Read(FrameHeader& header);
134  void Write(const FrameHeader& header);
135 
136  void Read(MatrixHeader& header);
137  void Write(const MatrixHeader& header);
138 
139  void Read(OpeningsFrame& frame);
140  void Write(const OpeningsFrame& frame);
141 
142  void Read(Matrix& matrix);
143  void Write(const Matrix& matrix);
144 
145  void Read(TypeId& header);
146  void Write(const TypeId& header);
147 
148  void SkipMatrixData(const Matrix& matrix);
149  void ReadMatrixData(Matrix& matrix);
150  void WriteMatrixData(const Matrix& matrix);
151 
152 public:
153  void Read(Storage& storage);
154  void Write(const Storage& storage);
155 
156  void Read(Frame& frame);
157  void Write(const Frame& frame);
158 
159 private:
160  void _FixByteOrder(
161  CLAM::TByte* ptr,CLAM::TUInt32 nElems,CLAM::TUInt32 elemSize);
162 };
163 
164 inline int File::Pos(void)
165 {
166  int pos = lseek(mFile,0,SEEK_CUR);
167  return pos;
168 }
169 
170 inline int File::Pos(int pos)
171 {
172  return lseek(mFile,pos,SEEK_SET);
173 }
174 
175 inline void File::Read(CLAM::TByte* ptr,int n)
176 {
177  if (read(mFile,(char*)ptr,n)!=n) {
178  throw CLAM::Err("DataFileIO read error");
179  }
180 }
181 
182 inline void File::Write(const CLAM::TByte* ptr,int n)
183 {
184  if (write(mFile,(const char*)ptr,n)!=n) {
185  throw CLAM::Err("DataFileIO read error");
186  }
187 }
188 
189 inline bool File::Done(void)
190 {
191  return Pos()>=mSize;
192 }
193 
194 inline void File::FixByteOrder(CLAM::TByte* ptr,
195  CLAM::TUInt32 nElems,CLAM::TUInt32 elemSize)
196 {
197 #ifdef CLAM_LITTLE_ENDIAN
198  _FixByteOrder(ptr,nElems,elemSize);
199 #else
200 #ifndef CLAM_BIG_ENDIAN
201 #pragma message ("BYTE ORDER NOT DEFINED!")
202 #endif
203 #endif
204 }
205 
206 }
207 
208 #endif
209