tstream.h
Go to the documentation of this file.
1 /*
2  Harmonic sinusoidal modelling and tools
3 
4  C++ code package for harmonic sinusoidal modelling and relevant signal processing.
5  Centre for Digital Music, Queen Mary, University of London.
6  This file copyright 2011 Wen Xue.
7 
8  This program is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 2 of the
11  License, or (at your option) any later version.
12 */
13 #ifndef TSTREAM_H
14 #define TSTREAM_H
15 
23 #include <stdio.h>
24 
25 enum TSeekOrigin {soFromBeginning, soFromCurrent, soFromEnd};
26 class TStream
27 {
28 public:
29  TStream(){};
30  ~TStream(){};
31  virtual int Read(void*, int){return 0;}
32  virtual int Write(void*, int){return 0;}
33  virtual int Seek(int, TSeekOrigin){return Position;}
34  int Position;
35 };
36 
37 enum FileMode {fmRead, fmWrite, fmReadWrite};
38 class TFileStream : public TStream
39 {
40 public:
41  FILE* fp;
42  TFileStream(char* filename, FileMode mode) : TStream()
43  {
44  if (mode==fmWrite) fp=fopen(filename, "wb");
45  else if (mode==fmReadWrite) fp=fopen(filename, "rb+");
46  else fp=fopen(filename, "rb");
47  }
48  ~TFileStream()
49  {
50  fclose(fp);
51  }
52  virtual int Read(void* buffer, int size)
53  {
54  int result=fread(buffer, 1, size, fp);
55  Position=ftell(fp);
56  return result;
57  }
58  virtual int Write(void* buffer, int size)
59  {
60  int result=fwrite(buffer, 1, size, fp);
61  Position=ftell(fp);
62  return result;
63  }
64  virtual int Seek(int Offset, TSeekOrigin Origin)
65  {
66  fseek(fp, Offset, Origin);
67  Position=ftell(fp);
68  return Position;
69  }
70 };
71 
72 #endif // TSTREAM_H
Definition: tstream.h:38
Definition: tstream.h:26