📄 asl_ini.cpp
字号:
{
string str = SafeReadString(szKey);
if (str != "")
{
return atoi(str.c_str());
}
else
{
throw ASLIniException(m_szFileName, szKey);
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadFloat()
// 功 能: 读浮点值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [lfDefault] - 默认值
// 返回值: [double] - 读出值
//-----------------------------------------------------------------------------
double ASLIni::ReadFloat(LPCSTR szSection, LPCSTR szKey, double lfDefault)
{
SetSection(szSection);
return ReadFloat(szKey, lfDefault);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadFloat()
// 功 能: 读浮点值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [lfDefault] - 默认值
// 返回值: [double] - 读出值
//-----------------------------------------------------------------------------
double ASLIni::ReadFloat(LPCSTR szKey, double lfDefault)
{
string str = ReadString(szKey, "");
if (str == "")
{
return lfDefault;
}
else
{
return atof(str.c_str());
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadFloat()
// 功 能: 读整型值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// 返回值: [double] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
double ASLIni::SafeReadFloat(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadFloat(szKey);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadFloat()
// 功 能: 读整型值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// 返回值: [double] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
double ASLIni::SafeReadFloat(LPCSTR szKey) throw(ASLIniException)
{
string str = SafeReadString(szKey);
if (str != "")
{
return atof(str.c_str());
}
else
{
throw ASLIniException(m_szFileName, szKey);
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadBoolean()
// 功 能: 读布尔值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [bDefault] - 默认值
// 返回值: [bool] - 读出值
//-----------------------------------------------------------------------------
bool ASLIni::ReadBoolean(LPCSTR szSection, LPCSTR szKey, bool bDefault)
{
SetSection(szSection);
return ReadBoolean(szKey, bDefault);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadBoolean()
// 功 能: 读布尔值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [bDefault] - 默认值
// 返回值: [bool] - 读出值
//-----------------------------------------------------------------------------
bool ASLIni::ReadBoolean(LPCSTR szKey, bool bDefault)
{
string str = ReadString(szKey, "");
if (str[0] == '1')
{
return true;
}
else if (str[0] == '0')
{
return false;
}
else
{
return bDefault;
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadBoolean()
// 功 能: 读布尔值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// 返回值: [bool] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
bool ASLIni::SafeReadBoolean(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadBoolean(szKey);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadBoolean()
// 功 能: 读布尔值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// 返回值: [bool] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
bool ASLIni::SafeReadBoolean(LPCSTR szKey) throw(ASLIniException)
{
string str = SafeReadString(szKey);
if (str[0] == '1')
{
return true;
}
else if (str[0] == '0')
{
return false;
}
else
{
throw ASLIniException(m_szFileName, szKey);
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteString()
// 功 能: 写字符串
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [szValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteString(LPCSTR szSection, LPCSTR szKey, LPCSTR szValue)
{
SetSection(szSection);
WriteString(szKey, szValue);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteString()
// 功 能: 写字符串, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [szValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteString(LPCSTR szKey, LPCSTR szValue)
{
ASSERT(m_nCurrent >= 0);
Iter it = m_vSection[m_nCurrent].itPos; // 当前段开始行指针
string strWrite = szKey; // 待写入内容
strWrite += "=";
strWrite += szValue;
while (it != m_lData.end())
{
string::size_type pos;
// 去掉前导空白
pos = (*it).find_first_not_of(' ');
// 空白行, 直接跳过
if (pos == string::npos)
{
++it;
continue;
}
// 已到下个段, 查找失败
if ((*it)[pos] == '[')
{
// 新建一个键, 返回
m_lData.insert(m_vSection[m_nCurrent].itPos, strWrite);
return;
}
// 查找键名
if ((*it).substr(pos, strlen(szKey)) == szKey)
{
pos += strlen(szKey);
// 去掉等号前空白
pos = (*it).find_first_not_of(' ', pos);
// 等号存在, 已经找到指定键
if (pos != string::npos && (*it)[pos] == '=')
{
(*it) = strWrite; // 换掉原值
return;
}
}
++it;
} // end while (it != m_lData.end())
// 到链表尾仍没找到, 新建一个键
m_lData.insert(m_vSection[m_nCurrent].itPos, strWrite);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteInteger()
// 功 能: 写整型值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [nValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteInteger(LPCSTR szSection, LPCSTR szKey, int nValue)
{
SetSection(szSection);
WriteInteger(szKey, nValue);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteInteger()
// 功 能: 写整型值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [nValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteInteger(LPCSTR szKey, int nValue)
{
char szBuffer[20];
sprintf(szBuffer, "%d", nValue);
WriteString(szKey, szBuffer);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteFloat()
// 功 能: 写浮点值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [lfValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteFloat(LPCSTR szSection, LPCSTR szKey, double lfValue)
{
SetSection(szSection);
WriteFloat(szKey, lfValue);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteFloat()
// 功 能: 写浮点值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [lfValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteFloat(LPCSTR szKey, double lfValue)
{
char szBuffer[20];
sprintf(szBuffer, "%lf", lfValue);
WriteString(szKey, szBuffer);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteBoolean()
// 功 能: 写布尔值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [bValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteBoolean(LPCSTR szSection, LPCSTR szKey, bool bValue)
{
SetSection(szSection);
WriteBoolean(szKey, bValue);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::WriteBoolean()
// 功 能: 写布尔值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [bValue] - 写入值
// 返回值: [void] - 无
//-----------------------------------------------------------------------------
void ASLIni::WriteBoolean(LPCSTR szKey, bool bValue)
{
char szBuffer[2] = "0";
if (bValue)
{
szBuffer[0] = '1';
}
WriteString(szKey, szBuffer);
}
} // namespace ASL
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -