Ogre读取中文路径名的文件失败的解决办法
生活随笔
收集整理的這篇文章主要介紹了
Ogre读取中文路径名的文件失败的解决办法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Ogre的文件讀取是使用的標準庫的io庫讀取的,眾所周知的是,在vs2005是存在著bug的。
因此想要一勞永逸的解決這個辦法唯有去修改Ogre的源代碼,以下為修改方法:
打開OgreFileSystem.cpp文件,找到FileSystemArchive::open方法,使用以下代碼替換之:
DataStreamPtr?FileSystemArchive::open(const?String&?filename)?const
????{
????????String?full_path?=?concatenate_path(mName,?filename);
????????//?Use?filesystem?to?determine?size?
????????//?(quicker?than?streaming?to?the?end?and?back)
????????struct?stat?tagStat;
????????int?ret?=?stat(full_path.c_str(),?&tagStat);
????????assert(ret?==?0?&&?"Problem?getting?file?size"?);
????????//?Always?open?in?binary?mode
????????static?std::vector<wchar_t>????s_wchar_buf((size_t)128);
????????size_t?lengthUnicode?=?MultiByteToWideChar(CP_ACP,?0,?full_path.c_str(),?full_path.size(),?NULL,?0);
????????if?(s_wchar_buf.size()?<?lengthUnicode?+?1)
????????{
????????????s_wchar_buf.resize(lengthUnicode?*?2);
????????}
????????wchar_t*?szUnicode?=?&s_wchar_buf[0];
????????MultiByteToWideChar(CP_ACP,?0,?full_path.c_str(),?full_path.size(),?szUnicode,?lengthUnicode);
????????szUnicode[lengthUnicode]?=?0;
????????std::ifstream*?origStream?=?new?std::ifstream();
????????origStream->open(szUnicode,?std::ios::in?|?std::ios::binary);
????????//?Should?check?ensure?open?succeeded,?in?case?fail?for?some?reason.
????????if?(origStream->fail())
????????{
????????????delete?origStream;
????????????OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND,
????????????????"Cannot?open?file:?"?+?filename,
????????????????"FileSystemArchive::open");
????????}
????????/**////?Construct?return?stream,?tell?it?to?delete?on?destroy
????????FileStreamDataStream*?stream?=?new?FileStreamDataStream(filename,
????????????origStream,?tagStat.st_size,?true);
????????return?DataStreamPtr(stream);
????}
posted on 2008-12-09 03:42 楊粼波 閱讀(957) 評論(2) ?編輯?收藏 引用
??
re: Ogre讀取中文路徑名的文件失敗的解決辦法[未登錄] 2010-09-12 00:56 你知道setlocale這個方法的代價有多大么?會帶來什么影響么?????
??
因此想要一勞永逸的解決這個辦法唯有去修改Ogre的源代碼,以下為修改方法:
打開OgreFileSystem.cpp文件,找到FileSystemArchive::open方法,使用以下代碼替換之:
DataStreamPtr?FileSystemArchive::open(const?String&?filename)?const
????{
????????String?full_path?=?concatenate_path(mName,?filename);
????????//?Use?filesystem?to?determine?size?
????????//?(quicker?than?streaming?to?the?end?and?back)
????????struct?stat?tagStat;
????????int?ret?=?stat(full_path.c_str(),?&tagStat);
????????assert(ret?==?0?&&?"Problem?getting?file?size"?);
????????//?Always?open?in?binary?mode
????????static?std::vector<wchar_t>????s_wchar_buf((size_t)128);
????????size_t?lengthUnicode?=?MultiByteToWideChar(CP_ACP,?0,?full_path.c_str(),?full_path.size(),?NULL,?0);
????????if?(s_wchar_buf.size()?<?lengthUnicode?+?1)
????????{
????????????s_wchar_buf.resize(lengthUnicode?*?2);
????????}
????????wchar_t*?szUnicode?=?&s_wchar_buf[0];
????????MultiByteToWideChar(CP_ACP,?0,?full_path.c_str(),?full_path.size(),?szUnicode,?lengthUnicode);
????????szUnicode[lengthUnicode]?=?0;
????????std::ifstream*?origStream?=?new?std::ifstream();
????????origStream->open(szUnicode,?std::ios::in?|?std::ios::binary);
????????//?Should?check?ensure?open?succeeded,?in?case?fail?for?some?reason.
????????if?(origStream->fail())
????????{
????????????delete?origStream;
????????????OGRE_EXCEPT(Exception::ERR_FILE_NOT_FOUND,
????????????????"Cannot?open?file:?"?+?filename,
????????????????"FileSystemArchive::open");
????????}
????????/**////?Construct?return?stream,?tell?it?to?delete?on?destroy
????????FileStreamDataStream*?stream?=?new?FileStreamDataStream(filename,
????????????origStream,?tagStat.st_size,?true);
????????return?DataStreamPtr(stream);
????}
posted on 2008-12-09 03:42 楊粼波 閱讀(957) 評論(2) ?編輯?收藏 引用
Feedback
re: Ogre讀取中文路徑名的文件失敗的解決辦法 2010-09-05 19:16 用setlocale(LC_ALL,"Chinese-simplified");就可以了。??????
re: Ogre讀取中文路徑名的文件失敗的解決辦法[未登錄] 2010-09-12 00:56 你知道setlocale這個方法的代價有多大么?會帶來什么影響么?????
??
總結
以上是生活随笔為你收集整理的Ogre读取中文路径名的文件失败的解决办法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 可视化理解CNN
- 下一篇: JDK 5.0 的新语法