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 | 744 | const std::string extension = Lower(path.extension().string()); | |
| 27 | 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 | 33 | if (!std::isfinite(nearest)) { | |
| 40 | return nearest; | ||
| 41 | } | ||
| 42 | 299 | for (int digits = 1; digits <= 17; ++digits) { | |
| 43 | 298 | const double degrees = std::strtod(FormatNumber(nearest, digits).c_str(), nullptr); | |
| 44 | 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 | 55 | if (!file) { | |
| 54 | 8 | throw std::runtime_error("Cannot read " + path.string()); | |
| 55 | } | ||
| 56 | 102 | std::string bytes{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()}; | |
| 57 | 51 | if (file.bad()) { | |
| 58 | ✗ | 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 | 120 | if (!file) { | |
| 66 | 10 | throw std::runtime_error("Cannot write " + path.string()); | |
| 67 | } | ||
| 68 | 115 | file.write(bytes.data(), static_cast<std::streamsize>(bytes.size())); | |
| 69 | 115 | file.close(); | |
| 70 | 115 | if (!file) { | |
| 71 | 4 | throw std::runtime_error("Cannot write " + path.string()); | |
| 72 | } | ||
| 73 | 120 | } | |
| 74 | |||
| 75 | } // namespace terraingen | ||
| 76 |