#ifndef n_ConfigDict_H
#define n_ConfigDict_H

/*
 *
 * nucleo/utils/ConfigDict.H --
 *
 * Copyright (C) Nicolas Roussel
 *
 * See the file LICENSE for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#include <string>
#include <map>
#include <sstream>

namespace nucleo {

  class ConfigDict {

  private:

    typedef std::map<std::string, std::string> ConfigDictImpl ;

    std::string _configfile ;
    ConfigDictImpl _map ;

  public:

    ConfigDict(void) {}
    ConfigDict(std::string filename) { loadFrom(filename) ; }
    ConfigDict(const char *filename) { loadFrom(filename) ; }

    bool loadFrom(std::string filename) { return loadFrom(filename.c_str()) ; }
    bool loadFrom(const char *filename) ;
    std::string getFilename(void) { return _configfile ; }
    template <class X> X get(const char *key, X defval) ;
    void debug(void) ;

  } ;

  template <class X> inline X ConfigDict::get(const char *key, X defval) {
    ConfigDictImpl::iterator i = _map.find(key) ;
    if (i==_map.end()) return defval ;

    X result ;
    std::istringstream in(i->second) ;
    in >> result ;
    return result ;
  }

  template <> inline bool ConfigDict::get(const char *key, bool defval) {
    ConfigDictImpl::iterator i = _map.find(key) ;
    if (i==_map.end()) return defval ;
  
    if (i->second=="true" || i->second=="1") return true ;
    if (i->second=="false" || i->second=="0") return false ;
    return defval ;
  }

  template <> inline std::string ConfigDict::get(const char *key, std::string defval) {
    ConfigDictImpl::iterator i = _map.find(key) ;
    if (i==_map.end()) return defval ;
    return i->second ;
  }

}

#endif
