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 
26 using namespace armarx;
27 
28 Lines::Lines(const std::string& name, const std::string& i, const LineType t) :
29  name(), indentChars(i), lastLineType(t)
30 {
31 }
32 
33 void
35 {
36  this->line("{");
37  indent++;
38  lastLineType = Lines::LineType::StartBlock;
39 }
40 
41 void
42 Lines::startBlock(const std::string& line)
43 {
44  this->line(line);
45  this->line("{");
46  indent++;
47  lastLineType = Lines::LineType::StartBlock;
48 }
49 
50 void
52 {
53  indent--;
54  this->line("}");
55  lastLineType = Lines::LineType::EndBlock;
56 }
57 
58 void
59 Lines::endBlock(const std::string& line)
60 {
61  indent--;
62  this->line(std::string("}") + line);
63  lastLineType = Lines::LineType::EndBlock;
64 }
65 
66 void
67 Lines::endBlockComment(std::string comment)
68 {
69  indent--;
70  this->line(std::string("} // ") + comment);
71  lastLineType = Lines::LineType::EndBlock;
72 }
73 
74 void
75 Lines::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 
95 void
96 Lines::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 
117 void
119 {
120  lineInternal("", indent);
121 }
122 
123 void
125 {
126  switch (getLastLineType())
127  {
131  break;
132 
135  // no empty line needed
136  break;
137  }
138 }
139 
142 {
143  return lastLineType;
144 }
145 
146 void
147 Lines::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 
158 MetaWriter::MetaWriter(const std::string& doc) :
159  header("header"), body("body"), footer("footer"), fileDoc(doc)
160 {
161 }
162 
163 void
164 MetaWriter::ConvertToTextWithMaxWidth(unsigned int maxTextWidth,
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 
181 std::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 }
armarx::MetaWriter::header
Lines header
Definition: MetaWriter.h:95
armarx::Lines::startBlock
void startBlock()
Definition: MetaWriter.cpp:34
armarx::Lines::endBlockComment
void endBlockComment(std::string comment)
Definition: MetaWriter.cpp:67
armarx::MetaWriter::MetaWriter
MetaWriter(const std::string &doc)
Definition: MetaWriter.cpp:158
armarx::Lines::LineType
LineType
Definition: MetaWriter.h:42
armarx::Lines::LineType::Normal
@ Normal
armarx::Lines::LineType::Empty
@ Empty
armarx::Lines::ss
std::stringstream ss
Definition: MetaWriter.h:51
armarx::Lines::lineCheckDuplicate
void lineCheckDuplicate(const std::string &line, int indentDelta=0)
Definition: MetaWriter.cpp:96
armarx::Lines::Lines
Lines()=delete
armarx::Lines::line
void line()
Definition: MetaWriter.cpp:124
armarx::Lines::LineType::StartBlock
@ StartBlock
armarx::Lines::indent
int indent
Definition: MetaWriter.h:53
armarx::MetaWriter::footer
Lines footer
Definition: MetaWriter.h:97
armarx::MetaWriter::ConvertToTextWithMaxWidth
static void ConvertToTextWithMaxWidth(unsigned int maxTextWidth, const std::vector< std::string > &docLines, std::vector< std::string > &linesOut)
Definition: MetaWriter.cpp:164
armarx::Lines::enforceEmptyLine
void enforceEmptyLine()
Definition: MetaWriter.cpp:118
armarx::Lines::endBlock
void endBlock()
Definition: MetaWriter.cpp:51
armarx::Lines::LineType::EndBlock
@ EndBlock
armarx::MetaWriter::body
Lines body
Definition: MetaWriter.h:96
armarx::Lines::lines
std::vector< std::string > lines
Definition: MetaWriter.h:52
MetaWriter.h
armarx::MetaWriter::getString
std::string getString()
Definition: MetaWriter.cpp:182
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::Lines::getLastLineType
Lines::LineType getLastLineType()
Definition: MetaWriter.cpp:141
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:38