From eeaf910c27245d301f3955c54181f6436c27d20b Mon Sep 17 00:00:00 2001 From: Michael Ochmann Date: Wed, 22 Feb 2023 10:50:01 +0100 Subject: [PATCH] added option to take data from parameter instead of stdin --- main.cpp | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/main.cpp b/main.cpp index 27abb04..d639751 100644 --- a/main.cpp +++ b/main.cpp @@ -2,6 +2,7 @@ #include #include #include + #include "QR.hpp" #define UNUSED(var) (void) var; @@ -17,6 +18,7 @@ Released under MIT license. Usage: -f --format output file format. can be one of "cli, png, svg" -h --help show this help + -i --input take data from this argument instead of stdin -o --output output file name -s --size desired output file size in pixels -t --type output QR code type. can be one of "small, medium, large" @@ -32,6 +34,7 @@ int main(int argc, char* argv[]) { } const option options[] = { {"help", no_argument, nullptr, 'h'}, + {"input", required_argument, nullptr, 'i'}, {"output", required_argument, nullptr, 'o'}, {"size", required_argument, nullptr, 's'}, {"type", required_argument, nullptr, 't'}, @@ -43,10 +46,12 @@ int main(int argc, char* argv[]) { size_t segmentSize = 0; massivedynamic::Type type = massivedynamic::Type::MEDIUM; massivedynamic::Format format = massivedynamic::Format::CONSOLE; + bool fromStdin = true; + std::string paramData = ""; for(;;) { int index = -1; - int result = getopt_long(argc, argv, "ho:s:t:f:", options, &index); + int result = getopt_long(argc, argv, "ho:s:t:f:i:", options, &index); if (result == -1) break; @@ -90,6 +95,11 @@ int main(int argc, char* argv[]) { } break; } + case 'i': { + fromStdin = false; + paramData = optarg; + break; + } case 'h': default: printHelp(); @@ -100,23 +110,31 @@ int main(int argc, char* argv[]) { std::string line; std::stringstream data; - if (!isatty(fileno(stdin))) { - while (std::getline(std::cin, line)) { - if (std::cin.eof() || line.empty()) { - std::cin.clear(); - break; + if (fromStdin) { + if (!isatty(fileno(stdin))) { + while (std::getline(std::cin, line)) { + if (std::cin.eof() || line.empty()) { + std::cin.clear(); + break; + } + data << line; } - data << line; } - } - if (isatty(fileno(stdin)) || data.str().empty()) { - std::cerr << "ERROR: no data from stdinput." << std::endl; + if (isatty(fileno(stdin)) || data.str().empty()) { + std::cerr << "ERROR: no data from stdinput." << std::endl; + exit(1); + } + } else + data << paramData; + + if (data.str().empty()) { + std::cerr << "ERROR: input data is empty" << std::endl; exit(1); } if ((format == massivedynamic::Format::PNG || format == massivedynamic::Format::SVG) && outputFile.empty()) { - std::cerr << "output file name (-o, --output) can not be empty" << std::endl; + std::cerr << "ERROR: output file name (-o, --output) can not be empty" << std::endl; return 1; }