CMS 3D CMS Logo

DocFormatHelper.cc
Go to the documentation of this file.
1 
3 
4 #include <algorithm>
5 #include <ostream>
6 #include <iomanip>
7 
8 namespace edm {
9 
10  namespace {
11  void wrapAndPrintLine(std::ostream& os, std::string const& text, size_t indent, size_t suggestedWidth) {
12  char oldFill = os.fill();
13 
14  size_t length = text.size();
15 
16  // The position in the text where we start printing the next line
17  size_t startLine = 0U;
18 
19  // The position in the text where we start looking for the next blank space
20  size_t startNextSearch = 0U;
21 
22  // Loop over spaces in the text
23  while (true) {
24  // If the rest of the text fits on the current line,
25  // then print it and we are done
26  if ((length - startLine) <= suggestedWidth) {
27  os << std::setfill(' ') << std::setw(indent) << "";
28  if (startLine == 0)
29  os << text;
30  else
31  os << text.substr(startLine);
32  os << "\n";
33  break;
34  }
35 
36  // Look for next space
37  size_t pos = text.find_first_of(' ', startNextSearch);
38 
39  // No more spaces
40  if (pos == std::string::npos) {
41  // The rest of the comment cannot fit in the width or we
42  // would have already printed it. Break the line at the
43  // end of the previous word if there is one and print the
44  // first part. Then print the rest whether it fits or not.
45  if (startNextSearch != startLine) {
46  os << std::setfill(' ') << std::setw(indent) << "";
47  os << text.substr(startLine, startNextSearch - startLine);
48  os << "\n";
49  startLine = startNextSearch;
50  }
51  os << std::setfill(' ') << std::setw(indent) << "";
52  os << text.substr(startLine);
53  os << "\n";
54  break;
55  }
56 
57  if ((pos + 1U - startLine) > suggestedWidth) {
58  // With this word the line is too long. Print out to
59  // the end of the previous word if there was one.
60  // If there was not, then print to the end of this word
61  // even though it exceeds the width.
62  if (startNextSearch != startLine) {
63  os << std::setfill(' ') << std::setw(indent) << "";
64  os << text.substr(startLine, startNextSearch - startLine);
65  os << "\n";
66  startLine = startNextSearch;
67  }
68  if ((pos + 1U - startLine) > suggestedWidth) {
69  os << std::setfill(' ') << std::setw(indent) << "";
70  os << text.substr(startLine, pos + 1U - startLine);
71  os << "\n";
72  startLine = pos + 1U;
73  }
74  }
75  startNextSearch = pos + 1U;
76  }
77  os.fill(oldFill);
78  }
79  } // namespace
80 
81  // Print and wrap text to an output stream.
82  // This function looks for new line chars in the text,
83  // and calls wrapAndPrintLine() to output each line,
84  // which wraps appropriately. That function
85  // inserts new lines to try to break the text to fit into
86  // the suggested width. At the beginning and after every
87  // inserted newline this will print "indent" blank spaces.
88  // The function will consider inserting a new line after
89  // every blank space. If the text to the next blank
90  // space will exceed the "suggestedWidth" it inserts a
91  // new line. If the text between two blank spaces (a "word")
92  // is longer than the suggested width, then it prints the whole
93  // word anyway (exceeding the "suggestedWidth" for lines).
94  // The output will look nice if the input has words separated
95  // by a single blank space with no extra space in the input text,
96  // otherwise the extra spaces and newlines get printed
97  // making the output not nicely formatted ...
98  void DocFormatHelper::wrapAndPrintText(std::ostream& os,
99  std::string const& text,
100  size_t indent,
101  size_t suggestedWidth) {
102  size_t pos = text.find_first_of('\n');
103  if (pos == std::string::npos) {
104  // no embedded newlines
105  wrapAndPrintLine(os, text, indent, suggestedWidth);
106  } else {
107  // print the first line.
108  wrapAndPrintLine(os, text.substr(0, pos), indent, suggestedWidth);
109  // print all lines after the first.
110  wrapAndPrintText(os, text.substr(pos + 1), indent, suggestedWidth);
111  }
112  }
113 
115  section_ = std::string();
116  pass_ = 0;
117  column1_ = 0;
118  column2_ = 0;
119  column3_ = 0;
120  counter_ = 0;
121  parent_ = OTHER;
122  }
123 
125  // Make the length of a comment at least 30 characters
126  // per line, longer if there is more space available
127  size_t width = 30U;
128  if (lineWidth() > startColumn2() + 30U) {
129  width = lineWidth() - startColumn2();
130  }
131  return width;
132  }
133 
134  void DocFormatHelper::indent(std::ostream& os) const {
135  char oldFill = os.fill();
136  os << std::setfill(' ') << std::setw(indentation_) << "";
137  os.fill(oldFill);
138  }
139 
140  void DocFormatHelper::indent2(std::ostream& os) const {
141  char oldFill = os.fill();
142  os << std::setfill(' ') << std::setw(startColumn2_) << "";
143  os.fill(oldFill);
144  }
145 
148  }
149 
151  auto iter = std::find_if(
154  [&pluginCategory](std::pair<std::string, std::string> const& elem) { return elem.first == pluginCategory; });
155  if (iter == pluginCategoriesAlreadyPrinted_.end()) {
156  return std::string();
157  }
158  return iter->second;
159  }
160 
161 } // namespace edm
int startColumn2() const
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
std::vector< std::pair< std::string, std::string > > pluginCategoriesAlreadyPrinted_
size_t commentWidth() const
void indent2(std::ostream &os) const
DescriptionParent parent_
std::string const & section() const
const char * pluginCategory()
void addCategory(std::string const &pluginCategory, std::string const &section)
HLT enums.
size_t lineWidth() const
void indent(std::ostream &os) const
std::string sectionOfCategoryAlreadyPrinted(std::string const &pluginCategory) const