/
glowutils.cpp
76 lines (57 loc) · 1.46 KB
/
glowutils.cpp
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
#include <glowutils/glowutils.h>
#include <glowbase/File.h>
#include <glow/logging.h>
#include <glow/NamedString.h>
#ifdef _MSC_VER
#include "windows.h"
#include "dirent_msvc.h"
#else
#include <dirent.h>
#endif
namespace
{
std::vector<std::string> getFiles(const std::string & dirName)
{
std::vector<std::string> files;
DIR* dir = opendir(dirName.c_str());
dirent* entry;
if (!dir)
{
glow::warning() << "Could not open directory " << dirName << ".";
}
else
{
while ((entry = readdir(dir)))
{
std::string filename(entry->d_name);
if (filename == "." || std::string(entry->d_name) == "..")
continue;
files.push_back(filename);
}
closedir(dir);
}
return files;
}
std::string getExtension(const std::string & filename)
{
size_t pos = filename.find_last_of('.');
if (pos == std::string::npos)
return std::string();
return filename.substr(pos+1);
}
}
using namespace glow;
namespace glowutils
{
void scanDirectory(const std::string & directory, const std::string & fileExtension)
{
for (const std::string & file: getFiles(directory))
{
std::string extension = getExtension(file);
if (fileExtension != "*" && extension != fileExtension)
continue;
std::string fileName = directory+"/"+file;
NamedString::create("/"+fileName, new File(fileName));
}
}
} // namespace glowutils