|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
40 bool singleton{
false};
47 singleton(rhs.singleton)
50 backing = std::make_unique<Property>(*(rhs.
backing));
59 backing = std::make_unique<Property>(*(rhs.
backing));
106 std::map<std::string, PropertyItem>
data;
116 auto it = data.find(key);
117 if (it == data.end()) {
125 auto entry = data.find(key);
126 if (entry == data.end()) {
131 entry = data.find(key);
133 yCAssert(PROPERTY, entry != data.end());
134 return &(entry->second);
137 void put(
const std::string& key,
const std::string& val)
174 p->
backing = std::make_unique<Property>();
183 bool check(
const std::string& key)
const
186 if (owner->getMonitor() !=
nullptr) {
189 report.isFound = (p !=
nullptr);
190 owner->reportToMonitor(report);
200 if (owner->getMonitor() !=
nullptr) {
203 report.isFound =
true;
205 owner->reportToMonitor(report);
209 if (owner->getMonitor() !=
nullptr) {
212 owner->reportToMonitor(report);
223 return putBottle(key, b);
225 return putBottle(key, val);
267 fromBottle(bot, wipe);
275 bool qualified =
false;
276 for (
int i = 0; i < argc; i++) {
277 std::string work = argv[i];
279 if (work.length() >= 2) {
280 if (work[0] ==
'-' && work[1] ==
'-') {
281 work = work.substr(2, work.length() - 2);
283 if (work.find(
"::") != std::string::npos) {
295 if (work.find(
'\\') != std::string::npos) {
300 for (
char i : work) {
315 fromBottle(total, wipe);
322 for (
size_t i = 0; i < total.
size(); i++) {
325 if (term ==
nullptr) {
329 std::string base = key;
330 while (key.length() > 0) {
332 size_t at = key.find(
"::");
333 if (at != std::string::npos) {
334 base = key.substr(0, at);
335 key = key.substr(at + 2);
341 if (cursor ==
nullptr) {
342 cursor = &putBottle((base).c_str());
351 if (cursor !=
nullptr) {
358 bool readDir(
const std::string& dirname, yarp::os::impl::DIR*& dir, std::string& result,
const std::string& section = std::string())
361 yCDebug(PROPERTY,
"reading directory %s", dirname.c_str());
363 yarp::os::impl::dirent** namelist;
364 yarp::os::impl::closedir(dir);
366 int n = yarp::os::impl::scandir(dirname.c_str(), &namelist,
nullptr, yarp::os::impl::alphasort);
370 for (
int i = 0; i < n; i++) {
371 std::string name = namelist[i]->d_name;
373 auto len =
static_cast<int>(name.length());
377 if (name.substr(len - 4) !=
".ini") {
380 std::string fname = std::string(dirname) +
"/" + name;
381 std::replace(fname.begin(), fname.end(),
'\\',
'/');
382 if (section.empty()) {
383 ok = ok && readFile(fname, result,
false);
386 result.append(
"[include ").append(section).append(
" \"").append(fname).append(
"\" \"").append(fname).append(
"\"]\n");
393 bool readFile(
const std::string& fname, std::string& result,
bool allowDir)
396 yarp::os::impl::DIR* dir = yarp::os::impl::opendir(fname.c_str());
397 if (dir !=
nullptr) {
398 return readDir(fname, dir, result);
401 yCDebug(PROPERTY,
"reading file %s", fname.c_str());
402 FILE* fin = fopen(fname.c_str(),
"r");
403 if (fin ==
nullptr) {
407 while (fgets(buf,
sizeof(buf) - 1, fin) !=
nullptr) {
417 std::string searchPath = env.
check(
"CONFIG_PATH",
419 "path to search for config files")
422 yCDebug(PROPERTY,
"looking for %s, search path: %s", fname.c_str(), searchPath.c_str());
424 std::string pathPrefix;
428 if (!readFile(fname, txt,
true)) {
431 for (
int i = 0; i < ss.
size(); i++) {
432 std::string trial = ss.
get(i);
436 yCDebug(PROPERTY,
"looking for %s as %s", fname.c_str(), trial.c_str());
439 if (readFile(trial, txt,
true)) {
441 pathPrefix = ss.
get(i);
449 size_t index = fname.rfind(
'/');
450 if (index == std::string::npos) {
451 index = fname.rfind(
'\\');
453 if (index != std::string::npos) {
454 path = fname.substr(0, index);
458 yCError(PROPERTY,
"cannot read from %s", fname.c_str());
465 if (searchPath.length() > 0) {
468 searchPath += pathPrefix;
470 envExtended.
put(
"CONFIG_PATH", searchPath);
473 fromConfig(txt.c_str(), envExtended, wipe);
477 bool fromConfigDir(
const std::string& dirname,
const std::string& section,
bool wipe =
true)
480 if (section.empty()) {
481 return fromConfigFile(dirname, p, wipe);
484 yCDebug(PROPERTY,
"looking for %s", dirname.c_str());
486 yarp::os::impl::DIR* dir = yarp::os::impl::opendir(dirname.c_str());
487 if (dir ==
nullptr) {
488 yCError(PROPERTY,
"cannot read from %s", dirname.c_str());
493 if (!readDir(dirname, dir, txt, section)) {
494 yCError(PROPERTY,
"cannot read from %s", dirname.c_str());
498 fromConfig(txt.c_str(), p, wipe);
515 bool including =
false;
528 if (buf.find(
"//") != std::string::npos || buf.find(
'#') != std::string::npos) {
530 bool prespace =
true;
532 for (
unsigned int i = 0; i < buf.length(); i++) {
541 buf = buf.substr(0, i - 1);
546 if (ch ==
'#' && prespace) {
550 buf = buf.substr(0, i - 1);
555 prespace = (ch ==
' ' || ch ==
'\t');
564 buf = expand(buf.c_str(), env, *owner);
566 if (buf.length() > 0 && buf[0] ==
'[') {
567 size_t stop = buf.find(
']');
568 if (stop != std::string::npos) {
569 buf = buf.substr(1, stop - 1);
570 size_t space = buf.find(
' ');
571 if (space != std::string::npos) {
574 if (bot.
size() > 1) {
579 if (accum.
size() >= 1) {
580 putBottleCompat(tag.c_str(), accum);
584 if (bot.
size() > 2) {
587 if (bot.
size() == 3) {
593 }
else if (bot.
size() == 4) {
599 Bottle* target = getBottle(key);
600 if (target ==
nullptr) {
604 putBottleCompat(key.c_str(),
610 yCError(PROPERTY,
"bad include");
616 if (getBottle(subName) !=
nullptr) {
625 yCTrace(PROPERTY,
">>> tag %s accum %s\n",
629 if (accum.
size() >= 1) {
635 putBottleCompat(tag.c_str(),
643 yCTrace(PROPERTY,
"Including %s\n", fname.c_str());
644 fromConfigFile(fname, env,
false);
650 if (bot.
size() == 2 && !including) {
653 Bottle* target = getBottle(key);
654 if (target ==
nullptr) {
658 putBottleCompat(key.c_str(), init);
671 if (!isTag && !including) {
674 if (bot.
size() >= 1) {
676 putBottleCompat(bot.
get(0).
toString().c_str(), bot);
680 for (
size_t i = 0; i < bot.
size(); i++) {
693 if (accum.
size() >= 1) {
694 putBottleCompat(tag.c_str(), accum);
702 if (getBottle(tag) !=
nullptr) {
704 accum.
append(getBottle(tag)->tail());
706 "MERGE %s, got %s\n",
720 for (
size_t i = 0; i < bot.
size(); i++) {
732 for (
const auto& it : data) {
744 yCTrace(PROPERTY,
"expanding %s\n", txt);
745 std::string input = txt;
746 if (input.find(
'$') == std::string::npos) {
754 bool varHasParen =
false;
756 for (
size_t i = 0; i <= input.length(); i++) {
758 if (i < input.length()) {
781 if ((isalnum(ch) != 0) || (ch ==
'_')) {
785 if (ch ==
'(' || ch ==
'{') {
786 if (var.length() == 0) {
793 yCTrace(PROPERTY,
"VARIABLE %s\n", var.c_str());
802 if (var ==
"__YARP__") {
806 if (add.find(
'\\') != std::string::npos) {
821 if (varHasParen && (ch ==
'}' || ch ==
')')) {
843 char** szarg =
new char*[128 + 1];
844 char* szcmd =
new char[strlen(command) + 1];
845 strcpy(szcmd, command);
848 szarg[nargs] =
nullptr;
849 fromCommand(nargs, szarg, wipe);
859 char* pNext = azParam;
863 size_t len = strlen(azParam);
866 for (i = 0; i < len; i++) {
867 if ((quoted == 0) && (
'"' == azParam[i])) {
870 }
else if (((quoted) != 0) && (
'"' == azParam[i])) {
873 }
else if (((quoted) != 0) && (
' ' == azParam[i])) {
879 memset(argv, 0x00,
sizeof(
char*) * max_arg);
883 while ((
nullptr != pNext) && (*argc < max_arg)) {
884 splitArguments(pNext, &(argv[*argc]));
887 if (
nullptr != argv[*argc]) {
892 for (j = 0; j < *argc; j++) {
893 len = strlen(argv[j]);
894 for (i = 0; i < len; i++) {
895 if (
'\1' == argv[j][i]) {
904 char* pTmp = strchr(line,
' ');
905 if (pTmp !=
nullptr) {
908 while (*pTmp ==
' ') {
926 #ifndef YARP_NO_DEPRECATED // Since YARP 3.3
959 prop.mPriv =
nullptr;
967 for (
const auto& val : values) {
968 put(val.first, val.second);
981 Portable::operator=(
static_cast<const Portable&
>(rhs));
982 mPriv->data = rhs.mPriv->data;
991 Portable::operator=(std::move(
static_cast<Portable&
>(rhs)));
992 std::swap(mPriv, rhs.mPriv);
994 rhs.mPriv->owner = &rhs;
1000 mPriv->put(key, value);
1005 mPriv->put(key, value);
1011 mPriv->put(key, value);
1026 return mPriv->check(key);
1036 return mPriv->get(key);
1048 mPriv->fromString(txt, wipe);
1054 return mPriv->toString();
1063 mPriv->fromCommand(argc, argv, wipe);
1068 fromCommand(argc,
const_cast<char**
>(argv), skipFirst, wipe);
1073 mPriv->fromArguments(arguments, wipe);
1078 return mPriv->fromConfigDir(dirname, section, wipe);
1090 return mPriv->fromConfigFile(fname, env, wipe);
1101 mPriv->fromConfig(txt, env, wipe);
1109 bool ok = b.
read(reader);
1121 return b.
write(writer);
1127 Bottle* result = mPriv->getBottle(key);
1128 if (getMonitor() !=
nullptr) {
1129 SearchReport report;
1131 report.isGroup =
true;
1132 if (result !=
nullptr) {
1133 report.isFound =
true;
1136 reportToMonitor(report);
1137 if (result !=
nullptr) {
1138 std::string context = getMonitorContext();
1141 result->setMonitor(getMonitor(),
1146 if (result !=
nullptr) {
1158 std::string str = url;
1166 for (
char ch : str) {
1171 yCTrace(PROPERTY,
"adding key %s\n", key.c_str());
1172 }
else if (ch ==
'&') {
1173 yCTrace(PROPERTY,
"adding val %s\n", val.c_str());
1176 if (!key.empty() && !val.empty()) {
1180 }
else if (ch ==
'?') {
1185 }
else if (ch ==
'%') {
1190 if (ch >=
'0' && ch <=
'9') {
1193 if (ch >=
'A' && ch <=
'F') {
1194 hex = ch -
'A' + 10;
1196 if (ch >=
'a' && ch <=
'f') {
1197 hex = ch -
'a' + 10;
void put(const std::string &key, const std::string &val)
A simple collection of objects that can be described and transmitted in a portable way.
bool readFile(const std::string &fname, std::string &result, bool allowDir)
std::string toString() const
static Value & getNullValue()
Return an invalid, "null" Value.
std::string toString() const override
Gives a human-readable textual representation of the bottle.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
This is a base class for objects that can be both read from and be written to the YARP network.
void clear()
Empties the bottle of any objects it contains.
A base class for nested structures that can be searched.
std::map< std::string, PropertyItem > data
void unput(const std::string &key)
size_type size() const
Gets the number of elements in the bottle.
Property & addGroup(const std::string &key)
Bottle & putBottleCompat(const char *key, const Bottle &val)
~Property() override
Destructor.
Split a string into pieces.
bool write(ConnectionWriter &writer) const override
Write this object to a network connection.
static Value * makeFloat64(yarp::conf::float64_t x)
Create a 64-bit floating point Value.
std::string getEnvironment(const char *key, bool *found=nullptr)
Read a variable from the environment.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
static Value * makeInt32(std::int32_t x)
Create a 32-bit integer Value.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
PropertyItem(PropertyItem &&rhs) noexcept=default
PropertyItem * getPropNoCreate(const std::string &key) const
void fromString(const std::string &text)
Initializes bottle from a string.
void fromBottle(Bottle &bot, bool wipe=true)
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
void put(const std::string &key, Value *bit)
void fromArguments(const char *command, bool wipe=true)
PropertyItem & operator=(PropertyItem &&rhs) noexcept=default
std::string toString() const
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
void fromArguments(const char *arguments, bool wipe=true)
Interprets a list of command arguments as a list of properties.
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Bottle & addList()
Places an empty nested list in the bottle, at the end of the list.
std::string toString() const override
Return a standard text representation of the content of the object.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
An interface for writing to a network connection.
void fromQuery(const char *url, bool wipe=true)
Parses text in a url.
static bool isComplete(const char *txt)
virtual bool check(const std::string &key) const=0
Check if there exists a property of the given name.
void parseArguments(char *io_pLine, int *o_pArgc, char **o_pArgv)
Breaks up a line into multiple arguments.
virtual std::string asString() const
Get string value.
void put(const std::string &key, const Value &bit)
void parseArguments(char *azParam, int *argc, char **argv, int max_arg)
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
bool check(const std::string &key) const
Bottle & putBottle(const char *key, const Bottle &val)
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Property & operator=(const Property &prop)
Copy assignment operator.
void fromString(const std::string &txt, bool wipe=true)
virtual Bottle & findGroup(const std::string &key) const=0
Gets a list corresponding to a given keyword.
static Bottle & getNullBottle()
A special Bottle with no content.
virtual bool isList() const
Checks if value is a list.
Bottle & putBottle(const char *key)
void clear()
Remove all associations.
#define yCAssert(component, x)
bool isNull() const override
Checks if the object is invalid.
An interface for reading from a network connection.
Bottle tail() const
Get all but the first element of a bottle.
bool readDir(const std::string &dirname, yarp::os::impl::DIR *&dir, std::string &result, const std::string §ion=std::string())
#define yCError(component,...)
static Value * makeValue(const std::string &txt)
Create a Value from a text description.
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
An interface to the operating system, including Port based communication.
#define yCDebug(component,...)
Property & addGroup(const std::string &key)
Add a nested group.
void splitArguments(char *line, char **args)
std::string toString(const T &value)
convert an arbitrary type to string.
bool read(ConnectionReader &reader) override
Read this object from a network connection.
PropertyItem(const PropertyItem &rhs)
void copy(const Bottle &alt, size_type first=0, size_type len=npos)
Copy all or part of another Bottle.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
virtual Bottle * asList() const
Get list value.
Bottle * getBottle(const std::string &key) const
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
bool fromConfigDir(const std::string &dirname, const std::string §ion=std::string(), bool wipe=true)
Interprets all files in a directory as lists of properties as described in fromConfigFile().
Searchable & operator=(const Searchable &rhs)=default
Copy assignment operator.
void fromConfig(const char *txt, bool wipe=true)
Parses text in the configuration format described in fromConfigFile().
void add(const Value &value)
Add a Value to the bottle, at the end of the list.
#define yCTrace(component,...)
void fromConfig(const char *txt, Searchable &env, bool wipe=true)
Value & get(const std::string &key) const
std::string toString() const override
Return a standard text representation of the content of the object.
A single value (typically within a Bottle).
void fromCommand(int argc, char *argv[], bool wipe=true)
const char * get(int idx)
PropertyItem * getProp(const std::string &key, bool create=true)
std::unique_ptr< Property > backing
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
void unput(const std::string &key)
Remove the association from the given key to a value, if present.
bool fromConfigDir(const std::string &dirname, const std::string §ion, bool wipe=true)
bool fromConfigFile(const std::string &fname, Searchable &env, bool wipe=true)
A class for storing options and configuration information.
void append(const Bottle &alt)
Append the content of the given bottle to the current list.
std::string expand(const char *txt, Searchable &env, Searchable &env2)
PropertyItem & operator=(const PropertyItem &rhs)