summaryrefslogtreecommitdiff
path: root/src/libutil/FileUtils.cpp
blob: 975a78ce21a2022f2822923669f328cff6b4edfc (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "util/FileUtils.hpp"

#ifndef _WIN32
#include <sys/types.h>
#include <dirent.h>
#include <limits.h>
#else
#define WIN32_MEAN_AND_LEAN
#include <windows.h>
#include "util/win32/errormsg.hpp"
#include "win32/tcharutils.h"
#endif

#include <sstream>
#include <stdexcept>
#include <cstring>
#include <cerrno>

using namespace std;

#ifndef _WIN32
static vector<string> directory_entries_unix( const string &dir, bool absolute, bool recursive )
{
	vector<string> files;
	
	DIR *d = opendir( dir.c_str( ) );
	if( d == NULL ) {
		ostringstream ss;
		ss << "opendir failed with '" << dir << "': " << strerror( errno );
		throw runtime_error( ss.str( ) );
	}

	struct dirent *entry = readdir( d );
	while( entry != NULL ) {
		if( entry->d_type == DT_DIR ) {
			if( strcmp( entry->d_name, "." ) == 0 ||
				strcmp( entry->d_name, ".." ) == 0 ) {
				entry = readdir( d );
				continue;
			}
			if( recursive ) {
				ostringstream ss;
				ss << dir << "/" << entry->d_name;
				vector<string> subfiles = directory_entries_unix( ss.str( ), absolute, recursive );
				files.insert( files.end( ), subfiles.begin( ), subfiles.end( ) );
			}
		} else if( entry->d_type == DT_REG ) {
			if( absolute ) {
				ostringstream ss;
				ss << dir << "/" << entry->d_name;
				files.push_back( ss.str( ) );
			} else {
				files.push_back( string( entry->d_name ) );
			}
		}
		entry = readdir( d );
	}
	
	closedir( d );

	return files;
}
#endif

#ifdef _WIN32
static vector<string> directory_entries_win32( const string &dir, bool absolute, bool recursive )
{
	vector<string> files;
		
	wstringstream wpath;
	wpath << utf8toucs2( dir ) << L"\\*.";
	
	WIN32_FIND_DATAW fd;
	HANDLE h = FindFirstFileW( wpath.str( ).c_str( ), &fd );
	if( h == INVALID_HANDLE_VALUE ) {
		DWORD err = GetLastError( );
		if( err == ERROR_NO_MORE_FILES ) {
			FindClose( h );
			return files;
		}
		ostringstream ss;
		ss << "FindFirstFileW failed with '" << dir << "': " << getLastError( );
		throw runtime_error( ss.str( ) );
	}

	while( h != INVALID_HANDLE_VALUE ) {
		if( ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0 ) {
			if( wcscmp( fd.cFileName, L"." ) == 0 ||
				wcscmp( fd.cFileName, L".." ) == 0 ) {
				goto NEXT;
			}
			if( recursive ) {
				ostringstream ss;
				ss << dir << "\\" << wchartoutf8( fd.cFileName );
				vector<string> subfiles = directory_entries_win32( ss.str( ), absolute, recursive );
				files.insert( files.end( ), subfiles.begin( ), subfiles.end( ) );
			}
		} else if( ( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == 0 ) {
			if( absolute ) {
				ostringstream ss;
				ss << dir << "\\" << wchartoutf8( fd.cFileName );
				files.push_back( ss.str( ) );
			} else {
				files.push_back( string( wchartoutf8( fd.cFileName ) ) );
			}
		}
NEXT:
		if( !FindNextFileW( h, &fd ) ) {
			DWORD err = GetLastError( );
			if( err == ERROR_NO_MORE_FILES ) {
				// finish iterating
				FindClose( h );
				return files;
			} else { 
				FindClose( h );
				ostringstream ss;
				ss << "FindNextFileW failed with '" << dir << "': " << getLastError( );
				throw runtime_error( ss.str( ) );
			}
		}
	}

	FindClose( h );

	return files;
}
#endif

UTIL_DLL_VISIBLE vector<string> directory_entries( const string &dir, bool absolute, bool recursive )
{
#ifdef _WIN32
	return directory_entries_win32( dir, absolute, recursive );
#else
	return directory_entries_unix( dir, absolute, recursive );
#endif
}