src/util/text.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | |||
| 3 | /// @file | ||
| 4 | /// @brief@brief Text helpers: case-insensitive names and file extensions, number and angle | ||
| 5 | /// formatting, and whole files. | ||
| 6 | #pragma once | ||
| 7 | |||
| 8 | #include "terraingen/util/bits.h" | ||
| 9 | |||
| 10 | #include <filesystem> | ||
| 11 | #include <initializer_list> | ||
| 12 | #include <string> | ||
| 13 | #include <string_view> | ||
| 14 | |||
| 15 | namespace terraingen { | ||
| 16 | |||
| 17 | /// `text` with ASCII letters lowercased. | ||
| 18 | std::string Lower(std::string_view text); | ||
| 19 | |||
| 20 | /// Whether `path` ends in one of `extensions`, each with its dot (".tif"), ignoring case. | ||
| 21 | bool HasExtension(const std::filesystem::path& path, | ||
| 22 | std::initializer_list<std::string_view> extensions); | ||
| 23 | |||
| 24 | /// `value` as printf `%g` writes it with `digits` significant digits; 17 reads any double back | ||
| 25 | /// exactly. | ||
| 26 | std::string FormatNumber(double value, int digits); | ||
| 27 | |||
| 28 | /// An angle in degrees: the shortest decimal that `RadiansFromDegrees` turns back into exactly | ||
| 29 | /// `radians`, else the nearest. | ||
| 30 | double DegreesFromRadians(double radians); | ||
| 31 | |||
| 32 | /// An angle in radians from degrees. | ||
| 33 | 316 | inline double RadiansFromDegrees(double degrees) { | |
| 34 | 316 | return degrees * (kPi / 180.0); | |
| 35 | } | ||
| 36 | |||
| 37 | /// The bytes of a file. | ||
| 38 | /// @throws std::runtime_error "Cannot read <path>" if the file cannot be opened or read. | ||
| 39 | std::string ReadFile(const std::filesystem::path& path); | ||
| 40 | |||
| 41 | /// Replaces a file's contents with `bytes`. | ||
| 42 | /// @throws std::runtime_error "Cannot write <path>" if the file cannot be opened or written. | ||
| 43 | void WriteFile(const std::filesystem::path& path, std::string_view bytes); | ||
| 44 | |||
| 45 | } // namespace terraingen | ||
| 46 |