-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
222 lines (189 loc) · 4.21 KB
/
Main.cpp
File metadata and controls
222 lines (189 loc) · 4.21 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <iostream>
#include <vector>
#include <string>
#include <array>
#include <Windows.h>
#include "Log.h"
struct Options
{
std::vector<std::string> workPaths;
bool sizeSuffixes = false;
bool silent = false;
std::array<char, 5> suffixes = { 'B', 'K', 'M', 'G', 'T' };
};
struct FileSize
{
double value;
char suffix;
};
auto retrieveArgs(int argc, const char* argv[])
{
std::vector<std::string> args(argc);
for (auto i = 0; i < argc; i++)
{
args[i] = std::string(argv[i]);
}
return args;
}
auto parseArgs(std::vector<std::string>& args) {
Options opt;
for (auto i = 1; i < args.size(); i++)
{
const auto& arg = args[i];
if (arg.empty()) continue;
if (arg.front() == '-')
{
if (arg == "-h")
{
opt.sizeSuffixes = true;
continue;
}
else if (arg == "-s")
{
opt.silent = true;
}
else
{
if (opt.silent)
{
exit(2);
}
Log::warning() << "Unknown parameter " << arg << "\n";
}
}
else
{
opt.workPaths.push_back(arg);
}
}
return opt;
}
std::string getWildcard(const std::string& path)
{
if (path.empty())
return "*";
if (path.back() == '/' || path.back() == '\\')
return path + "*";
return path + "/*";
}
std::string joinPaths(const std::string& path, const std::string& otherPath)
{
if (path.empty())
return otherPath;
if (otherPath.empty())
return path;
if (path.back() == '/' || path.back() == '\\')
{
if (otherPath.front() == '/' || otherPath.front() == '\\')
return path + otherPath.substr(1);
return path + otherPath;
}
else
{
if (otherPath.front() == '/' || otherPath.front() == '\\')
return path + otherPath;
return path + "/" + otherPath;
}
}
FileSize getFileSize(const Options& opts, uint64_t iValue)
{
if (!opts.sizeSuffixes)
return { double(iValue), 'B' };
double value = iValue;
for (auto i = 0; i < opts.suffixes.size(); i++)
{
if (value < 1024)
return { value, opts.suffixes[i] };
value /= 1024.0;
}
return { value, opts.suffixes.back() };
}
uint64_t handlePath(const Options& opts, const std::string& path, const std::string &relativePath = "./")
{
WIN32_FIND_DATAA selfData;
auto selfFind = FindFirstFileA(path.c_str(), &selfData);
if (selfFind == INVALID_HANDLE_VALUE)
{
if (!opts.silent)
{
Log::error() << "Path " << path << " cannot be found\n";
}
return 0;
}
if (!(selfData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
{
return selfData.nFileSizeLow | (selfData.nFileSizeHigh << (sizeof(DWORD) * 8));
}
FindClose(selfFind);
WIN32_FIND_DATAA data;
auto find = FindFirstFileA(getWildcard(path).c_str(), &data);
if (find == INVALID_HANDLE_VALUE)
{
if (!opts.silent)
Log::error() << "Cannot find first file for directory " << path << "\n";
return 0;
}
uint64_t size = 0;
do
{
auto fileName = std::string(data.cFileName);
if (fileName == "." || fileName == "..")
continue;
auto relativeFileName = joinPaths(relativePath, fileName);
uint64_t fileSize = 0;
if (data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
fileSize = handlePath(opts, joinPaths(path, fileName), relativeFileName);
}
else
{
fileSize = data.nFileSizeLow | (data.nFileSizeHigh << (sizeof(DWORD) * 8));
}
size += fileSize;
if (!opts.silent)
{
auto fz = getFileSize(opts, fileSize);
Log::fileSize(fz.value) << fz.suffix << " " << relativeFileName << "\n";
}
}
while (FindNextFileA(find, &data));
FindClose(find);
return size;
}
int main(int argc, const char* argv[])
{
auto args = retrieveArgs(argc, argv);
auto opts = parseArgs(args);
if (opts.workPaths.empty())
{
if (!opts.silent)
{
Log::error() << "No work paths were specified.\n";
}
return 1;
}
else if (!opts.silent)
{
Log::info() << "Working with " << opts.workPaths.size() << " work paths\n";
}
std::vector<std::pair<std::string, FileSize>> summary;
for (const auto& path : opts.workPaths)
{
auto fz = getFileSize(opts, handlePath(opts, path));
summary.emplace_back(path, fz);
if (!opts.silent)
Log::fileSize(fz.value) << fz.suffix << " " << path << "\n";
}
if (!opts.silent)
{
Log::info() << "Summary: \n";
}
for (const auto& entry : summary)
{
Log::fileSize(entry.second.value)
<< entry.second.suffix
<< " "
<< entry.first << "\n";
}
return 0;
}