/***************************************************************************
                          CSV.h  -  description
                             -------------------
    begin                : Die Feb 8 2005
    copyright            : (C) 2005 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_CSV_H
#define _LIBMELODY_CSV_H

#include "types.h"
#include "Melody.h"

class CSVReader
{
   static const char *header[];
   enum { onset = 0, onsetics, takt, beat, ticks, pitch, durs, durtic, dur16};
public:
   CSVReader(const string& inFile, Melody *pMel):m_sInFile(inFile), m_pMel(pMel), m_dTimeUnit(1.0)
   {
      
   }
   ~CSVReader(){}

   bool     read();
   string   getErrText() const{ return m_sErrText;}
   void     dumpMelody();
   double   getTimeUnit(){ return m_dTimeUnit;}

protected:
   string   m_sInFile;
   Melody   *m_pMel;
   string   m_sErrText;
   uint     m_iOnsetIdx;
   uint     m_iDurationIdx;
   uint     m_iPitchIdx;
   iData    m_headerIdz;
   uint     m_nFirstOneIdx;
   double   m_dTimeUnit;
   char    *subchr(char *str, char from, char to);

   string   getToken(const char *str, uint which, const char delim = ';') const;

   bool     parseSignature(const char *buf, Signature& sig);
   bool     parseDataDescr(const char* sDataDescr);
   bool     parseDataLine(const char *buf, Tone& tone);
   int      parseBarposition(const char* buf, int &measure, uint& beat, uint &ticks) const;
   double   guessTempo(const string& buf1, const string& buf2, const Signature& sig) const;
   double   guessTimeUnit(const string &buf1, const string& buf2) const;
   
};
#endif
