summaryrefslogtreecommitdiff
path: root/src/libutil/FileUtils.cpp
blob: 64b1bb1171e85a1b35897d6cfa475b710af3efed (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
#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>
#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;
		
	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
}