# HG changeset patch # User František Kučera # Date 1557260516 -7200 # Node ID f937ad57351fa672fedd7d4d7bc51fdd4eaff852 # Parent e4558df9ba2d6aa2d46c09518b4d4a0d9d1d3bbd read and un-escape the AWK output, convert to relational attributes diff -r e4558df9ba2d -r f937ad57351f src/AwkHandler.h --- a/src/AwkHandler.h Tue May 07 21:33:59 2019 +0200 +++ b/src/AwkHandler.h Tue May 07 22:21:56 2019 +0200 @@ -156,6 +156,36 @@ return value; } + void processAwkOutput(int awkOutputReaderFD) { + locale::global(locale("")); // needed for processing unicode texts, otherwise getline() stopped working on first line with non-ascii characters; TODO: move somewhere else? + + __gnu_cxx::stdio_filebuf awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in); + std::wistream awkOutputReader(&awkOutputReaderBuffer); + + if (currentRelationConfiguration->drop) { + for (wchar_t ch; awkOutputReader.read(&ch, 1).good();); // just eat the lines from the AWK + } else { + std::wstringstream currentValue; + for (wchar_t ch; awkOutputReader.read(&ch, 1).good();) { + if (ch == '\t' || ch == '\n') { + relationalWriter->writeAttribute(currentValue.str()); + currentValue.str(L""); + currentValue.clear(); + } else if (ch == '\\') { + ch = awkOutputReader.get(); + if (ch == 't') currentValue << L'\t'; + else if (ch == 'n') currentValue << L'\n'; + else if (ch == '\\') currentValue << L'\\'; + else throw cli::RelpipeCLIException(L"Unknown escape sequence. Only \\t, \\n and \\\\ are supported.", cli::CLI::EXIT_CODE_UNEXPECTED_ERROR); // TODO: better exceptions? + } else { + currentValue << ch; + } + } + } + + closeOrThrow(awkOutputReaderFD); + } + public: /** @@ -257,11 +287,6 @@ // Writer child process closeOrThrow(awkInputWriterFD); - locale::global(locale("")); // needed for processing unicode texts, otherwise getline() stopped working on first line with non-ascii characters; TODO: move somewhere else? - - __gnu_cxx::stdio_filebuf awkOutputReaderBuffer(awkOutputReaderFD, std::ios::in); - std::wistream awkOutputReader(&awkOutputReaderBuffer); - if (currentRelationConfiguration->drop) { // TODO: omit whole this process and pipe AWK output to /dev/null? } else { @@ -271,15 +296,7 @@ }, true); } - for (string_t line; getline(awkOutputReader, line).good();) { - if (currentRelationConfiguration->drop) { - // just eat the lines from the AWK - } else { - relationalWriter->writeAttribute(line); - } - } - - closeOrThrow(awkOutputReaderFD); + processAwkOutput(awkOutputReaderFD); exit(0); } else { // Parent process