33#define RESOURCE_FINDER_CACHE_TIME 10
36#ifndef YARP_NO_DEPRECATED
46 std::vector<std::string> out;
47 for (
size_t i = 0;
i < b.
size(); ++
i) {
56 for (
const auto& s : v) {
92 const std::string& resourceType)
94 if (resourceType.empty()) {
98 if (path.length() > 0) {
99 if (path[path.length() - 1] !=
slash[0]) {
103 path += resourceType;
107 const std::string& resourceType)
109 if (resourceType.empty()) {
113 path = resourceType +
slash + path;
117 const std::string& resourceType)
119 for (
auto& txt : paths) {
130 std::string configFilePath;
132 bool mainActive{
false};
133 bool useNearMain{
false};
150 std::string s{
fname};
151 auto n = s.rfind(
'/');
153 if (
n == std::string::npos) {
157 if (
n != std::string::npos) {
158 return s.substr(0,
n);
166 p.fromCommand(argc, argv,
skip);
170 if (
p.check(
"verbose")) {
178 if (
p.check(
"context")) {
180 std::string
c =
p.check(
"context",
Value(
"default")).asString();
186 if (config.
check(
"from")) {
187 std::string from = config.
check(
"from",
Value(
"config.ini")).toString();
207 if (!config.
check(key)) {
208 config.
put(key, val);
215 if (str.length() > 0 && (str[0] ==
'/' || str[0] ==
'\\')) {
218 if (str.length() > 1) {
231 if (str.length() >= 2) {
232 if (str[0] ==
'.' && (str[1] ==
'/' || str[1] ==
'\\')) {
235 }
else if (str ==
".") {
242 const std::string&
base2,
243 const std::string&
base3,
244 const std::string& name)
253 if (!
base1.empty()) {
263 if (!
base2.empty()) {
272 if (!
base3.empty()) {
282 const std::string&
base2,
283 const std::string&
base3,
284 const std::string& name,
287 const std::string&
doc2)
293 if (prev !=
nullptr) {
304 std::string base =
doc.toString();
305 yCDebug(
RESOURCEFINDER,
"checking [%s] (%s%s%s)", s.c_str(), base.c_str(), (base.length() == 0) ?
"" :
" ",
doc2.c_str());
311 status.
asList()->addInt32(ok ? 1 : 0);
312 cache.
put(s, status);
330 std::vector<std::string> paths;
350 std::string result =
findFileBase(config,
"",
true,
nullptr);
351 if (result.empty()) {
372 std::vector<std::string> output;
378 return output.empty() ? std::string{} : output.at(0);
392 size_t prelen = output.size();
394 if (output.size() !=
prelen) {
405 static void addString(std::vector<std::string>& output,
const std::string& txt)
407 for (
const auto& str : output) {
412 output.push_back(txt);
419 if (!reason.empty()) {
420 doc.addString(reason);
424 std::string resourceType =
opts.resourceType;
430 if (name.empty() &&
isDir) {
449 if (!configFilePath.empty()) {
450 std::string str =
check(configFilePath, resourceType,
"", name,
isDir,
doc,
"defaultConfigFile path");
469 std::vector<std::string> paths;
472 opts2.resourceType =
"robots";
476 for (
const auto& path : paths) {
477 std::string str =
check(path,
494 for (
size_t i = 0;
i < apps.
size();
i++) {
502 std::vector<std::string> paths;
509 for (
const auto& path : paths) {
510 std::string str =
check(path,
"",
"", name,
isDir,
doc,
"context");
526 std::string str =
check(home,
"",
"", name,
isDir,
doc,
"YARP_CONFIG_HOME");
541 std::string str =
check(home,
"",
"", name,
isDir,
doc,
"YARP_DATA_HOME");
555 for (
const auto&
dir : dirs) {
576 for (
const auto&
dir : dirs) {
598 opts2.resourceType =
"config";
617 for (
const auto& path :
paths_v) {
618 std::string str =
check(path,
"",
"", name,
isDir,
doc,
"yarp.d");
675 return configFilePath;
680 if (path.length() > 1) {
681 if (path[path.length() - 1] ==
slash[0]) {
682 path = path.substr(0, path.length() -
slash.size());
700 return configFilePath;
710 if (path.length() > 1) {
711 if (path[path.length() - 1] ==
slash[0]) {
712 path = path.substr(0, path.length() -
slash.size());
734 m_isConfiguredFlag(
false),
745 m_isConfiguredFlag(
false),
756 m_nullConfig(data.isNull()),
757 m_isConfiguredFlag(
true),
777 *(mPriv) = *(
alt.mPriv);
779 m_nullConfig =
alt.m_nullConfig;
780 m_isConfiguredFlag =
alt.m_isConfiguredFlag;
781 m_configprop =
alt.m_configprop;
788 m_isConfiguredFlag =
true;
793bool ResourceFinder::addContext(
const std::string&
appName)
799 return mPriv->addAppName(
appName);
802bool ResourceFinder::clearContext()
805 return mPriv->clearAppNames();
811 val2.fromString(val.c_str());
812 return mPriv->setDefault(m_configprop, key,
val2);
817 return mPriv->setDefault(m_configprop, key,
Value(val));
822 return mPriv->setDefault(m_configprop, key,
Value(val));
827 return mPriv->setDefault(m_configprop, key, val);
833 return mPriv->findFile(m_configprop, name,
nullptr);
840 return mPriv->findFile(m_configprop, name, &options);
846 return mPriv->findFileByName(m_configprop, name,
nullptr);
853 return mPriv->findFileByName(m_configprop, name, &options);
860 return mPriv->findPath(m_configprop, name,
nullptr);
867 return mPriv->findPath(m_configprop, name, &options);
886 return mPriv->findPath(m_configprop);
889#ifndef YARP_NO_DEPRECATED
905 return m_configprop.
check(key);
911 return m_configprop.
find(key);
923 return m_nullConfig || m_configprop.
isNull();
934 return mPriv->getContext();
939 return mPriv->getHomeContextPath(m_configprop, mPriv->getContext());
944 return mPriv->getHomeRobotPath();
949 return mPriv->getContexts();
966 const std::string& key,
969 std::vector<std::string> paths = mPriv->findPaths(config, key, &options,
false);
971 for (
auto it = paths.rbegin();
it != paths.rend(); ++
it) {
972 const auto& path = *
it;
976 return !paths.empty();
static void appendResourceType(std::string &path, const std::string &resourceType)
static void prependResourceType(std::string &path, const std::string &resourceType)
#define RESOURCE_FINDER_CACHE_TIME
static std::string getPwd()
constexpr fs::value_type slash
bool setDefault(Property &config, const std::string &key, const yarp::os::Value &val)
bool addAppName(const std::string &appName)
std::string getPath(const std::string &base1, const std::string &base2, const std::string &base3, const std::string &name)
static void addString(std::vector< std::string > &output, const std::string &txt)
std::vector< std::string > findPaths(Property &config, const std::string &name, const ResourceFinderOptions *externalOptions, bool enforcePlural=true)
bool exists(const std::string &fname, bool isDir)
bool isRooted(const std::string &str)
std::string findFileBase(Property &config, const std::string &name, bool isDir, const ResourceFinderOptions *externalOptions)
std::string getHomeContextPath(Property &config, const std::string &context)
bool configureProp(Property &config, int argc, char *argv[], bool skip)
void findFileBase(Property &config, const std::string &name, bool isDir, std::vector< std::string > &output, const ResourceFinderOptions &opts)
std::string findPath(Property &config)
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)
std::string findFileByName(Property &config, const std::string &fname, const ResourceFinderOptions *externalOptions)
void findFileBaseInner(Property &config, const std::string &name, bool isDir, bool allowPathd, std::vector< std::string > &output, const ResourceFinderOptions &opts, const Bottle &predoc, const std::string &reason)
bool canShowErrors(const ResourceFinderOptions &opts) const
std::string findPath(Property &config, const std::string &name, const ResourceFinderOptions *externalOptions)
bool isAbsolute(const std::string &str)
std::string getHomeRobotPath()
std::string findFile(Property &config, const std::string &name, const ResourceFinderOptions *externalOptions)
static std::string extractPath(const std::string &fname)
A simple collection of objects that can be described and transmitted in a portable way.
size_type size() const
Gets the number of elements in the bottle.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Bottle tail() const
Get all but the first element of a bottle.
void clear()
Empties the bottle of any objects it contains.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
A mini-server for performing network communication in the background.
BufferedPort()
Constructor.
static void autoInitMinimum()
Basic system initialization, not including plugins.
A class for storing options and configuration information.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
std::string toString() const override
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.
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
These options are loosely based on http://wiki.icub.org/wiki/YARP_ResourceFinder.
Helper class for finding config files and other external resources.
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
bool configure(int argc, char *argv[], bool skipFirstArgument=true)
Sets up the ResourceFinder.
std::string getContext()
Return the default "context" or "application name" used in searching for configuration files.
std::string getHomeRobotPath()
Return the path to the "user" robot directory.
std::string findPath()
Find the first existing directory in the search path.
std::string toString() const override
Return a standard text representation of the content of the object.
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
bool isNull() const override
Checks if the object is invalid.
static ResourceFinder & getResourceFinderSingleton()
Access a ResourceFinder singleton whose lifetime will match that of the YARP library.
yarp::os::Bottle findPaths(const std::string &name)
Expand a partial path to a list of paths.
bool setDefault(const std::string &key, const std::string &val)
Provide a default value for a given key.
std::string findFileByName(const std::string &name)
Find the full path to a file.
const ResourceFinder & operator=(const ResourceFinder &alt)
bool readConfig(Property &config, const std::string &key, const ResourceFinderOptions &options)
std::string findFile(const std::string &name)
Find the full path to a file.
bool setQuiet(bool quiet=true)
Request that information be suppressed from the console.
virtual ~ResourceFinder()
bool setVerbose(bool verbose=true)
Request that information be printed to the console on how resources are being found.
std::string getHomeContextPath()
Return the path to the "user" context directory for the current context.
virtual ResourceFinder findNestedResourceFinder(const std::string &key)
Gets a section as a ResourceFinder object, retaining the context and configuration of the current Res...
yarp::os::Bottle getContexts()
Return the full stack of contexts used in searching for configuration files.
A base class for nested structures that can be searched.
virtual bool isNull() const
Checks if the object is invalid.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
static double nowSystem()
A single value (typically within a Bottle).
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual Bottle * asList() const
Get list value.
virtual std::string asString() const
Get string value.
#define yCAssert(component, x)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_OS_LOG_COMPONENT(name, name_string)
#define YARP_OS_NON_CONST_LOG_COMPONENT(name, name_string)
std::string yarpdatahome()
Returns the directory where user-specific YARP data files should be written.
std::vector< std::string > yarpdatadirs()
Returns the directories where YARP data files should be searched.
std::vector< std::string > yarpconfigdirs()
Returns the directories where YARP configuration files should be searched.
std::string yarpconfighome()
Returns the directory where user-specific YARP configuration files should be written.
std::string get_string(const std::string &key, bool *found=nullptr)
Read a string from an environment variable.
static constexpr value_type preferred_separator
The components from which ports and connections are built.
An interface to the operating system, including Port based communication.
char * getcwd(char *buf, size_t size)
Portable wrapper for the getcwd() function.
int stat(const char *path)
Portable wrapper for the stat() function.
int mkdir_p(const char *p, int ignoreLevels=0)
Create a directory and all parent directories needed.