Skip to content

Commit

Permalink
the command yarp read ... /port envelope now displays the envelope …
Browse files Browse the repository at this point in the history
…inline with the data, while the command `yarp read ... /port envelope2` displays the envelope and the data on two different lines
  • Loading branch information
randaz81 committed Dec 3, 2024
1 parent 3636677 commit 1d07ba9
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 18 deletions.
59 changes: 47 additions & 12 deletions src/libYARP_companion/src/yarp/companion/impl/BottleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#include <yarp/os/Semaphore.h>
#include <yarp/os/impl/BottleImpl.h>

#include <sstream>
#include <iomanip>

using yarp::os::Bottle;
using yarp::os::Contact;
using yarp::os::ConnectionReader;
Expand All @@ -36,7 +39,7 @@ class BottleReader :
private:
yarp::os::Semaphore done;
bool raw;
bool env;
Companion::showEnvelopeEnum eShowEnvelope;
std::string::size_type trim;
bool justOnce;
Contact address;
Expand All @@ -45,15 +48,15 @@ class BottleReader :

BottleReader() : done(0) {
raw = false;
env = false;
eShowEnvelope = Companion::showEnvelopeEnum::do_not_show;
trim = std::string::npos;
justOnce = false;
core.setReader(*this);
core.setReadOnly();
}

void open(const char *name, bool showEnvelope, bool _justonce, int trim_at = -1) {
env = showEnvelope;
void open(const char *name, Companion::showEnvelopeEnum _showEnvelope, bool _justonce, int trim_at = -1) {
eShowEnvelope = _showEnvelope;
justOnce = _justonce;
trim = (trim_at > 0 ? static_cast<std::string::size_type>(trim_at) : std::string::npos);
if (core.open(name)) {
Expand All @@ -70,14 +73,20 @@ class BottleReader :
Companion::setActivePort(nullptr);
}

void showEnvelope() {
if (env) {
std::string showEnvelope() {
if (eShowEnvelope != Companion::showEnvelopeEnum::do_not_show) {
Bottle envelope;
core.getEnvelope(envelope);
if (envelope.size()>0) {
yCInfo(COMPANION, "%s ", envelope.toString().c_str());
if (envelope.size()>0)
{
std::ostringstream stream;
stream << std::fixed << std::setprecision(4) << envelope.get(1).asFloat64();
std::string formattedValue = stream.str();

return envelope.get(0).toString() + " " + formattedValue;
}
}
return "";
}

bool read(ConnectionReader& reader) override {
Expand All @@ -90,17 +99,43 @@ class BottleReader :
if (bot.size()==2 && bot.isInt32(0) && bot.isString(1) && !raw) {
int code = bot.get(0).asInt32();
if (code!=1) {
showEnvelope();
yCInfo(COMPANION, "%s", bot.get(1).asString().substr(0, trim).c_str());
if (eShowEnvelope == Companion::showEnvelopeEnum::do_not_show)
{
yCInfo(COMPANION, "%s", bot.get(1).asString().substr(0, trim).c_str());
}
else if (eShowEnvelope == Companion::showEnvelopeEnum::show_inline)
{
std::string envstring = showEnvelope();
yCInfo(COMPANION, "%s %s", envstring.c_str(), bot.get(1).asString().substr(0, trim).c_str());
}
else if (eShowEnvelope == Companion::showEnvelopeEnum::show_two_lines)
{
std::string envstring = showEnvelope();
yCInfo(COMPANION, "%s", envstring.c_str());
yCInfo(COMPANION, "%s", bot.get(1).asString().substr(0, trim).c_str());
}
fflush(stdout);
}
if (code==1) {
done.post();
}
} else {
// raw = true; // don't make raw mode "sticky"
showEnvelope();
yCInfo(COMPANION, "%s", bot.toString().substr(0, trim).c_str());
if (eShowEnvelope == Companion::showEnvelopeEnum::do_not_show)
{
yCInfo(COMPANION, "%s", bot.toString().substr(0, trim).c_str());
}
if (eShowEnvelope == Companion::showEnvelopeEnum::show_inline)
{
std::string envstring = showEnvelope();
yCInfo(COMPANION, "%s %s", envstring.c_str(), bot.toString().substr(0, trim).c_str());
}
else if (eShowEnvelope == Companion::showEnvelopeEnum::show_two_lines)
{
std::string envstring = showEnvelope();
yCInfo(COMPANION, "%s", envstring.c_str());
yCInfo(COMPANION, "%s", bot.toString().substr(0, trim).c_str());
}
fflush(stdout);
}
if (justOnce) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using yarp::os::NetworkBase;
* @param trim number of characters of the string that should be printed
* @return 0 on success, non-zero on failure
*/
int Companion::read(const char *name, const char *src, bool showEnvelope, bool justOnce, int trim)
int Companion::read(const char *name, const char *src, Companion::showEnvelopeEnum showEnvelope, bool justOnce, int trim)
{
Companion::installHandler();
BottleReader reader;
Expand Down Expand Up @@ -58,12 +58,15 @@ int Companion::cmdRead(int argc, char *argv[])

const char *name = argv[0];
const char *src = nullptr;
bool showEnvelope = false;
showEnvelopeEnum showEnvelope = showEnvelopeEnum::do_not_show;
size_t trim = -1;
bool justOnce = false;
bool envelopeInline = true;
while (argc>1) {
if (strcmp(argv[1], "envelope")==0) {
showEnvelope = true;
if (strcmp(argv[1], "envelope") == 0) {
showEnvelope = showEnvelopeEnum::show_inline;
} else if (strcmp(argv[1], "envelope2") == 0) {
showEnvelope = showEnvelopeEnum::show_two_lines;
} else if (strcmp(argv[1], "trim") == 0) {
argc--;
argv++;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ int Companion::cmdReadWrite(int argc, char *argv[])

Companion::installHandler();
BottleReader reader;
reader.open(read_port_name, false, false);
reader.open(read_port_name, Companion::showEnvelopeEnum::do_not_show, false);

int ret = write(write_port_name, 1, (char**)&verbatim);

Expand Down
8 changes: 7 additions & 1 deletion src/libYARP_companion/src/yarp/companion/impl/Companion.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ class YARP_companion_API Companion

int dispatch(const char *name, int argc, char *argv[]);

enum showEnvelopeEnum
{
do_not_show = 0,
show_inline = 1,
show_two_lines = 2
};

// Defined in Companion.cmdCheck.cpp
int cmdCheck(int argc, char *argv[]);
Expand Down Expand Up @@ -102,7 +108,7 @@ class YARP_companion_API Companion
int cmdPrioritySched(int argc, char *argv[]);

// Defined in Companion.cmdRead.cpp
int read(const char *name, const char *src = nullptr, bool showEnvelope = false, bool justOnce = false, int trim = -1);
int read(const char *name, const char *src = nullptr, showEnvelopeEnum showEnvelope = showEnvelopeEnum::do_not_show, bool justOnce = false, int trim = -1);
int cmdRead(int argc, char *argv[]);

// Defined in Companion.cmdReadWrite.cpp
Expand Down

0 comments on commit 1d07ba9

Please sign in to comment.