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 blockpattern(R"(\|\s*:\-*:\s*\|)");
38 std::regex includepattern(R"(#include\s+(.+))");
39
40 //search for the line containing the pattern, then close the file.
41 //the pattern can be present or not.
42 //the idea is to skip all the lines before the pattern.
43 //the output of this block is the counter linepattern, which
44 //is the number of lines to skip in the next block
45 int linepattern = -1;
46 size_t i=0;
47 while (std::getline(inputfile, line))
48 {
49 //find for possible lines #include <filename> and process it if found
50 std::smatch match;
51 if (std::regex_search(line, match, includepattern))
52 {
53 bool brec = parseMdParams(match[1]);
54 if (!brec)
55 {
56 std::cerr << "Unable to process included file" << std::endl;
57 return false;
58 }
59 else
60 {
61 //success, added x params
62 }
63 linepattern = i;
64 }
65
66 //find the end of the header block |:--------------:|
67 if (std::regex_search(line, blockpattern))
68 {
69 linepattern = i;
70 break;
71 }
72 i++;
73 }
74 inputfile.close();
75
76 //reopen the file
77 inputfile.open(inputfilename);
78 b = inputfile.is_open();
79 if (!b)
80 {
81 std::cerr << "Unable to open file: " << inputfilename << std::endl;
82 return false;
83 }
84
85 // Read the file line by line
86 while (std::getline(inputfile, line))
87 {
88 //skip the first lines (before the pattern has been found)
89 if (linepattern>=0) {linepattern--; continue;}
90
91 //after the pattern, data is valid and ready to be processed
92 std::stringstream ss(line);
93 std::string item;
94 Parameter param;
95
96 //count the number of '|' chars in the line
97 size_t separators = 0;
98 for (char c : line) {
99 if (c == '|') { separators++; }
100 }
101
102 // Leggi i valori separati da '|'
103 std::getline(ss, item, '|'); // Ignora la prima barra vuota
104 std::getline(ss, item, '|');
105 std::string group_string = trimSpaces(item);
106 if (containsOnlySymbols(group_string)) group_string = "";
107 std::getline(ss, item, '|');
108 std::string param_string = trimSpaces(item);
109 if (containsOnlySymbols(param_string)) param_string = "";
110
111 std::string fully_scoped_param_name;
112 if (!group_string.empty())
113 {fully_scoped_param_name = group_string+std::string("::")+param_string;}
114 else
115 {fully_scoped_param_name = param_string;}
116
117 param.setFullyScopedParamName(fully_scoped_param_name);
118
119
120 std::getline(ss, item, '|');
121 param.type = trimSpaces(item);
122
123 std::getline(ss, item, '|');
124 param.units = trimSpaces(item);
125 if (containsOnlySymbols(param.units)) param.units = "";
126
127 std::getline(ss, item, '|');
128 param.defaultValue = trimSpaces(item);
129 if (containsOnlySymbols(param.defaultValue)) param.defaultValue = "";
130
131 std::getline(ss, item, '|');
132 std::string req_s = trimSpaces(item);
133 if (req_s == "Yes" || req_s == "yes" || req_s == "True" || req_s == "true" || req_s == "1")
134 param.required = true;
135
136 std::getline(ss, item, '|');
137 param.description = trimSpaces(escapeQuotes(item));
138 if (containsOnlySymbols(param.description)) param.description = "";
139
140 std::getline(ss, item, '|');
141 param.notes = trimSpaces(escapeQuotes(item));
142 if (containsOnlySymbols(param.notes)) param.notes = "";
143
144 if (separators == 10)
145 {
146 //an optional parameter is present
147 std::getline(ss, item, '|');
150 }
151 else if (separators == 9)
152 {
153 //this block is standard, no optional params found
154 }
155 else
156 {
157 std::cerr << "Detected a line with an invalid format in:" << inputfilename;
158 }
159
160 std::getline(ss, item, '\n');
161
162 // Add the param
163 m_params.push_back(param);
164 m_sectionGroup.insert(param);
165 }
166
167//just for test
168#if 0
169 Parameter ppp1;
170 ppp1.setFullyScopedParamName(std::string("aa::bb::cc::kk"));
171 ppp1.type="string";
172 m_params.push_back(ppp1);
174
175 Parameter ppp2;
176 ppp2.setFullyScopedParamName(std::string("aa::dd::ee::kk"));
177 ppp2.type = "string";
178 m_params.push_back(ppp2);
180#endif
181
182 inputfile.close();
183 return true;
184}
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