YARP
Yet Another Robot Platform
property/main.cpp

How to read a configuration file.

How to read a configuration file. This example is designed to read a file like the following (property/config.txt):

/*
* SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
* SPDX-FileCopyrightText: 2006-2010 RobotCub Consortium
* SPDX-License-Identifier: BSD-3-Clause
*/
#include <stdio.h>
#include <stdlib.h>
using namespace yarp::os;
int main(int argc, char *argv[]) {
Property cmdLine;
cmdLine.fromCommand(argc,argv);
if (!cmdLine.check("file")) {
printf("Please call with: --file config.txt\n");
exit(1);
}
std::string fname = cmdLine.find("file").asString();
printf("Working with config file %s\n", fname.c_str());
Property robot;
robot.fromConfigFile(fname.c_str());
if (!robot.check("GENERAL")) {
printf("Cannot understand config file\n");
exit(1);
}
int joints = robot.findGroup("GENERAL").find("Joints").asInt32();
printf("Robot has %d joints\n", joints);
Bottle& maxes = robot.findGroup("LIMITS").findGroup("Max");
printf("Robot has limits: ");
for (int i=1; i<maxes.size(); i++) {
printf("%d ", maxes.get(i).asInt32());
}
printf("\n");
//printf("If you wanted to transmit this configuration, here it is in Bottle format:\n");
//printf("%s\n", robot.toString().c_str());
return 0;
}
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:64
size_type size() const
Gets the number of elements in the bottle.
Definition: Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition: Bottle.cpp:246
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Bottle.cpp:302
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Bottle.cpp:287
A class for storing options and configuration information.
Definition: Property.h:33
Value & find(const std::string &key) const override
Gets a value corresponding to a given keyword.
Definition: Property.cpp:1051
bool fromConfigFile(const std::string &fname, bool wipe=true)
Interprets a file as a list of properties.
Definition: Property.cpp:1098
bool check(const std::string &key) const override
Check if there exists a property of the given name.
Definition: Property.cpp:1041
Bottle & findGroup(const std::string &key) const override
Gets a list corresponding to a given keyword.
Definition: Property.cpp:1142
void fromCommand(int argc, char *argv[], bool skipFirst=true, bool wipe=true)
Interprets a list of command arguments as a list of properties.
Definition: Property.cpp:1074
virtual std::int32_t asInt32() const
Get 32-bit integer value.
Definition: Value.cpp:204
virtual std::string asString() const
Get string value.
Definition: Value.cpp:234
An interface to the operating system, including Port based communication.
int main(int argc, char *argv[])
Definition: yarpros.cpp:265