/***************************************************************************
                          SIMPLY POWERFUL TOOLKIT (SPTK)
                          CStrings.cpp  -  description
                             -------------------
    begin                : Thu August 11 2005
    copyright            : (C) 2005 by Alexey Parshin
    email                : alexeyp@gmail.com
 ***************************************************************************/

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

   This library is distributed in the hope that it will be useful, but 
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library
   General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; if not, write to the Free Software Foundation, 
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.

   Please report all bugs and problems to "alexeyp@gmail.com"
 ***************************************************************************/

#include <fstream>
#include <sptk3/CStrings.h>
#include <sptk3/string_ext.h>

using namespace std;

void CStrings::parseString(const char *src,const char *delimitter) {
   if (!src || !src[0]) return;
   string buffer(src);
   clear();
   int dlen = strlen(delimitter);
   if (!dlen) return;
   char *p = (char *)buffer.c_str();
   for (;;) {
      char *end = strstr(p,delimitter);
      if (end) {
         char sc = *end;
         *end = 0;
         push_back(p);
         *end = sc;
         p = end + dlen;
      } else {
         push_back(p);
         break;
      }
   }
}

string CStrings::asString(const char *separator) const {
   string result;
   const_iterator itor = begin();
   if (itor != end()) {
      result = *itor;
      itor++;
   }
   while (itor != end()) {
      result += separator + *itor;
      itor++;
   }
   return result;
}

int CStrings::indexOf(string s) const {
   const_iterator itor = find(begin(),end(),s.c_str());
   if (itor == end()) return -1;
   return distance(begin(),itor);
}

void CStrings::saveToFile(string fileName) const throw(CException) {
   const_iterator itor = begin();
   ofstream outfile(fileName.c_str());
   for (; itor != end(); itor++) {
      const idstring& str = *itor;
      outfile << str.c_str() << endl;
      if (!outfile.good())
         throw CException("Can't write to file "+fileName);
   }
   outfile.close();
}

void CStrings::loadFromFile(string fileName) throw(CException)  {
   string line;
   clear();
   ifstream infile;
   infile.open(fileName.c_str());
   if (!infile.is_open())
      throw CException("Can't open file "+fileName+" for reading");
   while (getline(infile,line,'\n')) 
      push_back (line);
   infile.close();
}
