OpenMW
components/files/multidircollection.hpp
Go to the documentation of this file.
00001 #ifndef COMPONENTS_FILES_MULTIDIRSOLLECTION_HPP
00002 #define COMPONENTS_FILES_MULTIDIRSOLLECTION_HPP
00003 
00004 #include <map>
00005 #include <vector>
00006 #include <string>
00007 #include <locale>
00008 #include <cctype>
00009 
00010 #include <boost/filesystem/path.hpp>
00011 
00012 namespace Files
00013 {
00014     typedef std::vector<boost::filesystem::path> PathContainer;
00015 
00016     struct NameLess
00017     {
00018         bool mStrict;
00019 
00020         NameLess (bool strict) : mStrict (strict) {}
00021 
00022         bool operator() (const std::string& left, const std::string& right) const
00023         {
00024             if (mStrict)
00025                 return left<right;
00026 
00027             std::size_t min = std::min (left.length(), right.length());
00028             std::locale loc;
00029 
00030             for (std::size_t i=0; i<min; ++i)
00031             {
00032                 char l = std::tolower (left[i], loc);
00033                 char r = std::tolower (right[i], loc);
00034 
00035                 if (l<r)
00036                     return true;
00037                 if (l>r)
00038                     return false;
00039             }
00040 
00041             return left.length()<right.length();
00042         }
00043     };
00044 
00050     class MultiDirCollection
00051     {
00052         public:
00053 
00054             typedef std::map<std::string, boost::filesystem::path, NameLess> TContainer;
00055             typedef TContainer::const_iterator TIter;
00056 
00057         private:
00058 
00059             TContainer mFiles;
00060 
00061         public:
00062 
00063             MultiDirCollection (const Files::PathContainer& directories,
00064                 const std::string& extension, bool foldCase);
00069 
00070             boost::filesystem::path getPath (const std::string& file) const;
00075 
00076             bool doesExist (const std::string& file) const;
00078 
00079             TIter begin() const;
00081 
00082             TIter end() const;
00084 
00085     };
00086 }
00087 
00088 #endif