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 
35 {
36  this->line("{");
37  indent++;
38  lastLineType = Lines::LineType::StartBlock;
39 }
40 
41 void Lines::startBlock(const std::string& line)
42 {
43  this->line(line);
44  this->line("{");
45  indent++;
46  lastLineType = Lines::LineType::StartBlock;
47 }
48 
50 {
51  indent--;
52  this->line("}");
53  lastLineType = Lines::LineType::EndBlock;
54 }
55 
56 void Lines::endBlock(const std::string& line)
57 {
58  indent--;
59  this->line(std::string("}") + line);
60  lastLineType = Lines::LineType::EndBlock;
61 }
62 
63 void Lines::endBlockComment(std::string comment)
64 {
65  indent--;
66  this->line(std::string("} // ") + comment);
67  lastLineType = Lines::LineType::EndBlock;
68 }
69 
70 void Lines::line(const std::string& line, int indentDelta)
71 {
72  if (line.empty())
73  {
74  this->line();
75  return;
76  }
77 
78  if (const auto& it = std::find(lines.begin(), lines.end(), line); it == lines.end())
79  {
80  lines.push_back(line);
81  }
82 
83  std::vector<std::string> docLines = simox::alg::split(line, "\n", false);
84  for (auto& l : docLines)
85  {
86  lineInternal(l, indent + indentDelta);
87  }
88 }
89 
90 void Lines::lineCheckDuplicate(const std::string& line, int indentDelta)
91 {
92  // Empty lines are ok
93  if (line.empty())
94  {
95  this->line();
96  return;
97  }
98 
99  if (const auto& it = std::find(lines.begin(), lines.end(), line); it == lines.end())
100  {
101  lines.push_back(line);
102 
103  std::vector<std::string> docLines = simox::alg::split(line, "\n", false);
104  for (auto& l : docLines)
105  {
106  lineInternal(l, indent + indentDelta);
107  }
108  }
109 }
110 
112 {
113  lineInternal("", indent);
114 }
116 {
117  switch (getLastLineType())
118  {
122  break;
123 
126  // no empty line needed
127  break;
128  }
129 }
130 
132 {
133  return lastLineType;
134 }
135 
136 void Lines::lineInternal(const std::string& line, int indent)
137 {
138  for (int i = 0; i < indent; i++)
139  {
140  ss << indentChars;
141  }
142 
143  ss << line << std::endl;
144  lastLineType = line.empty() ? Lines::LineType::Empty : Lines::LineType::Normal;
145 }
146 
147 MetaWriter::MetaWriter(const std::string& doc) :
148  header("header"),
149  body("body"),
150  footer("footer"),
151  fileDoc(doc)
152 {
153 }
154 
155 void MetaWriter::ConvertToTextWithMaxWidth(unsigned int maxTextWidth, const std::vector<std::string>& docLines, std::vector<std::string>& linesOut)
156 {
157  for (std::string line : docLines)
158  {
159  while (line.size() > maxTextWidth)
160  {
161  auto pos = line.rfind(" ", maxTextWidth);
162  linesOut.push_back(line.substr(0, pos));
163  line.erase(0, pos);
164  }
165 
166  linesOut.push_back(line);
167  }
168 }
169 
171 {
172  std::stringstream ss;
173  ss << header.ss.str();
174  ss << body.ss.str();
175  ss << footer.ss.str();
176  return ss.str();
177 }
armarx::MetaWriter::header
Lines header
Definition: MetaWriter.h:89
armarx::Lines::startBlock
void startBlock()
Definition: MetaWriter.cpp:34
armarx::Lines::endBlockComment
void endBlockComment(std::string comment)
Definition: MetaWriter.cpp:63
armarx::MetaWriter::MetaWriter
MetaWriter(const std::string &doc)
Definition: MetaWriter.cpp:147
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:45
armarx::Lines::lineCheckDuplicate
void lineCheckDuplicate(const std::string &line, int indentDelta=0)
Definition: MetaWriter.cpp:90
armarx::Lines::Lines
Lines()=delete
armarx::Lines::line
void line()
Definition: MetaWriter.cpp:115
armarx::Lines::LineType::StartBlock
@ StartBlock
armarx::Lines::indent
int indent
Definition: MetaWriter.h:47
armarx::MetaWriter::footer
Lines footer
Definition: MetaWriter.h:91
armarx::MetaWriter::ConvertToTextWithMaxWidth
static void ConvertToTextWithMaxWidth(unsigned int maxTextWidth, const std::vector< std::string > &docLines, std::vector< std::string > &linesOut)
Definition: MetaWriter.cpp:155
armarx::Lines::enforceEmptyLine
void enforceEmptyLine()
Definition: MetaWriter.cpp:111
armarx::Lines::endBlock
void endBlock()
Definition: MetaWriter.cpp:49
armarx::Lines::LineType::EndBlock
@ EndBlock
armarx::MetaWriter::body
Lines body
Definition: MetaWriter.h:90
armarx::Lines::lines
std::vector< std::string > lines
Definition: MetaWriter.h:46
MetaWriter.h
armarx::MetaWriter::getString
std::string getString()
Definition: MetaWriter.cpp:170
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::Lines::getLastLineType
Lines::LineType getLastLineType()
Definition: MetaWriter.cpp:131
armarx::split
std::vector< std::string > split(const std::string &source, const std::string &splitBy, bool trimElements=false, bool removeEmptyElements=false)
Definition: StringHelpers.cpp:36