summaryrefslogtreecommitdiffstats
path: root/mmc_updater/src/DirIterator.cpp
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2013-12-02 00:55:24 +0100
committerPetr Mrázek <peterix@gmail.com>2013-12-02 00:55:24 +0100
commit6aa9bd0f77dcb5128167fae62e32aa5252fe85c6 (patch)
tree632994a61888929af9289927d338bd19a2b3f32c /mmc_updater/src/DirIterator.cpp
parent613699b3626aea750093ab7eaaeccaa28c0e87c6 (diff)
downloadMultiMC-6aa9bd0f77dcb5128167fae62e32aa5252fe85c6.tar
MultiMC-6aa9bd0f77dcb5128167fae62e32aa5252fe85c6.tar.gz
MultiMC-6aa9bd0f77dcb5128167fae62e32aa5252fe85c6.tar.lz
MultiMC-6aa9bd0f77dcb5128167fae62e32aa5252fe85c6.tar.xz
MultiMC-6aa9bd0f77dcb5128167fae62e32aa5252fe85c6.zip
Renew the updater branch
Now with some actual consensus on what the updater will do!
Diffstat (limited to 'mmc_updater/src/DirIterator.cpp')
-rw-r--r--mmc_updater/src/DirIterator.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/mmc_updater/src/DirIterator.cpp b/mmc_updater/src/DirIterator.cpp
new file mode 100644
index 00000000..a4604f05
--- /dev/null
+++ b/mmc_updater/src/DirIterator.cpp
@@ -0,0 +1,85 @@
+#include "DirIterator.h"
+
+#include "Log.h"
+#include "StringUtils.h"
+
+#ifdef PLATFORM_UNIX
+ #include <dirent.h>
+#endif
+
+#include <string.h>
+
+DirIterator::DirIterator(const char* path)
+{
+ m_path = path;
+
+#ifdef PLATFORM_UNIX
+ m_dir = opendir(path);
+ m_entry = 0;
+#else
+ // to list the contents of a directory, the first
+ // argument to FindFirstFile needs to be a wildcard
+ // of the form: C:\path\to\dir\*
+ std::string searchPath = m_path;
+ if (!endsWith(searchPath,"/"))
+ {
+ searchPath.append("/");
+ }
+ searchPath.append("*");
+ m_findHandle = FindFirstFile(searchPath.c_str(),&m_findData);
+ m_firstEntry = true;
+#endif
+}
+
+DirIterator::~DirIterator()
+{
+#ifdef PLATFORM_UNIX
+ closedir(m_dir);
+#else
+ FindClose(m_findHandle);
+#endif
+}
+
+bool DirIterator::next()
+{
+#ifdef PLATFORM_UNIX
+ m_entry = readdir(m_dir);
+ return m_entry != 0;
+#else
+ bool result;
+ if (m_firstEntry)
+ {
+ m_firstEntry = false;
+ return m_findHandle != INVALID_HANDLE_VALUE;
+ }
+ else
+ {
+ result = FindNextFile(m_findHandle,&m_findData);
+ }
+ return result;
+#endif
+}
+
+std::string DirIterator::fileName() const
+{
+#ifdef PLATFORM_UNIX
+ return m_entry->d_name;
+#else
+ return m_findData.cFileName;
+#endif
+}
+
+std::string DirIterator::filePath() const
+{
+ return m_path + '/' + fileName();
+}
+
+bool DirIterator::isDir() const
+{
+#ifdef PLATFORM_UNIX
+ return m_entry->d_type == DT_DIR;
+#else
+ return (m_findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
+#endif
+}
+