1/*
2Open Tracker License
3
4Terms and Conditions
5
6Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
7
8Permission is hereby granted, free of charge, to any person obtaining a copy of
9this software and associated documentation files (the "Software"), to deal in
10the Software without restriction, including without limitation the rights to
11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12of the Software, and to permit persons to whom the Software is furnished to do
13so, subject to the following conditions:
14
15The above copyright notice and this permission notice applies to all licensees
16and shall be included in all copies or substantial portions of the Software.
17
18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25Except as contained in this notice, the name of Be Incorporated shall not be
26used in advertising or otherwise to promote the sale, use or other dealings in
27this Software without prior written authorization from Be Incorporated.
28
29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30of Be Incorporated in the United States and other countries. Other brand product
31names are registered trademarks or trademarks of their respective holders.
32All rights reserved.
33*/
34#ifndef __SETTINGS_FILE__
35#define __SETTINGS_FILE__
36
37
38#include <SupportDefs.h>
39#include <stdarg.h>
40#include <stdio.h>
41#include <string.h>
42#include <stdlib.h>
43
44
45class BFile;
46class BDirectory;
47class BRect;
48
49namespace BPrivate {
50
51class Settings;
52
53typedef const char* (*ArgvHandler)(int argc, const char* const *argv,
54	void* params);
55	// return 0 or error string if parsing failed
56
57const int32 kBufferSize = 1024;
58
59class ArgvParser {
60	// this class opens a text file and passes the context in argv
61	// format to a specified handler
62public:
63	static status_t EachArgv(const char* name,
64		ArgvHandler argvHandlerFunc, void* passThru);
65
66private:
67	ArgvParser(const char* name);
68	~ArgvParser();
69
70	status_t EachArgvPrivate(const char* name,
71		ArgvHandler argvHandlerFunc, void* passThru);
72	char GetCh();
73
74	status_t SendArgv(ArgvHandler argvHandlerFunc, void* passThru);
75		// done with a whole line of argv, send it off and get ready
76		// to build a new one
77
78	void NextArgv();
79		// done with current string, get ready to start building next
80	void NextArgvIfNotEmpty();
81		// as above, don't commint current string if empty
82
83	void MakeArgvEmpty();
84
85	FILE* fFile;
86	char* fBuffer;
87	int32 fPos;
88
89	int fArgc;
90	char** fCurrentArgv;
91
92	int32 fCurrentArgsPos;
93	char fCurrentArgs[1024];
94
95	bool fSawBackslash;
96	bool fEatComment;
97	bool fInDoubleQuote;
98	bool fInSingleQuote;
99
100	int32 fLineNo;
101	const char* fFileName;
102};
103
104class SettingsArgvDispatcher {
105	// base class for a single setting item
106public:
107	SettingsArgvDispatcher(const char* name);
108	virtual ~SettingsArgvDispatcher() {};
109
110	void SaveSettings(Settings* settings, bool onlyIfNonDefault);
111
112	const char* Name() const { return name; }
113		// name as it appears in the settings file
114
115	virtual const char* Handle(const char* const *argv) = 0;
116		// override this adding an argv parser that reads in the
117		// values in argv format for this setting
118		// return a pointer to an error message or null if parsed OK
119
120	// some handy reader/writer calls
121	bool HandleRectValue(BRect&, const char* const *argv,
122		bool printError = true);
123	void WriteRectValue(Settings*, BRect);
124
125protected:
126	virtual void SaveSettingValue(Settings* settings) = 0;
127		// override this to save the current value of this setting in a
128		// text format
129
130	virtual bool NeedsSaving() const
131		{ return true; }
132		// override to return false if current value is equal to the default
133		// and does not need saving
134
135private:
136	const char* name;
137};
138
139
140class Settings {
141	// this class is a list of all the settings handlers, reads and
142	// saves the settings file
143public:
144	Settings(const char* filename, const char* settingsDirName);
145	~Settings();
146	void TryReadingSettings();
147	void SaveSettings(bool onlyIfNonDefault = true);
148
149	bool Add(SettingsArgvDispatcher*);
150		// return false if argv dispatcher with the same name already
151		// registered
152
153	void Write(const char* format, ...);
154	void VSWrite(const char*, va_list);
155
156private:
157	void MakeSettingsDirectory(BDirectory*);
158
159	SettingsArgvDispatcher* Find(const char*);
160	static const char* ParseUserSettings(int, const char* const *argv, void*);
161	void SaveCurrentSettings(bool onlyIfNonDefault);
162
163	const char* fFileName;
164	const char* fSettingsDir;
165		// currently unused
166	SettingsArgvDispatcher** fList;
167	int32 fCount;
168	int32 fListSize;
169	BFile* fCurrentSettings;
170};
171
172}
173
174using namespace BPrivate;
175
176#endif	// __SETTINGS_FILE__
177