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 <SimoxUtility/algorithm/string/string_tools.h>
29 
30 #include <sstream>
31 #include <iomanip>
32 
33 namespace armarx
34 {
36  std::string name,
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(output, "%casesensitive%", formatCaseSensitivity(casesensitivity));
56  output = simox::alg::replace_first(output, "%required%", formatRequirement(requirement));
57  output = simox::alg::replace_first(output, "%regex%", formatRegex(regex));
58  output = simox::alg::replace_first(output, "%values%", formatValues(values));
59 
60  std::string valueEntry;
61 
62  if (!value.empty())
63  {
64  valueEntry = value;
65  }
66  else
67  {
68  if (default_.empty())
69  {
70  valueEntry = "<set value!>";
71  }
72  else
73  {
74  valueEntry = default_;
75  }
76  }
77 
78  if (requirement.compare("no") == 0 && value.empty())
79  {
80  // comment the property if it is not required
81  name = "# " + name;
82  }
83 
84  output = simox::alg::replace_first(output, "%name%", name);
85  output = simox::alg::replace_first(output, "%value%", valueEntry);
86 
87  return output + "\n";
88  }
89 
91  {
92  return std::string("%name%:")
93  + " %description%\n"
94  + "# Attributes:\n"
95  + "%default%"
96  + "%bounds%"
97  + "%casesensitive%"
98  + "%required%"
99  + "%regex%"
100  + "%values%"
101  + "%name% = %value%\n\n";
102  }
103 
104  std::string PropertyDefinitionConfigFormatter::formatName(std::string name)
105  {
106  return "# " + name;
107  }
108 
109  std::string PropertyDefinitionConfigFormatter::formatDescription(std::string description)
110  {
111  std::string commentDescription = simox::alg::replace_all(description, "\n", "\n# ");
112 
113  return commentDescription;
114  }
115 
116  std::string PropertyDefinitionConfigFormatter::formatBounds(std::string min, std::string max)
117  {
118  std::string bounds;
119 
120  if (!min.empty() && max.empty())
121  {
122  bounds = formatAttribute("Min:", min);
123  }
124  else if (min.empty() && !max.empty())
125  {
126  bounds = formatAttribute("Max:", max);
127  }
128  else if (!min.empty() && !max.empty())
129  {
130  bounds = formatAttribute("Bounds:", "[" + min + "; " + max + "]");
131  }
132 
133  return bounds;
134  }
135 
136  std::string PropertyDefinitionConfigFormatter::formatDefault(std::string default_)
137  {
138  return formatAttribute("Default:", default_);
139  }
140 
141  std::string PropertyDefinitionConfigFormatter::formatCaseSensitivity(std::string caseSensitivity)
142  {
143  return formatAttribute("Case sensitivity:", caseSensitivity);
144  }
145 
146  std::string PropertyDefinitionConfigFormatter::formatRequirement(std::string requirement)
147  {
148  return formatAttribute("Required:", requirement);
149  }
150 
151  std::string PropertyDefinitionConfigFormatter::formatRegex(std::string regex)
152  {
153  return formatAttribute("Format:", regex);
154  }
155 
156  std::string PropertyDefinitionConfigFormatter::formatValues(std::vector<std::string> mapValues)
157  {
158  std::string valueStrings;
159 
160  if (mapValues.size() > 0)
161  {
162  valueStrings += "# - Possible values: {";
163 
164  std::vector<std::string>::iterator it = mapValues.begin();
165 
166  while (it != mapValues.end())
167  {
168  if (!it->empty())
169  {
170  valueStrings += formatValue(*it);
171  }
172 
173  ++it;
174 
175  valueStrings += (it != mapValues.end() ? ", " : "}\n");
176  }
177  }
178 
179  return valueStrings ;
180  }
181 
183  {
184  return value;
185  }
186 
187  std::string PropertyDefinitionConfigFormatter::formatAttribute(std::string name, std::string details)
188  {
189  if (!details.empty())
190  {
191  std::stringstream strStream;
192  strStream << std::setfill(' ') << std::left << std::setw(20) << name;
193  strStream << details;
194 
195  return "# - " + strStream.str() + "\n";
196  }
197 
198  return std::string();
199  }
200 
201  std::string PropertyDefinitionConfigFormatter::formatHeader(std::string headerText)
202  {
203  std::string formattedHeaderText = "# " + headerText;
204  formattedHeaderText = simox::alg::replace_all(formattedHeaderText, "\n", "\n# ");
205 
206  return "# ==================================================================\n"
207  + formattedHeaderText + "\n"
208  + "# ==================================================================\n\n";
209  }
210 }
armarx::PropertyDefinitionConfigFormatter::formatHeader
std::string formatHeader(std::string headerText) override
Definition: PropertyDefinitionConfigFormatter.cpp:201
armarx::PropertyDefinitionConfigFormatter::formatName
std::string formatName(std::string name) override
Definition: PropertyDefinitionConfigFormatter.cpp:104
armarx::max
std::vector< T > max(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:267
ProsthesisInterface.values
values
Definition: ProsthesisInterface.py:190
armarx::PropertyDefinitionConfigFormatter::formatValue
std::string formatValue(std::string value) override
Definition: PropertyDefinitionConfigFormatter.cpp:182
armarx::PropertyDefinitionConfigFormatter::formatRegex
std::string formatRegex(std::string regex) override
Definition: PropertyDefinitionConfigFormatter.cpp:151
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:926
armarx::PropertyDefinitionConfigFormatter::formatDefault
std::string formatDefault(std::string default_) override
Definition: PropertyDefinitionConfigFormatter.cpp:136
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:35
armarx::PropertyDefinitionConfigFormatter::formatRequirement
std::string formatRequirement(std::string requirement) override
Definition: PropertyDefinitionConfigFormatter.cpp:146
armarx::PropertyDefinitionFormatter::getPrefix
virtual std::string getPrefix() const
Definition: PropertyDefinitionFormatter.h:75
armarx::PropertyDefinitionConfigFormatter::getFormat
std::string getFormat() override
Definition: PropertyDefinitionConfigFormatter.cpp:90
armarx::PropertyDefinitionConfigFormatter::formatBounds
std::string formatBounds(std::string min, std::string max) override
Definition: PropertyDefinitionConfigFormatter.cpp:116
armarx::PropertyDefinitionConfigFormatter::formatDescription
std::string formatDescription(std::string description) override
Definition: PropertyDefinitionConfigFormatter.cpp:109
armarx::min
std::vector< T > min(const std::vector< T > &v1, const std::vector< T > &v2)
Definition: VectorHelpers.h:294
armarx::PropertyDefinitionConfigFormatter::formatValues
std::string formatValues(std::vector< std::string > values) override
Definition: PropertyDefinitionConfigFormatter.cpp:156
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:28
armarx::PropertyDefinitionConfigFormatter::formatAttribute
std::string formatAttribute(std::string name, std::string details) override
Definition: PropertyDefinitionConfigFormatter.cpp:187