You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.2 KiB
153 lines
3.2 KiB
#include "basicSettingsProvider.h"
|
|
|
|
#ifdef WIN32
|
|
#ifdef DEBUG
|
|
#pragma comment(lib, "libconfigd.lib")
|
|
#else
|
|
#pragma comment(lib, "libconfig.lib")
|
|
#endif
|
|
#endif
|
|
|
|
#define LOG(X) m_pLog->Log("basicSettingsProvider", this, X)
|
|
#define LOG_ERROR(X) m_pLog->Log("basicSettingsProvider", this, _LOG_ERROR, X)
|
|
|
|
// apt-get install libconfig++8-dev
|
|
// https://github.com/jterrace/craq/blob/master/libconfig-1.4.2/examples/c/
|
|
|
|
basicSettingsProvider * basicSettingsProvider::_instance = NULL;
|
|
|
|
/**
|
|
* Returns the single instance of the object.
|
|
*/
|
|
basicSettingsProvider * basicSettingsProvider::getInstance(CSTR configName)
|
|
{
|
|
// check if the instance has been created yet
|
|
if (0 == _instance)
|
|
{
|
|
// if not, then create it
|
|
_instance = new basicSettingsProvider(configName);
|
|
}
|
|
// return the single instance
|
|
return _instance;
|
|
}
|
|
|
|
// Constructor implementation
|
|
basicSettingsProvider::basicSettingsProvider(CSTR configName)
|
|
{
|
|
m_pLog = basicLogCollection::getInstance();
|
|
if (configName != NULL)
|
|
{
|
|
strncpy(m_pFileName, configName, MAX_PATH);
|
|
}
|
|
else
|
|
{
|
|
strncpy(m_pFileName, DEFAULT_SETTINGS_FILE_NAME, MAX_PATH);
|
|
};
|
|
config_init(&m_sCfg);
|
|
m_bLoadded = load();
|
|
}
|
|
|
|
basicSettingsProvider::~basicSettingsProvider()
|
|
{
|
|
if (m_bLoadded)
|
|
{
|
|
save();
|
|
clear();
|
|
};
|
|
config_destroy(&m_sCfg);
|
|
}
|
|
|
|
bool basicSettingsProvider::load(void)
|
|
{
|
|
m_bModified = false;
|
|
if (fileExist(m_pFileName))
|
|
{
|
|
if (! config_read_file(&m_sCfg, m_pFileName))
|
|
{
|
|
// fprintf( stderr, "%s:%d - %s\n", config_error_file( &m_sCfg ), config_error_line( &m_sCfg ), config_error_text( &m_sCfg ) );
|
|
// printf( "%s:%d - %s\n", config_error_file( &m_sCfg ), config_error_line( &m_sCfg ), config_error_text( &m_sCfg ) );
|
|
LOG_ERROR("Error Parsing Config File");
|
|
m_pRootNode = config_root_setting(&m_sCfg);
|
|
return false;
|
|
};
|
|
m_pRootNode = config_root_setting(&m_sCfg);
|
|
}
|
|
else
|
|
{
|
|
m_pRootNode = config_root_setting(&m_sCfg);
|
|
if (! config_write_file(&m_sCfg, m_pFileName))
|
|
{
|
|
LOG_ERROR("Error Write Config File");
|
|
return false;
|
|
};
|
|
return true;
|
|
};
|
|
return true;
|
|
}
|
|
|
|
config_setting_t * basicSettingsProvider::getSubNode(config_setting_t * Parent, CSTR NodeName)
|
|
{
|
|
config_setting_t * mpSubNode;
|
|
if (! m_bLoadded)
|
|
{
|
|
return NULL;
|
|
}
|
|
if (Parent == NULL)
|
|
{
|
|
Parent = m_pRootNode;
|
|
}
|
|
mpSubNode = config_setting_get_member(Parent, NodeName);
|
|
if (! mpSubNode)
|
|
{
|
|
mpSubNode = config_setting_add(Parent, NodeName, CONFIG_TYPE_GROUP);
|
|
m_bModified = true;
|
|
};
|
|
return mpSubNode;
|
|
}
|
|
|
|
bool basicSettingsProvider::clear(void)
|
|
{
|
|
config_destroy(&m_sCfg);
|
|
config_init(&m_sCfg);
|
|
m_bModified = false;
|
|
m_bLoadded = false;
|
|
return true;
|
|
}
|
|
|
|
bool basicSettingsProvider::saveTo(CSTR fileName)
|
|
{
|
|
strncpy(m_pFileName, fileName, MAX_PATH);
|
|
if (! config_write_file(&m_sCfg, m_pFileName))
|
|
{
|
|
LOG_ERROR("Error while writing file");
|
|
clear();
|
|
return false;
|
|
};
|
|
m_bModified = false;
|
|
return true;
|
|
}
|
|
|
|
bool basicSettingsProvider::save(void)
|
|
{
|
|
if (! config_write_file(&m_sCfg, m_pFileName))
|
|
{
|
|
LOG_ERROR("Error while writing file");
|
|
clear();
|
|
return false;
|
|
};
|
|
m_bModified = false;
|
|
return true;
|
|
}
|
|
|
|
bool basicSettingsProvider::validate(void)
|
|
{
|
|
if (m_bModified)
|
|
{
|
|
save();
|
|
return true;
|
|
};
|
|
return false;
|
|
}
|
|
|
|
#undef LOG
|
|
#undef LOG_LVL
|