Jump to content

Recommended Posts

Posted

 

 

EasyConfig C++ Class

 

 

Config files such as *.ini You'll see it's not hard at all to use it.

Please note that there are 2 constructors, an empty one that doesn't require any argument and another one that takes the file name.

If you declare an object without any file name, you'll have to use the SetFileName method.

 

Here's an exemple:

(

cpp):
#include "stdafx.h"

using namespace std;

int main(int argc, char* argv[])
{
ConfigFile myIni;

myIni.SetFileName("C:\\Hax.txt");

cout << myIni.ReadStringValue("LOL", "value1").c_str() << endl;

    cout << "Writing int into value2..." << endl;

    myIni.WriteValue("LOL", "value2", 1337);
    cout << myIni.ReadIntValue("LOL", "value2") << endl;

    cout << "Writing float into value3..." << endl;

    myIni.WriteValue("LOL", "value3", 3.14f);
    cout << myIni.ReadFloatValue("LOL", "value3") << endl;

    return 0;
}

 

 

 

WriteValue is an overloaded function that works with float, int and strings which is actually what I needed.

The code launches a ConfigFileException when shit happens.

 

For exemple:

 

(cpp):
ConfigFile myIni;

cout << myIni.ReadStringValue("LOL", "value1").c_str() << endl;

 

 

In this code, we didn't set a file name. The code will launch an exception that contains,

The file name (In this case, it's ) And the error description To avoid unhandled...

Exceptions:

(cpp):
ConfigFile myIni;

try
{
cout << myIni.ReadStringValue("LOL", "value1").c_str() << endl;
}
catch( ConfigFileException e )
{
cout << "*** EXCEPTION CAUGHT ***"<< endl;
cout << e.fileName << endl;
cout << e.errMessage<< endl;
}

 

 

 

I added the exceptions stuff today, so if you find anything i could add/correct, just say And now, the file you're all waiting for:

EasyCfg.cpp:

(cpp):
#pragma once

#define FILE_NOT_EXISTING        "The file you tried to open doesn't exist"
#define FILENAME_NOT_DEFINED    "You didn't declare a file name"
#define NO_FILE_NAME            "<no File Name>"

class ConfigFileException
{
public:
std::string errMessage;
std::string fileName;

ConfigFileException( std::string tmpErrMessage, std::string tmpFileName )
{
	fileName = tmpFileName;
	errMessage = tmpErrMessage;
}
};

class ConfigFile
{
std::string FileName;

public:
////////////////////////////////////
ConfigFile( std::string strFile ) {
	this->FileName = strFile;

	if(!FileExists())
	{
	ConfigFileException e(FILE_NOT_EXISTING, this->FileName);

	throw e;
	}
}

ConfigFile( ) {
}

~ConfigFile( ) {
}
////////////////////////////////////


////////////////////////////////////
void SetFileName( std::string newFileName )
{
	this->FileName = newFileName;

	if(!FileExists())
	{
		ConfigFileException e(FILE_NOT_EXISTING, this->FileName);

		throw e;
	}
}

std::string GetFileName()
{
	if(this->FileName.size() < 1 )
	{
		ConfigFileException e(FILENAME_NOT_DEFINED, NO_FILE_NAME);

		throw e;    

		return false;
	}

	return this->FileName;
}

bool FileExists() //If no file specified, just check if the FileName file exists
{
	if(this->FileName.size() < 1 )
	{
		ConfigFileException e(FILENAME_NOT_DEFINED, NO_FILE_NAME);

		throw e;    

		return false;
	}

	bool retVal = false;
	std::fstream tmpFile;

	tmpFile.open(this->FileName.c_str(), std::ios::in);

	if( tmpFile.is_open() )
		retVal = true;

	tmpFile.close();

	return retVal;
}

bool FileExists( std::string fileName )
{
	bool retVal = false;
	std::fstream tmpFile;

	tmpFile.open(fileName.c_str(), std::ios::in);

	if( tmpFile.is_open() )
		retVal = true;

	tmpFile.close();

	return retVal;
}

int ReadIntValue( std::string section, std::string option )
{
	if(!FileExists())
	{
		ConfigFileException e(FILE_NOT_EXISTING, this->FileName);

		throw e; //Have fun
	}

	return GetPrivateProfileIntA(section.c_str(), option.c_str(), -1, this->FileName.c_str());
}

float ReadFloatValue( std::string section, std::string option )
{
	if(!FileExists())
	{
		ConfigFileException e(FILE_NOT_EXISTING, this->FileName);

		throw e; //Have fun
	}

	char tmpStr[16] = {'\0'};

	GetPrivateProfileStringA(section.c_str(), option.c_str(), NULL, tmpStr, 15, this->FileName.c_str());

	return (float)atof(tmpStr);
}

std::string ReadStringValue( std::string section, std::string option )
{
	if(!FileExists())
	{
		ConfigFileException e(FILE_NOT_EXISTING, this->FileName);

		throw e;
	}

	char tmpStr[256] = {'\0'};

	GetPrivateProfileStringA(section.c_str(), option.c_str(), NULL, tmpStr, 255, this->FileName.c_str());

	return std::string(tmpStr);
}

void WriteValue( std::string section, std::string option, std::string val )
{
	if(this->FileName.size() < 1 )
	{
		ConfigFileException e(FILENAME_NOT_DEFINED, NO_FILE_NAME);

		throw e;
	}

	WritePrivateProfileStringA(section.c_str(), option.c_str(), val.c_str(), this->FileName.c_str());
}

void WriteValue( std::string section, std::string option, int val )
{
	if(this->FileName.size() < 1 )
	{
		ConfigFileException e(FILENAME_NOT_DEFINED, NO_FILE_NAME);

		throw e;    
	}

	char tmpResult[10] = {'\0'};
	_itoa_s (val, tmpResult, 9, 10);
	WritePrivateProfileStringA(section.c_str(), option.c_str(), tmpResult, this->FileName.c_str());
}

void WriteValue( std::string section, std::string option, float val )
{
	if(this->FileName.size() < 1 )
	{
		ConfigFileException e(FILENAME_NOT_DEFINED, NO_FILE_NAME);

		throw e;    
	}

	char tmpResult[16] = {'\0'};
	sprintf(tmpResult, "%f", val);
	WritePrivateProfileStringA(section.c_str(), option.c_str(), tmpResult, this->FileName.c_str());
}
};

 

"Download" EasyCfg.cpp

 

 

by Lightness

  • 3 weeks later...
  • 2 months later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now


  • Posts

    • Hello everyone, This topic has been created to report any content that is considered illegal under applicable law or in violation of the forum rules. This includes, but is not limited to: Illegal software (pirated, cracked, or unauthorized software) Copyright-infringing material Malware, viruses, or any harmful code Scams, fraud, phishing attempts, or impersonation Illegal banking or financial services Money laundering activities or related instructions Any other illegal, unethical, or rule-violating activity — you name it If you encounter any such content, please report it here so it can be reviewed and removed promptly. Legal Disclaimer All content published on this forum is created and posted by its users. The forum administration does not take responsibility for user-generated content. However, we make every reasonable effort to monitor, review, remove, and maintain the forum by deleting illegal or rule-violating content as soon as it is reported or identified. By using this forum, you acknowledge and agree to these terms.     Moderator Notice We would like to inform all users that we are currently developing a custom AI-powered API tool that will assist our moderation team in scanning the forum database for illegal or rule-violating activity. This system will be used strictly as a support tool to help identify potentially problematic content, which will then be reviewed by human moderators before any action is taken. The goal is to improve forum safety, compliance, and response time while maintaining fairness and transparency. 🚧 Coming soon — more details will be shared once the system is ready. Thank you for your cooperation and for helping us keep the forum clean and lawful.
    • Reporting Illegal or Rule-Violating Content (post here)
    • If anyone is reading this, until we find the 488 protocol system that works with AuthD + Authgated from High Five (HF), just use L2Filter included in the leak as the login server to be able to play. hAuthd does not works with this chronicle, @Hint. do you happen to know about this?   Be aware of the system you use! If UseEMailAccount is set as true in l2.ini, your user_info table linked to your account must have email column not NULL with a valid e-mail, and you will use that e-mail to login instead of the account name. (Post merged)   OKAY! Nevermind about the above, I just figured it out, AuthD and Authgated from HF requires GameGuard to be enabled and UseEMailAccount set to false. Enable GameGuard at server side as per Fyyre instructions,  Add the missing GameGuard files in your client system: https://mega.nz/file/xZMWQBjK#triEj7My9B9roiLqAKk32HOLcLmPynoOm-QhBI_Ligw
    • Πολυ δυνατος σερβερ παιδια. Συνεχιστε την καλη σας προσπαθεια και παμε γερα
  • Topics

×
×
  • Create New...

AdBlock Extension Detected!

Our website is made possible by displaying online advertisements to our members.

Please disable AdBlock browser extension first, to be able to use our community.

I've Disabled AdBlock