-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdirectory.hpp
137 lines (113 loc) · 3.51 KB
/
directory.hpp
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
137
#ifndef DIRECTORY_HPP
#define DIRECTORY_HPP
#include "logging.hpp"
#include <dirent.h>
#include <string>
#include <iostream>
#include <vector>
#include <memory>
#include <mutex>
/**
* There is no Standard Filesystem and directory methods for C++/11
* At this time, 17 has some new expermental from Boost libs but
* Most systems are not up to 17 yet. Were using some C API
* Which are not thread safe so we'll wrap them un mutex for now.
*/
/**
* @class directory
* @author Michael Griffin
* @date 03/03/2018
* @file directory.hpp
* @brief Thread-Safe Directory Class for Parsing all Files in a folder.
*/
class Directory
{
public:
explicit Directory(void)
: m_log(Logging::getInstance())
, m() // Mutex
{ }
~Directory()
{
m_log.write<Logging::DEBUG_LOG>("~Directory()");
}
/**
* @brief Returns the Extension of the current file name
* @param value
* @return
*/
std::string getFileExtension(const std::string &value)
{
std::string::size_type idx = value.rfind(".");
std::string extension = "";
if(idx != std::string::npos)
{
extension = value.substr(idx+1);
}
return extension;
}
/**
* @brief Retrieves All files in directory by Extension (ex.. .yaml)
* @param dir
* @return
*/
std::vector<std::string> getAllFilesInDirectoryByExtension(const std::string &dir, std::string extension)
{
std::lock_guard<std::mutex> lock(m);
// Check if were pulling by specific or all files with extensions.
bool isAllExtensions = false;
if(extension.size() > 0)
{
isAllExtensions = true;
}
std::vector<std::string> file_list;
std::shared_ptr<DIR> local_directory_ptr(opendir(dir.c_str()), [](DIR* directory)
{
directory && closedir(directory);
});
if(!local_directory_ptr)
{
m_log.write<Logging::ERROR_LOG>("Error opening directory=", errno, dir, __LINE__, __FILE__);
return file_list;
}
struct dirent *dirent_ptr;
while((dirent_ptr = readdir(local_directory_ptr.get())) != nullptr)
{
// Skip All Directories "., .." in current folder.
std::string file_name = dirent_ptr->d_name;
if(file_name[file_name.size()-1] != '.')
{
// If File Extension matches then add to the list.
std::string file_ext = getFileExtension(file_name);
if(isAllExtensions && file_ext == extension)
{
// By Specific Extension
file_list.push_back(file_name);
}
else if(!isAllExtensions && file_ext != "")
{
// By All Extensions (Skip folders and executables)
file_list.push_back(file_name);
}
}
}
return file_list;
}
/**
* @brief Get File Listing Helper Method
* @param directory
* @param extension
* @return
*/
std::vector<std::string> getFileListPerDirectory(std::string directory, std::string extension)
{
const auto& directory_path = directory;
const auto& file_list = getAllFilesInDirectoryByExtension(directory_path, extension);
return file_list;
}
private:
Logging &m_log;
mutable std::mutex m;
};
typedef std::shared_ptr<Directory> directory_ptr;
#endif // DIRECTORY_HPP