// src/global_preferences.cc - Copyright 2005, 2006, University
//               of Padova, dept. of Pure and Applied
//               Mathematics
//
// This file is part of SGPEMv2.
//
// This 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.
//
// SGPEMv2 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 General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with SGPEMv2; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

#include "config.h"
#include <sgpemv2/key_file.hh>
#include <fstream>
#include <string.h>

#include <iostream>
using namespace std;


namespace sgpem
{

  KeyFile::KeyFile()
  {}


  KeyFile::elements_iterator
  KeyFile::elements_begin() const
  {
    return _elements.begin();
  }


  KeyFile::elements_iterator
  KeyFile::elements_end() const
  {
    return _elements.end();
  }


  void
  KeyFile::insert_key_value(const Glib::ustring& key, const Glib::ustring& value)
  {
    std::map<Glib::ustring, Glib::ustring>::value_type val_pair(key, value);
    _elements.insert(val_pair);
  }


  const Glib::ustring*
  KeyFile::search_value(const Glib::ustring& key)
  {
    const Glib::ustring* ret = 0;
    elements_iterator cur  = _elements.find(key);
    if (cur != elements_end())
    {
      ret = &((*cur).second);
    }
    return ret;
  }

  void
  KeyFile::file_read(const Glib::ustring& filename)
  {
    std::ifstream ifs(filename.c_str());
    if (ifs)
    {
      file_read(ifs);
    } // end - if(ifs)
  }

  void
  KeyFile::file_read(std::istream& is)
  {
    if (is)
    {
      _elements.clear(); // erase all elements
      char buff[KEY_FILE_BUF_LEN]; //
      while (is)
      {
        is.getline(buff, (KEY_FILE_BUF_LEN), '\n');
        // if not a comment line...

        if (*buff != '\0' && *buff != '#')
        {
          char* pos = strchr(buff, '=');
          if (pos != 0)
          {
            *pos = '\0';
            const Glib::ustring key(buff);
            const Glib::ustring value(pos + 1);
            insert_key_value(key, value);
          }
        } // end - if not a comment line...

      } // end - while(ifs))
    } // end - if(ifs)
  }

  void
  KeyFile::file_write(const Glib::ustring& filename)
  {
    std::ofstream ofs(filename.c_str());
    if (ofs)
    {
      file_write(ofs);
    } // end - if(ofs)
  }

  void
  KeyFile::file_write(std::ostream& os)
  {
    if (os)
    {
      elements_iterator iter;
      for (iter = elements_begin(); iter != elements_end(); iter++)
      {
        os << (*iter).first << "=" << (*iter).second << std::endl;
      }
    } // end - if(ofs)
  }

}
