How do you create a configuration/preferences file?
The short answer is, however you’d like. There is no hard and fast rule on this. I use a very simple text format of key=value (I hex-encode value where needed), others use XML or Plist, still others use a SQLiteDatabase.
It’s really up to you.
Thanks, Kem. I’m an absolute beginner at this, learning how to program. Sorry for the very naive, general question. I need to create a configuration/preferences file so that I can save the initial settings for my program. Given my application, how would I use, say, Plist? Is there a good tutorial for it somewhere? Is it Xojo-specific?
Sam
I also use a simple binary file for my Prefs. Some like NSUserDefaults or CFPreferences for Mac builds.
Whatever you choose, you should store it in SpecialFolder.ApplicationData. Using SpecialFolder.Preferences, despite its name, can be problematic with later Operating systems
Chiming in on your 2nd question, if you’re just starting, I would recommend just using a simple Binary File. Your Prefs does not HAVE to be any kind of special file. See the Xojo docs on writing to a binaryStream and just write your values to a simple file.
One solution that is pretty simple is to use JSONItem to store values in a Dictionary-like manner. Then you can just save and load this JSON data to a file pretty easily.
For an example of this, check out the Developing a Text Editor webinars. I believe part 2 covers saving preferences.
I use the same preferences class for both user and application settings. It uses simple key-value pairs and it supports the storage and retrieval of the standard data types plus Color and FolderItem. It uses a simple xml file in the background to store key, value and data type.
One problem I’ve had in the past is developers shipping their settings with product, so I’ve added a little twist - an overwrite file. If found, then after loading the preferences, it’s values will be used to overwrite them and on save, values loaded from the overwrite file and changed will be written back to the overwrite file.
My SVN commit script filters out the overwrite file as do my build scripts.
I use a class I wrote years ago the in some respects mimics the old Windows INI file… but works on all platforms…
I’ll post a link if anyone is interested
Please do so Dave that would be very helpful. Thank you again.
I normally prefer to use Plist for OS X and XML for Windows.
Since Plist is essentially an XML file, this permits to have very similar files in both platforms.
And XML is nicer when you have to change the format of the file and still keep a good degree of retro-compatibility.
I tend to use a SQLite database. Works cross-platform out of the box.
Here it is with a bit of sample code…
Normally you drop this class into your project. and modifiy the 3 methods in the modOPTIONS module depending on the parameters you are loading and saving. It includes overloaded functions to PUT and GET most all XOJO standard datatypes.
I have used this in every project I have written for the past several years…
(downloaded)
Dave, thank you. I am going to check this out.
same here… i keep my config and login info in sqlite file
I do this also if my application will require a database anyway. Otherwise I tend to use a binary file.
Yes but it is not so easy to just open it up and make a change. Or even worse as a use at the end of the phone to tell you what the values are
I use XML. I am not so sure about SQLite or JSON but XML is really easy to debug. I often use messageboxes to display the information saved by an XML to read if everything is how I want it and I can write or modify an XML file in an external text editor, which is also great for debugging!
[quote=60798:@Dave S]I use a class I wrote years ago the in some respects mimics the old Windows INI file… but works on all platforms…
I’ll post a link if anyone is interested[/quote]
‘mimics the old Windows INI file’, are you saying that support for INI files has been dropped by Microsoft or something? Or has INI files become an old way of doing things.
Thanks
I keep getting an error message if I try to post a link directly to a zip file, so here is a blog link instead. My Preferences project.
Bug reports, tips etc are most welcome, as I’m just starting out with XOJO
Windows dropped support for INI files in preference to the awful REGISTRY years and years ago.
my class “mimics” INI in the fact that is has sections, keys and values… and stores things in plain text