autfilt, dstar2tgba: add CSV input

Fixes #91.

* bin/autfilt.cc, bin/dstar2tgba.cc: Implement reading CSV files.
* bin/common_finput.cc: Fix comments.
* bin/common_aoutput.cc: Show %<, %> in help text.
* NEWS, doc/org/csv.org: Document it.
* tests/core/readsave.test: Add a short test case.
This commit is contained in:
Alexandre Duret-Lutz 2016-08-08 12:21:34 +02:00
parent f423c424eb
commit ca0d81b5d7
7 changed files with 229 additions and 51 deletions

View file

@ -24,6 +24,8 @@
#include <limits>
#include <set>
#include <memory>
#include <sys/stat.h>
#include <unistd.h>
#include <argp.h>
#include "error.h"
@ -874,6 +876,90 @@ namespace
SPOT_UNREACHABLE();
}
int process_string(const std::string& input, const char* filename,
int linenum) override
{
std::ostringstream loc;
loc << filename << ':' << linenum;
std::string locstr = loc.str();
return process_automaton_stream
(spot::automaton_stream_parser(input.c_str(), locstr, opt_parse),
locstr.c_str());
}
int
aborted(const spot::const_parsed_aut_ptr& h, const char* filename)
{
std::cerr << filename << ':' << h->loc << ": aborted input automaton\n";
return 2;
}
int
process_file(const char* filename) override
{
// If we have a filename like "foo/NN" such
// that:
// ① foo/NN is not a file,
// ② NN is a number,
// ③ foo is a file,
// then it means we want to open foo as
// a CSV file and process column NN.
if (const char* slash = strrchr(filename, '/'))
{
char* end;
errno = 0;
long int col = strtol(slash + 1, &end, 10);
if (errno == 0 && !*end && col != 0)
{
struct stat buf;
if (stat(filename, &buf) != 0)
{
col_to_read = col;
if (real_filename)
free(real_filename);
real_filename = strndup(filename, slash - filename);
// Special case for stdin.
if (real_filename[0] == '-' && real_filename[1] == 0)
return process_stream(std::cin, real_filename);
std::ifstream input(real_filename);
if (input)
return process_stream(input, real_filename);
error(2, errno, "cannot open '%s' nor '%s'",
filename, real_filename);
}
}
}
return process_automaton_stream(spot::automaton_stream_parser(filename,
opt_parse),
filename);
}
int process_automaton_stream(spot::automaton_stream_parser&& hp,
const char* filename)
{
int err = 0;
while (!abort_run)
{
auto haut = hp.parse(opt->dict);
if (!haut->aut && haut->errors.empty())
break;
if (haut->format_errors(std::cerr))
err = 2;
if (!haut->aut)
error(2, 0, "failed to read automaton from %s", filename);
else if (haut->aborted)
err = std::max(err, aborted(haut, filename));
else
process_automaton(haut, filename);
}
return err;
}
int
process_automaton(const spot::const_parsed_aut_ptr& haut,
const char* filename)
@ -1111,42 +1197,14 @@ namespace
++match_count;
printer.print(aut, nullptr, filename, -1, conversion_time, haut);
printer.print(aut, nullptr, filename, -1, conversion_time, haut,
prefix, suffix);
if (opt_max_count >= 0 && match_count >= opt_max_count)
abort_run = true;
return 0;
}
int
aborted(const spot::const_parsed_aut_ptr& h, const char* filename)
{
std::cerr << filename << ':' << h->loc << ": aborted input automaton\n";
return 2;
}
int
process_file(const char* filename) override
{
auto hp = spot::automaton_stream_parser(filename, opt_parse);
int err = 0;
while (!abort_run)
{
auto haut = hp.parse(opt->dict);
if (!haut->aut && haut->errors.empty())
break;
if (haut->format_errors(std::cerr))
err = 2;
if (!haut->aut)
error(2, 0, "failed to read automaton from %s", filename);
else if (haut->aborted)
err = std::max(err, aborted(haut, filename));
else
process_automaton(haut, filename);
}
return err;
}
};
}
@ -1155,7 +1213,7 @@ main(int argc, char** argv)
{
setup(argv);
const argp ap = { options, parse_opt, "[FILENAMES...]",
const argp ap = { options, parse_opt, "[FILENAME[/COL]...]",
argp_program_doc, children, nullptr, nullptr };
try

View file

@ -181,6 +181,12 @@ static const argp_option io_options[] =
"one word accepted by the output automaton", 0 },
{ "%%", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
"a single %", 0 },
{ "%<", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
"the part of the line before the automaton if it "
"comes from a column extracted from a CSV file", 4 },
{ "%>", 0, nullptr, OPTION_DOC | OPTION_NO_USAGE,
"the part of the line after the automaton if it "
"comes from a column extracted from a CSV file", 4 },
{ nullptr, 0, nullptr, 0, nullptr, 0 }
};

View file

@ -305,7 +305,7 @@ job_processor::process_file(const char* filename)
// If we have a filename like "foo/NN" such
// that:
// ① foo/NN is not a file (already the case),
// ② NN is a number > 0,
// ② NN is a number,
// ③ foo is a file,
// then it means we want to open foo as
// a CSV file and process column NN.
@ -315,7 +315,6 @@ job_processor::process_file(const char* filename)
char* end;
errno = 0;
long int col = strtol(slash + 1, &end, 10);
// strtol ate all remaining characters and NN is positive
if (errno == 0 && !*end && col != 0)
{
col_to_read = col;

View file

@ -22,6 +22,8 @@
#include <string>
#include <iostream>
#include <memory>
#include <sys/stat.h>
#include <unistd.h>
#include <argp.h>
#include "error.h"
@ -123,19 +125,15 @@ namespace
SPOT_UNREACHABLE();
}
int
process_automaton(const spot::const_parsed_aut_ptr& haut,
const char* filename)
int process_string(const std::string& input, const char* filename,
int linenum) override
{
spot::stopwatch sw;
sw.start();
auto nba = spot::to_generalized_buchi(haut->aut);
auto aut = post.run(nba, nullptr);
const double conversion_time = sw.stop();
printer.print(aut, nullptr, filename, -1, conversion_time, haut);
flush_cout();
return 0;
std::ostringstream loc;
loc << filename << ':' << linenum;
std::string locstr = loc.str();
return process_automaton_stream
(spot::automaton_stream_parser(input.c_str(), locstr, opt_parse),
locstr.c_str());
}
int
@ -148,7 +146,50 @@ namespace
int
process_file(const char* filename) override
{
auto hp = spot::automaton_stream_parser(filename, opt_parse);
// If we have a filename like "foo/NN" such
// that:
// ① foo/NN is not a file,
// ② NN is a number,
// ③ foo is a file,
// then it means we want to open foo as
// a CSV file and process column NN.
if (const char* slash = strrchr(filename, '/'))
{
char* end;
errno = 0;
long int col = strtol(slash + 1, &end, 10);
if (errno == 0 && !*end && col != 0)
{
struct stat buf;
if (stat(filename, &buf) != 0)
{
col_to_read = col;
if (real_filename)
free(real_filename);
real_filename = strndup(filename, slash - filename);
// Special case for stdin.
if (real_filename[0] == '-' && real_filename[1] == 0)
return process_stream(std::cin, real_filename);
std::ifstream input(real_filename);
if (input)
return process_stream(input, real_filename);
error(2, errno, "cannot open '%s' nor '%s'",
filename, real_filename);
}
}
}
return process_automaton_stream(spot::automaton_stream_parser(filename,
opt_parse),
filename);
}
int process_automaton_stream(spot::automaton_stream_parser&& hp,
const char* filename)
{
int err = 0;
while (!abort_run)
{
@ -166,6 +207,22 @@ namespace
}
return err;
}
int
process_automaton(const spot::const_parsed_aut_ptr& haut,
const char* filename)
{
spot::stopwatch sw;
sw.start();
auto nba = spot::to_generalized_buchi(haut->aut);
auto aut = post.run(nba, nullptr);
const double conversion_time = sw.stop();
printer.print(aut, nullptr, filename, -1, conversion_time, haut);
flush_cout();
return 0;
}
};
}
@ -174,7 +231,7 @@ main(int argc, char** argv)
{
setup(argv);
const argp ap = { options, parse_opt, "[FILENAMES...]",
const argp ap = { options, parse_opt, "[FILENAME[/COL]...]",
argp_program_doc, children, nullptr, nullptr };
if (int err = argp_parse(&ap, argc, argv, ARGP_NO_HELP, nullptr, nullptr))