diff --git a/modules/device/include/ifm3d/device/err.h b/modules/device/include/ifm3d/device/err.h index 7eb027c4..ae1b6c31 100644 --- a/modules/device/include/ifm3d/device/err.h +++ b/modules/device/include/ifm3d/device/err.h @@ -59,6 +59,7 @@ extern IFM3D_EXPORT const int IFM3D_CORRUPTED_STRUCT; extern IFM3D_EXPORT const int IFM3D_DEVICE_PORT_INCOMPATIBLE_WITH_ORGANIZER; extern IFM3D_EXPORT const int IFM3D_DEVICE_PORT_NOT_SUPPORTED; extern IFM3D_EXPORT const int IFM3D_INDEX_OUT_OF_RANGE; +extern IFM3D_EXPORT const int IFM3D_NO_INPUT_PROVIDED; // sensor errors extern IFM3D_EXPORT const int IFM3D_XMLRPC_OBJ_NOT_FOUND; extern IFM3D_EXPORT const int IFM3D_INVALID_PARAM; diff --git a/modules/device/include/ifm3d/device/util.h b/modules/device/include/ifm3d/device/util.h index 36e1c772..26663468 100644 --- a/modules/device/include/ifm3d/device/util.h +++ b/modules/device/include/ifm3d/device/util.h @@ -10,6 +10,18 @@ #include #include #include +#include +#ifdef _WIN32 +# ifndef WIN32_LEAN_AND_MEAN +# define WIN32_LEAN_AND_MEAN +# endif +# include +# include +#elif __unix__ +# include +# include +# include +#endif namespace ifm3d { @@ -37,6 +49,8 @@ namespace ifm3d IFM3D_EXPORT std::vector split(const std::string& in, char delim); + IFM3D_EXPORT bool IsStdinAvailable(int timeoutSeconds = 2); + } // end: namespace ifm3d #endif // IFM3D_CAMERA_UTIL_H diff --git a/modules/device/src/libifm3d_device/err.cpp b/modules/device/src/libifm3d_device/err.cpp index b29cc965..00ea79de 100644 --- a/modules/device/src/libifm3d_device/err.cpp +++ b/modules/device/src/libifm3d_device/err.cpp @@ -49,6 +49,7 @@ const int IFM3D_CORRUPTED_STRUCT = -100035; const int IFM3D_DEVICE_PORT_INCOMPATIBLE_WITH_ORGANIZER = -100036; const int IFM3D_DEVICE_PORT_NOT_SUPPORTED = -100037; const int IFM3D_INDEX_OUT_OF_RANGE = -100038; +const int IFM3D_NO_INPUT_PROVIDED = -100039; // sensor errors const int IFM3D_XMLRPC_OBJ_NOT_FOUND = 100000; const int IFM3D_INVALID_PARAM = 101000; @@ -163,6 +164,9 @@ ifm3d::strerror(int errnum) "organizers"; case IFM3D_DEVICE_PORT_NOT_SUPPORTED: return "Lib: Port is not supported by the device"; + case IFM3D_NO_INPUT_PROVIDED: + return "Lib: No input provided. Please provide input via stdin, or use " + "the correct flags/options to specify an input."; case IFM3D_INDEX_OUT_OF_RANGE: return "Lib: index is out of range"; case IFM3D_XMLRPC_OBJ_NOT_FOUND: diff --git a/modules/device/src/libifm3d_device/util.cpp b/modules/device/src/libifm3d_device/util.cpp index 87e75b35..ccd37661 100644 --- a/modules/device/src/libifm3d_device/util.cpp +++ b/modules/device/src/libifm3d_device/util.cpp @@ -40,3 +40,67 @@ ifm3d::split(const std::string& in, char delim) } return tokens; } + +bool +ifm3d::IsStdinAvailable(int timeoutSeconds) +{ +#ifdef _WIN32 + if (!(_isatty(_fileno(stdin)))) + { + HANDLE hInput = GetStdHandle(STD_INPUT_HANDLE); + + if (hInput == INVALID_HANDLE_VALUE) + { + std::cerr << "Error getting standard input handle." << std::endl; + return false; + } + + DWORD waitResult = WaitForSingleObject(hInput, timeoutSeconds * 1000); + + if (waitResult == WAIT_OBJECT_0) + { + return true; + } + else + { + return false; + } + } + else + { + return false; + } + +#elif __unix__ + if (!(isatty(fileno(stdin)))) + { + fd_set set; + + FD_ZERO(&set); + FD_SET(STDIN_FILENO, &set); + + struct timeval timeout; + + timeout.tv_sec = timeoutSeconds; + timeout.tv_usec = 0; + + int result = select(STDIN_FILENO + 1, &set, NULL, NULL, &timeout); + + if (result > 0) + { + return true; + } + else + { + return false; + } + } + else + { + return false; + } + +#else +# error Unsupported platform! Code may not behave as expected. +#endif +} \ No newline at end of file diff --git a/modules/framegrabber/include/ifm3d/fg/frame.h b/modules/framegrabber/include/ifm3d/fg/frame.h index a5e133cb..497845c7 100644 --- a/modules/framegrabber/include/ifm3d/fg/frame.h +++ b/modules/framegrabber/include/ifm3d/fg/frame.h @@ -178,7 +178,7 @@ namespace ifm3d /** @hideinitializer * @brief The point cloud encoded as a 3 channel XYZ image */ - XYZ = std::numeric_limits::max(), + XYZ = (std::numeric_limits::max)(), /** @hideinitializer * @brief \c diff --git a/modules/swupdater/src/libifm3d_swupdater/swupdater_impl.hpp b/modules/swupdater/src/libifm3d_swupdater/swupdater_impl.hpp index 5fb5449c..07c8f52b 100644 --- a/modules/swupdater/src/libifm3d_swupdater/swupdater_impl.hpp +++ b/modules/swupdater/src/libifm3d_swupdater/swupdater_impl.hpp @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef _WIN32 # include diff --git a/modules/tools/src/libifm3d_tools/common/config_set_app.cpp b/modules/tools/src/libifm3d_tools/common/config_set_app.cpp index e23ced2d..343c1993 100644 --- a/modules/tools/src/libifm3d_tools/common/config_set_app.cpp +++ b/modules/tools/src/libifm3d_tools/common/config_set_app.cpp @@ -11,6 +11,7 @@ #include #include #include +#include ifm3d::ConfigSetApp::~ConfigSetApp() {} @@ -28,11 +29,19 @@ ifm3d::ConfigSetApp::Execute(CLI::App* app) } else if (infile == "-") { - std::string line; std::ostringstream buff; - while (std::getline(std::cin, line)) + + if (ifm3d::IsStdinAvailable()) + { + std::string line; + while (std::getline(std::cin, line)) + { + buff << line << std::endl; + } + } + else { - buff << line << std::endl; + throw ifm3d::Error(IFM3D_NO_INPUT_PROVIDED); } jstr.assign(buff.str()); diff --git a/modules/tools/src/libifm3d_tools/common/swupdater/flash_sw_app.cpp b/modules/tools/src/libifm3d_tools/common/swupdater/flash_sw_app.cpp index 0a503a0c..88a0b51f 100644 --- a/modules/tools/src/libifm3d_tools/common/swupdater/flash_sw_app.cpp +++ b/modules/tools/src/libifm3d_tools/common/swupdater/flash_sw_app.cpp @@ -168,12 +168,11 @@ ifm3d::FlashSWApp::CreateCommand(CLI::App* parent) swupdate_cmd->group(""); #endif - command - ->add_option("file", - this->swu_file, - "Input file, use `-` to read from stdin (good for reading " - "off a pipeline)") - ->required(); + command->add_option( + "file", + this->swu_file, + "Input file, use `-` to read from stdin (good for reading " + "off a pipeline)"); command ->add_flag("-q,--quiet", diff --git a/modules/tools/src/libifm3d_tools/legacy/import_app.cpp b/modules/tools/src/libifm3d_tools/legacy/import_app.cpp index d176f4e2..f8eb21bc 100644 --- a/modules/tools/src/libifm3d_tools/legacy/import_app.cpp +++ b/modules/tools/src/libifm3d_tools/legacy/import_app.cpp @@ -14,6 +14,7 @@ #include #include #include +#include ifm3d::ImportApp::~ImportApp() {} @@ -27,12 +28,19 @@ ifm3d::ImportApp::Execute(CLI::App* app) if (this->input_file == "-") { - ifs.reset(&std::cin, [](...) {}); + if (ifm3d::IsStdinAvailable()) + { + ifs.reset(&std::cin, [](...) {}); - char b; - while (ifs->get(b)) + char b; + while (ifs->get(b)) + { + bytes.push_back(*(reinterpret_cast(&b))); + } + } + else { - bytes.push_back(*(reinterpret_cast(&b))); + throw ifm3d::Error(IFM3D_NO_INPUT_PROVIDED); } } else diff --git a/modules/tools/src/libifm3d_tools/main_command.cpp b/modules/tools/src/libifm3d_tools/main_command.cpp index 4b91d90c..7243e6a4 100644 --- a/modules/tools/src/libifm3d_tools/main_command.cpp +++ b/modules/tools/src/libifm3d_tools/main_command.cpp @@ -155,4 +155,4 @@ ifm3d::MainCommand::GetDevice(bool throwIfUnavailable) this->xmlrpc_port, this->password, throwIfUnavailable); -} \ No newline at end of file +}