Jump to content

  • Log In with Google      Sign In   
  • Create Account


Using INI files


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Dimension10   Members   -  Reputation: 123

Like
0Likes
Like

Posted 13 June 2013 - 10:42 PM

So I'm working with DirectX, and I'm wanting to have most of my main configuration constants stored in an .ini file.

 

I used this method originally:

http://www.codeproject.com/Articles/10809/A-Small-Class-to-Read-INI-File

 

I combined the two classes into a single class with a single header and .cpp file, with a couple of my own tweaks. Worked like a charm! It's nice beting able to choose file/section/key/value, and declare each file as its own object.

 

 

However, when trying to implement this into windows programs, it seems to work differently.

 

 

In the case here:

 

GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, m_szFileName); 

 

I get errors because it's trying to pass char* pointers, but I guess the winAPI version looks for an LPCTSTR instead. 

 

So if I change all char* to LPCTSTR, then memset and memcpy have issues, since they are looking for a char*

 

 

 

 

 

All that aside, is there an easy method for working with .ini files? I know they're considered outdated, but it's used heavily in the Unreal Engines, and I prefer that method of configuration over using the registry as M$ recommends.



Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 3764

Like
0Likes
Like

Posted 14 June 2013 - 01:01 AM

Microsoft does not recommend using the registry and hasn't for a very long time now.

 

You can try calling GetPrivateProfileStringA directly if it exists, but it would be much cleaner to sort out how your inconsistent attitudes towards unicode in the build settings.



#3 dmatter   Crossbones+   -  Reputation: 2998

Like
0Likes
Like

Posted 14 June 2013 - 04:33 AM

You could take a look at Boost ptree, which has support for INI, XML, JSON and something they call INFO: http://www.boost.org/doc/libs/1_41_0/doc/html/property_tree.html



#4 chowarth   Members   -  Reputation: 516

Like
0Likes
Like

Posted 14 June 2013 - 04:53 AM

I used the same guide myself when creating mine. Here's my version of getting a string value, if it helps:

string CConfig::ReadStringValue( const char* section, const char* key, const char* defaultVal )
{
	char* temp = new char[255];

	GetPrivateProfileString( section, key, defaultVal, temp, 255, mFilename );

	string value = temp;
	delete temp;

	return value;
}

But as BitMaster has already said, it would be better if you correct your project settings if you want to use the multi-byte version of GetPrivateProfileString.


Ramblings: www.chowarth.co.uk

#5 NightCreature83   Crossbones+   -  Reputation: 2685

Like
2Likes
Like

Posted 14 June 2013 - 05:23 AM

I used the same guide myself when creating mine. Here's my version of getting a string value, if it helps:

string CConfig::ReadStringValue( const char* section, const char* key, const char* defaultVal )
{
	char* temp = new char[255];

	GetPrivateProfileString( section, key, defaultVal, temp, 255, mFilename );

	string value = temp;
	delete temp;

	return value;
}

But as BitMaster has already said, it would be better if you correct your project settings if you want to use the multi-byte version of GetPrivateProfileString.

There is no need to use a new in that code char temp[255]; will achieve the same thing and then passing &temp[0] to the function, it avoids you having to remember to delete your dynamic memory.


Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, Mad Max

#6 BitMaster   Crossbones+   -  Reputation: 3764

Like
2Likes
Like

Posted 14 June 2013 - 05:29 AM

Also, using plain delete after new[] is undefined behavior. Sadly it generally does not cause problems on MSVC but will as soon as you switch compilers, platforms or maybe next version.



#7 Kaptein   Prime Members   -  Reputation: 1945

Like
0Likes
Like

Posted 14 June 2013 - 09:35 AM

XML can be a nice solution, but if you just need regular settings like screen.width = 100, then you want a good old-fashioned .ini file that is human readable and has nice ASCII art smile.png

Just look at unreal, at least back when i tinkered with it. If you want a hierarchy, then the . character is that hierarchy:

##################

#        Rendering       #

##################

renderer.width         =   100

renderer.height        =   200   # some comment

renderer.fullscreen  =  true

 

Then create a static config:: system that either just treats the variables as plain text, which is just fine, or relates to specific variables, depending on your needs smile.png

XML is fine too, if you have a configuration tool for your game/application. I admit it's very easy today to make that kind of tool in .NET, and it might be a huge timesaver, depending on how familiar you are with the .NET framework.

 

If you want a solution in C++, here is one of mine:

Config.hpp

#ifndef __CONFIG_HPP
#define __CONFIG_HPP

#include <map>
#include <utility>
#include <string>

namespace platformer
{
	class Config
	{
		static std::map<std::string, std::string> kv;
		
	public:
		static bool get(const std::string, const bool);
		static int get(const std::string, const int);
		static float get(const std::string, const float);
		static std::string get(const std::string, const std::string);

		//Changing the value in the key map.
		template <typename T>
		static void set( std::string v, T b )
		{
			if ( Config::kv.find(v) == Config::kv.end()) 
				return;
			
			kv[v] = b;
		}
		static bool load( std::string file );
	};
}

#endif

Config.cpp

#include "config.hpp"
#include <fstream>
#include <iostream>
#include <sstream>

namespace platformer
{	
	std::map<std::string, std::string> Config::kv;
	
	//Gets a bool value from map
	bool Config::get(const std::string v, const bool def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		bool b;
		std::istringstream convert( kv[v] );
		if ( !(convert >> b) )
			return def;
		return b;
	}
	
	//Gets an int value from map
	int Config::get(const std::string v, const int def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		int i;
		std::istringstream convert( kv[v] );
		if ( !(convert >> i) )
			return def;
		return i;
	}
	
	//Gets a float value from map
	float Config::get(const std::string v, const float def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		float f;
		std::istringstream convert( kv[v] );
		if ( !(convert >> f) )
			return def;
		return f;
	}
	//Gets a string value from map
	std::string Config::get(const std::string v, const std::string def)
	{
		if ( Config::kv.find(v) == Config::kv.end()) 
			return def;
		return kv[v];
	}
	//Loading the config file into the keymap.
	bool Config::load( std::string f )
	{
		std::fstream filestream;
		filestream.open( f.c_str(), std::fstream::in );
		
		if (!filestream) return false; // could not open file
		
		//Reads one line in config file at a time and place it in the map.
		while( !filestream.eof() )
		{
			char line[256];
			filestream.getline( line, 256 );
			std::string test( line );
			std::string en;
			std::string to;
			bool temp = true;
			//en is the value name and to is the value.
			for ( std::string::iterator it=test.begin(); it!=test.end(); ++it)
			{
				if( *it == '#' ) 
					break;
				if( *it == '=' )
					temp = false;
				if( *it != '=' && *it != ' ' )
				{
					if(temp)
						en += *it;
					else
						to += *it;
				}
			}
			if( en.length() > 0)
				kv.insert( std::pair<std::string,std::string>(en,to) );
		}
		filestream.close();
		return true;
	}

}

It's not very efficient, but it doesn't have to be!


Edited by Kaptein, 14 June 2013 - 09:43 AM.


#8 chowarth   Members   -  Reputation: 516

Like
1Likes
Like

Posted 15 June 2013 - 06:46 AM

There is no need to use a new in that code char temp[255]; will achieve the same thing and then passing &temp[0] to the function, it avoids you having to remember to delete your dynamic memory.

 

 

Also, using plain delete after new[] is undefined behavior. Sadly it generally does not cause problems on MSVC but will as soon as you switch compilers, platforms or maybe next version.

 

Two very good points that I had missed, thanks :)


Ramblings: www.chowarth.co.uk




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS