|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
34 #define RTARGET stderr
35 #define RESOURCE_FINDER_CACHE_TIME 10
38 #ifndef YARP_NO_DEPRECATED // since YARP 3.4
63 if (errno != ERANGE) {
82 const char* at = txt.c_str();
87 result.
addString(std::string(at, len - slash_tweak));
93 slash_tweak = (ch ==
slash && len > 0) ? 1 : 0;
97 result.
addString(std::string(at, len - slash_tweak));
104 const std::string& resourceType)
106 if (resourceType.empty()) {
110 if (path.length() > 0) {
111 if (path[path.length() - 1] !=
slash[0]) {
115 path += resourceType;
119 const std::string& resourceType)
121 if (resourceType.empty()) {
125 path = resourceType +
slash + path;
129 const std::string& resourceType)
131 if (resourceType.empty()) {
134 for (
size_t i = 0; i < paths.
size(); i++) {
146 std::string configFilePath;
148 bool mainActive{
false};
149 bool useNearMain{
false};
166 std::string s{fname};
167 auto n = s.rfind(
'/');
169 if (n == std::string::npos) {
173 if (n != std::string::npos) {
174 return s.substr(0, n);
184 bool user_specified_from = p.
check(
"from");
186 if (p.
check(
"verbose")) {
187 yCWarning(RESOURCEFINDER,
"The 'verbose' option is deprecated.");
190 yCDebug(RESOURCEFINDER,
"configuring");
192 bool configured_normally =
true;
194 if (p.
check(
"context")) {
196 std::string c = p.
check(
"context",
Value(
"default")).asString();
197 addAppName(c.c_str());
198 yCDebug(RESOURCEFINDER,
"added context %s", c.c_str());
202 if (config.
check(
"from")) {
203 std::string from = config.
check(
"from",
206 yCDebug(RESOURCEFINDER,
"default config file specified as %s", from.c_str());
208 std::string corrected = findFile(config, from,
nullptr);
210 if (!corrected.empty()) {
213 std::string fromPath = extractPath(from.c_str());
214 configFilePath = fromPath;
216 configured_normally =
false;
220 return configured_normally;
225 if (!config.
check(key)) {
226 config.
put(key, val);
233 if (str.length() > 0 && (str[0] ==
'/' || str[0] ==
'\\')) {
236 if (str.length() > 1) {
246 if (isAbsolute(str)) {
249 if (str.length() >= 2) {
250 if (str[0] ==
'.' && (str[1] ==
'/' || str[1] ==
'\\')) {
253 }
else if (str ==
".") {
260 const std::string& base2,
261 const std::string& base3,
262 const std::string& name)
264 if (isAbsolute(name)) {
271 if (!base1.empty()) {
276 if (isRooted(base2)) {
281 if (!base2.empty()) {
285 if (isRooted(base3)) {
290 if (!base3.empty()) {
299 std::string
check(
const std::string& base1,
300 const std::string& base2,
301 const std::string& base3,
302 const std::string& name,
305 const std::string& doc2)
307 std::string s = getPath(base1, base2, base3, name);
311 if (prev !=
nullptr) {
323 yCDebug(RESOURCEFINDER,
"checking [%s] (%s%s%s)", s.c_str(), base.c_str(), (base.length() == 0) ?
"" :
" ", doc2.c_str());
325 bool ok = exists(s.c_str(), isDir);
330 cache.
put(s, status);
332 yCDebug(RESOURCEFINDER,
"found %s", s.c_str());
340 std::string fname = config.
check(name,
Value(name)).asString();
341 std::string result = findFileBase(config, fname,
true, externalOptions);
347 std::string fname = config.
check(name,
Value(name)).asString();
349 if (externalOptions !=
nullptr) {
351 findFileBase(config, fname,
true, paths, *externalOptions);
356 if (externalOptions !=
nullptr) {
357 opts = *externalOptions;
362 findFileBase(config, fname,
true, paths, opts);
368 std::string result = findFileBase(config,
"",
true,
nullptr);
369 if (result.empty()) {
377 std::string fname = config.
check(name,
Value(name)).asString();
378 std::string result = findFileBase(config, fname,
false, externalOptions);
384 std::string result = findFileBase(config, fname,
false, externalOptions);
392 if (externalOptions ==
nullptr) {
393 externalOptions = &opts;
395 findFileBase(config, name, isDir, output, *externalOptions);
410 size_t prelen = output.
size();
411 findFileBaseInner(config, name, isDir,
true, output, opts, doc, {});
412 if (output.
size() != prelen) {
417 if (canShowErrors(opts)) {
418 yCDebug(RESOURCEFINDER,
"did not find %s", name.c_str());
425 for (
size_t i = 0; i < output.
size(); i++) {
437 if (!reason.empty()) {
448 if (name.empty() && isDir) {
449 addString(output,
getPwd());
454 std::string str = check(
getPwd(), resourceType,
"", name, isDir, doc,
"pwd");
459 addString(output, str);
467 if (!configFilePath.empty()) {
468 std::string str = check(configFilePath, resourceType,
"", name, isDir, doc,
"defaultConfigFile path");
470 addString(output, str);
492 findFileBaseInner(config, robot,
true, allowPathd, paths, opts2, doc,
"robot");
494 for (
size_t j = 0; j < paths.
size(); j++) {
503 addString(output, str);
512 for (
size_t i = 0; i < apps.
size(); i++) {
525 findFileBaseInner(config, app,
true, allowPathd, paths, opts2, doc,
"context");
527 for (
size_t j = 0; j < paths.
size(); j++) {
528 std::string str = check(paths.
get(j).
asString(),
"",
"", name, isDir, doc,
"context");
530 addString(output, str);
544 std::string str = check(home,
"",
"", name, isDir, doc,
"YARP_CONFIG_HOME");
546 addString(output, str);
559 std::string str = check(home,
"",
"", name, isDir, doc,
"YARP_DATA_HOME");
561 addString(output, str);
573 for (
size_t i = 0; i < dirs.
size(); i++) {
582 addString(output, str);
594 for (
size_t i = 0; i < dirs.
size(); i++) {
603 addString(output, str);
617 findFileBaseInner(config,
"path.d",
true,
false, pathds, opts2, doc,
"path.d");
619 for (
size_t i = 0; i < pathds.
size(); i++) {
630 for (
size_t i = 0; i < sections.
size(); i++) {
631 std::string search_name = sections.
get(i).
asString();
635 for (
size_t j = 0; j < paths.
size(); j++) {
636 std::string str = check(paths.
get(j).
asString(),
"",
"", name, isDir, doc,
"yarp.d");
638 addString(output, str);
649 bool exists(
const std::string& fname,
bool isDir)
693 return configFilePath;
698 if (path.length() > 1) {
699 if (path[path.length() - 1] ==
slash[0]) {
700 path = path.substr(0, path.length() -
slash.size());
710 yCWarning(RESOURCEFINDER,
"Could not create %s directory", path.c_str());
718 return configFilePath;
728 if (path.length() > 1) {
729 if (path[path.length() - 1] ==
slash[0]) {
730 path = path.substr(0, path.length() -
slash.size());
740 yCWarning(RESOURCEFINDER,
"Could not create %s directory", path.c_str());
751 isConfiguredFlag(false),
762 isConfiguredFlag(false),
773 nullConfig(data.isNull()),
774 isConfiguredFlag(true),
778 this->mPriv = altPriv;
794 *(mPriv) = *(alt.mPriv);
796 nullConfig = alt.nullConfig;
797 isConfiguredFlag = alt.isConfiguredFlag;
805 isConfiguredFlag =
true;
806 return mPriv->configure(config, argc, argv, skipFirstArgument);
810 bool ResourceFinder::addContext(
const std::string& appName)
812 if (appName[0] ==
'\0') {
815 yCDebug(RESOURCEFINDER,
"adding context [%s]", appName.c_str());
816 return mPriv->addAppName(appName);
819 bool ResourceFinder::clearContext()
821 yCDebug(RESOURCEFINDER,
"clearing context");
822 return mPriv->clearAppNames();
829 return mPriv->setDefault(config, key, val2);
834 return mPriv->setDefault(config, key,
Value(val));
839 return mPriv->setDefault(config, key,
Value(val));
844 return mPriv->setDefault(config, key, val);
849 yCDebug(RESOURCEFINDER,
"finding file [%s]", name.c_str());
850 return mPriv->findFile(config, name,
nullptr);
856 yCDebug(RESOURCEFINDER,
"finding file [%s]", name.c_str());
857 return mPriv->findFile(config, name, &options);
862 yCDebug(RESOURCEFINDER,
"finding file %s", name.c_str());
863 return mPriv->findFileByName(config, name,
nullptr);
869 yCDebug(RESOURCEFINDER,
"finding file %s", name.c_str());
870 return mPriv->findFileByName(config, name, &options);
876 yCDebug(RESOURCEFINDER,
"finding path [%s]", name.c_str());
877 return mPriv->findPath(config, name,
nullptr);
883 yCDebug(RESOURCEFINDER,
"finding path [%s]", name.c_str());
884 return mPriv->findPath(config, name, &options);
889 yCDebug(RESOURCEFINDER,
"finding paths [%s]", name.c_str());
890 return mPriv->findPaths(config, name,
nullptr);
896 yCDebug(RESOURCEFINDER,
"finding paths [%s]", name.c_str());
897 return mPriv->findPaths(config, name, &options);
902 yCDebug(RESOURCEFINDER,
"finding path");
903 return mPriv->findPath(config);
906 #ifndef YARP_NO_DEPRECATED // Since YARP 3.4
922 return config.
check(key);
928 return config.
find(key);
940 return nullConfig || config.
isNull();
951 return mPriv->getContext();
956 return mPriv->getHomeContextPath(config, mPriv->getContext());
961 return mPriv->getHomeRobotPath();
966 return mPriv->getContexts();
982 std::string ResourceFinder::getDataHomeWithPossibleCreation(
bool mayCreate)
988 if (!yarp_version.empty()) {
994 return createIfAbsent(mayCreate, xdg_version +
slash +
"yarp");
998 if (app_version !=
"") {
999 return createIfAbsent(mayCreate, app_version +
slash +
"yarp");
1003 #if defined(__APPLE__)
1004 if (home_version !=
"") {
1005 return createIfAbsent(mayCreate,
1008 +
slash +
"Application Support"
1012 if (!home_version.empty()) {
1013 return createIfAbsent(mayCreate,
1023 std::string ResourceFinder::getConfigHomeWithPossibleCreation(
bool mayCreate)
1030 return yarp_version;
1035 return createIfAbsent(mayCreate, xdg_version +
slash +
"yarp");
1039 if (app_version !=
"") {
1040 return createIfAbsent(mayCreate,
1041 app_version +
slash +
"yarp" +
slash +
"config");
1045 #if defined(__APPLE__)
1047 if (home_mac_version !=
"") {
1048 return createIfAbsent(mayCreate,
1050 +
slash +
"config");
1054 if (!home_version.empty()) {
1055 return createIfAbsent(mayCreate,
1063 std::string ResourceFinder::createIfAbsent(
bool mayCreate,
1064 const std::string& path)
1080 return yarp_version;
1090 if (app_version !=
"") {
1099 result.
addString(
"/usr/local/share/yarp");
1111 return yarp_version;
1121 if (app_version !=
"") {
1130 #if defined(__APPLE__)
1131 result.
addString(
"/Library/Preferences/yarp");
1139 const std::string& key,
1142 Bottle bot = mPriv->findPaths(config, key, &options,
false);
1144 for (
int i = bot.
size() - 1; i >= 0; i--) {
1149 return bot.
size() >= 1;
#define RESOURCE_FINDER_CACHE_TIME
A simple collection of objects that can be described and transmitted in a portable way.
SearchFlavor searchFlavor
bool readConfig(Property &config, const std::string &key, const ResourceFinderOptions &options)
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.
void clear()
Empties the bottle of any objects it contains.
std::string findFileBase(Property &config, const std::string &name, bool isDir, const ResourceFinderOptions *externalOptions)
bool configure(Property &config, int argc, char *argv[], bool skip)
A base class for nested structures that can be searched.
yarp::os::Bottle getContexts()
Return the full stack of contexts used in searching for configuration files.
size_type size() const
Gets the number of elements in the bottle.
std::string getHomeRobotPath()
Return the path to the "user" robot directory.
#define yCWarning(component,...)
virtual ~ResourceFinder()
void findFileBaseInner(Property &config, const std::string &name, bool isDir, bool allowPathd, Bottle &output, const ResourceFinderOptions &opts, const Bottle &predoc, const std::string &reason)
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.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
static constexpr value_type path_separator
DuplicateFilesPolicy duplicateFilesPolicy
std::string findFile(Property &config, const std::string &name, const ResourceFinderOptions *externalOptions)
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
yarp::os::Bottle findPaths(const std::string &name)
Expand a partial path to a list of paths.
bool isAbsolute(const std::string &str)
void addFloat64(yarp::conf::float64_t x)
Places a 64-bit floating point number in the bottle, at the end of the list.
yarp::os::Bottle findPaths(Property &config, const std::string &name, const ResourceFinderOptions *externalOptions, bool enforcePlural=true)
static double nowSystem()
constexpr fs::value_type sep
virtual Bottle & findGroup(const std::string &key) const=0
Gets a list corresponding to a given keyword.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
std::string getContext()
Return the default "context" or "application name" used in searching for configuration files.
static void prependResourceType(std::string &path, const std::string &resourceType)
std::string findFile(const std::string &name)
Find the full path to a file.
const ResourceFinder & operator=(const ResourceFinder &alt)
std::string findPath()
Find the first existing directory in the search path.
static Bottle getConfigDirs()
Locations where system administrator data and config files are stored.
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
bool setDefault(const std::string &key, const std::string &val)
Provide a default value for a given key.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
static Bottle parsePaths(const std::string &txt)
std::string findPath(Property &config)
std::string toString() const override
Return a standard text representation of the content of the object.
SearchLocations searchLocations
std::string getHomeRobotPath()
static std::string getPwd()
bool isRooted(const std::string &str)
virtual bool check(const std::string &key) const=0
Check if there exists a property of the given name.
virtual std::string asString() const
Get string value.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string getHomeContextPath()
Return the path to the "user" context directory for the current context.
void findFileBase(Property &config, const std::string &name, bool isDir, Bottle &output, const ResourceFinderOptions &opts)
void addInt32(std::int32_t x)
Places a 32-bit integer in the bottle, at the end of the list.
MessageFilter messageFilter
bool check(const std::string &key) const override
Check if there exists a property of the given name.
bool canShowErrors(const ResourceFinderOptions &opts) const
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
int stat(const char *path)
Portable wrapper for the stat() function.
static void autoInitMinimum()
Basic system initialization, not including plugins.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
bool addAppName(const std::string &appName)
int mkdir_p(const char *p, int ignoreLevels=0)
Create a directory and all parent directories needed.
#define yCAssert(component, x)
static Bottle getDataDirs()
Locations where packaged data and config files are stored.
std::string getPath(const std::string &base1, const std::string &base2, const std::string &base3, const std::string &name)
std::string getHomeContextPath(Property &config, const std::string &context)
int mkdir(const char *p)
Portable wrapper for the mkdir() function.
Bottle tail() const
Get all but the first element of a bottle.
static ResourceFinder & getResourceFinderSingleton()
Access a ResourceFinder singleton whose lifetime will match that of the YARP library.
void addString(Bottle &output, const std::string &txt)
bool setVerbose(bool verbose=true)
Request that information be printed to the console on how resources are being found.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
virtual ResourceFinder findNestedResourceFinder(const std::string &key)
Gets a section as a ResourceFinder object, retaining the context and configuration of the current Res...
An interface to the operating system, including Port based communication.
#define yCDebug(component,...)
bool isNull() const override
Checks if the object is invalid.
bool setDefault(Property &config, const std::string &key, const yarp::os::Value &val)
static std::string getDataHomeNoCreate()
Variant of getDataHome that will never create the directory returned.
#define YARP_OS_NON_CONST_LOG_COMPONENT(name, name_string)
bool setQuiet(bool quiet=true)
Request that information be suppressed from the console.
std::string findPath(Property &config, const std::string &name, const ResourceFinderOptions *externalOptions)
void fromString(const char *str)
Set value to correspond to a textual representation.
virtual bool isNull() const
Checks if the object is invalid.
static constexpr value_type preferred_separator
static std::string getConfigHomeNoCreate()
Variant of getConfigHome that will never create the directory returned.
virtual Bottle * asList() const
Get list value.
bool exists(const std::string &fname, bool isDir)
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
std::string findFileByName(const std::string &name)
Find the full path to a file.
A single value (typically within a Bottle).
static void appendResourceType(std::string &path, const std::string &resourceType)
std::string findFileByName(Property &config, const std::string &fname, const ResourceFinderOptions *externalOptions)
static std::string getDataHome()
Location where user data files are stored.
#define YARP_OS_LOG_COMPONENT(name, name_string)
The components from which ports and connections are built.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
std::string check(const std::string &base1, const std::string &base2, const std::string &base3, const std::string &name, bool isDir, const Bottle &doc, const std::string &doc2)
constexpr fs::value_type slash
A class for storing options and configuration information.
static std::string extractPath(const std::string &fname)
std::string toString() const override
Return a standard text representation of the content of the object.
Helper class for finding config files and other external resources.