wxFileConfig question

49 views
Skip to first unread message

Igor Korot

unread,
Oct 26, 2012, 6:20:51 PM10/26/12
to wx-u...@googlegroups.com
Hi, ALL,
I am trying to write an application that uses wxFileConfig on both
Windows and Linux.
I made the test file called "test.ini" and placed it in the appropriate place.

test.ini:

[Device1]
URL=www.google.com
Port=10001

[Device2]
URL=www.google.com
Port=10002

Now in the code here is what I do:

wxFileConfig *config = new wxFileConfig( "test.ini" );
wxString oldPath = config->GetPath();
wxString path = "Device";
wxString address;
int port;
devicenum = config->GetNumberOfGroups();
for( size_t i = 1; i <= devicenum; i++ )
{
config->SetPath( path << i );
if( !config->Read( "Port", &port ) )
{
printf( "There is a problem with configuration file.\r\n" );
printf( "No port supplied for the device #%d", i );
delete config;
return -1;
}
if( config->Read( "Address", &address ) )
{
devices.push_back( new Device( address, port, false ) );
}
else if( config->Read( "URL", &address ) )
{
devices.push_back( new Device( address, port, true ) );
}
else
{
printf( "There is a problem with configuration file.\r\n" );
printf( "No address/URL supplied for the device #%d", i );
delete config;
return -1;
}
path = "Device";
}
config->SetPath( oldPath );

The problem is that the loop succeeds on the first iteration for the
"[Device1]", but fails to read
the data on the second iteration for "[Device2]".

However if I change the code to read:

for( size_t i = 1; i <= devicenum; i++ )
{
wxString test = wxString::Format( "Device%d", i );
if( !config->Read( test + "/Port", &port ) )
{
printf( "There is a problem with configuration file.\r\n" );
printf( "No port supplied for the device #%d", i );
delete config;
return -1;
}
if( config->Read( test + "/Address", &address ) )
{
devices.push_back( new Device( address, port, false ) );
}
else if( config->Read( test + "/URL", &address ) )
{
devices.push_back( new Device( address, port, true ) );
}
}

I can read both "Device1" and "Device2" parts.
Is this a known problem?

I'm currently on Windows (7) with wx-2.9.4 compiled with MSVC-2010.

Thank you.

Eric Jensen

unread,
Oct 26, 2012, 7:10:50 PM10/26/12
to Igor Korot
Hello Igor,

Saturday, October 27, 2012, 12:20:51 AM, you wrote:

IK> Hi, ALL,
IK> I am trying to write an application that uses wxFileConfig on both
IK> Windows and Linux.
IK> I made the test file called "test.ini" and placed it in the appropriate place.

IK> test.ini:

IK> [Device1]
IK> URL=www.google.com
IK> Port=10001

IK> [Device2]
IK> URL=www.google.com
IK> Port=10002

IK> Now in the code here is what I do:

IK> wxFileConfig *config = new wxFileConfig( "test.ini" );
IK> wxString oldPath = config->GetPath();
IK> wxString path = "Device";
IK> wxString address;
IK> int port;
devicenum = config->>GetNumberOfGroups();
IK> for( size_t i = 1; i <= devicenum; i++ )
IK> {
config->>SetPath( path << i );
if( !config->>Read( "Port", &port ) )
IK> {
IK> printf( "There is a problem with configuration file.\r\n" );
IK> printf( "No port supplied for the device #%d", i );
IK> delete config;
IK> return -1;
IK> }
if( config->>Read( "Address", &address ) )
IK> {
IK> devices.push_back( new Device( address, port, false ) );
IK> }
else if( config->>Read( "URL", &address ) )
IK> {
IK> devices.push_back( new Device( address, port, true ) );
IK> }
IK> else
IK> {
IK> printf( "There is a problem with configuration file.\r\n" );
IK> printf( "No address/URL supplied for the device #%d", i );
IK> delete config;
IK> return -1;
IK> }
IK> path = "Device";
IK> }
config->>SetPath( oldPath );

IK> The problem is that the loop succeeds on the first iteration for the
IK> "[Device1]", but fails to read
IK> the data on the second iteration for "[Device2]".

IK> However if I change the code to read:

IK> for( size_t i = 1; i <= devicenum; i++ )
IK> {
IK> wxString test = wxString::Format( "Device%d", i );
if( !config->>Read( test + "/Port", &port ) )
IK> {
IK> printf( "There is a problem with configuration file.\r\n" );
IK> printf( "No port supplied for the device #%d", i );
IK> delete config;
IK> return -1;
IK> }
if( config->>Read( test + "/Address", &address ) )
IK> {
IK> devices.push_back( new Device( address, port, false ) );
IK> }
else if( config->>Read( test + "/URL", &address ) )
IK> {
IK> devices.push_back( new Device( address, port, true ) );
IK> }
IK> }

IK> I can read both "Device1" and "Device2" parts.
IK> Is this a known problem?

IK> I'm currently on Windows (7) with wx-2.9.4 compiled with MSVC-2010.


Put a "wxLogMessage( path );" after the following line. You should notice
something:
config->SetPath( path << i );

Welcome to the wonderful world for printf-like debugging if you can't use
a real debugger.

Eric


Reply all
Reply to author
Forward
0 new messages