增加Data::Exists方法,判断数据是否存在

This commit is contained in:
Nomango 2018-09-04 22:57:40 +08:00
parent a67eefc0b4
commit 997aca7e6e
5 changed files with 85 additions and 50 deletions

View File

@ -106,7 +106,7 @@ void e2d::Renderer::EndDraw()
ThrowIfFailed(
GetWriteFactory()->CreateTextLayout(
(const WCHAR *)fpsText,
(const wchar_t *)fpsText,
(UINT32)fpsText.GetLength(),
fps_text_format_,
0,

View File

@ -323,7 +323,7 @@ void e2d::Text::CreateFormat()
ThrowIfFailed(
Renderer::GetWriteFactory()->CreateTextFormat(
(const WCHAR *)font_.family,
(const wchar_t *)font_.family,
nullptr,
DWRITE_FONT_WEIGHT(font_.weight),
font_.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL,
@ -387,7 +387,7 @@ void e2d::Text::CreateLayout()
{
ThrowIfFailed(
writeFactory->CreateTextLayout(
(const WCHAR *)text_,
(const wchar_t *)text_,
length,
text_format_,
style_.wrap_width,
@ -406,7 +406,7 @@ void e2d::Text::CreateLayout()
// 为防止文本对齐问题,根据先创建 layout 以获取宽度
ThrowIfFailed(
writeFactory->CreateTextLayout(
(const WCHAR *)text_,
(const wchar_t *)text_,
length,
text_format_,
0,
@ -425,7 +425,7 @@ void e2d::Text::CreateLayout()
SafeRelease(text_layout_);
ThrowIfFailed(
writeFactory->CreateTextLayout(
(const WCHAR *)text_,
(const wchar_t *)text_,
length,
text_format_,
size_.width,

View File

@ -8,80 +8,116 @@ e2d::Data::Data(const String & key, const String & field)
{
}
void e2d::Data::SaveInt(int value)
bool e2d::Data::Exists() const
{
::WritePrivateProfileString(
wchar_t temp[256] = { 0 };
::GetPrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
L"",
temp,
255,
(LPCWSTR)data_path_
);
return temp[0] == L'\0';
}
bool e2d::Data::SaveInt(int value)
{
BOOL ret = ::WritePrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
(LPCWSTR)String::Parse(value),
(LPCWSTR)data_path_
);
return ret != 0;
}
void e2d::Data::SaveDouble(float value)
bool e2d::Data::SaveFloat(float value)
{
::WritePrivateProfileString(
BOOL ret = ::WritePrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
(LPCWSTR)String::Parse(value),
(LPCWSTR)data_path_
);
return ret != 0;
}
void e2d::Data::SaveBool(bool value)
bool e2d::Data::SaveDouble(double value)
{
::WritePrivateProfileString(
BOOL ret = ::WritePrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
(LPCWSTR)String::Parse(value),
(LPCWSTR)data_path_
);
return ret != 0;
}
bool e2d::Data::SaveBool(bool value)
{
BOOL ret = ::WritePrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
(value ? L"1" : L"0"),
(LPCWSTR)data_path_
);
return ret != 0;
}
void e2d::Data::SaveString(const String& value)
bool e2d::Data::SaveString(const String& value)
{
::WritePrivateProfileString(
BOOL ret = ::WritePrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
(LPCWSTR)value,
(LPCWSTR)data_path_
);
return ret != 0;
}
int e2d::Data::GetInt(int default_value)
int e2d::Data::GetInt() const
{
return ::GetPrivateProfileInt(
return ::GetPrivateProfileIntW(
(LPCWSTR)field_,
(LPCWSTR)key_,
default_value,
0,
(LPCWSTR)data_path_
);
}
float e2d::Data::GetDouble(float default_value)
float e2d::Data::GetFloat() const
{
wchar_t temp[32] = { 0 };
::GetPrivateProfileString((LPCWSTR)field_, (LPCWSTR)key_, (LPCWSTR)String::Parse(default_value), temp, 31, (LPCWSTR)data_path_);
::GetPrivateProfileStringW((LPCWSTR)field_, (LPCWSTR)key_, L"0.0", temp, 31, (LPCWSTR)data_path_);
return std::stof(temp);
}
bool e2d::Data::GetBool(bool default_value)
double e2d::Data::GetDouble() const
{
int nValue = ::GetPrivateProfileInt(
wchar_t temp[32] = { 0 };
::GetPrivateProfileStringW((LPCWSTR)field_, (LPCWSTR)key_, L"0.0", temp, 31, (LPCWSTR)data_path_);
return std::stod(temp);
}
bool e2d::Data::GetBool() const
{
int nValue = ::GetPrivateProfileIntW(
(LPCWSTR)field_,
(LPCWSTR)key_,
default_value ? 1 : 0,
0,
(LPCWSTR)data_path_);
return nValue != 0;
}
e2d::String e2d::Data::GetString(const String& default_value)
e2d::String e2d::Data::GetString()
{
wchar_t temp[256] = { 0 };
::GetPrivateProfileString(
::GetPrivateProfileStringW(
(LPCWSTR)field_,
(LPCWSTR)key_,
(LPCWSTR)default_value,
L"",
temp,
255,
(LPCWSTR)data_path_

View File

@ -56,7 +56,7 @@ const e2d::String& e2d::Path::GetLocalAppDataPath()
if (local_app_data_path.IsEmpty())
{
// 获取 AppData/Local 文件夹的路径
WCHAR path[MAX_PATH] = { 0 };
wchar_t path[MAX_PATH] = { 0 };
::SHGetFolderPath(NULL, CSIDL_LOCAL_APPDATA, NULL, SHGFP_TYPE_CURRENT, path);
local_app_data_path = path;
}

View File

@ -370,49 +370,48 @@ public:
const String& field = L"Defalut" /* 字段名称 */
);
// 该数据是否存在
bool Exists() const;
// 保存 int 类型的值
void SaveInt(
int value /* 数据 */
bool SaveInt(
int value
);
// 保存 float 类型的值
void SaveDouble(
float value /* 数据 */
bool SaveFloat(
float value
);
// 保存 double 类型的值
bool SaveDouble(
double value
);
// 保存 bool 类型的值
void SaveBool(
bool value /* 数据 */
bool SaveBool(
bool value
);
// 保存 字符串 类型的值
void SaveString(
const String& value /* 数据 */
// 保存 String 类型的值
bool SaveString(
const String& value
);
// 获取 int 类型的值
// (若不存在则返回 default_value 参数的值)
int GetInt(
int default_value /* 默认值 */
);
int GetInt() const;
// 获取 float 类型的值
// (若不存在则返回 default_value 参数的值)
float GetDouble(
float default_value /* 默认值 */
);
float GetFloat() const;
// 获取 double 类型的值
double GetDouble() const;
// 获取 bool 类型的值
// (若不存在则返回 default_value 参数的值)
bool GetBool(
bool default_value /* 默认值 */
);
bool GetBool() const;
// 获取 字符串 类型的值
// (若不存在则返回 default_value 参数的值)
String GetString(
const String& default_value /* 默认值 */
);
String GetString();
protected:
String key_;