Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2012-01-07 14:21:22 +0000
committerStijn Buys <ingar@osirion.org>2012-01-07 14:21:22 +0000
commitf9403975df404eb03db29a6ffa655158d2739b1f (patch)
tree3867af5575126af5617c7210814c464c5e7de93a /src
parent59139096145529c90c34ec67192faa8babbaa083 (diff)
Added class to read filenames from a directory.
Diffstat (limited to 'src')
-rw-r--r--src/filesystem/Makefile.am2
-rw-r--r--src/filesystem/directory.cc82
-rw-r--r--src/filesystem/directory.h67
-rw-r--r--src/filesystem/file.cc2
-rw-r--r--src/filesystem/filestream.h2
-rw-r--r--src/filesystem/filesystem.h1
6 files changed, 154 insertions, 2 deletions
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 <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
+
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 <string>
+#include <list>
+
+namespace filesystem
+{
+
+/**
+ * @brief an abstract interface to handle file access
+ */
+class Directory {
+
+public:
+ typedef std::list<std::string> 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"