MetaWriter.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
19 * @author
20 * @date
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24#include "MetaWriter.h"
25
26using namespace armarx;
27
28Lines::Lines(const std::string& name, const std::string& i, const LineType t) :
29 name(), indentChars(i), lastLineType(t)
30{
31}
32
33void
35{
36 this->line("{");
37 indent++;
38 lastLineType = Lines::LineType::StartBlock;
39}
40
41void
42Lines::startBlock(const std::string& line)
43{
44 this->line(line);
45 this->line("{");
46 indent++;
47 lastLineType = Lines::LineType::StartBlock;
48}
49
50void
52{
53 indent--;
54 this->line("}");
55 lastLineType = Lines::LineType::EndBlock;
56}
57
58void
59Lines::endBlock(const std::string& line)
60{
61 indent--;
62 this->line(std::string("}") + line);
63 lastLineType = Lines::LineType::EndBlock;
64}
65
66void
67Lines::endBlockComment(std::string comment)
68{
69 indent--;
70 this->line(std::string("} // ") + comment);
71 lastLineType = Lines::LineType::EndBlock;
72}
73
74void
75Lines::line(const std::string& line, int indentDelta)
76{
77 if (line.empty())
78 {
79 this->line();
80 return;
81 }
82
83 if (const auto& it = std::find(lines.begin(), lines.end(), line); it == lines.end())
84 {
85 lines.push_back(line);
86 }
87
88 std::vector<std::string> docLines = simox::alg::split(line, "\n", false);
89 for (auto& l : docLines)
90 {
91 lineInternal(l, indent + indentDelta);
92 }
93}
94
95void
96Lines::lineCheckDuplicate(const std::string& line, int indentDelta)
97{
98 // Empty lines are ok
99 if (line.empty())
100 {
101 this->line();
102 return;
103 }
104
105 if (const auto& it = std::find(lines.begin(), lines.end(), line); it == lines.end())
106 {
107 lines.push_back(line);
108
109 std::vector<std::string> docLines = simox::alg::split(line, "\n", false);
110 for (auto& l : docLines)
111 {
112 lineInternal(l, indent + indentDelta);
113 }
114 }
115}
116
117void
119{
120 lineInternal("", indent);
121}
122
123void
125{
126 switch (getLastLineType())
127 {
131 break;
132
135 // no empty line needed
136 break;
137 }
138}
139
142{
143 return lastLineType;
144}
145
146void
147Lines::lineInternal(const std::string& line, int indent)
148{
149 for (int i = 0; i < indent; i++)
150 {
151 ss << indentChars;
152 }
153
154 ss << line << std::endl;
155 lastLineType = line.empty() ? Lines::LineType::Empty : Lines::LineType::Normal;
156}
157
158MetaWriter::MetaWriter(const std::string& doc) :
159 header("header"), body("body"), footer("footer"), fileDoc(doc)
160{
161}
162
163void
165 const std::vector<std::string>& docLines,
166 std::vector<std::string>& linesOut)
167{
168 for (std::string line : docLines)
169 {
170 while (line.size() > maxTextWidth)
171 {
172 auto pos = line.rfind(" ", maxTextWidth);
173 linesOut.push_back(line.substr(0, pos));
174 line.erase(0, pos);
175 }
176
177 linesOut.push_back(line);
178 }
179}
180
181std::string
183{
184 std::stringstream ss;
185 ss << header.ss.str();
186 ss << body.ss.str();
187 ss << footer.ss.str();
188 return ss.str();
189}
Lines::LineType getLastLineType()
Lines()=delete
void startBlock()
void lineCheckDuplicate(const std::string &line, int indentDelta=0)
std::vector< std::string > lines
Definition MetaWriter.h:52
void enforceEmptyLine()
std::stringstream ss
Definition MetaWriter.h:51
std::string name
Definition MetaWriter.h:50
void endBlockComment(std::string comment)
static void ConvertToTextWithMaxWidth(unsigned int maxTextWidth, const std::vector< std::string > &docLines, std::vector< std::string > &linesOut)
MetaWriter(const std::string &doc)
std::string getString()
std::string fileDoc
Definition MetaWriter.h:100
This file offers overloads of toIce() and fromIce() functions for STL container types.