Best: wxFileConfig encoding, wxString UTF8 C-strings

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
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by Mick P. »

Sorry, does anybody know if wxFileConfig can be forced to use UTF-8 (assuming it uses system locale) and if APIs accepts C-strings, then is it possible to avoid wxString::FromUTF8 by communicating that C-strings are UTF8? If so, how? If not, C-strings are not so useful I guess.

I suppose any Unicode format is acceptable. I'm worried about loss on the config file front.

Also (sorry) is there a systemwide switch (similar to locale) to control the format of file paths? Like to request paths as URLs or to not use backslashes?
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by doublemax »

does anybody know if wxFileConfig can be forced to use UTF-8 (assuming it uses system locale)
Pass wxConvUTF8 as "conv" parameter to its ctor.
if APIs accepts C-strings
No. It only takes wxString as parameters. If you were to pass a C string if would be implicitly converted to wxString using locale encoding.
is there a systemwide switch (similar to locale) to control the format of file paths? Like to request paths as URLs or to not use backslashes?
No. As paths are usually not shared across platforms, that should not be an issue though. What exactly is the problem? wxFileName has a few methods that should help to build paths for the current platform, e.g. wxFileName::GetPathSeparator()
Use the source, Luke!
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by Mick P. »

Thanks. I was hoping for a tip on how to override the locale. On the other point, it's just simpler to work with standard paths, or URLs like I say, that are standard.
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by Mick P. »

Off-topic: I think SetRecordDefaults is not working. I dunno. The INI file only has values that are modified by Set APIs.

Code: Select all

const wxString &_ = wxEmptyString;
	if(i==cfg.size())
	cfg.push_back(std::make_pair(name,new wxFileConfig(name,_,_,_,wxCONFIG_USE_LOCAL_FILE,wxConvUTF8)));
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by doublemax »

I think SetRecordDefaults is not working. I dunno. The INI file only has values that are modified by Set APIs.
I don't see how this comment is related to the code you posted. Can you elaborate?
Use the source, Luke!
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by Mick P. »

doublemax wrote: Fri Sep 06, 2019 7:26 am
I think SetRecordDefaults is not working. I dunno. The INI file only has values that are modified by Set APIs.
I don't see how this comment is related to the code you posted. Can you elaborate?
SetRecordDefaults is a method of wxFileConfig's. I don't want to make a post about this, but thought someone might see it and take mercy on me.

It seems to be a feature that is unimplemented.
User avatar
doublemax
Moderator
Moderator
Posts: 19114
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by doublemax »

It seems to be a feature that is unimplemented.
Looking into the sources, i'd say it is implemented. But maybe this method does not what you think it does.

What it does is: SetRecordDefaults() just sets an internal flag. If you make a wxConfig::Read() call and the record does not exist and this flag is set, the record will be created with the default value passed to the Read() call.
Use the source, Luke!
Mick P.
Earned some good credits
Earned some good credits
Posts: 145
Joined: Thu Jun 06, 2019 3:41 am
Contact:

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by Mick P. »

doublemax wrote: Fri Sep 06, 2019 12:26 pm
It seems to be a feature that is unimplemented.
Looking into the sources, i'd say it is implemented. But maybe this method does not what you think it does.

What it does is: SetRecordDefaults() just sets an internal flag. If you make a wxConfig::Read() call and the record does not exist and this flag is set, the record will be created with the default value passed to the Read() call.
I actually have two INI files, and I just noticed the second is working as expected. As near as I tell it is likely not implemented for most implementations of Read. There are several overloads, maybe 10s. The ones that deal with binary data types don't seem to work with this feature. The secondary INI file saves hotkeys, and always uses a string version of Read. That seems to be the only version that works (with SetRecordDefaults) that I'm using.

Code: Select all

	void configure::_get(const char *key, void *val, int lt)
	{
		wxFileConfig *s = xcv_cfg(name).second; switch(lt)
		{
		default: assert(0); break;
		case ui_live_int: s->Read(key,(int*)val); break;
		case ui_live_float: s->Read(key,(float*)val); break;
		case ui_live_double: s->Read(key,(double*)val); break;
		
		case 0: //case ui_live_text: 
			
			//TODO: Is FromUTF8 required in this case?
			//https://forums.wxwidgets.org/viewtopic.php?f=1&t=46272&p=193659
			temporary = s->Read(key,wxString::FromUTF8((char*)val)).ToUTF8(); 
			break;

		case ui_live_char: 
			const char buf[2] = {*(char*)val}; //const is required.
			*(char*)val = (char)s->Read(key,buf)[0]; break;
		}	
	}
The others are using the pointer forms, for simplicity. There are also overloads that take a non-pointer default and return the same type. From what I can tell these pointer forms appear to work, but don't record the defaults.

EDITED: If I had to guess (in hindsight) I suppose the semantics of these is that the value pointed to is only to receive the read value, and so it's not considered a default. I went to look them up in the code, with reverse search, and I see there are versions that accept a default in addition to the pointer. Kind of a mess.
ONEEYEMAN
Part Of The Furniture
Part Of The Furniture
Posts: 7459
Joined: Sat Apr 16, 2005 7:22 am
Location: USA, Ukraine

Re: Best: wxFileConfig encoding, wxString UTF8 C-strings

Post by ONEEYEMAN »

Hi,
Did you post the code that is working, ro the one you think it does not?
Also, what type of data do you save in the "non-working" INI file? The fact that you store pointer to some data doesn't help much...

Thank you.
Post Reply