-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFindFileExt.cpp
More file actions
162 lines (148 loc) · 3.2 KB
/
FindFileExt.cpp
File metadata and controls
162 lines (148 loc) · 3.2 KB
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include "FindFileExt.h"
#include "main.h"
#include "Utils.h"
#include <format>
#include <iostream>
namespace b = ::boost;
using ::std::cout;
using ::std::endl;
using ::std::format;
using ::std::ostream;
using ::std::string;
using ::std::string_view;
#if !defined(CMDLINEUTIL_TEST_MODE)
int main(int argCount, const char*const*const argList)
{
return commonMain<FindFileExt>(argCount, argList);
}
#endif
int FindFileExt::usage(ostream& out, string_view progName, const char* pMsg)
{
int exitCode = EXIT_SUCCESS;
if (pMsg != nullptr && *pMsg != '\0')
{
exitCode = EXIT_FAILURE;
out << endl << pMsg << endl;
}
out << "\n"
"Usage: " << progName << " [-c] [-r] [-w] <dir1> <dir2> ...\n"
"\n"
"Compiles a list of all file extensions found within the indicated\n"
"directories.\n"
"\n"
"Options:\n"
"\n"
" -c Includes counts in the output\n"
"\n"
" -r Search in sub-directories recursively\n"
"\n"
" -w Report extensions as wildcards\n"
<< endl;
return exitCode;
}
FindFileExt::FindFileExt(const ::std::span<const char*const> args) :
m_includeCounts(false),
m_outputAsWildcards(false),
m_fileEnumerator(),
m_extToCountMap(),
m_noExtList()
{
for (auto pArg : args)
{
if (isIEqual(pArg, "-?") || isIEqual(pArg, "-h") || isIEqual(pArg, "-help"))
{
throw CmdLineError();
}
else if (isIEqual(pArg, "-c"))
{
m_includeCounts = true;
}
else if (isIEqual(pArg, "-r"))
{
m_fileEnumerator.setRecursive();
}
else if (isIEqual(pArg, "-w"))
{
m_outputAsWildcards = true;
}
else
{
auto p = Path{pArg};
if (!exists(p))
{
throw CmdLineError(format("'{0}' does not exist", pArg));
}
else if (!is_directory(p))
{
throw CmdLineError(format("'{0}' is not a directory", pArg));
}
p /= "*";
m_fileEnumerator.insert(p);
}
}
if (m_fileEnumerator.numFileSpecs() <= 0)
{
throw CmdLineError("No directories specified");
}
}
int FindFileExt::run()
{
countFiles();
cout << endl;
b::for_each(m_extToCountMap,
[this](const StrToCountMap::value_type& extToCountMapping)
{
reportExtension(extToCountMapping);
});
cout << endl;
return EXIT_SUCCESS;
}
void FindFileExt::countFiles()
{
m_fileEnumerator.enumerateFiles([this](const Path& file)
{
string ext = file.extension().generic_string();
auto result = m_extToCountMap.insert(make_pair(ext, 0));
result.first->second += 1;
if (ext.empty())
{
m_noExtList.push_back(file);
}
});
}
void FindFileExt::reportExtension(const StrToCountMap::value_type& extToCountMapping)
{
const bool isNoExtensionEntry = extToCountMapping.first.empty();
string ext(isNoExtensionEntry
? "<no extension>"
: extToCountMapping.first);
if (m_includeCounts && m_outputAsWildcards)
{
if (!isNoExtensionEntry)
{
cout << format("*{0} -- {1}", ext, extToCountMapping.second) << endl;
}
}
else if (m_includeCounts)
{
cout << format("{0} -- {1}", ext, extToCountMapping.second) << endl;
if (isNoExtensionEntry)
{
for (const auto& noExtPath : m_noExtList)
{
cout << format(" {0}", noExtPath.generic_string()) << endl;
}
}
}
else if (m_outputAsWildcards)
{
if (!isNoExtensionEntry)
{
cout << format("*{0}", ext) << endl;
}
}
else
{
cout << ext << endl;
}
}