using one instance of wxFileConfig Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
sfx81
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sun Sep 28, 2008 12:11 pm

using one instance of wxFileConfig

Post by sfx81 »

Hi. In my app, I have option to save different files. Now to do that every time I create a wxFileConfig object, write to the file, and destory that object something like this

Code: Select all

wxFileConfig* mConfig = new wxFileConfig("","", dir + "\\" +fileName + extension);
...set path in config file
...write values
mConfig->Flush();
delete mConfig;
I was wondering if there is any other way so that I do not have to create and destroy object every time. Just crete an object at startup, and use it throughout the life cycle to create different files e.g C:\file1.txt, D:file2.txt, and destory it at end.

Any help will be appreciated.
Regards Kazz
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

sfx81
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sun Sep 28, 2008 12:11 pm

Post by sfx81 »

if i have mConfig pointing to "c:\file1.txt", then
mConfig->DeleteAll(), will delete all the configuration. Thats what I want. But now after deleting it, how can I create another file with name "d:\file2.txt"?
Effectively what I am after is how can we change the files at run time using wxConfig.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

Hi,
Then why do you need to use wxFileConfig for that? Are those files will keep the program configuration or some data that the program will use?

wxFileConfig works the same way as a registry. It saves data that your program will read at startup and use it during the run (one good example is persistent control - if you open a window/dialog in some position with specific size, you can store this info, so next time it will be open with the same position/size).

Maybe you can tell me what you are trying to accomplish, but deleting and recreating the wxFileConfig file storage shouldn't be neccessary.

Thank you.
sfx81
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sun Sep 28, 2008 12:11 pm

Post by sfx81 »

I want exactly what you said in last post. But rather than automatically loading and saving, user has the option to load and save specific configuration.
Here is how I have implemented, so that you can point out where I went wrong.
User Saves a file by using dir selector to locate target destination "C:\Dir1", and this creates a fileConfig object e.g

Code: Select all

wxFileConfig("","","C:\Dir1\File1.txt")
object for that path. But next time when he saves to "C:\DirNew", I delete the existing fileConfig object and create a new one e.g

Code: Select all

 wxFileConfig("","","C:\DirNew\File1.txt")
So infact there should be some way of changing the filepath which I have to specify in ctor of wxFileConfig.
Regards
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Post by ONEEYEMAN »

Hi,
Well, that's kind of strange design. Program configuration/user configuration should stay in one file (or 2 files).

Why do you need to re-create it every single time? This stuff should be hidden from user. He shouldn't even know the program saves it's configuration somewhere.

Thank you.
sfx81
Knows some wx things
Knows some wx things
Posts: 45
Joined: Sun Sep 28, 2008 12:11 pm

Post by sfx81 »

Hi, I think I am on wrong track here. Thankyou for all the information, it was very helpful.
Regards.
ngpaton
Knows some wx things
Knows some wx things
Posts: 25
Joined: Tue May 20, 2008 8:23 am

Post by ngpaton »

Hi,

I can understand exactly the problem that sfx81 is having. I'm having the same problem. The wxFileConfig/wxBaseConfig classes do not cope with multiple configs very well.

I have an application that can have several different configurations and can import configurations from other users. It is a data visualisation tool and depending on the data being collected the user might want to have different ways of presenting that data. So if they are performing task A they might use config A whereas if they are performing task B they will use a different config.

An example problem is loading a config modifying it then saving it to a different file. You can do this using wxFileConfig::Save() but it would be nicer if you can just change the local or global file associated with the wxFileConfig object.

Another problem is if the user makes changes but then decides they don't want to save them to the config. There is no built in way of preventing wx from writing the config. One workaround when using wxFileConfig is again to use Save to dump the config to a stream you can then throw away (this clears the internal dirty flag and prevents the write on delete).

Also a shame the wxFileConfig::IsDirty() method is not public as you can't tell if the config has changed. Can be useful if you want to prompt the user if they want to save the changed config or not.

It really looks like these classes have been designed with one particular way of using config files in mind rather than being a bit more generic.

You can probably add some of this capability by creating a new class with wxFileConfig as the base class. Although not as easily as you might think as the file name data and dirty flag are not accessible. The Flush method is virtual so you could hide stuff in here to save it to a given file stream and just ignore the normal Flush.

Cheers

Nigel
Post Reply