?? asl_ini.cpp
字號:
{
string str = SafeReadString(szKey);
if (str != "")
{
return atoi(str.c_str());
}
else
{
throw ASLIniException(m_szFileName, szKey);
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadFloat()
// 功 能: 讀浮點(diǎn)值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [lfDefault] - 默認(rèn)值
// 返回值: [double] - 讀出值
//-----------------------------------------------------------------------------
double ASLIni::ReadFloat(LPCSTR szSection, LPCSTR szKey, double lfDefault)
{
SetSection(szSection);
return ReadFloat(szKey, lfDefault);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadFloat()
// 功 能: 讀浮點(diǎn)值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [lfDefault] - 默認(rèn)值
// 返回值: [double] - 讀出值
//-----------------------------------------------------------------------------
double ASLIni::ReadFloat(LPCSTR szKey, double lfDefault)
{
string str = ReadString(szKey, "");
if (str == "")
{
return lfDefault;
}
else
{
return atof(str.c_str());
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadFloat()
// 功 能: 讀整型值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// 返回值: [double] - 讀出值
// 讀字符串失敗則拋出ASLIniException異常
//-----------------------------------------------------------------------------
double ASLIni::SafeReadFloat(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadFloat(szKey);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadFloat()
// 功 能: 讀整型值, 要求先調(diào)用SetSection()
// 參 數(shù): [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);
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadBoolean()
// 功 能: 讀布爾值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [bDefault] - 默認(rèn)值
// 返回值: [bool] - 讀出值
//-----------------------------------------------------------------------------
bool ASLIni::ReadBoolean(LPCSTR szSection, LPCSTR szKey, bool bDefault)
{
SetSection(szSection);
return ReadBoolean(szKey, bDefault);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadBoolean()
// 功 能: 讀布爾值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [bDefault] - 默認(rèn)值
// 返回值: [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;
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadBoolean()
// 功 能: 讀布爾值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// 返回值: [bool] - 讀出值
// 讀字符串失敗則拋出ASLIniException異常
//-----------------------------------------------------------------------------
bool ASLIni::SafeReadBoolean(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadBoolean(szKey);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadBoolean()
// 功 能: 讀布爾值, 要求先調(diào)用SetSection()
// 參 數(shù): [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);
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteString()
// 功 能: 寫字符串
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [szValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteString(LPCSTR szSection, LPCSTR szKey, LPCSTR szValue)
{
SetSection(szSection);
WriteString(szKey, szValue);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteString()
// 功 能: 寫字符串, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [szValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteString(LPCSTR szKey, LPCSTR szValue)
{
ASSERT(m_nCurrent >= 0);
Iter it = m_vSection[m_nCurrent].itPos; // 當(dāng)前段開始行指針
string strWrite = szKey; // 待寫入內(nèi)容
strWrite += "=";
strWrite += szValue;
while (it != m_lData.end())
{
string::size_type pos;
// 去掉前導(dǎo)空白
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);
// 等號存在, 已經(jīng)找到指定鍵
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);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteInteger()
// 功 能: 寫整型值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [nValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteInteger(LPCSTR szSection, LPCSTR szKey, int nValue)
{
SetSection(szSection);
WriteInteger(szKey, nValue);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteInteger()
// 功 能: 寫整型值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [nValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteInteger(LPCSTR szKey, int nValue)
{
char szBuffer[20];
sprintf(szBuffer, "%d", nValue);
WriteString(szKey, szBuffer);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteFloat()
// 功 能: 寫浮點(diǎn)值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [lfValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteFloat(LPCSTR szSection, LPCSTR szKey, double lfValue)
{
SetSection(szSection);
WriteFloat(szKey, lfValue);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteFloat()
// 功 能: 寫浮點(diǎn)值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [lfValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteFloat(LPCSTR szKey, double lfValue)
{
char szBuffer[20];
sprintf(szBuffer, "%lf", lfValue);
WriteString(szKey, szBuffer);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteBoolean()
// 功 能: 寫布爾值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [bValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteBoolean(LPCSTR szSection, LPCSTR szKey, bool bValue)
{
SetSection(szSection);
WriteBoolean(szKey, bValue);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::WriteBoolean()
// 功 能: 寫布爾值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [bValue] - 寫入值
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::WriteBoolean(LPCSTR szKey, bool bValue)
{
char szBuffer[2] = "0";
if (bValue)
{
szBuffer[0] = '1';
}
WriteString(szKey, szBuffer);
}
} // namespace ASL
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -