1
0
Fork 0

Codechange: let ReadLine return a string instead of passing a buffer

pull/10979/head
Rubidium 2023-06-09 18:10:24 +02:00 committed by rubidium42
parent 81f957b9f8
commit 6d597879d0
4 changed files with 17 additions and 21 deletions

View File

@ -102,14 +102,10 @@ struct StringListReader : StringReader {
{ {
} }
char *ReadLine(char *buffer, const char *last) override std::optional<std::string> ReadLine() override
{ {
if (this->p == this->end) return nullptr; if (this->p == this->end) return std::nullopt;
return *this->p++;
strecpy(buffer, this->p->c_str(), last);
this->p++;
return buffer;
} }
}; };

View File

@ -84,9 +84,11 @@ struct FileStringReader : StringReader {
this->input_stream.open(file, std::ifstream::binary); this->input_stream.open(file, std::ifstream::binary);
} }
char *ReadLine(char *buffer, const char *last) override std::optional<std::string> ReadLine() override
{ {
return this->input_stream.getline(buffer, last - buffer) ? buffer : nullptr; std::string result;
if (!std::getline(this->input_stream, result)) return std::nullopt;
return result;
} }
void HandlePragma(char *str) override; void HandlePragma(char *str) override;

View File

@ -67,11 +67,9 @@ struct StringReader {
/** /**
* Read a single line from the source of strings. * Read a single line from the source of strings.
* @param buffer The buffer to read the data in to. * @return The line, or std::nullopt if at the end of the file.
* @param last The last element in the buffer.
* @return The buffer, or nullptr if at the end of the file.
*/ */
virtual char *ReadLine(char *buffer, const char *last) = 0; virtual std::optional<std::string> ReadLine() = 0;
/** /**
* Handle the pragma of the file. * Handle the pragma of the file.

View File

@ -739,16 +739,13 @@ void StringReader::HandlePragma(char *str)
} }
} }
static void rstrip(char *buf) static void StripTrailingWhitespace(std::string &str)
{ {
size_t i = strlen(buf); str.erase(str.find_last_not_of("\r\n ") + 1);
while (i > 0 && (buf[i - 1] == '\r' || buf[i - 1] == '\n' || buf[i - 1] == ' ')) i--;
buf[i] = '\0';
} }
void StringReader::ParseFile() void StringReader::ParseFile()
{ {
char buf[2048];
_warnings = _errors = 0; _warnings = _errors = 0;
_translation = this->translation; _translation = this->translation;
@ -765,9 +762,12 @@ void StringReader::ParseFile()
strecpy(_lang.digit_decimal_separator, ".", lastof(_lang.digit_decimal_separator)); strecpy(_lang.digit_decimal_separator, ".", lastof(_lang.digit_decimal_separator));
_cur_line = 1; _cur_line = 1;
while (this->data.next_string_id < this->data.max_strings && this->ReadLine(buf, lastof(buf)) != nullptr) { while (this->data.next_string_id < this->data.max_strings) {
rstrip(buf); std::optional<std::string> line = this->ReadLine();
this->HandleString(buf); if (!line.has_value()) return;
StripTrailingWhitespace(line.value());
this->HandleString(line.value().data());
_cur_line++; _cur_line++;
} }