00001
00002
00003 #include <dirent.h>
00004 #include <sys/types.h>
00005 #include <sys/stat.h>
00006 #include <unistd.h>
00007
00008 namespace dynamic
00009 {
00010 const int dir_separator = '/';
00011
00013 class posix_filesystem_api
00014 {
00015 public:
00016 static int dir_separator() { return '/'; }
00017
00018 struct dir_iterator
00019 {
00020 DIR * dp;
00021 struct dirent ep;
00022
00023 dir_iterator() : dp(0) { }
00024 ~dir_iterator() { if(dp) closedir(dp); }
00025 };
00026
00027 static bool first_file(std::string name, dir_iterator & data)
00028 {
00029 data.dp = opendir(name.c_str());
00030 if(data.dp)
00031 {
00032 struct dirent * epp;
00033 readdir_r(data.dp, &data.ep, &epp);
00034 return epp;
00035 }
00036 else
00037 {
00038 return false;
00039 }
00040 }
00041
00042 static bool find_next(dir_iterator & data)
00043 {
00044 if(data.dp)
00045 {
00046 struct dirent * epp;
00047 readdir_r(data.dp, &data.ep, &epp);
00048 return epp;
00049 }
00050 else
00051 {
00052 return false;
00053 }
00054 }
00055
00056 static const char * get_filename(const dir_iterator & data)
00057 {
00058 return data.ep.d_name;
00059 }
00060
00061 static bool is_directory(const dir_iterator & data, std::string const & filename)
00062 {
00063 return is_directory(filename);
00064 }
00065
00067 static bool is_directory(std::string const & filename)
00068 {
00069 struct stat mstat;
00070 return stat(filename.c_str(), &mstat) == 0 && S_ISDIR(mstat.st_mode);
00071 }
00072 };
00073
00075 typedef posix_filesystem_api filesystem_api;
00076 }