6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved.
8 Permission is hereby granted, free of charge, to any person obtaining a copy of
9 this software and associated documentation files (the "Software"), to deal in
10 the Software without restriction, including without limitation the rights to
11 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
12 of the Software, and to permit persons to whom the Software is furnished to do
13 so, subject to the following conditions:
15 The above copyright notice and this permission notice applies to all licensees
16 and shall be included in all copies or substantial portions of the Software.
18 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY,
20 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
21 BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
22 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION
23 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 Except as contained in this notice, the name of Be Incorporated shall not be
26 used in advertising or otherwise to promote the sale, use or other dealings in
27 this Software without prior written authorization from Be Incorporated.
29 Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks
30 of Be Incorporated in the United States and other countries. Other brand product
31 names are registered trademarks or trademarks of their respective holders.
34 #ifndef _SETTINGS_FILE_H
35 #define _SETTINGS_FILE_H
38 #include <SupportDefs.h>
53 typedef const char* (*ArgvHandler
)(int argc
, const char* const *argv
,
55 // return 0 or error string if parsing failed
57 const int32 kBufferSize
= 1024;
61 // this class opens a text file and passes the context in argv
62 // format to a specified handler
64 static status_t
EachArgv(const char* name
,
65 ArgvHandler argvHandlerFunc
, void* passThru
);
68 ArgvParser(const char* name
);
71 status_t
EachArgvPrivate(const char* name
,
72 ArgvHandler argvHandlerFunc
, void* passThru
);
75 status_t
SendArgv(ArgvHandler argvHandlerFunc
, void* passThru
);
76 // done with a whole line of argv, send it off and get ready
80 // done with current string, get ready to start building next
81 void NextArgvIfNotEmpty();
82 // as above, don't commint current string if empty
93 int32 fCurrentArgsPos
;
94 char fCurrentArgs
[1024];
102 const char* fFileName
;
106 class SettingsArgvDispatcher
{
107 // base class for a single setting item
109 SettingsArgvDispatcher(const char* name
);
110 virtual ~SettingsArgvDispatcher() {};
112 void SaveSettings(Settings
* settings
, bool onlyIfNonDefault
);
114 const char* Name() const { return name
; }
115 // name as it appears in the settings file
117 virtual const char* Handle(const char* const *argv
) = 0;
118 // override this adding an argv parser that reads in the
119 // values in argv format for this setting
120 // return a pointer to an error message or null if parsed OK
122 // some handy reader/writer calls
123 bool HandleRectValue(BRect
&, const char* const *argv
,
124 bool printError
= true);
125 void WriteRectValue(Settings
*, BRect
);
128 virtual void SaveSettingValue(Settings
* settings
) = 0;
129 // override this to save the current value of this setting in a
132 virtual bool NeedsSaving() const { return true; }
133 // override to return false if current value is equal to the default
134 // and does not need saving
142 // this class is a list of all the settings handlers, reads and
143 // saves the settings file
145 Settings(const char* filename
, const char* settingsDirName
);
147 void TryReadingSettings();
148 void SaveSettings(bool onlyIfNonDefault
= true);
150 bool Add(SettingsArgvDispatcher
*);
151 // return false if argv dispatcher with the same name already
154 void Write(const char* format
, ...);
155 void VSWrite(const char*, va_list);
158 void MakeSettingsDirectory(BDirectory
*);
160 SettingsArgvDispatcher
* Find(const char*);
161 static const char* ParseUserSettings(int, const char* const *argv
, void*);
162 void SaveCurrentSettings(bool onlyIfNonDefault
);
164 const char* fFileName
;
165 const char* fSettingsDir
;
167 SettingsArgvDispatcher
** fList
;
170 BFile
* fCurrentSettings
;
173 } // namespace BPrivate
175 using namespace BPrivate
;
178 #endif // _SETTINGS_FILE_H