-
Notifications
You must be signed in to change notification settings - Fork 0
/
01-fs_test.cc
61 lines (51 loc) · 1.81 KB
/
01-fs_test.cc
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
//
// Program
// C++17 Filesystem: Shows current and temporary directories and evaluates disk sizes.
//
// Compile
// g++ -Wall -Wextra -pedantic -std=c++17 -o 01-fs_test 01-fs_test.cc -lstdc++fs
//
// Execution
// ./01-fs_test
//
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
constexpr uintmax_t bytes_kilo = 1LL << 10;
constexpr uintmax_t bytes_mega = 1LL << 20;
constexpr uintmax_t bytes_giga = 1LL << 30;
constexpr uintmax_t bytes_tera = 1LL << 40;
// Convert bytes into std::string using storage units
std::string bytestostring(uintmax_t bytes) {
long double bytes_f = bytes * 1.0f;
if (bytes > bytes_tera) {
return std::to_string( bytes_f / bytes_tera) + std::string(" TB");
}
else if (bytes > bytes_giga) {
return std::to_string(bytes_f / bytes_giga) + std::string(" GB");
}
else if (bytes > bytes_mega) {
return std::to_string(bytes_f / bytes_mega) + std::string(" MB");
}
else if (bytes > bytes_kilo) {
return std::to_string(bytes_f / bytes_kilo) + std::string(" KB");
}
return std::to_string(bytes_f) + std::string(" Bytes");
}
//
// Entry function
//
int main() {
fs::path curr_path = fs::current_path();
fs::path tmp_path = fs::temp_directory_path();
std::cout << "Hello world -- std::filesystem" << '\n';
std::cout << " - Current working directory: " << curr_path.string() << '\n';
std::cout << " - Temporary directory: " << tmp_path.string() << '\n';
fs::space_info info = fs::space(curr_path);
std::cout << '\n';
std::cout << " - Directory info: " << curr_path.string() << '\n';
std::cout << " - Total size: " << bytestostring(info.capacity) << '\n';
std::cout << " - Free space size: " << bytestostring(info.free) << '\n';
std::cout << " - Available size: " << bytestostring(info.available) << '\n';
return 0;
}