YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
parse_md.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 <iomanip>
9#include <algorithm>
10#include <string>
11#include <iostream>
12#include <fstream>
13#include <regex>
14
15// Example:
16
17// | Group | Parameter | Type | Units | Default Value | Required | Description | Notes |
18// |:--------------:|:------------:|:---------:|:--------:|:--------------:|:---------:|:-------------:|:-------:|
19// | myGroupName | myParamName | string | myUnits | myDefaultValue | true | myDescription | myNotes |
20// | myGroupName | myParamName | string | myUnits | myDefaultValue | true | myDescription | myNotes |
21// | myGroupName | myParamName | string | myUnits | myDefaultValue | true | myDescription | myNotes |
22
23bool ParamsFilesGenerator::parseMdParams(const std::string inputfilename)
24{
25 std::string line;
26
27 std::ifstream inputfile(inputfilename);
28 std::vector<Parameter> params;
29 bool b = inputfile.is_open();
30 if (!b)
31 {
32 std::cerr << "Unable to open file: " << inputfilename << std::endl;
33 return false;
34 }
35
36 //The following regex matches the presence of a block similar to |:--------------:|
37 std::regex pattern(R"(\|\s*:\-*:\s*\|)");
38
39 //search for the line containing the pattern, then close the file.
40 //the pattern can be present or not.
41 //the idea is to skip all the lines before the pattern.
42 //the output of this block is the counter linepattern, which
43 //is the number of lines to skip in the next block
44 int linepattern = -1;
45 size_t i=0;
46 while (std::getline(inputfile, line))
47 {
48 if (std::regex_search(line, pattern)) { linepattern = i; break;}
49 i++;
50 }
51 inputfile.close();
52
53 //reopen the file
54 inputfile.open(inputfilename);
55 b = inputfile.is_open();
56 if (!b)
57 {
58 std::cerr << "Unable to open file: " << inputfilename << std::endl;
59 return false;
60 }
61
62 // Read the file line by line
63 while (std::getline(inputfile, line))
64 {
65 //skip the first lines (before the pattern has been found)
66 if (linepattern>=0) {linepattern--; continue;}
67
68 //after the pattern, data is valid and ready to be processed
69 std::stringstream ss(line);
70 std::string item;
71 Parameter param;
72
73 //count the number of '|' chars in the line
74 size_t separators = 0;
75 for (char c : line) {
76 if (c == '|') { separators++; }
77 }
78
79 // Leggi i valori separati da '|'
80 std::getline(ss, item, '|'); // Ignora la prima barra vuota
81 std::getline(ss, item, '|');
82 std::string group_string = trimSpaces(item);
83 if (containsOnlySymbols(group_string)) group_string = "";
84 std::getline(ss, item, '|');
85 std::string param_string = trimSpaces(item);
86 if (containsOnlySymbols(param_string)) param_string = "";
87
88 std::string fully_scoped_param_name;
89 if (!group_string.empty())
90 {fully_scoped_param_name = group_string+std::string("::")+param_string;}
91 else
92 {fully_scoped_param_name = param_string;}
93
94 param.setFullyScopedParamName(fully_scoped_param_name);
95
96
97 std::getline(ss, item, '|');
98 param.type = trimSpaces(item);
99
100 std::getline(ss, item, '|');
101 param.units = trimSpaces(item);
102 if (containsOnlySymbols(param.units)) param.units = "";
103
104 std::getline(ss, item, '|');
105 param.defaultValue = trimSpaces(item);
106 if (containsOnlySymbols(param.defaultValue)) param.defaultValue = "";
107
108 std::getline(ss, item, '|');
109 std::string req_s = trimSpaces(item);
110 if (req_s == "Yes" || req_s == "yes" || req_s == "True" || req_s == "true" || req_s == "1")
111 param.required = true;
112
113 std::getline(ss, item, '|');
114 param.description = trimSpaces(escapeQuotes(item));
115 if (containsOnlySymbols(param.description)) param.description = "";
116
117 std::getline(ss, item, '|');
118 param.notes = trimSpaces(escapeQuotes(item));
119 if (containsOnlySymbols(param.notes)) param.notes = "";
120
121 if (separators == 10)
122 {
123 //an optional parameter is present
124 std::getline(ss, item, '|');
127 }
128 else if (separators == 9)
129 {
130 //this block is standard, no optional params found
131 }
132 else
133 {
134 std::cerr << "Detected a line with an invalid format in:" << inputfilename;
135 }
136
137 std::getline(ss, item, '\n');
138
139 // Add the param
140 m_params.push_back(param);
141 m_sectionGroup.insert(param);
142 }
143
144//just for test
145#if 0
146 Parameter ppp1;
147 ppp1.setFullyScopedParamName(std::string("aa::bb::cc::kk"));
148 ppp1.type="string";
149 m_params.push_back(ppp1);
151
152 Parameter ppp2;
153 ppp2.setFullyScopedParamName(std::string("aa::dd::ee::kk"));
154 ppp2.type = "string";
155 m_params.push_back(ppp2);
157#endif
158
159 inputfile.close();
160 return true;
161}
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 parseMdParams(std::string inputfilename)
Definition parse_md.cpp:23
std::deque< Parameter > m_params
Definition generator.h:34
SectionHandler m_sectionGroup
Definition generator.h:35
void insert(const Parameter &param)
bool containsOnlySymbols(const std::string &str)
Definition utils.h:33
std::string trimSpaces(const std::string &str)
Definition utils.h:43
std::string escapeQuotes(const std::string &str)
Definition utils.h:56