RemoteDrawingEngine: Reduce RP_READ_BITMAP result timeout.
[haiku.git] / src / kits / tracker / SettingsHandler.h
blob1d995e942a33e55bdf2599069c427b00c1635aec
1 /*
2 Open Tracker License
4 Terms and Conditions
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.
32 All rights reserved.
34 #ifndef _SETTINGS_FILE_H
35 #define _SETTINGS_FILE_H
38 #include <SupportDefs.h>
39 #include <stdarg.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <stdlib.h>
45 class BFile;
46 class BDirectory;
47 class BRect;
49 namespace BPrivate {
51 class Settings;
53 typedef const char* (*ArgvHandler)(int argc, const char* const *argv,
54 void* params);
55 // return 0 or error string if parsing failed
57 const int32 kBufferSize = 1024;
60 class ArgvParser {
61 // this class opens a text file and passes the context in argv
62 // format to a specified handler
63 public:
64 static status_t EachArgv(const char* name,
65 ArgvHandler argvHandlerFunc, void* passThru);
67 private:
68 ArgvParser(const char* name);
69 ~ArgvParser();
71 status_t EachArgvPrivate(const char* name,
72 ArgvHandler argvHandlerFunc, void* passThru);
73 char GetCh();
75 status_t SendArgv(ArgvHandler argvHandlerFunc, void* passThru);
76 // done with a whole line of argv, send it off and get ready
77 // to build a new one
79 void NextArgv();
80 // done with current string, get ready to start building next
81 void NextArgvIfNotEmpty();
82 // as above, don't commint current string if empty
84 void MakeArgvEmpty();
86 FILE* fFile;
87 char* fBuffer;
88 int32 fPos;
90 int fArgc;
91 char** fCurrentArgv;
93 int32 fCurrentArgsPos;
94 char fCurrentArgs[1024];
96 bool fSawBackslash;
97 bool fEatComment;
98 bool fInDoubleQuote;
99 bool fInSingleQuote;
101 int32 fLineNo;
102 const char* fFileName;
106 class SettingsArgvDispatcher {
107 // base class for a single setting item
108 public:
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);
127 protected:
128 virtual void SaveSettingValue(Settings* settings) = 0;
129 // override this to save the current value of this setting in a
130 // text format
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
136 private:
137 const char* name;
141 class Settings {
142 // this class is a list of all the settings handlers, reads and
143 // saves the settings file
144 public:
145 Settings(const char* filename, const char* settingsDirName);
146 ~Settings();
147 void TryReadingSettings();
148 void SaveSettings(bool onlyIfNonDefault = true);
150 bool Add(SettingsArgvDispatcher*);
151 // return false if argv dispatcher with the same name already
152 // registered
154 void Write(const char* format, ...);
155 void VSWrite(const char*, va_list);
157 private:
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;
166 // currently unused
167 SettingsArgvDispatcher** fList;
168 int32 fCount;
169 int32 fListSize;
170 BFile* fCurrentSettings;
173 } // namespace BPrivate
175 using namespace BPrivate;
178 #endif // _SETTINGS_FILE_H