Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: af1c58bec0bf2315e4a6cacd78e3747d9482e31d (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
/*
   filesystem/directory.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

// system headers
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>

// project headers
#include "filesystem/directory.h"

namespace filesystem
{

Directory::Directory() 
{	
}

Directory::Directory(const std::string &location) :
	directory_location(location)
{
	read();
}

Directory::~Directory() 
{
	clear();
}


void Directory::set_location(const std::string & location)
{
	clear();
	directory_location.assign(location);
	read();
}

void Directory::clear()
{
	directory_filenames.clear();
	directory_location.clear();
}

void Directory::read()
{
	DIR *directory_handle;
	struct dirent *directory_entry;
	struct stat file_stat;	
	
	directory_handle = opendir(directory_location.c_str());
	
	if (directory_handle) {
		while ((directory_entry = readdir(directory_handle))) {

			std::string file_path(directory_location.c_str());
			
			file_path += '/';
			file_path.append(directory_entry->d_name);
			    
			// skip invalid files
    			if (stat(file_path.c_str(), &file_stat)) {
				continue;
			}
			
			// skip subdiretories
    			if (S_ISDIR(file_stat.st_mode)) {
				continue;
			}
			
			file_path.assign(directory_entry->d_name);
			directory_filenames.push_back(file_path);
		}
	}
	
	closedir(directory_handle);
}

} // namespace filesystem