view tstream.h @ 13:de3961f74f30 tip

Add Linux/gcc Makefile; build fix
author Chris Cannam
date Mon, 05 Sep 2011 15:22:35 +0100
parents 977f541d6683
children
line wrap: on
line source
/*
    Harmonic sinusoidal modelling and tools

    C++ code package for harmonic sinusoidal modelling and relevant signal processing.
    Centre for Digital Music, Queen Mary, University of London.
    This file copyright 2011 Wen Xue.

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation; either version 2 of the
    License, or (at your option) any later version.
*/
#ifndef TSTREAM_H
#define TSTREAM_H

/**
  \file tstream.h - a stream I/O interface without implementation.

  This file is included to allow compiling relevent functions that uses Borland VCL's TStream class for
  abstract I/O purposes.
*/

#include <stdio.h>

enum TSeekOrigin {soFromBeginning, soFromCurrent, soFromEnd};
class TStream
{
public:
  TStream(){};
  ~TStream(){};
  virtual int Read(void*, int){return 0;}
  virtual int Write(void*, int){return 0;}
  virtual int Seek(int, TSeekOrigin){return Position;}
  int Position;
};

enum FileMode {fmRead, fmWrite, fmReadWrite};
class TFileStream : public TStream
{
public:
  FILE* fp;
  TFileStream(char* filename, FileMode mode) : TStream()
  {
    if (mode==fmWrite) fp=fopen(filename, "wb");
    else if (mode==fmReadWrite) fp=fopen(filename, "rb+");
    else fp=fopen(filename, "rb");
  }
  ~TFileStream()
  {
    fclose(fp);
  }
  virtual int Read(void* buffer, int size)
  {
    int result=fread(buffer, 1, size, fp);
    Position=ftell(fp);
    return result;
  }
  virtual int Write(void* buffer, int size)
  {
    int result=fwrite(buffer, 1, size, fp);
    Position=ftell(fp);
    return result;
  }
  virtual int Seek(int Offset, TSeekOrigin Origin)
  {
    fseek(fp, Offset, Origin);
    Position=ftell(fp);
    return Position;
  }
};

#endif // TSTREAM_H