aboutsummaryrefslogtreecommitdiff
path: root/source/10-common
diff options
context:
space:
mode:
Diffstat (limited to 'source/10-common')
-rw-r--r--source/10-common/Utils.cpp23
-rw-r--r--source/10-common/Utils.hpp13
2 files changed, 36 insertions, 0 deletions
diff --git a/source/10-common/Utils.cpp b/source/10-common/Utils.cpp
index dc76b0a..f0ff76d 100644
--- a/source/10-common/Utils.cpp
+++ b/source/10-common/Utils.cpp
@@ -77,6 +77,29 @@ std::string Utils::ReadFileAsString(const fs::path& path) {
return result;
}
+bool Utils::ReadCstdioLine(FILE* file, std::string& buffer) {
+ buffer.clear();
+ while (true) {
+ int c = fgetc(file);
+ if (c == EOF) {
+ if (buffer.empty() || buffer.back() != '\n') {
+ buffer += '\n';
+ }
+ return false;
+ } else if (c == '\n') {
+ buffer += '\n';
+ return true;
+ } else {
+ buffer += c;
+ }
+ }
+}
+
+bool Utils::ReadCstdioLine(FILE* file, char* buffer, size_t bufferSize, size_t* outLineLength) {
+ // TODO
+ assert(false && "Unimplemented");
+}
+
bool Utils::InRangeInclusive(int n, int lower, int upper) {
if (lower > upper) {
std::swap(lower, upper);
diff --git a/source/10-common/Utils.hpp b/source/10-common/Utils.hpp
index 9560b57..fdf0f5d 100644
--- a/source/10-common/Utils.hpp
+++ b/source/10-common/Utils.hpp
@@ -19,8 +19,21 @@ enum IoMode {
FILE* OpenCstdioFile(const std::filesystem::path& path, IoMode mode, bool binary = false);
FILE* OpenCstdioFile(const char* path, IoMode mode, bool binary = false);
+/// Retrieve a whole line (marked by `\n` or EOF) into the buffer. If the line ends with EOF, two things happen:
+/// 1. a `\n` character is appended to the line content, emulating as-if the line ended with `\n`.
+/// 2. `false` is returned
+/// Otherwise, `true` is returned.
+///
+/// Empty lines are not skipped at all, including the very last empty line if it exists.
+bool ReadCstdioLine(FILE* file, std::string& buffer);
+/// Same as the other overload, except working with a fixed-size buffer.
+/// NOTE: this also gives the length of the line compared to `std::fgets`.
+/// `std::fgets` requires us to run `std::strlen` on the output again to find the length
+bool ReadCstdioLine(FILE* file, char* buffer, size_t bufferSize, size_t* outLineLength = nullptr);
+
std::string ReadFileAsString(const std::filesystem::path& path);
+
constexpr float Abs(float v) noexcept {
return v < 0.0f ? -v : v;
}