SETUPMANAGER
Classes
- vorlib::setupmanager
- vorlib::setupmanager_e (exception)
Files
- setupmanager.h
- setupmanager.cpp
This class can be used when your application needs to remember something between starts. You can easilly store data here and let it automatically take care of saving and loading it. However, be carefull what you are saving here, because it is stored in a plain-text file. It is OK to remember window layout, default walues, language settings or so, but it is a bad idea to store passwords here, for example.
Item identification
Each item is specified by two std::string s. One is called section and the other is value_name. The point is, that the whole setup is divided into sections and inside them, they can not have identical names. However, you can have more items with the same names, as long as they are each in other section.
Interface
-
setupmanager
This is constructor and it is called automatically when you write setupmanager manager. It will create an empty setupmanager. However, you can create it with parameters as well. It is manager(file_with_setup, automanagment). The file_with_setup is the file, where the data are stored. If automanagment is true, the file is automatically loaded and saved before removing the setupmanager. Is is just the same as create it and call setfile. The automanagment is true if not specified.
-
~setupmanager
This one is default destructor. It is called automatically when written delete manager (if you have it dynamically created with new), or when variable's lifetime ends. If you create it with new, be sure you delete it as well. Not that the memory would not be freed (and it is bad enough), the data wouldn't be saved, if you use automanagment and don't call seve.
-
setfile
This functin takes two parameters. One, std::string, is name of file to set, and the other is bool automanagment. If you set the automanagment to true, the file will be loaded imediatelly and saved automatically when the manager dies.
-
load
This function does not take any parameters. It is used fot two things. First, when you don't want to use automanagment for some reason, this is the way how to load the file. And the second, if you have it loaded, to reload it from file (to forget all changes you made to the setup from last save).
-
connectfiles
This does few things. First, it sets the automanagment to true. Then, it loads file given by a parameter, but does not clear the old content. (this means it will connect together with the old one). And, at last, it sets file to witch it will be written to the new file. It is used when you want to put more setup files into single one. (the one to witch you will save it must be connected last)
-
save
Don't wait for saving until deleting the manager and do it now.
-
getstring
Returns vanted value as a string. This may fail only if the section or value_name does not exist. Specify first the section, then the name.
-
getint
This is very similar to the one above, but it converts it into integer. Therefore it may fail fore another reason - thet thing you trying to get is something that con not be converted into it.
-
getfloat
Just like above, but it returns float. Inside the file, floats are saved with ".", so use eather float functions or string functions for all occasions.
-
getbool
Like all theese get functions, returning bool. true, yes and Y means true and false, no and N means false. It does not convert ints to bools as C/C++.
-
setstring, setint, setfloat, setbool
Theese functions change the value specified by first two parameters (section and name) to the third. The third parameter is of coresponding type (by the name of function) and, if is not string, is converted to string, so it will be stored inside the file human readable. If the value is not inside, it will be added.
-
remove
Removes identified value. Note that empty sections aren't saved into file. So, if yoy want to delete section, just delete all inside.
-
clear
Deletes all.
-
getSectionList
Takes no arguments. Returns stringlist containing names of all sections
-
getEntryList
This returns stringlist containig list of all entry names inside a section. Takes the section name as an argument.
File format
The setup file is very similar to the ini file. Lines starting with # are comments. But note, all comments will be lost if the file is saved. Comments makes sence only for read-only setup (do not automanage and load it after that). File looking like [this] means start of section (with the name between that brackets) and other lines all values. It should look like name=value.
Error handling
It is done like elsewhere in vorlib, trough the exception class's heirs. This one is setupmanager_e. The error ids are here:
- 1: File not found. You provided file name (to setupmanager, setfile , or connectfiles) that does not exist.
- 2: File not closed. This might happen especially when saving, and it would probably mean that there is not enough disk space.
- 3: Duplicit section name. The file you are loading has some section twice, or you are connecting files each having the same section.
- 4: ] missing in section name. That means that some line starts with [, but does not end with ].
- 5: = missing in value. That means that the line is written wrongly.
- 6: Data out of section. Data line was before any section name.
- 7: Empty file name. That mean you want to load or save and you did not set the file yet.
- 8: Can not write to file.
- 9: Value not of that type. That means you vant for example int and the value is something that can not be int.
- 10: Section or name not found. You requested value that is not there.
Recommended use
You should test, weather the file you want to load exists and, if not, create an empty one. Then, when you load some value, put it between try-catch and test, weather it has been loaded. If not (it is not inside), set the value you are loading to some default (but not the manager) and if it changes (by user or so), put it into the setup. That will lead to setup file that will contain only non-default values (or, lets say, changed) and it will not crash when it does not have the setup file, it will just create new one and start with defaults.