From 6aa9bd0f77dcb5128167fae62e32aa5252fe85c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Mon, 2 Dec 2013 00:55:24 +0100 Subject: Renew the updater branch Now with some actual consensus on what the updater will do! --- mmc_updater/src/DirIterator.cpp | 85 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 mmc_updater/src/DirIterator.cpp (limited to 'mmc_updater/src/DirIterator.cpp') 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 +#endif + +#include + +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 +} + -- cgit v1.2.3