Skip to content

Commit

Permalink
Parsing tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
dashodanger committed Jun 23, 2024
1 parent b7f1175 commit 3af5087
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 37 deletions.
15 changes: 0 additions & 15 deletions source/lib_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,21 +564,6 @@ int StringPrefixCaseCompare(std::string_view A, std::string_view B)
}
}

void StringRemoveCRLF(std::string *str)
{
if (!str->empty())
{
if (str->back() == '\n')
{
str->pop_back();
}
if (!str->empty() && str->back() == '\r')
{
str->pop_back();
}
}
}

void StringReplaceChar(std::string *str, char old_ch, char new_ch)
{
// when 'new_ch' is zero, the character is simply removed
Expand Down
1 change: 0 additions & 1 deletion source/lib_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ int StringPrefixCompare(std::string_view A, std::string_view B);
int StringCaseCompare(std::string_view A, std::string_view B);
int StringPrefixCaseCompare(std::string_view A, std::string_view B);

void StringRemoveCRLF(std::string *str);
void StringReplaceChar(std::string *str, char old_ch, char new_ch);

std::string StringFormat(std::string_view fmt, ...);
Expand Down
5 changes: 3 additions & 2 deletions source/m_cookie.cc
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ bool Cookie_Load(std::string filename)
buffer.clear();
while ((c = fgetc(cookie_fp)) != EOF)
{
buffer.push_back(c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

if (!Cookie_ParseLine(buffer))
Expand Down
8 changes: 3 additions & 5 deletions source/m_dialog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -809,14 +809,12 @@ void UI_GlossaryViewer::ReadGlossary()
buffer.clear();
while ((c = fgetc(gloss_file)) != EOF)
{
buffer.push_back(c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

// remove any newline at the end (LF or CR/LF)
StringRemoveCRLF(&buffer);

// remove any DEL characters (mainly to workaround an FLTK bug)
StringReplaceChar(&buffer, 0x7f, 0);

Expand Down
5 changes: 3 additions & 2 deletions source/m_options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,10 @@ bool Options_Load(std::string filename)
buffer.clear();
while ((c = fgetc(option_fp)) != EOF)
{
buffer.push_back(c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

Options_ParseLine(buffer);
Expand Down
5 changes: 3 additions & 2 deletions source/m_theme.cc
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,10 @@ bool Theme_Options_Load(std::string filename)
buffer.clear();
while ((c = fgetc(option_fp)) != EOF)
{
buffer.push_back(c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

if (!Theme_Options_ParseLine(buffer))
Expand Down
16 changes: 11 additions & 5 deletions source/m_trans.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1015,9 +1015,10 @@ void Trans_Read_PO_File(FILE *fp)
buffer.clear();
while ((c = fgetc(fp)) != EOF)
{
buffer.push_back(c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

po_state.line_number += 1;
Expand Down Expand Up @@ -1110,9 +1111,10 @@ void Trans_Init()

while ((c = fgetc(trans_fp)) != EOF)
{
buffer.push_back((char)c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

Trans_ParseLangLine((char *)buffer.c_str());
Expand Down Expand Up @@ -1157,10 +1159,14 @@ void Trans_SetLanguage()
{
// if language has a territory field (like zh_TW or en_AU) then
// try again with the plain language code.

path = StringFormat("%s/language/%s.po", install_dir.c_str(), lang_plain.c_str());
}

if (!FileExists(path))
{
FatalError("WTF: %s\n", path.c_str());
}

FILE *fp = FileOpen(path, "rb");
if (!fp)
{
Expand Down
8 changes: 3 additions & 5 deletions source/sys_debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -347,14 +347,12 @@ void LogReadLines(log_display_func_t display_func, void *priv_data)
buffer.clear();
while ((c = fgetc(log_file)) != EOF)
{
buffer.push_back(c);
if (c == '\n')
if (c == '\n' || c == '\r')
break;
else
buffer.push_back(c);
}

// remove any newline at the end (LF or CR/LF)
StringRemoveCRLF(&buffer);

// remove any DEL characters (mainly to workaround an FLTK bug)
StringReplaceChar(&buffer, 0x7f, 0);

Expand Down

0 comments on commit 3af5087

Please sign in to comment.