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

pull/603/head
Rubidium 12 months ago committed by rubidium42
parent 81f957b9f8
commit 6d597879d0

@ -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;
strecpy(buffer, this->p->c_str(), last);
this->p++;
return buffer;
if (this->p == this->end) return std::nullopt;
return *this->p++;
}
};

@ -84,9 +84,11 @@ struct FileStringReader : StringReader {
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;

@ -67,11 +67,9 @@ struct StringReader {
/**
* Read a single line from the source of strings.
* @param buffer The buffer to read the data in to.
* @param last The last element in the buffer.
* @return The buffer, or nullptr if at the end of the file.
* @return The line, or std::nullopt 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.

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

Loading…
Cancel
Save