📄 asl_ini.cpp
字号:
//-----------------------------------------------------------------------------
//
// ____ Azure Star Game Engine 蓝星游戏引擎 ____
//
// Copyright (c) 2006, 蓝星工作室
// All rights reserved.
//
// 文件名称: asl_ini.cpp
// 摘 要: 高效ini文件读写类实现
//
// 当前版本: 1.0
// 作 者: 汤 祺
// 创建日期: 2006-8-16
//
//-----------------------------------------------------------------------------
#include "asl_ini.h"
#include <algorithm>
using namespace std;
namespace ASL
{
//-----------------------------------------------------------------------------
// 函数名: ASLIni::Load()
// 功 能: 加载ini文件
// 参 数: [&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)
{
// 去掉前导空白
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;
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::Save()
// 功 能: 保存ini文件
// 参 数: [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);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SetSection()
// 功 能: 设置当前段, 若不存在则创建新段
// 参 数: [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);
// 将新段信息放入段信息数组中
SectionInfo si;
si.strName = szSection;
si.itPos = m_lData.end();
m_vSection.push_back(si);
m_nCurrent = (int)m_vSection.size() - 1;
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadString()
// 功 能: 读字符串
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [szDefault] - 默认值
// 返回值: [string] - 读出值
//-----------------------------------------------------------------------------
string ASLIni::ReadString(LPCSTR szSection, LPCSTR szKey, LPCSTR szDefault)
{
SetSection(szSection);
return ReadString(szKey, szDefault);
}
//-----------------------------------------------------------------------------
// 函数名: IsSpace()
// 功 能: 判断一个字符是否为空白. 全局函数, 用于ASLIni::ReadString().
// 参 数: [c] - 字符
// 返回值: [bool] - true如果该字符不是空白
//-----------------------------------------------------------------------------
bool IsNotSpace(char c)
{
return c != ' ' && c != '\t';
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadString()
// 功 能: 读字符串, 要求先调用SetSection()
// 参 数: [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;
// 去掉前导空白
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);
// 等号存在, 已经找到指定键
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;
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadString()
// 功 能: 安全读字符串
// 参 数: [szSection] - 段名
// [szKey] - 键名
// 返回值: [string] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
string ASLIni::SafeReadString(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadString(szKey);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadString()
// 功 能: 安全读字符串, 要求先调用SetSection()
// 参 数: [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);
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadInteger()
// 功 能: 读整型值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// [nDefault] - 默认值
// 返回值: [int] - 读出值
//-----------------------------------------------------------------------------
int ASLIni::ReadInteger(LPCSTR szSection, LPCSTR szKey, int nDefault)
{
SetSection(szSection);
return ReadInteger(szKey, nDefault);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadInteger()
// 功 能: 读整型值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// [nDefault] - 默认值
// 返回值: [int] - 读出值
//-----------------------------------------------------------------------------
int ASLIni::ReadInteger(LPCSTR szKey, int nDefault)
{
string str = ReadString(szKey, "");
if (str == "")
{
return nDefault;
}
else
{
return atoi(str.c_str());
}
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::SafeReadInteger()
// 功 能: 读整型值
// 参 数: [szSection] - 段名
// [szKey] - 键名
// 返回值: [int] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
int ASLIni::SafeReadInteger(LPCSTR szSection, LPCSTR szKey) throw(ASLIniException)
{
SetSection(szSection);
return SafeReadInteger(szKey);
}
//-----------------------------------------------------------------------------
// 函数名: ASLIni::ReadInteger()
// 功 能: 读整型值, 要求先调用SetSection()
// 参 数: [szKey] - 键名
// 返回值: [int] - 读出值
// 读字符串失败则抛出ASLIniException异常
//-----------------------------------------------------------------------------
int ASLIni::SafeReadInteger(LPCSTR szKey) throw(ASLIniException)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -