/* 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 #include #include #include // 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.c_str()); 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; } file_path.assign(directory_entry->d_name); directory_filenames.push_back(file_path); } } closedir(directory_handle); } } // namespace filesystem