OpenMW
components/file_finder/filename_less.hpp
Go to the documentation of this file.
00001 #ifndef FILE_FINDER_LESS_H
00002 #define FILE_FINDER_LESS_H
00003 
00004 #include <libs/platform/strings.h>
00005 #include <string>
00006 
00007 namespace FileFinder{
00008 
00009 // Used for maps of file paths. Compares file paths, but ignores case
00010 // AND treats \ and / as the same character.
00011 struct path_less
00012 {
00013   int compareChar(char a, char b) const
00014   {
00015     if(a>b) return 1;
00016     else if(a<b) return -1;
00017     return 0;
00018   }
00019 
00020   int comparePathChar(char a, char b) const
00021   {
00022     if(a >= 'a' && a <= 'z') a += 'A'-'a';
00023     else if(a == '\\') a = '/';
00024     if(b >= 'a' && b <= 'z') b += 'A'-'a';
00025     else if(b == '\\') b = '/';
00026     return compareChar(a,b);
00027   }
00028 
00029   int compareString(const char *a, const char *b) const
00030   {
00031     while(*a && *b)
00032       {
00033         int i = comparePathChar(*a,*b);
00034         if(i != 0) return i;
00035         a++; b++;
00036       }
00037     // At this point, one or both of the chars is a null terminator.
00038     // Normal char comparison will get the correct final result here.
00039     return compareChar(*a,*b);
00040   }
00041 
00042   bool operator() (const std::string& a, const std::string& b) const
00043   {
00044     return compareString(a.c_str(), b.c_str()) < 0;
00045   }
00046 };
00047 
00048 struct path_slash
00049 {
00050   int compareChar(char a, char b) const
00051   {
00052     if(a>b) return 1;
00053     else if(a<b) return -1;
00054     return 0;
00055   }
00056 
00057   int comparePathChar(char a, char b) const
00058   {
00059    if(a == '\\') a = '/';
00060    if(b == '\\') b = '/';
00061     return compareChar(a,b);
00062   }
00063 
00064   int compareString(const char *a, const char *b) const
00065   {
00066     while(*a && *b)
00067       {
00068         int i = comparePathChar(*a,*b);
00069         if(i != 0) return i;
00070         a++; b++;
00071       }
00072     // At this point, one or both of the chars is a null terminator.
00073     // Normal char comparison will get the correct final result here.
00074     return compareChar(*a,*b);
00075   }
00076 
00077   bool operator() (const std::string& a, const std::string& b) const
00078   {
00079     return compareString(a.c_str(), b.c_str()) < 0;
00080   }
00081 };
00082  
00083 }
00084 #endif