2013-12-19 17:10:14 +00:00
|
|
|
/*
|
|
|
|
Config file crap
|
|
|
|
Supports various things, as virtual config entries and such crap
|
2013-12-24 00:56:44 +00:00
|
|
|
Works surprisingly well considering how old it is ...
|
2013-12-19 17:10:14 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define _CRT_SECURE_NO_DEPRECATE (1)
|
2015-08-24 12:45:52 +00:00
|
|
|
#include <errno.h>
|
2013-12-19 17:10:14 +00:00
|
|
|
#include "cfg.h"
|
2015-08-16 21:35:13 +00:00
|
|
|
#include "ini.h"
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
string cfgPath;
|
2015-08-24 12:45:52 +00:00
|
|
|
bool save_config = true;
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
ConfigFile cfgdb;
|
|
|
|
|
|
|
|
void savecfgf()
|
|
|
|
{
|
|
|
|
FILE* cfgfile = fopen(cfgPath.c_str(),"wt");
|
|
|
|
if (!cfgfile)
|
2015-08-24 12:46:45 +00:00
|
|
|
{
|
2013-12-19 17:10:14 +00:00
|
|
|
printf("Error : Unable to open file for saving \n");
|
2015-08-24 12:46:45 +00:00
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
else
|
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
cfgdb.save(cfgfile);
|
2013-12-19 17:10:14 +00:00
|
|
|
fclose(cfgfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void cfgSaveStr(const wchar * Section, const wchar * Key, const wchar * String)
|
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
cfgdb.set(string(Section), string(Key), string(String));
|
2015-08-24 12:45:52 +00:00
|
|
|
if(save_config)
|
|
|
|
{
|
|
|
|
savecfgf();
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
//WritePrivateProfileString(Section,Key,String,cfgPath);
|
|
|
|
}
|
|
|
|
//New config code
|
|
|
|
|
|
|
|
/*
|
2013-12-24 00:56:44 +00:00
|
|
|
I want config to be really flexible .. so , here is the new implementation :
|
2015-08-18 17:36:19 +00:00
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
Functions :
|
2013-12-24 00:56:44 +00:00
|
|
|
cfgLoadInt : Load an int , if it does not exist save the default value to it and return it
|
|
|
|
cfgSaveInt : Save an int
|
|
|
|
cfgLoadStr : Load a str , if it does not exist save the default value to it and return it
|
|
|
|
cfgSaveStr : Save a str
|
|
|
|
cfgExists : Returns true if the Section:Key exists. If Key is null , it retuns true if Section exists
|
2013-12-19 17:10:14 +00:00
|
|
|
|
2013-12-24 00:56:44 +00:00
|
|
|
Config parameters can be read from the config file , and can be given at the command line
|
2013-12-19 17:10:14 +00:00
|
|
|
-cfg section:key=value -> defines a value at command line
|
|
|
|
If a cfgSave* is made on a value defined by command line , then the command line value is replaced by it
|
|
|
|
|
2013-12-24 00:56:44 +00:00
|
|
|
cfg values set by command line are not written to the cfg file , unless a cfgSave* is used
|
2013-12-19 17:10:14 +00:00
|
|
|
|
|
|
|
There are some special values , all of em are on the emu namespace :)
|
|
|
|
|
|
|
|
These are readonly :
|
|
|
|
|
|
|
|
emu:AppPath : Returns the path where the emulator is stored
|
|
|
|
emu:PluginPath : Returns the path where the plugins are loaded from
|
|
|
|
emu:DataPath : Returns the path where the bios/data files are
|
|
|
|
|
|
|
|
emu:FullName : str,returns the emulator's name + version string (ex."nullDC v1.0.0 Private Beta 2 built on {datetime}")
|
|
|
|
emu:ShortName : str,returns the emulator's name + version string , short form (ex."nullDC 1.0.0pb2")
|
|
|
|
emu:Name : str,returns the emulator's name (ex."nullDC")
|
|
|
|
|
|
|
|
These are read/write
|
|
|
|
emu:Caption : str , get/set the window caption
|
|
|
|
*/
|
|
|
|
|
|
|
|
///////////////////////////////
|
|
|
|
/*
|
|
|
|
** This will verify there is a working file @ ./szIniFn
|
|
|
|
** - if not present, it will write defaults
|
|
|
|
*/
|
|
|
|
|
|
|
|
bool cfgOpen()
|
|
|
|
{
|
2015-08-28 23:28:51 +00:00
|
|
|
const char* filename = "/emu.cfg";
|
|
|
|
string config_path_read = get_readonly_config_path(filename);
|
|
|
|
cfgPath = get_writable_config_path(filename);
|
2015-08-24 11:30:01 +00:00
|
|
|
|
2015-08-28 23:28:51 +00:00
|
|
|
FILE* cfgfile = fopen(config_path_read.c_str(),"r");
|
2015-08-24 12:45:52 +00:00
|
|
|
if(cfgfile != NULL) {
|
|
|
|
cfgdb.parse(cfgfile);
|
|
|
|
fclose(cfgfile);
|
2015-08-24 12:05:51 +00:00
|
|
|
}
|
2015-08-24 12:45:52 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// Config file can't be opened
|
|
|
|
int error_code = errno;
|
2015-08-28 23:28:51 +00:00
|
|
|
printf("Warning: Unable to open the config file '%s' for reading (%s)\n", config_path_read.c_str(), strerror(error_code));
|
|
|
|
if (error_code == ENOENT || cfgPath != config_path_read)
|
2015-08-24 12:45:52 +00:00
|
|
|
{
|
|
|
|
// Config file didn't exist
|
|
|
|
printf("Creating new empty config file at '%s'\n", cfgPath.c_str());
|
|
|
|
savecfgf();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// There was some other error (may be a permissions problem or something like that)
|
|
|
|
save_config = false;
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
//Implementations of the interface :)
|
|
|
|
//Section must be set
|
|
|
|
//If key is 0 , it looks for the section
|
|
|
|
//0 : not found
|
|
|
|
//1 : found section , key was 0
|
|
|
|
//2 : found section & key
|
|
|
|
s32 cfgExists(const wchar * Section, const wchar * Key)
|
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
if(cfgdb.has_entry(string(Section), string(Key)))
|
|
|
|
{
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return (cfgdb.has_section(string(Section)) ? 1 : 0);
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
void cfgLoadStr(const wchar * Section, const wchar * Key, wchar * Return,const wchar* Default)
|
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
string value = cfgdb.get(Section, Key, Default);
|
2016-02-22 17:31:36 +00:00
|
|
|
// FIXME: Buffer overflow possible
|
2015-08-18 17:36:19 +00:00
|
|
|
strcpy(Return, value.c_str());
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
2015-03-22 00:16:28 +00:00
|
|
|
string cfgLoadStr(const wchar * Section, const wchar * Key, const wchar* Default)
|
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
if(!cfgdb.has_entry(string(Section), string(Key)))
|
|
|
|
{
|
|
|
|
cfgSaveStr(Section, Key, Default);
|
|
|
|
}
|
|
|
|
return cfgdb.get(string(Section), string(Key), string(Default));
|
2015-03-22 00:16:28 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 17:10:14 +00:00
|
|
|
//These are helpers , mainly :)
|
2015-08-18 17:36:19 +00:00
|
|
|
void cfgSaveInt(const wchar * Section, const wchar * Key, s32 Int)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2015-08-24 12:48:01 +00:00
|
|
|
cfgdb.set_int(string(Section), string(Key), Int);
|
|
|
|
if(save_config)
|
|
|
|
{
|
|
|
|
savecfgf();
|
|
|
|
}
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
|
|
|
|
2015-08-18 17:36:19 +00:00
|
|
|
s32 cfgLoadInt(const wchar * Section, const wchar * Key,s32 Default)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2015-08-18 17:36:19 +00:00
|
|
|
if(!cfgdb.has_entry(string(Section), string(Key)))
|
|
|
|
{
|
|
|
|
cfgSaveInt(Section, Key, Default);
|
|
|
|
}
|
|
|
|
return cfgdb.get_int(string(Section), string(Key), Default);
|
2013-12-19 17:10:14 +00:00
|
|
|
}
|
2015-08-18 17:36:19 +00:00
|
|
|
|
2018-04-11 19:37:31 +00:00
|
|
|
void cfgSaveBool(const wchar * Section, const wchar * Key, bool BoolValue)
|
2016-03-02 05:48:34 +00:00
|
|
|
{
|
2018-04-11 19:37:31 +00:00
|
|
|
cfgdb.set_bool(string(Section), string(Key), BoolValue);
|
2016-03-02 05:48:34 +00:00
|
|
|
if(save_config)
|
|
|
|
{
|
|
|
|
savecfgf();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool cfgLoadBool(const wchar * Section, const wchar * Key,bool Default)
|
|
|
|
{
|
|
|
|
if(!cfgdb.has_entry(string(Section), string(Key)))
|
|
|
|
{
|
|
|
|
cfgSaveBool(Section, Key, Default);
|
|
|
|
}
|
|
|
|
return cfgdb.get_bool(string(Section), string(Key), Default);
|
|
|
|
}
|
|
|
|
|
2015-07-17 21:56:51 +00:00
|
|
|
void cfgSetVirtual(const wchar * Section, const wchar * Key, const wchar * String)
|
2013-12-19 17:10:14 +00:00
|
|
|
{
|
2015-08-19 02:43:54 +00:00
|
|
|
cfgdb.set(string(Section), string(Key), string(String), true);
|
2015-08-16 21:35:13 +00:00
|
|
|
}
|