boost文件系统封装类

 懒的写说明 直接铺代码

 

/* * @file FilePath.hpp * * 项目描述: 构造游戏引擎 * 文件描述: 游戏文件系统类 * 适用平台: Windows/Lunix/Unitx/.. * * 作者: ccsdu2004 * 电子邮件: [email protected] * 网址 : www.gaimo.net/ hi/csdn.net/ccsdu2004 * 创建日期: 2009-04-17 * 修改日期: .. * */ #ifndef G_UTIL_FILEPATH_HPP #define G_UTIL_FILEPATH_HPP #include <string> #include <set> #include <vector> #include <boost/noncopyable.hpp> #include <boost/filesystem.hpp> using namespace std; using namespace boost; using namespace boost::filesystem; namespace g { namespace file { class __declspec(dllexport) FilePath : public noncopyable { public: //参数为文件夹路径 FilePath(string filepath); ~FilePath(){} public: inline void find_in_sub_path(bool flag = false){subpath = flag;} //lie that: *.map3 *.wav ... void set_suffix(const string &suffixs); void get_files(vector<string> &files); private: void find(boost::filesystem::path p); private: string filepath; bool subpath; vector<string> file; set<string> suffix; bool usesf; }; } } #endif

 

#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/filesystem/exception.hpp>
#include "FilePath.hpp"

using namespace boost::algorithm;
namespace g
{
    namespace file
    {
        FilePath::FilePath(string _filepath): filepath(_filepath),
                                              subpath(false),
                                              usesf(false)
        {}  
       
        void FilePath::set_suffix(const string &suffixs)
        {
            string temp(suffixs);
            to_lower(temp);
            tokenizer<> sf(temp);                
            for(tokenizer<>::iterator itr = sf.begin(); itr != sf.end();++itr)
            {
                suffix.insert(*itr);
            }
            usesf = true;                       
        }                                    
       
        void FilePath::get_files(vector<string> &files)
        {
            boost::filesystem::path _filepath(filepath,boost::filesystem::native);
            find(_filepath);
            files = file;                   
        }
       
        void FilePath::find(boost::filesystem::path p)
        {
            try
            {
                if(boost::filesystem::exists(p) == true)
                {
                    boost::filesystem::directory_iterator item_begin(p);
                    boost::filesystem::directory_iterator item_end;
                   
                    for(;item_begin != item_end; item_begin++ )
                    {
                        if (boost::filesystem::is_directory( * item_begin) == true && subpath == true)
                        {
                           // (*item_begin)
        //(*itr)
        find(item_begin->path());   
                        }
                        else
                        {   
                           string filename =item_begin->leaf();
                           if(usesf == true)
                           {
                               to_lower(filename);
                              
                               set<string>::iterator itr = suffix.begin();
                               while(itr != suffix.end())
                               {
                                  if(ends_with(filename, *itr)== true)
                                    file.push_back(filename);      
                                  itr ++;         
                               }
                                   
                           }
                           else
                           {
                               file.push_back(filename);   
                           }
                        }
                    }  
                }
            }   
            catch(boost::filesystem::filesystem_error e)
            {   
            }  
        }
    }   
}

你可能感兴趣的:(游戏,String,File,iterator,Class,Path)