/* This file is part of cardwords
   (c) 1999 Tobias Peters
   see file COPYING for the copyright terms.
   
   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.

   This program 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 this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// cardwords_options.hh

// CardWords_Options is a class that parses an options file, provides options,
// lets options being set, and can save them in an options file.

#ifndef CARDWORDS_OPTIONS_HH
#define CARDWORDS_OPTIONS_HH

#include <map>
#include <iostream>
#include <string>

#define STRING_TYPE   CardWords_Options::String_Type
#define BOOL_TYPE     CardWords_Options::Bool_Type
#define UNSIGNED_TYPE CardWords_Options::Unsigned_Type
#define UNKNOWN_TYPE  CardWords_Options::Unknown_Type

class CardWords_Options
{
public:
  CardWords_Options(const string & filename);
  ~CardWords_Options();

  void
  read_file(const string & filename);
  
  // save returns 0 on success, -1 on error:
  int
  save(const string & filename);
  int
  save();
  
  // print prints all current options to the given ostream. used for
  // debugging and save():
  ostream &
  print(ostream & = cout) const;
  
  // if the current options are saved:
  bool
  is_saved(void) const;

  // Set an option: returns 0 on success, returns -1 in case of a type
  // mismatch, that is, an option with this name was declared before,
  // but had a value of a different type.
  int
  set(const string & option_name, const string & string_value);
  int
  set(const string & option_name, bool bool_value);
  int
  set(const string & option_name, unsigned unsigned_value);
  
  // an enum of option_types
  enum Type {
    String_Type,
    Bool_Type,
    Unsigned_Type,
    Unknown_Type // For a type query with an option that is unknown until now.
  };

  // Return the type of an option:
  enum Type // one of the above enums
  get_type(const string & option_name) const;

  // Accessing the options: returns 0 on success, -1 on failure
  int
  get(const string & option_name, string * string_value) const;
  int
  get(const string & option_name, bool * bool_value) const;
  int
  get(const string & option_name, unsigned * unsigned_value) const;

  // One for all: returns the type of that option. Only one of these pointers
  // will be set, according to the return value, or none, in case of
  // Unknown_Type. Pointers may be 0, in which case they are not used.
  Type
  get(const string & option_name,
      string * string_value,
      bool * bool_value,
      unsigned * unsigned_value) const;

  const string &
  get_filename(void) const;
  
  void // calls exit(1)
  false_entry(const string & option_name,
              const string & option_type);

  void // calls exit(1)
  false_entry(const string & option_name,
              enum Type option_type);

private:

  class Data_Type {
    enum Type option_type;
    string      string_value;
    bool        bool_value;
    unsigned    unsigned_value;
  public:
    Data_Type(const string & string_init_value);
    Data_Type(bool bool_init_value);
    Data_Type(unsigned unsigned_init_value);
    Data_Type(const Data_Type &);
    Data_Type();
    ~Data_Type();

    Data_Type & operator = (const Data_Type &);

    // set returns -1 on type mismatch, 0 if the set changed something
    // and 1 if the option had already had this value:
    int
    set(const string & string_init_value);
    int
    set(bool bool_init_value);
    int
    set(unsigned unsigned_init_value);

    // return the type
    enum Type
    get_type(void) const;
    // Accessing the options: returns 0 on success, -1 on failure
    int
    get(string * string_value) const;
    int
    get(bool * bool_value) const;
    int
    get(unsigned * unsigned_value) const;
  };

  map<string, Data_Type> options;
  typedef map<string, Data_Type>::iterator options_iterator;
  typedef map<string, Data_Type>::const_iterator options_const_iterator;
  typedef map<string, Data_Type>::value_type Value_Type;
  
  string filename;

  bool saved;
};

#endif
