From f9403975df404eb03db29a6ffa655158d2739b1f Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sat, 7 Jan 2012 14:21:22 +0000 Subject: Added class to read filenames from a directory. --- src/filesystem/Makefile.am | 2 ++ src/filesystem/directory.cc | 82 +++++++++++++++++++++++++++++++++++++++++++++ src/filesystem/directory.h | 67 ++++++++++++++++++++++++++++++++++++ src/filesystem/file.cc | 2 +- src/filesystem/filestream.h | 2 +- src/filesystem/filesystem.h | 1 + 6 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 src/filesystem/directory.cc create mode 100644 src/filesystem/directory.h diff --git a/src/filesystem/Makefile.am b/src/filesystem/Makefile.am index ed2e6bf..8f5bafd 100644 --- a/src/filesystem/Makefile.am +++ b/src/filesystem/Makefile.am @@ -3,6 +3,7 @@ INCLUDES = -I$(top_srcdir)/src noinst_LTLIBRARIES = libfilesystem.la noinst_HEADERS = \ + directory.h \ diskfile.h \ file.h \ filestream.h \ @@ -11,6 +12,7 @@ noinst_HEADERS = \ inistream.h libfilesystem_la_SOURCES = \ + directory.cc \ diskfile.cc \ file.cc \ filestream.cc \ 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 +#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); + + 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 + diff --git a/src/filesystem/directory.h b/src/filesystem/directory.h new file mode 100644 index 0000000..cdd9870 --- /dev/null +++ b/src/filesystem/directory.h @@ -0,0 +1,67 @@ +/* + filesystem/directory.h + This file is part of the Osirion project and is distributed under + the terms of the GNU General Public License version 2 +*/ + +#ifndef __INCLUDED_FILESYSTEM_DIRECTORY_H__ +#define __INCLUDED_FILESYSTEM_DIRECTORY_H__ + +// C++ headers +#include +#include + +namespace filesystem +{ + +/** + * @brief an abstract interface to handle file access + */ +class Directory { + +public: + typedef std::list FileNames; + + Directory(); + + Directory(const std::string & location); + + ~Directory(); + + // inspectors + + /** + * @brief returns the underlying filesystem location + */ + inline const std::string & location() const { + return directory_location; + } + + inline const FileNames & filenames() const { + return directory_filenames; + } + + // mutators + + /** + * @brief set the underlying filesystem location + */ + void set_location(const std::string & location); + + /** + * @brief read the filenames from current filesystem location + */ + void read(); + + void clear(); +private: + + std::string directory_location; + + FileNames directory_filenames; +}; + +} // namespace filesystem + +#endif // __INCLUDED_FILESYSTEM_DIRECTORY_H__ + diff --git a/src/filesystem/file.cc b/src/filesystem/file.cc index 628ba22..0eeb32d 100644 --- a/src/filesystem/file.cc +++ b/src/filesystem/file.cc @@ -1,5 +1,5 @@ /* - fs/file.cc + filesystem/file.cc This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ diff --git a/src/filesystem/filestream.h b/src/filesystem/filestream.h index a97d1fb..ceac6c5 100644 --- a/src/filesystem/filestream.h +++ b/src/filesystem/filestream.h @@ -1,5 +1,5 @@ /* - filesystem/ifilestream.h + filesystem/filestream.h This file is part of the Osirion project and is distributed under the terms of the GNU General Public License version 2 */ diff --git a/src/filesystem/filesystem.h b/src/filesystem/filesystem.h index 5fe97d0..9608b74 100644 --- a/src/filesystem/filesystem.h +++ b/src/filesystem/filesystem.h @@ -7,6 +7,7 @@ #ifndef __INCLUDED_FILESYSTEM_H__ #define __INCLUDED_FILESYSTEM_H__ +#include "filesystem/directory.h" #include "filesystem/file.h" #include "filesystem/diskfile.h" #include "filesystem/inifile.h" -- cgit v1.2.3