/* Network-Integrated Multimedia Middleware (NMM)
 * http://www.motama.com/nmm.html
 *  
 * Copyright (C) 2006-2007
 * Motama GmbH, Saarbruecken, Germany
 * http://www.motama.com
 *  
 * This file is part of NMM. 
 * 
 * NMM 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; only version 2 of the License.      
 * 
 * NMM 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 NMM; if not, write to the Free Software Foundation,    
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 */


#ifndef NMM_NMM_CONFIG_HPP
#define NMM_NMM_CONFIG_HPP NMM_NMM_CONFIG_HPP

#include "nmm/nmm_prefix.hpp"

#include <string>
#include <list>
#include <map>
#include "visibility.hpp"

#include "visibility.hpp"

namespace NMM { 

  /** A simple cnofiguration manager.
   * Thisis a helper class for parsing simple configuration files based on key value pairs.
   */
  class DLLEXPORT NMMConfig {
  public:
    /** A setting in a configuration file.
     * Helper class that stores multiple values for a setting including a description.
     */
    class Setting {
    private:
      std::string desc;                 ///< Description
      std::list<std::string> values;    ///< Values

    public:
      /** Constructor.
       *
       * @param d Description of the setting
       * @param v A value of the setting
       */
      Setting(const std::string& d = std::string(), const std::string& v = std::string());

      /** Get the description of this setting.
       *
       * @return Description of this setting
       */
      const std::string& getDescription() const {
        return desc;
      }

      /** Get the values of this setting.
       *
       * @return Values of this setting
       */
      const std::list<std::string>& getValues() const {
        return values;
      }
    };

   /** Constructor.
    */
    NMMConfig();

   /** Constructor.
    * This constructed parses a configuration file.
    * 
    * @param filename File name of configuration file
    */
    NMMConfig(const std::string filename);

    /** Destructor.
     */
    ~NMMConfig();

    /** Get value of the setting with the specific key.
     *
     * @param id A key
     * @param value Returns the value if a setting with the specified key exists
     * @return @c true if a setting with the given key exists, @c false otherwise
     */
    bool getValue(const std::string id, std::string& value);

    /** Get value of the setting with the specific key.
     * This method performs a lexical conversion of the value to the returned value type.
     *
     * @param id A key
     * @param value Returns the value if a setting with the specified key exists
     * @return @c true if a setting with the given key exists, @c false otherwise
     */
    bool getValue(const std::string id, bool& value);

    /** Get value of the setting with the specific key.
     * This method performs a lexical conversion of the value to the returned value type.
     *
     * @param id A key
     * @param value Returns the value if a setting with the specified key exists
     * @return @c true if a setting with the given key exists, @c false otherwise
     */
    bool getValue(const std::string id, int& value);

    /** Get value of the setting with the specific key.
     * This method performs a lexical conversion of the value to the returned value type.
     *
     * @param id A key
     * @param value Returns the value if a setting with the specified key exists
     * @return @c true if a setting with the given key exists, @c false otherwise
     */
    bool getValue(const std::string id, float& value);

    /** Set value of the setting with the specific key.
     *
     * @param id A key
     * @param value Value of the setting
     */
    void setValue( std::string id, std::string value );

    /** Save the configuration file.
     */
    void saveFile();

    /** Get the setting with the specified key.
     *
     * @param id A key
     * @param setting Returns a reference to a pointer to the setting
     */
    bool getSetting(const std::string& id, const Setting*& setting) const;

    /** Add a Setting with the specified key.
     * 
     * @param d Description of the setting
     * @param s Key of the setting
     * @param v Initial value of the setting
     */  
    void addSetting(const std::string& d = std::string(), const std::string& s = std::string(), const std::string& v = std::string());

    /** Open and parse the specfied configuration file.
     *
     * @param filename File name of configuration file
     */
    void parseFile(const std::string filename);
  protected:
    std::map<std::string,std::string> config_table; ///< Configuration table
    std::map<std::string,Setting> settings;         ///< Settings

    std::string rc_file;                            ///< File name of configuration file
  };

}

#include "nmm/nmm_suffix.hpp"

#endif
