00001 
00019 #ifndef COIL_FILE_H
00020 #define COIL_FILE_H
00021 
00022 #include <cstring>
00023 #include <sys/types.h>
00024 #include <dirent.h>
00025 #include <libgen.h>
00026 
00027 #include <coil/config_coil.h>
00028 #include <coil/stringutil.h>
00029 
00030 #ifdef __QNX__
00031 using std::strlen;
00032 using std::strcpy;
00033 #endif
00034 
00035 namespace coil
00036 {
00037 
00061   inline std::string dirname(char* path)
00062   {
00063     char path_name[strlen(path)+1];
00064     strcpy(path_name, path);
00065     std::string dir_name = ::dirname(path);
00066     return dir_name;
00067   }
00068 
00092   inline std::string basename(const char* path)
00093   {
00094     char path_name[strlen(path)+1];
00095     strcpy(path_name, path);
00096     std::string base_name = ::basename(path_name);
00097     return base_name;
00098   }
00099 
00125   inline coil::vstring filelist(const char* path, const char* glob_str = "")
00126   {
00127     struct dirent* ent; 
00128     coil::vstring flist;
00129     bool has_glob(false);
00130     std::string pattern;
00131 
00132     if (path == 0) { return flist; }
00133     if (glob_str[0] != '\0') { has_glob = true; }
00134 
00135     DIR* dir_ptr(::opendir(path));
00136     if (dir_ptr == 0) { return flist; }
00137     
00138     while ((ent = ::readdir(dir_ptr)) != 0)
00139       {
00140         bool match(true);
00141         if (has_glob)
00142           {
00143             const char* globc(glob_str);
00144             std::string fname(ent->d_name);
00145             for (size_t i(0); i < fname.size() && globc != '\0'; ++i, ++globc)
00146               {
00147                 if (*globc == '*')
00148                   {
00149                     
00150                     if (globc[1] == '\0') { break; }
00151                     
00152                     if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
00153 
00154                     
00155                     ++globc;
00156                     size_t pos(fname.find(*globc, i));
00157                     if (pos == std::string::npos) { match = false; break; }
00158                     
00159                     i = pos;
00160                   }
00161                 else if (*globc == '+')
00162                   {
00163                     
00164                     if (globc[1] == '\0' && !(i + 1 < fname.size())) { break; }
00165                     
00166                     if (globc[1] == '*' || globc[1] == '+') { --i; continue; }
00167 
00168                     
00169                     ++globc;
00170                     size_t pos(fname.find(*globc, i + 1));
00171                     if (pos == std::string::npos) { match = false; break; }
00172                     
00173                     i = pos;
00174                   }
00175                 else
00176                   {
00177                     if (fname[i] != *globc) { match = false; }
00178                   }
00179                 
00180                 
00181                 
00182                 if (i + 1 == fname.size() && 
00183                     globc[1] != '\0' && globc[1] != '*') { match = false; }
00184               }
00185           }
00186         if (match) { flist.push_back(ent->d_name); }
00187       }
00188     ::closedir(dir_ptr);
00189 
00190     return flist;
00191   }
00192 };
00193 
00194 #endif // COIL_FILE_H