/***************************************************************************
                          FileReader.h  -  description
                             -------------------
    begin                : Sam Nov 22 2003
    copyright            : (C) 2003 by klaus frieler
    email                : kgf@omniversum.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 _LIBMELODY_FILEREADER_H
#define _LIBMELODY_FILEREADER_H
#include "types.h"
#include "converter.h"
#include "Melody.h"
#include "Vector.h"

class FileIOBase{
public:
   FileIOBase(){}
   virtual ~FileIOBase(){};
   virtual  bool     read(const string& inFile, Melody** pMelody) = 0;
   virtual  bool     write(const string& outFile, const Melody* pMelody) = 0;
   virtual  void     setParameter(ConverterParameter  *pParam){m_pParam = pParam;}
   
   virtual string   getErrorText() const { return m_sErrorText; }
   virtual void     setErrorText(const string& txt){ m_sErrorText = txt;}

protected:
   ConverterParameter  *m_pParam;
   string               m_sErrorText;
};

class FileIOFactory{
public:
   FileIOFactory(){}
   ~FileIOFactory(){}
   static FileIOBase   *Create(Format format);
   static void          Destroy(FileIOBase **ppFileReader);
};

class ESACIO: public FileIOBase
{
public:
   ESACIO(){}
   ~ESACIO(){}

   bool     read(const string& inFile, Melody** pMelody );
   bool     write(const string& outFile, const Melody* pMelody);

protected:
   string   getMelodyString(const Melody* pMelody) const ;
   string   getToneString(const Tone& tone, const uint ticsLeft);
   
};

class CSVIO: public FileIOBase
{
public:
   CSVIO(){}
   ~CSVIO(){}

   bool     read(const string& inFile, Melody** pMelody);
   bool     write(const string& outFile, const Melody* pMelody);


protected:
   vector<Vector>    m_boundaryList;
   vector<string>    m_boundHeaders;

   string   getBoundaryHeader();
   string   getBoundaryValues(const uint idx);
   void     fillBoundaryList(const Melody* pMelody);
   void     addBoundaryToList(const Vector&  boundary, const string& name ="seg" )
   { 
      m_boundaryList.push_back(boundary); 
      m_boundHeaders.push_back(name); 
   }
   void     clearBoundaryList(){ m_boundaryList.clear(); m_boundHeaders.clear(); }

};

class GNUIO: public FileIOBase
{
public:
   GNUIO(){}
   ~GNUIO(){}

   bool     read(const string& inFile, Melody** pMelody);
   bool     write(const string& outFile, const Melody* pMelody);

protected:
};

class MIDIIO: public FileIOBase
{
public:
   MIDIIO():m_nTrkNum(0), m_nSigTrkNum(0){}
   ~MIDIIO(){}

   bool     read(const string& inFile, Melody** pMelody);
   bool     write(const string& outFile, const Melody* pMelody);

protected:
   uint     m_nTrkNum;
   uint     m_nSigTrkNum;

   bool     analyze(const string& inFile);

};
class NotesIO:public FileIOBase
{
public:
   NotesIO(){}
   ~NotesIO(){}

   bool     read(const string& inFile, Melody** pMelody);
   bool     write(const string& outFile, const Melody* pMelody);
protected:
};

#endif
