Skip to content

Commit

Permalink
Use stdutils::io for file parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
pierre-dejoue committed Aug 10, 2023
1 parent 0c8b825 commit 6819470
Show file tree
Hide file tree
Showing 6 changed files with 688 additions and 44 deletions.
65 changes: 21 additions & 44 deletions src/picross/src/picross_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
******************************************************************************/
#include <picross/picross_io.h>

#include <stdutils/io.h>
#include <stdutils/macros.h>
#include <stdutils/string.h>

Expand Down Expand Up @@ -62,8 +63,6 @@ std::string str_error_code(ErrorCodeT code)
namespace
{

constexpr unsigned int BUF_SZ = 2048u;

struct FileFormat
{
struct Native {};
Expand Down Expand Up @@ -110,7 +109,7 @@ class FileParser<FileFormat::Native>
std::istringstream iss(line_to_parse);
std::string token;

// Copy the first word in 'token'
// Copy the first word in 'token' (trailing whitespaces are skipped)
iss >> token;

if (token == "GRID")
Expand Down Expand Up @@ -167,14 +166,11 @@ class FileParser<FileFormat::Native>
}
else if (token == "#")
{
// Comment line are ignored
}
else if (token.empty())
{
// Empty lines are ignored
// Comment lines are ignored
}
else
{
assert(!token.empty()); // Blank lines are already filtered out
error_decorator(error_handler, "Invalid token " + token);
}
}
Expand Down Expand Up @@ -240,9 +236,7 @@ class FileParser<FileFormat::Nin>
{
UNUSED(error_handler);

// Ignore whiteline
if (line_to_parse.empty() || std::all_of(line_to_parse.cbegin(), line_to_parse.cend(), [](char c) { return c == '\t' || c == ' '; } ))
return;
// Blank lines are already filtered out

// Ignore comments
if (line_to_parse[0] == '#')
Expand Down Expand Up @@ -390,12 +384,9 @@ class FileParser<FileFormat::Non>
{
// Copy the first word in 'token'
iss >> token;
assert(!token.empty()); // Blank lines are already filtered out

if (token.empty())
{
// An empty line outside of the rows or columns sections can just be ignored
}
else if (token == "title")
if (token == "title")
{
std::stringbuf remaining;
iss >> &remaining;
Expand Down Expand Up @@ -525,8 +516,7 @@ class FileParser<FileFormat::Non>
if (pos0 != std::string::npos && pos1 != std::string::npos)
{
// Extract text in quotes
assert(pos1 > pos0);
return str.substr(pos0 + 1, pos1 - pos0 - 1);
return (pos0 + 1 < pos1) ? str.substr(pos0 + 1, pos1 - pos0 - 1) : "";
}
else
{
Expand Down Expand Up @@ -600,32 +590,19 @@ std::vector<IOGrid> parse_input_file_generic(std::string_view filepath, const Er
std::ifstream inputstream(filepath.data());
if (inputstream.is_open())
{
// Start line by line parsing
FileParser<F> parser;
// Line buffer and stream
char line_buf[BUF_SZ];
std::stringstream line_ss;
unsigned int line_nb = 0;
// Start parsing
while (inputstream.good())
auto line_stream = stdutils::io::SkipLineStream(inputstream).skip_blank_lines();
std::string line;
while (line_stream.getline(line))
{
const bool fail = inputstream.getline(line_buf, BUF_SZ).fail();
line_ss << line_buf;
if (!fail)
{
line_nb++;
std::string line = line_ss.str();
parser.parse_line(line, grids, [line_nb, &line, &error_handler](std::string_view msg)
{
std::ostringstream oss;
oss << "[" << msg << "] on line " << line_nb << ": " << line;
error_handler(ErrorCode::PARSING_ERROR, oss.str());
});
line_ss.str("");
}
else if (!inputstream.eof())
{
inputstream.clear(); // Line is longer than BUF_SZ
}
const auto line_nb = line_stream.line_nb();
parser.parse_line(line, grids, [line_nb, &line, &error_handler](std::string_view msg)
{
std::ostringstream oss;
oss << "[" << msg << "] on line " << line_nb << ": " << line;
error_handler(ErrorCode::PARSING_ERROR, oss.str());
});
}
std::for_each(grids.begin(), grids.end(), [&result](GridComponents& grid_comps) {
auto& new_grid = result.emplace_back(
Expand Down Expand Up @@ -704,11 +681,11 @@ void write_metadata_non_format(std::ostream& out, const std::map<std::string, st
const std::string quote = quoted ? "\"" : "";
if (meta.count(key) && !meta.at(key).empty())
{
out << key << ' ' << quote << meta.at(key) << quote << std::endl;
out << key << " " << quote << meta.at(key) << quote << std::endl;
}
else if (!quote.empty())
{
out << key << ' ' << quote << quote << std::endl;
out << key << " " << quote << quote << std::endl;
}
else
{
Expand Down
1 change: 1 addition & 0 deletions src/stdutils/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
include(compiler_options)

set(LIB_SOURCES
src/io.cpp
src/platform.cpp
src/string.cpp
)
Expand Down
Loading

0 comments on commit 6819470

Please sign in to comment.