Irrlicht系统学习---多种文件编码支持

              在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解析类提供一个公用且便捷的封装,具体见下版。。。

 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值