summaryrefslogtreecommitdiffstats
path: root/mmc_updater/src/DirIterator.cpp
blob: a4604f0526ccb00b181d0923330d449d4b8c331e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
}