./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
37 / 38
97.4%
Branches:
28 / 33
84.8%
Functions:
7 / 7
100.0%

src/util/text.cpp
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 #include "util/text.h"
4
5 #include <algorithm>
6 #include <array>
7 #include <cctype>
8 #include <cmath>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <fstream>
12 #include <iterator>
13 #include <stdexcept>
14
15 namespace terraingen {
16
17 820 std::string Lower(std::string_view text) {
18 820 std::string lower(text);
19 820 std::transform(lower.begin(), lower.end(), lower.begin(),
20 3527 [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
21 820 return lower;
22 }
23
24 248 bool HasExtension(const std::filesystem::path& path,
25 std::initializer_list<std::string_view> extensions) {
26
1/1
✓ Branch 2 taken 248 times.
744 const std::string extension = Lower(path.extension().string());
27
1/1
✓ Branch 1 taken 248 times.
248 return std::any_of(extensions.begin(), extensions.end(),
28 358 [&](std::string_view candidate) { return extension == Lower(candidate); });
29 248 }
30
31 400670 std::string FormatNumber(double value, int digits) {
32 400670 std::array<char, 40> buffer{};
33 400670 std::snprintf(buffer.data(), buffer.size(), "%.*g", digits, value);
34 400670 return buffer.data();
35 }
36
37 33 double DegreesFromRadians(double radians) {
38 33 const double nearest = radians * (180.0 / kPi);
39
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 2 times.
33 if (!std::isfinite(nearest)) {
40 return nearest;
41 }
42
2/2
✓ Branch 0 taken 298 times.
✓ Branch 1 taken 1 time.
299 for (int digits = 1; digits <= 17; ++digits) {
43 298 const double degrees = std::strtod(FormatNumber(nearest, digits).c_str(), nullptr);
44
2/2
✓ Branch 0 taken 268 times.
✓ Branch 1 taken 30 times.
298 if (RadiansFromDegrees(degrees) == radians) {
45 return degrees;
46 }
47 }
48 return nearest;
49 }
50
51 55 std::string ReadFile(const std::filesystem::path& path) {
52 55 std::ifstream file(path, std::ios::binary);
53
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 51 times.
55 if (!file) {
54
3/3
✓ Branch 2 taken 4 times.
✓ Branch 5 taken 4 times.
✓ Branch 8 taken 4 times.
8 throw std::runtime_error("Cannot read " + path.string());
55 }
56
2/3
✓ Branch 1 taken 51 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 51 times.
102 std::string bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
57
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 51 times.
51 if (file.bad()) {
58
0/3
✗ Branch 2 not taken.
✗ Branch 5 not taken.
✗ Branch 8 not taken.
✗ throw std::runtime_error("Cannot read " + path.string());
59 }
60 51 return bytes;
61 55 }
62
63 120 void WriteFile(const std::filesystem::path& path, std::string_view bytes) {
64 120 std::ofstream file(path, std::ios::binary);
65
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 115 times.
120 if (!file) {
66
3/3
✓ Branch 2 taken 5 times.
✓ Branch 5 taken 5 times.
✓ Branch 8 taken 5 times.
10 throw std::runtime_error("Cannot write " + path.string());
67 }
68
1/1
✓ Branch 1 taken 115 times.
115 file.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
69
1/1
✓ Branch 1 taken 115 times.
115 file.close();
70
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 113 times.
115 if (!file) {
71
3/3
✓ Branch 2 taken 2 times.
✓ Branch 5 taken 2 times.
✓ Branch 8 taken 2 times.
4 throw std::runtime_error("Cannot write " + path.string());
72 }
73 120 }
74
75 } // namespace terraingen
76