// $Source: /usr/local/SimSoft/SoftShop/Basics/io/IniFile.h,v $
// $Revision: 1.19 $
// $Author: baum $
// $Date: 2001/11/29 15:52:23 $
//
// Login name of the user who locked the revision
// $Locker:  $
// Tag name used to checkout the file
// $Name: Release-20 $
// State assigned to revision
// $State: Exp $
//
//
// $Log: IniFile.h,v $
// Revision 1.19  2001/11/29 15:52:23  baum
// -In Kalender.cpp und Kalender.h Handsteuerung eingebaut
//

#ifndef _INIFILE_H
#define _INIFILE_H   1

#ifdef  _MSC_VER
#pragma warning(disable:4251)
#endif  // _MSC_VER

#include <istream.h>
#include <fstream.h>
#include "Strings.h"

#ifdef _INIFILE_CPP
#  undef _impexp
#  define _impexp __declspec(dllexport)
#elif defined BUILD_BASICS_DLL
#  undef _impexp
#  define _impexp __declspec(dllexport)
#else
#  undef _impexp
#  define _impexp __declspec(dllimport)
#endif

#if defined __GNUG__ || defined __hpux
#  undef _impexp
#  define _impexp
#endif

#define LLENGTH     255

//##ModelId=3FBCBCC601C5
class _impexp IniFile {
public:
	//##ModelId=3FBCBCC601C6
  IniFile  (char *fname);
	//##ModelId=3FBCBCC601D5
  ~IniFile ();

	//##ModelId=3FBCBCC601D9
  static IniFile DefIniFile;
	//##ModelId=3FBCBCC601E4
  static bool    isInitialized ();

  // setup and cleanup things
	//##ModelId=3FBCBCC601E6
  static void setup   (const char *fname);
	//##ModelId=3FBCBCC601E9
  static void cleanup ();

  // testing availabilty
	//##ModelId=3FBCBCC601F5
  bool operator ! () const;
	//##ModelId=3FBCBCC601F7
  operator void* () const;

  // Reading Variables
  // read and return a bool, return defVal if read failed
	//##ModelId=3FBCBCC601F9
  bool readBool (const char *section, const char *variable, bool defVal);
  // find Variable and return true if available
	//##ModelId=3FBCBCC60205
  bool findVar (const char *section, const char *variable);

  // read and return an int, return defVal if read failed
	//##ModelId=3FBCBCC60214
  int readInt (const char *section, const char *variable, int defVal);

  // read and return an unsigned, return defVal if read failed
	//##ModelId=3FBCBCC60222
  unsigned readUnsigned (const char *section, const char *variable, unsigned defVal);

  // read and return a double, return defVal if read failed
	//##ModelId=3FBCBCC60232
  double readDouble (const char *section, const char *variable, double defVal);

  // read and return a string, return defVal if read failed
	//##ModelId=3FBCBCC60236
  const char* readString (const char *section, const char *variable, const char *defVal);

  // read and return a quoted string, returns defVal if read failed
	//##ModelId=3FBCBCC60245
  const char* readQuoted (const char *section, const char *variable, const char *defVal);


  // Reading Values from Sections (all return true on success, else false)
  // read next line from IniFile's current Section into out
	//##ModelId=3FBCBCC60253
  bool readSectLine (char *out);

  // read next value from IniFile's current Section into out if it's a bool
	//##ModelId=3FBCBCC60261
  bool readSectBool (bool &out);

  // read next value from IniFile's current Section into out if it's an int
	//##ModelId=3FBCBCC60263
  bool readSectInt (int &out);

  // read next value from IniFile's current Section into out if it's an unsigned
	//##ModelId=3FBCBCC60271
  bool readSectUnsigned (unsigned &out);

  // read next value from IniFile's current Section into out if it's a double
	//##ModelId=3FBCBCC60273
  bool readSectDouble (double &out);


  // Writing Variables
  // write a bool
	//##ModelId=3FBCBCC60275
  void writeBool (const char *section, const char *variable, bool val);

  // write an int
	//##ModelId=3FBCBCC60283
  void writeInt (const char *section, const char *variable, int val);

  // write an unsigned
	//##ModelId=3FBCBCC60293
  void writeUnsigned (const char *section, const char *variable, unsigned val);

  // write a double
	//##ModelId=3FBCBCC602A1
  void writeDouble (const char *section, const char *variable, double val);

  // write a string
	//##ModelId=3FBCBCC602B0
  void writeString (const char *section, const char *variable, const char *val);

  // write a quoted string
	//##ModelId=3FBCBCC602C0
  void writeQuoted (const char *section, const char *variable, const char *val);


  // Writing Values to Sections
  // write a line (\n appended automatically)
	//##ModelId=3FBCBCC602CE
  void writeSectLine     (const char *section, const char *val);

  // write a bool
	//##ModelId=3FBCBCC602D1
  void writeSectBool     (const char *section, bool val);

  // write an int
	//##ModelId=3FBCBCC602DF
  void writeSectInt      (const char *section, int val);

  // write an unsigned
	//##ModelId=3FBCBCC602EE
  void writeSectUnsigned (const char *section, unsigned val);

  // write a double
	//##ModelId=3FBCBCC602F1
  void writeSectDouble   (const char *section, double val);

  
  // others
  // return the filename
	//##ModelId=3FBCBCC602FE
  const char *FileName () const;
  
  // place the cursor after a section
	//##ModelId=3FBCBCC60300
  bool placeInSection (const char *section);
  
  // deletes a complete section
	//##ModelId=3FBCBCC6030D
  void deleteSection (const char *section);    

private:
	//##ModelId=3FBCBCC6030F
  char* placeAfterVariable (const char *section, const char *variable);
	//##ModelId=3FBCBCC6031E
  bool  isComment          ();
	//##ModelId=3FBCBCC6031F
  bool  isNewSection       ();
	//##ModelId=3FBCBCC6032C
  bool  readNextLine       ();
	//##ModelId=3FBCBCC6032D
  void  reset              ();
	//##ModelId=3FBCBCC6032E
  bool  prepareWrite       (const char *section, const char *variable);
	//##ModelId=3FBCBCC6033D
  void  doWrite            (const char *section, const char *variable = NULL);
	//##ModelId=3FBCBCC6034C
  void  doAppend           (const char* section);
	//##ModelId=3FBCBCC6034E
  void  doOverwrite        ();
	//##ModelId=3FBCBCC6035B
  void  doInsert           ();

  // used internally
	//##ModelId=3FBCBCC6035C
  IniFile ();

  // declared, but not defined
	//##ModelId=3FBCBCC6035D
  IniFile (const IniFile&);
	//##ModelId=3FBCBCC6036B
  IniFile& operator = (const IniFile&);

  // data members
	//##ModelId=3FBCBCC6036E
  mutable ifstream  ObjFile;

	//##ModelId=3FBCBCC6037B
  Ustring   ObjFileName;
	//##ModelId=3FBCBCC6037F
  char      ObjReadBuf  [LLENGTH];
	//##ModelId=3FBCBCC6038A
  char      ObjWriteBuf [LLENGTH];
	//##ModelId=3FBCBCC6038C
  streampos ObjLinePos;
  
  // static members
	//##ModelId=3FBCBCC6039B
  static bool ClassIsInitialized;

};

#endif /* _INIFILE_H */
