PropertyDefinitionConfigFormatter.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5  *
6  * ArmarX is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * ArmarX is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * @package ArmarXCore::core
19  * @author Jan Issac (jan dot issac at gmx dot de)
20  * @date 2012
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 
27 
28 #include <iomanip>
29 #include <sstream>
30 
31 #include <SimoxUtility/algorithm/string/string_tools.h>
32 
33 namespace armarx
34 {
35  std::string
37  std::string description,
38  std::string min,
39  std::string max,
40  std::string default_,
41  std::string casesensitivity,
42  std::string requirement,
43  std::string regex,
44  std::vector<std::string> values,
45  std::string value)
46  {
47  std::string output = getFormat();
48 
49  name = getPrefix() + name;
50 
51  output = simox::alg::replace_first(output, "%name%", formatName(name));
52  output = simox::alg::replace_first(output, "%description%", formatDescription(description));
53  output = simox::alg::replace_first(output, "%bounds%", formatBounds(min, max));
54  output = simox::alg::replace_first(output, "%default%", formatDefault(default_));
55  output = simox::alg::replace_first(
56  output, "%casesensitive%", formatCaseSensitivity(casesensitivity));
57  output = simox::alg::replace_first(output, "%required%", formatRequirement(requirement));
58  output = simox::alg::replace_first(output, "%regex%", formatRegex(regex));
59  output = simox::alg::replace_first(output, "%values%", formatValues(values));
60 
61  std::string valueEntry;
62 
63  if (!value.empty())
64  {
65  valueEntry = value;
66  }
67  else
68  {
69  if (default_.empty())
70  {
71  valueEntry = "<set value!>";
72  }
73  else
74  {
75  valueEntry = default_;
76  }
77  }
78 
79  if (requirement.compare("no") == 0 && value.empty())
80  {
81  // comment the property if it is not required
82  name = "# " + name;
83  }
84 
85  output = simox::alg::replace_first(output, "%name%", name);
86  output = simox::alg::replace_first(output, "%value%", valueEntry);
87 
88  return output + "\n";
89  }
90 
91  std::string
93  {
94  return std::string("%name%:") + " %description%\n" + "# Attributes:\n" + "%default%" +
95  "%bounds%" + "%casesensitive%" + "%required%" + "%regex%" + "%values%" +
96  "%name% = %value%\n\n";
97  }
98 
99  std::string
101  {
102  return "# " + name;
103  }
104 
105  std::string
107  {
108  std::string commentDescription = simox::alg::replace_all(description, "\n", "\n# ");
109 
110  return commentDescription;
111  }
112 
113  std::string
115  {
116  std::string bounds;
117 
118  if (!min.empty() && max.empty())
119  {
120  bounds = formatAttribute("Min:", min);
121  }
122  else if (min.empty() && !max.empty())
123  {
124  bounds = formatAttribute("Max:", max);
125  }
126  else if (!min.empty() && !max.empty())
127  {
128  bounds = formatAttribute("Bounds:", "[" + min + "; " + max + "]");
129  }
130 
131  return bounds;
132  }
133 
134  std::string
136  {
137  return formatAttribute("Default:", default_);
138  }
139 
140  std::string
142  {
143  return formatAttribute("Case sensitivity:", caseSensitivity);
144  }
145 
146  std::string
148  {
149  return formatAttribute("Required:", requirement);
150  }
151 
152  std::string
154  {
155  return formatAttribute("Format:", regex);
156  }
157 
158  std::string
159  PropertyDefinitionConfigFormatter::formatValues(std::vector<std::string> mapValues)
160  {
161  std::string valueStrings;
162 
163  if (mapValues.size() > 0)
164  {
165  valueStrings += "# - Possible values: {";
166 
167  std::vector<std::string>::iterator it = mapValues.begin();
168 
169  while (it != mapValues.end())
170  {
171  if (!it->empty())
172  {
173  valueStrings += formatValue(*it);
174  }
175 
176  ++it;
177 
178  valueStrings += (it != mapValues.end() ? ", " : "}\n");
179  }
180  }
181 
182  return valueStrings;
183  }
184 
185  std::string
187  {
188  return value;
189  }
190 
191  std::string
192  PropertyDefinitionConfigFormatter::formatAttribute(std::string name, std::string details)
193  {
194  if (!details.empty())
195  {
196  std::stringstream strStream;
197  strStream << std::setfill(' ') << std::left << std::setw(20) << name;
198  strStream << details;
199 
200  return "# - " + strStream.str() + "\n";
201  }
202 
203  return std::string();
204  }
205 
206  std::string
208  {
209  std::string formattedHeaderText = "# " + headerText;
210  formattedHeaderText = simox::alg::replace_all(formattedHeaderText, "\n", "\n# ");
211 
212  return "# ==================================================================\n" +
213  formattedHeaderText + "\n" +
214  "# ==================================================================\n\n";
215  }
216 } // namespace armarx
armarx::PropertyDefinitionConfigFormatter::formatHeader
std::string formatHeader(std::string headerText) override
Definition: PropertyDefinitionConfigFormatter.cpp:207
armarx::PropertyDefinitionConfigFormatter::formatName
std::string formatName(std::string name) override
Definition: PropertyDefinitionConfigFormatter.cpp:100
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:297
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::PropertyDefinitionConfigFormatter::formatValue
std::string formatValue(std::string value) override
Definition: PropertyDefinitionConfigFormatter.cpp:186
armarx::PropertyDefinitionConfigFormatter::formatRegex
std::string formatRegex(std::string regex) override
Definition: PropertyDefinitionConfigFormatter.cpp:153
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::PropertyDefinitionConfigFormatter::formatDefault
std::string formatDefault(std::string default_) override
Definition: PropertyDefinitionConfigFormatter.cpp:135
PropertyDefinitionConfigFormatter.h
armarx::PropertyDefinitionConfigFormatter::formatDefinition
std::string formatDefinition(std::string name, std::string description, std::string min, std::string max, std::string default_, std::string casesensitivity, std::string requirement, std::string reged, std::vector< std::string > values, std::string value) override
Definition: PropertyDefinitionConfigFormatter.cpp:36
armarx::PropertyDefinitionConfigFormatter::formatRequirement
std::string formatRequirement(std::string requirement) override
Definition: PropertyDefinitionConfigFormatter.cpp:147
armarx::PropertyDefinitionFormatter::getPrefix
virtual std::string getPrefix() const
Definition: PropertyDefinitionFormatter.h:78
armarx::PropertyDefinitionConfigFormatter::getFormat
std::string getFormat() override
Definition: PropertyDefinitionConfigFormatter.cpp:92
armarx::PropertyDefinitionConfigFormatter::formatBounds
std::string formatBounds(std::string min, std::string max) override
Definition: PropertyDefinitionConfigFormatter.cpp:114
armarx::PropertyDefinitionConfigFormatter::formatDescription
std::string formatDescription(std::string description) override
Definition: PropertyDefinitionConfigFormatter.cpp:106
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:327
armarx::PropertyDefinitionConfigFormatter::formatValues
std::string formatValues(std::vector< std::string > values) override
Definition: PropertyDefinitionConfigFormatter.cpp:159
armarx::PropertyDefinitionConfigFormatter::formatCaseSensitivity
std::string formatCaseSensitivity(std::string caseSensitivity) override
Definition: PropertyDefinitionConfigFormatter.cpp:141
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::PropertyDefinitionConfigFormatter::formatAttribute
std::string formatAttribute(std::string name, std::string details) override
Definition: PropertyDefinitionConfigFormatter.cpp:192