diff options
author | Stijn Buys <ingar@osirion.org> | 2012-01-07 14:21:22 +0000 |
---|---|---|
committer | Stijn Buys <ingar@osirion.org> | 2012-01-07 14:21:22 +0000 |
commit | f9403975df404eb03db29a6ffa655158d2739b1f (patch) | |
tree | 3867af5575126af5617c7210814c464c5e7de93a /src/filesystem/directory.cc | |
parent | 59139096145529c90c34ec67192faa8babbaa083 (diff) |
Added class to read filenames from a directory.
Diffstat (limited to 'src/filesystem/directory.cc')
-rw-r--r-- | src/filesystem/directory.cc | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/filesystem/directory.cc b/src/filesystem/directory.cc new file mode 100644 index 0000000..7339f17 --- /dev/null +++ b/src/filesystem/directory.cc @@ -0,0 +1,82 @@ +/* + filesystem/directory.cc + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +// system headers +#include <dirent.h> +#include <unistd.h> +#include <sys/stat.h> +#include <sys/types.h> + +// project headers +#include "filesystem/directory.h" + +namespace filesystem +{ + +Directory::Directory() +{ +} + +Directory::Directory(const std::string &location) : + directory_location(location) +{ + read(); +} + +Directory::~Directory() +{ + clear(); +} + + +void Directory::set_location(const std::string & location) +{ + clear(); + directory_location.assign(location); + read(); +} + +void Directory::clear() +{ + directory_filenames.clear(); + directory_location.clear(); +} + +void Directory::read() +{ + DIR *directory_handle; + struct dirent *directory_entry; + struct stat file_stat; + + directory_handle = opendir(directory_location.c_str()); + + if (directory_handle) { + while ((directory_entry = readdir(directory_handle))) { + + std::string file_path(directory_location); + + file_path += '/'; + file_path.append(directory_entry->d_name); + + // skip invalid files + if (stat(file_path.c_str(), &file_stat)) { + continue; + } + + // skip subdiretories + if (S_ISDIR(file_stat.st_mode)) { + continue; + } + + directory_filenames.push_back(file_path); + } + } + + closedir(directory_handle); +} + +} // namespace filesystem + |