?? asl_ini.cpp
字號:
//-----------------------------------------------------------------------------
//
// ____ Azure Star Game Engine 藍星游戲引擎 ____
//
// Copyright (c) 2006, 藍星工作室
// All rights reserved.
//
// 文件名稱: asl_ini.cpp
// 摘 要: 高效ini文件讀寫類實現(xiàn)
//
// 當(dāng)前版本: 1.0
// 作 者: 湯 祺
// 創(chuàng)建日期: 2006-8-16
//
//-----------------------------------------------------------------------------
#include "asl_ini.h"
#include <algorithm>
using namespace std;
namespace ASL
{
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::Load()
// 功 能: 加載ini文件
// 參 數(shù): [&file] - 打開了文件的ASLFile類對象
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::Load(ASLFile *pFile)
{
ASSERT(pFile != NULL);
string::size_type pos, pos2;
SectionInfo si;
// 逐行讀取文件, 放入鏈表中
char vBuffer[80];
while (pFile->GetLine(vBuffer, 80))
{
m_lData.push_back(vBuffer);
}
// 讀取文件名
strcpy(m_szFileName, pFile->GetName());
// 刪除文件
SAFE_DELETE(pFile);
// 把所有段位置記錄下來, 加快查找速度
for (Iter it = m_lData.begin(); it != m_lData.end(); ++it)
{
// 去掉前導(dǎo)空白
pos = (*it).find_first_not_of(' ');
// 空白行, 直接跳過
if (pos == string::npos)
{
continue;
}
// 查找段
if ((*it)[pos] == '[')
{
pos2 = (*it).find(']', pos);
if (pos2 != string::npos) // 找到一個段
{
si.strName = (*it).substr(pos+1, pos2-pos-1);
si.itPos = it;
si.itPos++;
m_vSection.push_back(si);
}
}
}
m_nCurrent = m_vSection.size() > 0 ? 0 : -1;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::Save()
// 功 能: 保存ini文件
// 參 數(shù): [szFileName] - 要保存的文件名
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::Save(LPCSTR szFileName)
{
FILE *fp = fopen(szFileName, "wt");
for (Iter it = m_lData.begin(); it != m_lData.end(); ++it)
{
LPCSTR str = (*it).c_str();
fprintf(fp, "%s\n", str);
}
fclose(fp);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SetSection()
// 功 能: 設(shè)置當(dāng)前段, 若不存在則創(chuàng)建新段
// 參 數(shù): [szSection] - 段名
// 返回值: [void] - 無
//-----------------------------------------------------------------------------
void ASLIni::SetSection(LPCSTR szSection)
{
// 查找段
for (int i = 0; i < (int)m_vSection.size(); ++i)
{
if (m_vSection[i].strName == szSection)
{
m_nCurrent = i;
return;
}
}
// 查找失敗, 在尾部插入一個新段
string strSection = "[";
strSection += szSection;
strSection += "]";
m_lData.push_back(strSection);
// 將新段信息放入段信息數(shù)組中
SectionInfo si;
si.strName = szSection;
si.itPos = m_lData.end();
m_vSection.push_back(si);
m_nCurrent = (int)m_vSection.size() - 1;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadString()
// 功 能: 讀字符串
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [szDefault] - 默認值
// 返回值: [string] - 讀出值
//-----------------------------------------------------------------------------
string ASLIni::ReadString(LPCSTR szSection, LPCSTR szKey, LPCSTR szDefault)
{
SetSection(szSection);
return ReadString(szKey, szDefault);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: IsSpace()
// 功 能: 判斷一個字符是否為空白. 全局函數(shù), 用于ASLIni::ReadString().
// 參 數(shù): [c] - 字符
// 返回值: [bool] - true如果該字符不是空白
//-----------------------------------------------------------------------------
bool IsNotSpace(char c)
{
return c != ' ' && c != '\t';
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadString()
// 功 能: 讀字符串, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [szDefault] - 默認值
// 返回值: [string] - 讀出值
//-----------------------------------------------------------------------------
string ASLIni::ReadString(LPCSTR szKey, LPCSTR szDefault)
{
ASSERT(m_nCurrent >= 0);
string strRtn;
Iter it = m_vSection[m_nCurrent].itPos;
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] == '[')
{
return szDefault;
}
// 查找鍵名
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] == '=')
{
++pos;
if ((*it)[pos] != '\0')
{
// 去掉等號后空白
pos = (*it).find_first_not_of(' ', pos);
if (pos != string::npos)
{
strRtn = (*it).substr(pos);
// 去掉字符串尾部空白
strRtn.erase(find_if(strRtn.rbegin(), strRtn.rend(),
ptr_fun(IsNotSpace)).base(), strRtn.end());
return strRtn;
}
}
strRtn = szDefault;
return strRtn;
}
} // end if ((*it).substr(pos, strlen(szKey)) == szKey)
++it;
} // end while (it != m_lData.end())
strRtn = szDefault;
return strRtn;
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadString()
// 功 能: 安全讀字符串
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// 返回值: [string] - 讀出值
// 讀字符串失敗則拋出ASLIniException異常
//-----------------------------------------------------------------------------
string ASLIni::SafeReadString(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadString(szKey);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadString()
// 功 能: 安全讀字符串, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// 返回值: [string] - 讀出值
// 讀字符串失敗則拋出ASLIniException異常
//-----------------------------------------------------------------------------
string ASLIni::SafeReadString(LPCSTR szKey) throw(ASLIniException)
{
string str = ReadString(szKey, "\n\n");
if (str != "\n\n")
{
return str;
}
else
{
throw ASLIniException(m_szFileName, szKey);
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadInteger()
// 功 能: 讀整型值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// [nDefault] - 默認值
// 返回值: [int] - 讀出值
//-----------------------------------------------------------------------------
int ASLIni::ReadInteger(LPCSTR szSection, LPCSTR szKey, int nDefault)
{
SetSection(szSection);
return ReadInteger(szKey, nDefault);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadInteger()
// 功 能: 讀整型值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// [nDefault] - 默認值
// 返回值: [int] - 讀出值
//-----------------------------------------------------------------------------
int ASLIni::ReadInteger(LPCSTR szKey, int nDefault)
{
string str = ReadString(szKey, "");
if (str == "")
{
return nDefault;
}
else
{
return atoi(str.c_str());
}
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::SafeReadInteger()
// 功 能: 讀整型值
// 參 數(shù): [szSection] - 段名
// [szKey] - 鍵名
// 返回值: [int] - 讀出值
// 讀字符串失敗則拋出ASLIniException異常
//-----------------------------------------------------------------------------
int ASLIni::SafeReadInteger(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadInteger(szKey);
}
//-----------------------------------------------------------------------------
// 函數(shù)名: ASLIni::ReadInteger()
// 功 能: 讀整型值, 要求先調(diào)用SetSection()
// 參 數(shù): [szKey] - 鍵名
// 返回值: [int] - 讀出值
// 讀字符串失敗則拋出ASLIniException異常
//-----------------------------------------------------------------------------
int ASLIni::SafeReadInteger(LPCSTR szKey) throw(ASLIniException)
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -