Data class adds field functions

This commit is contained in:
Nomango 2018-03-13 23:35:00 +08:00
parent 0a80483ba6
commit c8a499ece0
2 changed files with 47 additions and 53 deletions

View File

@ -2,54 +2,54 @@
static e2d::String s_sDefaultFileName = L"DefaultData.ini";
void e2d::Data::saveInt(const String & key, int value, const String & fileName)
void e2d::Data::saveInt(const String & key, int value, const String & field)
{
::WritePrivateProfileString(L"Default", key, String::toString(value), Data::getDataFilePath(fileName));
::WritePrivateProfileString(field, key, String::toString(value), Data::getDataFilePath());
}
void e2d::Data::saveDouble(const String & key, double value, const String & fileName)
void e2d::Data::saveDouble(const String & key, double value, const String & field)
{
::WritePrivateProfileString(L"Default", key, String::toString(value), Data::getDataFilePath(fileName));
::WritePrivateProfileString(field, key, String::toString(value), Data::getDataFilePath());
}
void e2d::Data::saveBool(const String & key, bool value, const String & fileName)
void e2d::Data::saveBool(const String & key, bool value, const String & field)
{
const wchar_t* sValue = value ? L"1" : L"0";
::WritePrivateProfileString(L"Default", key, sValue, Data::getDataFilePath(fileName));
::WritePrivateProfileString(field, key, sValue, Data::getDataFilePath());
}
void e2d::Data::saveString(const String & key, const String & value, const String & fileName)
void e2d::Data::saveString(const String & key, const String & value, const String & field)
{
::WritePrivateProfileString(L"Default", key, value, Data::getDataFilePath(fileName));
::WritePrivateProfileString(field, key, value, Data::getDataFilePath());
}
int e2d::Data::getInt(const String & key, int defaultValue, const String & fileName)
int e2d::Data::getInt(const String & key, int defaultValue, const String & field)
{
return ::GetPrivateProfileInt(L"Default", key, defaultValue, Data::getDataFilePath(fileName));
return ::GetPrivateProfileInt(field, key, defaultValue, Data::getDataFilePath());
}
double e2d::Data::getDouble(const String & key, double defaultValue, const String & fileName)
double e2d::Data::getDouble(const String & key, double defaultValue, const String & field)
{
wchar_t temp[32] = { 0 };
::GetPrivateProfileString(L"Default", key, String::toString(defaultValue), temp, 31, Data::getDataFilePath(fileName));
::GetPrivateProfileString(field, key, String::toString(defaultValue), temp, 31, Data::getDataFilePath());
return std::stof(temp);
}
bool e2d::Data::getBool(const String & key, bool defaultValue, const String & fileName)
bool e2d::Data::getBool(const String & key, bool defaultValue, const String & field)
{
int nDefaultValue = defaultValue ? 1 : 0;
int nValue = ::GetPrivateProfileInt(L"Default", key, nDefaultValue, Data::getDataFilePath(fileName));
int nValue = ::GetPrivateProfileInt(field, key, nDefaultValue, Data::getDataFilePath());
return nValue != 0;
}
e2d::String e2d::Data::getString(const String & key, const String & defaultValue, const String & fileName)
e2d::String e2d::Data::getString(const String & key, const String & defaultValue, const String & field)
{
wchar_t temp[256] = { 0 };
::GetPrivateProfileString(L"Default", key, defaultValue, temp, 255, Data::getDataFilePath(fileName));
::GetPrivateProfileString(field, key, defaultValue, temp, 255, Data::getDataFilePath());
return temp;
}
void e2d::Data::setDataFilePath(const String & fileName)
void e2d::Data::setDataFileName(const String & fileName)
{
if (!fileName.isEmpty())
{
@ -58,11 +58,7 @@ void e2d::Data::setDataFilePath(const String & fileName)
}
}
e2d::String e2d::Data::getDataFilePath(const String & fileName)
e2d::String e2d::Data::getDataFilePath()
{
if (fileName.isEmpty())
{
return Path::getDefaultSavePath() + s_sDefaultFileName;
}
return Path::getDefaultSavePath() + fileName + L".ini";
return Path::getDefaultSavePath() + s_sDefaultFileName;
}

View File

@ -122,73 +122,71 @@ class Data
public:
// 保存 int 类型的值
static void saveInt(
const String & key,
int value,
const String & fileName = L""
const String & key, /* 键值 */
int value, /* 数据 */
const String & field = L"Defalut" /* 字段名称 */
);
// 保存 double 类型的值
static void saveDouble(
const String & key,
double value,
const String & fileName = L""
const String & key, /* 键值 */
double value, /* 数据 */
const String & field = L"Defalut" /* 字段名称 */
);
// 保存 bool 类型的值
static void saveBool(
const String & key,
bool value,
const String & fileName = L""
const String & key, /* 键值 */
bool value, /* 数据 */
const String & field = L"Defalut" /* 字段名称 */
);
// 保存 字符串 类型的值
static void saveString(
const String & key,
const String & value,
const String & fileName = L""
const String & key, /* 键值 */
const String & value, /* 数据 */
const String & field = L"Defalut" /* 字段名称 */
);
// 获取 int 类型的值
// (若不存在则返回 defaultValue 参数的值)
static int getInt(
const String & key,
int defaultValue,
const String & fileName = L""
const String & key, /* 键值 */
int defaultValue, /* 默认值 */
const String & field = L"Defalut" /* 字段名称 */
);
// 获取 double 类型的值
// (若不存在则返回 defaultValue 参数的值)
static double getDouble(
const String & key,
double defaultValue,
const String & fileName = L""
const String & key, /* 键值 */
double defaultValue, /* 默认值 */
const String & field = L"Defalut" /* 字段名称 */
);
// 获取 bool 类型的值
// (若不存在则返回 defaultValue 参数的值)
static bool getBool(
const String & key,
bool defaultValue,
const String & fileName = L""
const String & key, /* 键值 */
bool defaultValue, /* 默认值 */
const String & field = L"Defalut" /* 字段名称 */
);
// 获取 字符串 类型的值
// (若不存在则返回 defaultValue 参数的值)
static String getString(
const String & key,
const String & defaultValue,
const String & fileName = L""
const String & key, /* 键值 */
const String & defaultValue, /* 默认值 */
const String & field = L"Defalut" /* 字段名称 */
);
// 修改数据文件的默认名称
static void setDataFilePath(
const String & fileName
// 修改数据文件的名称
static void setDataFileName(
const String & strFileName /* 文件名称 */
);
// 获取数据文件的完整路径
static String getDataFilePath(
const String & fileName
);
static String getDataFilePath();
};