/***************************************************************************
                          ESACIO.cpp  -  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.                                   *
 *                                                                         *
 ***************************************************************************/
#include "FileReader.h"
#include "ESAC.h"
class ConverterParameter;

bool ESACIO::read(const string& inFile, Melody** ppMelody)
{
   if(!ppMelody || !m_pParam) {
      setErrorText("Internal error: Invalid pointer");
      return false;
   }
   EsACDecoder esacDecoder;
   bool bRet = false;
   char buf[1024] ="";
   string data("");
   FILE *fp = 0;
   fp = fopen(inFile.c_str(), "r");
   if(!fp){
      sprintf(buf, "Could not open %s", inFile.c_str());
      setErrorText(string(buf));
      return bRet;
   }
   while(!feof(fp)){
      fgets(buf, sizeof(buf), fp);
      if(ferror(fp)){
         setErrorText("Error while reading.");
         goto out;
      }
      data += string(buf);
   }
   esacDecoder.setData(data);
   if(!esacDecoder.parse()){
      setErrorText(esacDecoder.getErrText());
      goto out;
   }
   esacDecoder.setCut(m_pParam->getCut());
   esacDecoder.setPhraseIndex(m_pParam->getPhraseIndex());
   esacDecoder.setTempo(m_pParam->getTempo());
   if(!esacDecoder.parseMelody()){
      setErrorText(esacDecoder.getErrText());      
   }
   else{
      bRet = true;
      *ppMelody = esacDecoder.getMelody();
   }
out:
   fclose(fp);
   return bRet;

}

string removeExt(const string& str)
{
   uint pos = str.find_last_of('.');
   if(pos == string::npos){
      return str;
   }
   return str.substr(0, pos);
}

bool ESACIO::write(const string& outFile, const Melody* pMelody)
{
   bool bRet = true;

   EsACEncoder encoder(*pMelody, removeExt(outFile));
   string mel = encoder.encode();

   if(mel.length()==0){
      setErrorText(encoder.getErrText());
      return false;
   }
   //cout<<"Encoded"<<endl<<mel<<endl;
   FILE *fp = fopen(outFile.c_str(), "w");
   if(!fp){
      string errTxt = string("Could not open ") + outFile;
      setErrorText(errTxt);
      return false;
   }
   
   fprintf(fp, "%s", mel.c_str());
   if(ferror(fp)){
      string errTxt = string("I/O error during writing ") + outFile;
      setErrorText(errTxt);
      bRet =  false;
   }
   fclose(fp);
   return bRet;
}
