CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DocFormatHelper.cc
Go to the documentation of this file.
1 
3 
4 #include <ostream>
5 #include <iomanip>
6 
7 namespace edm {
8 
9  namespace {
10  void wrapAndPrintLine(std::ostream& os, std::string const& text, size_t indent, size_t suggestedWidth) {
11 
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 
25  // If the rest of the text fits on the current line,
26  // then print it and we are done
27  if ((length - startLine) <= suggestedWidth) {
28  os << std::setfill(' ') << std::setw(indent) << "";
29  if (startLine == 0) os << text;
30  else os << text.substr(startLine);
31  os << "\n";
32  break;
33  }
34 
35  // Look for next space
36  size_t pos = text.find_first_of(' ', startNextSearch);
37 
38  // No more spaces
39  if (pos == std::string::npos) {
40  // The rest of the comment cannot fit in the width or we
41  // would have already printed it. Break the line at the
42  // end of the previous word if there is one and print the
43  // first part. Then print the rest whether it fits or not.
44  if (startNextSearch != startLine) {
45  os << std::setfill(' ') << std::setw(indent) << "";
46  os << text.substr(startLine, startNextSearch - startLine);
47  os << "\n";
48  startLine = startNextSearch;
49  }
50  os << std::setfill(' ') << std::setw(indent) << "";
51  os << text.substr(startLine);
52  os << "\n";
53  break;
54  }
55 
56  if ((pos + 1U - startLine) > suggestedWidth) {
57 
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  }
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 
103  size_t pos = text.find_first_of('\n');
104  if (pos == std::string::npos) {
105  // no embedded newlines
106  wrapAndPrintLine(os, text, indent, suggestedWidth);
107  } else {
108  // print the first line.
109  wrapAndPrintLine(os, text.substr(0, pos), indent, suggestedWidth);
110  // print all lines after the first.
111  wrapAndPrintText(os, text.substr(pos + 1), indent, suggestedWidth);
112  }
113  }
114 
116  section_ = std::string();
117  pass_ = 0;
118  column1_ = 0;
119  column2_ = 0;
120  column3_ = 0;
121  counter_ = 0;
122  parent_ = OTHER;
123  }
124 
126 
127  // Make the length of a comment at least 30 characters
128  // per line, longer if there is more space available
129  size_t width = 30U;
130  if (lineWidth() > startColumn2() + 30U) {
131  width = lineWidth() - startColumn2();
132  }
133  return width;
134  }
135 
136  void DocFormatHelper::indent(std::ostream& os) const {
137  char oldFill = os.fill();
138  os << std::setfill(' ') << std::setw(indentation_) << "";
139  os.fill(oldFill);
140  }
141 
142  void DocFormatHelper::indent2(std::ostream& os) const {
143  char oldFill = os.fill();
144  os << std::setfill(' ') << std::setw(startColumn2_) << "";
145  os.fill(oldFill);
146  }
147 }
static void wrapAndPrintText(std::ostream &os, std::string const &text, size_t indent, size_t suggestedWidth)
int startColumn2() const
size_t lineWidth() const
tuple text
Definition: runonSM.py:42
void indent2(std::ostream &os) const
DescriptionParent parent_
void indent(std::ostream &os) const
size_t commentWidth() const