Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance rs-terminal with files input #9271

Merged
merged 13 commits into from
Jul 18, 2021
8 changes: 4 additions & 4 deletions common/model-views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@ namespace rs2
cur_len--;
return cur_len - 2;
};
int num_of_decimal_digits_displayed = num_of_decimal_digits(range.step);
int num_of_decimal_digits_displayed = (int)num_of_decimal_digits(range.step);

// displaying in cm instead of meters for D405
if (use_cm_units)
Expand Down Expand Up @@ -5242,7 +5242,7 @@ namespace rs2
default_path.c_str(), default_filename.c_str()))
{
path = ret;
if (!ends_with(to_lower(path), ".bag")) path += ".bag";
if (!ends_with(utilities::string::to_lower(path), ".bag")) path += ".bag";
}
}

Expand Down Expand Up @@ -5713,7 +5713,7 @@ namespace rs2
for (auto&& p : sub->get_selected_profiles())
{
rs2_stream stream_type = p.stream_type();
std::string stream_format_key = to_string() << "stream-" << to_lower(rs2_stream_to_string(stream_type)) << "-format";
std::string stream_format_key = to_string() << "stream-" << utilities::string::to_lower(rs2_stream_to_string(stream_type)) << "-format";
std::string stream_format_value = rs2_format_to_string(p.format());

if (stream_type == RS2_STREAM_DEPTH)
Expand Down Expand Up @@ -6085,7 +6085,7 @@ namespace rs2

const auto save_to_json = [&, serializable](std::string full_filename)
{
if (!ends_with(to_lower(full_filename), ".json")) full_filename += ".json";
if (!ends_with(utilities::string::to_lower(full_filename), ".json")) full_filename += ".json";
std::ofstream outfile(full_filename);
json saved_configuraion;
if (serializable)
Expand Down
1 change: 1 addition & 0 deletions common/output-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <librealsense2/hpp/rs_internal.hpp>

using namespace rs2;
using namespace utilities::string;

void output_model::thread_loop()
{
Expand Down
118 changes: 48 additions & 70 deletions common/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <string>
#include <map>
#include <regex>
#include <fstream>
#include <sstream>
#include <vector>
Expand All @@ -14,6 +15,7 @@
#endif

#include "../third-party/rapidxml/rapidxml.hpp"
#include "./utilities/string/string-utilities.h"

#define MAX_PARAMS 4

Expand Down Expand Up @@ -194,50 +196,26 @@ struct commands_xml
std::map<std::string, custom_formatter> custom_formatters;
};

inline std::string hexify(unsigned char n)
// While working from files, convert the file content into recognized raw format
inline void file_argument_to_blob(std::vector<std::string>& params)
{
std::string res;

do
if (!params.size())
return;

//The last parameter provided by the user can represent a fully-qualified file path
std::ifstream file(params.back(), std::ios::binary | std::ios::in);
if (file.good())
{
res += "0123456789ABCDEF"[n % 16];
n >>= 4;
} while (n);

std::reverse(res.begin(), res.end());

if (res.size() == 1)
{
res.insert(0, "0");
auto data = std::vector<uint8_t>((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());

size_t unmodified_params = params.size() - 1; // non-negative due to the file parameter
std::vector<std::string> modified_params(unmodified_params + data.size());
if (unmodified_params) // copy leading parameters
std::copy(modified_params.begin(), modified_params.begin() + unmodified_params - 1,params.begin());
maloel marked this conversation as resolved.
Show resolved Hide resolved
std::transform(data.begin(), data.end(), modified_params.begin() + unmodified_params, [](uint8_t c) { return utilities::string::hexify(c); });
params = modified_params; // substitute original params with modified data
}

return res;
}

inline std::string to_lower(std::string x)
{
transform(x.begin(), x.end(), x.begin(), tolower);
return x;
}

inline bool string_to_bool(const std::string& x)
{
return (to_lower(x) == "true");
}

inline unsigned int string_to_hex(std::string str)
{
std::string delimiter = "x";

auto pos = str.find(delimiter);
str.erase(0, pos + delimiter.length());

std::stringstream ss;
unsigned int value;
ss << str;
ss >> std::hex >> value;

return value;
}

inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
Expand All @@ -259,10 +237,10 @@ inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
std::string value = AttI->value();
std::string att_name = AttI->name();
if (att_name == "Name") { cmd.name = value; }
else if (att_name == "Opcode") { cmd.op_code = string_to_hex(value); }
else if (att_name == "IsWriteOnly") { cmd.is_write_only = string_to_bool(value); }
else if (att_name == "Opcode") { cmd.op_code = utilities::string::ascii_hex_string_to_uint(value); }
else if (att_name == "IsWriteOnly") { cmd.is_write_only = utilities::string::string_to_bool(value); }
else if (att_name == "ReadFormat") { cmd.read_format = value; }
else if (att_name == "IsReadCommand") { cmd.is_read_command = string_to_bool(value); }
else if (att_name == "IsReadCommand") { cmd.is_read_command = utilities::string::string_to_bool(value); }
else if (att_name == "CmdPermission") { cmd.cmd_permission = value; }
else if (att_name == "TimeOut") { cmd.time_out = stoi(value); }
else if (att_name == "Description") { cmd.description = value; }
Expand All @@ -284,7 +262,7 @@ inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
std::string value = paramAtt->value();
std::string att_name = paramAtt->name();
if (att_name == "Name") { param.name = value; }
else if (att_name == "IsDecimal") { param.is_decimal = string_to_bool(value); }
else if (att_name == "IsDecimal") { param.is_decimal = utilities::string::string_to_bool(value); }
}
cmd.parameters.push_back(param);
}
Expand All @@ -299,7 +277,7 @@ inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
std::string data_att_name = dataAtt->name();

if (data_att_name == "Name") { param.name = value; }
else if (data_att_name == "IsReverseBytes") { param.is_reverse_bytes = string_to_bool(value); }
else if (data_att_name == "IsReverseBytes") { param.is_reverse_bytes = utilities::string::string_to_bool(value); }
else if (data_att_name == "FormatLength") { param.format_length = stoi(value); }
}
cmd.parameters.push_back(param);
Expand All @@ -323,7 +301,7 @@ inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
}
}
}
cmd_xml.commands.insert(std::pair<std::string, command_from_xml>(to_lower(cmd.name), cmd));
cmd_xml.commands.insert(std::pair<std::string, command_from_xml>(utilities::string::to_lower(cmd.name), cmd));
}
else if (!strcmp(NodeI->name(), "CustomFormatter"))
{
Expand Down Expand Up @@ -353,7 +331,7 @@ inline void parse_xml_from_memory(const char * content, commands_xml& cmd_xml)
}

}
cmd_xml.custom_formatters.insert(std::pair<std::string, custom_formatter>(to_lower(customFormatter.name), customFormatter));
cmd_xml.custom_formatters.insert(std::pair<std::string, custom_formatter>(utilities::string::to_lower(customFormatter.name), customFormatter));
}
}
}
Expand All @@ -362,7 +340,7 @@ inline bool parse_xml_from_file(const std::string& xml_full_file_path, commands_
{
std::ifstream fin(xml_full_file_path);

if (!fin)
if (!fin.good())
return false;

std::stringstream ss;
Expand Down Expand Up @@ -406,7 +384,7 @@ inline void update_format_type_to_lambda(std::map<std::string, xml_parser_functi
format_type_to_lambda.insert(std::make_pair("HexByte", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
check_section_size(sec.size, sizeof(HexByte), sec.name.c_str(), "HexByte");
auto hexByte = reinterpret_cast<const HexByte*>(data_offset + sec.offset);
tempStr << hexify(hexByte->Version4);
tempStr << utilities::string::hexify(hexByte->Version4);
}));

format_type_to_lambda.insert(std::make_pair("LiguriaVersion", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
Expand Down Expand Up @@ -447,39 +425,39 @@ inline void update_format_type_to_lambda(std::map<std::string, xml_parser_functi
format_type_to_lambda.insert(std::make_pair("HexNumber", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
check_section_size(sec.size, sizeof(HexNumber), sec.name.c_str(), "HexNumber");
auto hexNumber = reinterpret_cast<const HexNumber*>(data_offset + sec.offset);
tempStr << hexify(hexNumber->number1) <<
((sec.size >= 2) ? hexify(hexNumber->number2) : "") <<
((sec.size >= 3) ? hexify(hexNumber->number3) : "") <<
((sec.size >= 4) ? hexify(hexNumber->number4) : "");
tempStr << utilities::string::hexify(hexNumber->number1) <<
((sec.size >= 2) ? utilities::string::hexify(hexNumber->number2) : "") <<
((sec.size >= 3) ? utilities::string::hexify(hexNumber->number3) : "") <<
((sec.size >= 4) ? utilities::string::hexify(hexNumber->number4) : "");
}));

format_type_to_lambda.insert(std::make_pair("HexNumberTwoBytes", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
check_section_size(sec.size, sizeof(HexNumber), sec.name.c_str(), "HexNumber");
auto hexNumber = reinterpret_cast<const HexNumber*>(data_offset + sec.offset);
tempStr << hexify(hexNumber->number2) <<
((sec.size >= 2) ? hexify(hexNumber->number1) : "");
tempStr << utilities::string::hexify(hexNumber->number2) <<
((sec.size >= 2) ? utilities::string::hexify(hexNumber->number1) : "");
}));

format_type_to_lambda.insert(std::make_pair("HexNumberReversed", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
check_section_size(sec.size, sizeof(HexNumberReversed), sec.name.c_str(), "HexNumberReversed");
auto hexNumberReversed = reinterpret_cast<const HexNumberReversed*>(data_offset + sec.offset);
tempStr << hexify(hexNumberReversed->number4) <<
((sec.size >= 2) ? hexify(hexNumberReversed->number3) : "") <<
((sec.size >= 3) ? hexify(hexNumberReversed->number2) : "") <<
((sec.size >= 4) ? hexify(hexNumberReversed->number1) : "");
tempStr << utilities::string::hexify(hexNumberReversed->number4) <<
((sec.size >= 2) ? utilities::string::hexify(hexNumberReversed->number3) : "") <<
((sec.size >= 3) ? utilities::string::hexify(hexNumberReversed->number2) : "") <<
((sec.size >= 4) ? utilities::string::hexify(hexNumberReversed->number1) : "");
}));

format_type_to_lambda.insert(std::make_pair("BarCodeSerial12Char", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
check_section_size(sec.size, sizeof(BarCodeSerial12Char), sec.name.c_str(), "BarCodeSerial12Char");
auto barCodeSerial12Char = reinterpret_cast<const BarCodeSerial12Char*>(data_offset + sec.offset);
tempStr << hexify(barCodeSerial12Char->number1) <<
((sec.size >= 2) ? hexify(barCodeSerial12Char->number2) : "") <<
((sec.size >= 3) ? hexify(barCodeSerial12Char->number3) : "") <<
((sec.size >= 4) ? hexify(barCodeSerial12Char->number4) : "") <<
((sec.size >= 5) ? hexify(barCodeSerial12Char->number5) : "") <<
((sec.size >= 6) ? hexify(barCodeSerial12Char->number6) : "") <<
((sec.size >= 7) ? hexify(barCodeSerial12Char->number7) : "") <<
((sec.size >= 8) ? hexify(barCodeSerial12Char->number8) : "");
tempStr << utilities::string::hexify(barCodeSerial12Char->number1) <<
((sec.size >= 2) ? utilities::string::hexify(barCodeSerial12Char->number2) : "") <<
((sec.size >= 3) ? utilities::string::hexify(barCodeSerial12Char->number3) : "") <<
((sec.size >= 4) ? utilities::string::hexify(barCodeSerial12Char->number4) : "") <<
((sec.size >= 5) ? utilities::string::hexify(barCodeSerial12Char->number5) : "") <<
((sec.size >= 6) ? utilities::string::hexify(barCodeSerial12Char->number6) : "") <<
((sec.size >= 7) ? utilities::string::hexify(barCodeSerial12Char->number7) : "") <<
((sec.size >= 8) ? utilities::string::hexify(barCodeSerial12Char->number8) : "");
}));

format_type_to_lambda.insert(std::make_pair("WideMajorMinorVersion", [&](const uint8_t* data_offset, const section& sec, std::stringstream& tempStr) {
Expand Down Expand Up @@ -509,7 +487,7 @@ inline void update_sections_data(const uint8_t* data_offset, std::vector<section
else
{
// FormatType not found. searching in the custom format list
auto it = custom_formatters.find(to_lower(sec.format_type));
auto it = custom_formatters.find(utilities::string::to_lower(sec.format_type));
if (it != custom_formatters.end())
{
if (it->second.key_size == "Byte")
Expand Down Expand Up @@ -616,7 +594,7 @@ inline void decode_string_from_raw_data(const command_from_xml& command, const s
{
++bytes_per_line;
++bytes;
buffer.push_back(hexify(static_cast<unsigned char>(*(data_offset + offset))));
buffer.push_back(utilities::string::hexify(static_cast<unsigned char>(*(data_offset + offset))));

if (bytes == num_of_bytes_for_space)
{
Expand Down
Loading