Skip to content

Commit

Permalink
Default stdin command changes, issue #339
Browse files Browse the repository at this point in the history
  • Loading branch information
NikitaS20 committed Oct 8, 2024
1 parent 2be4922 commit c0f7d2a
Show file tree
Hide file tree
Showing 10 changed files with 115 additions and 15 deletions.
1 change: 1 addition & 0 deletions modules/device/include/ifm3d/device/err.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
14 changes: 14 additions & 0 deletions modules/device/include/ifm3d/device/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
#include <string>
#include <vector>
#include <ifm3d/device/module_device.h>
#include <iostream>
#ifdef _WIN32
# ifndef WIN32_LEAN_AND_MEAN
# define WIN32_LEAN_AND_MEAN
# endif
# include <windows.h>
# include <io.h>
#elif __unix__
# include <sys/select.h>
# include <unistd.h>
# include <cstdio>
#endif

namespace ifm3d
{
Expand Down Expand Up @@ -37,6 +49,8 @@ namespace ifm3d
IFM3D_EXPORT std::vector<std::string> split(const std::string& in,
char delim);

IFM3D_EXPORT bool IsStdinAvailable(int timeoutSeconds = 2);

} // end: namespace ifm3d

#endif // IFM3D_CAMERA_UTIL_H
4 changes: 4 additions & 0 deletions modules/device/src/libifm3d_device/err.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
64 changes: 64 additions & 0 deletions modules/device/src/libifm3d_device/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion modules/framegrabber/include/ifm3d/fg/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ namespace ifm3d
/** @hideinitializer
* @brief The point cloud encoded as a 3 channel XYZ image
*/
XYZ = std::numeric_limits<std::uint32_t>::max(),
XYZ = (std::numeric_limits<std::uint32_t>::max)(),

/** @hideinitializer
* @brief \c
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <ifm3d/device/err.h>
#include <ifm3d/common/logging/log.h>
#include <ifm3d/common/json.hpp>
#include <ifm3d/device/util.h>

#ifdef _WIN32
# include <io.h>
Expand Down
15 changes: 12 additions & 3 deletions modules/tools/src/libifm3d_tools/common/config_set_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <string>
#include <ifm3d/device/device.h>
#include <ifm3d/device/err.h>
#include <ifm3d/device/util.h>

ifm3d::ConfigSetApp::~ConfigSetApp() {}

Expand All @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
16 changes: 12 additions & 4 deletions modules/tools/src/libifm3d_tools/legacy/import_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <string>
#include <vector>
#include <ifm3d/device.h>
#include <ifm3d/device/util.h>

ifm3d::ImportApp::~ImportApp() {}

Expand All @@ -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<std::uint8_t*>(&b)));
}
}
else
{
bytes.push_back(*(reinterpret_cast<std::uint8_t*>(&b)));
throw ifm3d::Error(IFM3D_NO_INPUT_PROVIDED);
}
}
else
Expand Down
2 changes: 1 addition & 1 deletion modules/tools/src/libifm3d_tools/main_command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,4 @@ ifm3d::MainCommand::GetDevice(bool throwIfUnavailable)
this->xmlrpc_port,
this->password,
throwIfUnavailable);
}
}

0 comments on commit c0f7d2a

Please sign in to comment.