YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
parse_ini.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2024-2024 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "generator.h"
7#include <sstream>
8#include <string>
9#include <regex>
10#include <iostream>
11#include <fstream>
12
13// Example:
14
15//((group: myGroupName)(name: myParamName)(type: string)(required: true)(units: myUnits)(defaultValue: myDefaultValue)(description: myDescription)(notes: myNotes)(optionalVariableName: myVar))
16//((group: myGroupName)(name: myParamName)(type: string)(required: true)(units: myUnits)(defaultValue: myDefaultValue)(description: myDescription)(notes: myNotes)(optionalVariableName: myVar))
17//((group: myGroupName)(name: myParamName)(type: string)(required: true)(units: myUnits)(defaultValue: myDefaultValue)(description: myDescription)(notes: myNotes)(optionalVariableName: myVar))
18
19bool ParamsFilesGenerator::parseIniParams(const std::string inputfilename)
20{
21 std::string line;
22
23 std::ifstream inputfile(inputfilename);
24
25 bool b = inputfile.is_open();
26 if (!b)
27 {
28 std::cerr << "Unable to open file: " << inputfilename << std::endl;
29 return false;
30 }
31
32 std::regex pattern(R"(\‍((\w+): ([^)]+)\))"); // Matches "(attribute: value)"
33 std::smatch matches;
34 Parameter param;
35
36 while (std::getline(inputfile, line))
37 {
38 auto textBegin = std::sregex_iterator(line.begin(), line.end(), pattern);
39 auto textEnd = std::sregex_iterator();
40
41 // Iterate over each line
42 for (std::sregex_iterator i = textBegin; i != textEnd; ++i)
43 {
44 std::smatch match = *i;
45 std::string attribute = match[1];
46 std::string value = match[2];
47 std::string group_string;
48 std::string param_string;
49
50 if (attribute == "group")
51 {
52 group_string = trimSpaces(value);
53 }
54 else if (attribute == "name")
55 {
56 param_string = trimSpaces(value);
57 }
58 else if (attribute == "type")
59 {
60 param.type = trimSpaces(value);
61 }
62 else if (attribute == "required")
63 {
64 if (value == "Yes" || value == "yes" || value == "True" || value == "true" || value == "1")
65 param.required = true;
66 }
67 else if (attribute == "units")
68 {
69 param.units = trimSpaces(value);
70 }
71 else if (attribute == "defaultValue")
72 {
73 param.defaultValue = trimSpaces(value);
74 }
75 else if (attribute == "description")
76 {
77 param.description = trimSpaces(escapeQuotes(value));
78 }
79 else if (attribute == "notes")
80 {
81 param.notes = trimSpaces(escapeQuotes(value));
82 }
83 else if (attribute == "optionalVariableName")
84 {
86 }
87 else
88 {
89 std::cout << "Syntax error in attribute:" << attribute << std::endl;
90 return false;
91 }
92
93 //create the name of the param
94 {
95 std::string fully_scoped_param_name;
96 if (!group_string.empty())
97 {
98 fully_scoped_param_name = group_string + std::string("::") + param_string;
99 }
100 else
101 {
102 fully_scoped_param_name = param_string;
103 }
104 param.setFullyScopedParamName(fully_scoped_param_name);
105 }
106
107 m_params.push_back(param);
108 m_sectionGroup.insert(param);
109 }
110 }
111
112 inputfile.close();
113 return true;
114}
std::string units
Definition parameter.h:28
std::string notes
Definition parameter.h:32
std::string optional_variable_name
Definition parameter.h:33
std::string type
Definition parameter.h:27
bool required
Definition parameter.h:30
void setFullyScopedParamName(std::string fullyScopedParamName)
Definition parameter.cpp:21
std::string description
Definition parameter.h:31
std::string defaultValue
Definition parameter.h:29
bool parseIniParams(std::string inputfilename)
Definition parse_ini.cpp:19
std::deque< Parameter > m_params
Definition generator.h:34
SectionHandler m_sectionGroup
Definition generator.h:35
void insert(const Parameter &param)
std::string trimSpaces(const std::string &str)
Definition utils.h:43
std::string escapeQuotes(const std::string &str)
Definition utils.h:56