在Irrlicht引擎中有关于很多文件操作,其中不同文件类型都支持着开发者所需要的编码格式:ASCII, UTF8,UTF16,UTF32.也为各种编码格式提供了操作接口,比如xml文件格式的结构:
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(const char* filename);
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(FILE* file);
IRRLICHT_API IrrXMLReader* IRRCALLCONV createIrrXMLReader(IFileReadCallBack* callback);
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(const char* filename);
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(FILE* file);
IRRLICHT_API IrrXMLReaderUTF16* IRRCALLCONV createIrrXMLReaderUTF16(IFileReadCallBack* callback);
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(const char* filename);
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(FILE* file);
IRRLICHT_API IrrXMLReaderUTF32* IRRCALLCONV createIrrXMLReaderUTF32(IFileReadCallBack* callback);
这些是系统中提供的xml解析的操作接口,对于我自己的Irrlicht++引擎,我还添加了2个便捷的文件操作:
INI文件结构
XML的封装
INI接口实现:
template<class char_type, class super_class>
class IIrrINIReader : public super_class
{
public:
struct SItem
{
core::string<char_type> Name;
core::string<char_type> Value;
};
struct SSection
{
core::string<char_type> Name;
core::array<SItem> Items;
u32 getItemCount()
{
return Items.size();
}
int getItemIdxByName(const core::string<char_type> name)
{
for (int i=0;i<(int)Items.size();i++)
{
if (Items[i].Name == name)
{
return i;
}
}
return -1;
}
core::string<char_type> getItemValue(int idx)
{
if (idx >=0 && idx <(int)Items.size())
{
return Items[idx].Value;
}
return 0;
}
core::string<char_type> getItemValue(const core::string<char_type> name)
{
int idx = getItemIdxByName(name);
return getItemValue(idx);
}
int getItemValueAsInt(int idx)
{
if (idx >=0 && idx <(int)Items.size())
{
core::stringc c = Items[idx].Value.c_str();
return (int) fast_atof(c.c_str());
}
return 0;
}
int getItemValueAsInt(const core::string<char_type> name)
{
int idx = getItemIdxByName(name);
return getItemValueAsInt(idx);
}
float getItemValueAsFloat(int idx)
{
if (idx >=0 && idx <Items.size())
{
return fast_atof(Items[idx].Value.c_str());
}
return 0.0f;
}
float getItemValueAsFloat(const core::string<char_type> name) const
{
int idx = getItemIdxByName(name);
return getItemValueAsFloat(idx);
}
bool getItemValueAsBool(const core::string<char_type> name)
{
int idx = getItemIdxByName(name);
return getItemValueAsBool(idx);
}
bool getItemValueAsBool(int idx)
{
if (idx >=0 && idx <(int)Items.size())
{
if (Items[idx].Value == L"true" || Items[idx].Value == L"TRUE")
{
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return true;
}
}
_IRR_IMPLEMENT_MANAGED_MARSHALLING_BUGFIX;
return false;
}
};
public:
virtual ~IIrrINIReader() {};
virtual bool read() = 0;
//can fetch the section next ot next until end
virtual SSection* getSection() = 0;
virtual bool setSectionPointIndex(int idx = 0) = 0;
//use index to get the section
virtual SSection* getSection(int idx) = 0;
//use section name to get the section
virtual SSection* getSection(const core::string<char_type> name) = 0;
};
对于XML的封装,因为系统提供的是基本的文件解析,对于结构化的存储和便捷操作提供的不是很好,所有准备给
系统的XML解析类提供一个公用且便捷的封装,具体见下版。。。