1 /* 2 Open Tracker License 3 4 Terms and Conditions 5 6 Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 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: 14 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. 17 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. 24 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. 28 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. 33 */ 34 #ifndef _SETTINGS_FILE_H 35 #define _SETTINGS_FILE_H 36 37 38 #include <stdarg.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 43 #include <SupportDefs.h> 44 45 46 class BFile; 47 class BDirectory; 48 class BRect; 49 50 namespace BPrivate { 51 52 class Settings; 53 54 typedef const char* (*ArgvHandler)(int argc, const char* const *argv, 55 void* params); 56 // return 0 or error string if parsing failed 57 58 const int32 kBufferSize = 1024; 59 60 61 class ArgvParser { 62 // this class opens a text file and passes the context in argv 63 // format to a specified handler 64 public: 65 static status_t EachArgv(const char* name, 66 ArgvHandler argvHandlerFunc, void* passThru); 67 68 private: 69 ArgvParser(const char* name); 70 ~ArgvParser(); 71 72 status_t EachArgvPrivate(const char* name, 73 ArgvHandler argvHandlerFunc, void* passThru); 74 int GetCh(); 75 76 status_t SendArgv(ArgvHandler argvHandlerFunc, void* passThru); 77 // done with a whole line of argv, send it off and get ready 78 // to build a new one 79 80 void NextArgv(); 81 // done with current string, get ready to start building next 82 void NextArgvIfNotEmpty(); 83 // as above, don't commint current string if empty 84 85 void MakeArgvEmpty(); 86 87 FILE* fFile; 88 char* fBuffer; 89 int32 fPos; 90 91 int fArgc; 92 char** fCurrentArgv; 93 94 int32 fCurrentArgsPos; 95 char fCurrentArgs[1024]; 96 97 bool fSawBackslash; 98 bool fEatComment; 99 bool fInDoubleQuote; 100 bool fInSingleQuote; 101 102 int32 fLineNo; 103 const char* fFileName; 104 }; 105 106 107 class SettingsArgvDispatcher { 108 // base class for a single setting item 109 public: 110 SettingsArgvDispatcher(const char* name); 111 virtual ~SettingsArgvDispatcher() {}; 112 113 void SaveSettings(Settings* settings, bool onlyIfNonDefault); 114 115 const char* Name() const { return fName; } 116 // name as it appears in the settings file 117 118 virtual const char* Handle(const char* const *argv) = 0; 119 // override this adding an argv parser that reads in the 120 // values in argv format for this setting 121 // return a pointer to an error message or null if parsed OK 122 123 // some handy reader/writer calls 124 bool HandleRectValue(BRect&, const char* const *argv, 125 bool printError = true); 126 void WriteRectValue(Settings*, BRect); 127 128 protected: 129 virtual void SaveSettingValue(Settings* settings) = 0; 130 // override this to save the current value of this setting in a 131 // text format 132 133 virtual bool NeedsSaving() const { return true; } 134 // override to return false if current value is equal to the default 135 // and does not need saving 136 137 private: 138 const char* fName; 139 }; 140 141 142 class Settings { 143 // this class is a list of all the settings handlers, reads and 144 // saves the settings file 145 public: 146 Settings(const char* filename, const char* settingsDirName); 147 ~Settings(); 148 void TryReadingSettings(); 149 void SaveSettings(bool onlyIfNonDefault = true); 150 151 bool Add(SettingsArgvDispatcher*); 152 // return false if argv dispatcher with the same name already 153 // registered 154 155 void Write(const char* format, ...); 156 void VSWrite(const char*, va_list); 157 158 private: 159 void MakeSettingsDirectory(BDirectory*); 160 161 SettingsArgvDispatcher* Find(const char*); 162 static const char* ParseUserSettings(int, const char* const *argv, void*); 163 void SaveCurrentSettings(bool onlyIfNonDefault); 164 165 const char* fFileName; 166 const char* fSettingsDir; 167 // currently unused 168 SettingsArgvDispatcher** fList; 169 int32 fCount; 170 int32 fListSize; 171 BFile* fCurrentSettings; 172 }; 173 174 } // namespace BPrivate 175 176 using namespace BPrivate; 177 178 179 #endif // _SETTINGS_FILE_H 180