
/*	Copyright  (c)	Günter Woigk  1999 - 2003
  					mailto:kio@little-bat.de

 	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.  

 	Permission to use, copy, modify, distribute, and sell this software and
 	its documentation for any purpose is hereby granted without fee, provided
 	that the above copyright notice appear in all copies and that both that
 	copyright notice and this permission notice appear in supporting
 	documentation, and that the name of the copyright holder not be used
 	in advertising or publicity pertaining to distribution of the software
 	without specific, written prior permission.  The copyright holder makes no
 	representations about the suitability of this software for any purpose.
 	It is provided "as is" without express or implied warranty.

 	THE COPYRIGHT HOLDER DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 	INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 	EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 	CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 	DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 	TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 	PERFORMANCE OF THIS SOFTWARE.


	manage preferences
	------------------

	the preferences file is automatically read when a pref is read or written.
	prefs may be fetched via get() from the buffered data
	char pointers remain valid until another put() or read()
	the preferences file is written:
	- at program exit if data is dirty or
	- when write() is called and data is dirty

	file is located in ~/prefs/  or similar. (tries approx. 10 chances)
	file name is <basename>.prefs or similar (basename is passed to the constructor)

	preferences are stored in a plain vanilla textfile which the user may edit.


	getxxx(name,default)	get an entry
	putxxx(name,value)		store/replace an entry
	del(name)				delete entry
	read()					reread file
	write()					write data back to file now
	home()					get home directory path "/home/myname"  (unix only)
*/


#ifndef _prefs_h_
#define _prefs_h_

#include "kio/kio.h"


class Prefs
{
		long		appl_id;		// for muxing
		str			filename;
		str			home;
		ptr			data;
		long		size;
		long		used;
		bool		dirty;

		bool		IsOpen	( )		{ return data;			  }
		bool		IsDirty	( )		{ return data && dirty;	  }
		bool		NotOpen	( )		{ return !data;			  }
		bool		NotDirty( )		{ return !data || !dirty; }

public:
					Prefs	( cstr applname, cstr filename );
					~Prefs	( );

// read/write from buffer
		cstr	 	GetStr	( cstr idf, cstr defaultvalue=NULL, bool secure=0 );
		void		PutStr	( cstr idf, cstr text,			  	bool secure=0 );
		long		GetNum	( cstr idf, long defaultvalue=0,	bool secure=0 );
		void		PutNum	( cstr idf, long value,				bool secure=0 );
		bool		GetBool	( cstr idf, bool defaultvalue=false );
		void		PutBool	( cstr idf, bool text );

		void		PutHex	( cstr idf, int n, const void* buffer );
		void		GetHex	( cstr idf, int n,       void* buffer );
		void 		PutNums	( cstr idf, int n, const long*  array );
		void 		GetNums	( cstr idf, int n,       long*  array );
		void 		PutNums	( cstr idf, int n, const short* array );
		void 		GetNums	( cstr idf, int n,       short* array );
		void		PutBox	( cstr idf, cBox& );
		void		GetBox	( cstr idf, Box& );

		void		Del		( cstr idf );

// read/write file
		void		Read	( );		// open-read-close
		void		Write	( );		// open-write-close

// get members
#if unix
		cstr		GetHome ( )			{ return home; }
		cstr		GetFile ( )			{ return filename; }
#endif
};



#endif











