00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include "Directory.hh"
00025
00026 #include <sys/stat.h>
00027 #include <unistd.h>
00028
00029 namespace FbTk {
00030
00031 Directory::Directory(const char *dir):m_dir(0),
00032 m_num_entries(0) {
00033 if (dir != 0)
00034 open(dir);
00035 }
00036
00037 Directory::~Directory() {
00038 close();
00039 }
00040
00041 void Directory::rewind() {
00042 if (m_dir != 0)
00043 rewinddir(m_dir);
00044 }
00045
00046 struct dirent *Directory::read() {
00047 if (m_dir == 0)
00048 return 0;
00049
00050 return readdir(m_dir);
00051 }
00052
00053 std::string Directory::readFilename() {
00054 dirent *ent = read();
00055 if (ent == 0)
00056 return "";
00057 return (ent->d_name ? ent->d_name : "");
00058 }
00059
00060 void Directory::close() {
00061 if (m_dir != 0) {
00062 closedir(m_dir);
00063 m_dir = 0;
00064 m_num_entries = 0;
00065 }
00066 }
00067
00068
00069 bool Directory::open(const char *dir) {
00070 if (dir == 0)
00071 return false;
00072
00073 if (m_dir != 0)
00074 close();
00075
00076 m_dir = opendir(dir);
00077 if (m_dir == 0)
00078 return false;
00079
00080
00081 while (read())
00082 m_num_entries++;
00083
00084 rewind();
00085
00086 return true;
00087 }
00088
00089 bool Directory::isDirectory(const std::string &filename) {
00090 struct stat statbuf;
00091 if (stat(filename.c_str(), &statbuf) != 0)
00092 return false;
00093
00094 return S_ISDIR(statbuf.st_mode);
00095 }
00096
00097 bool Directory::isRegularFile(const std::string &filename) {
00098 struct stat statbuf;
00099 if (stat(filename.c_str(), &statbuf) != 0)
00100 return false;
00101
00102 return S_ISREG(statbuf.st_mode);
00103 }
00104
00105 };