Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-03-17 11:03:25

0001 
0002 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
0003 
0004 #include <ostream>
0005 #include <iomanip>
0006 
0007 namespace edm {
0008 
0009   namespace {
0010     void wrapAndPrintLine(std::ostream& os, std::string const& text, size_t indent, size_t suggestedWidth) {
0011       char oldFill = os.fill();
0012 
0013       size_t length = text.size();
0014 
0015       // The position in the text where we start printing the next line
0016       size_t startLine = 0U;
0017 
0018       // The position in the text where we start looking for the next blank space
0019       size_t startNextSearch = 0U;
0020 
0021       // Loop over spaces in the text
0022       while (true) {
0023         // If the rest of the text fits on the current line,
0024         // then print it and we are done
0025         if ((length - startLine) <= suggestedWidth) {
0026           os << std::setfill(' ') << std::setw(indent) << "";
0027           if (startLine == 0)
0028             os << text;
0029           else
0030             os << text.substr(startLine);
0031           os << "\n";
0032           break;
0033         }
0034 
0035         // Look for next space
0036         size_t pos = text.find_first_of(' ', startNextSearch);
0037 
0038         // No more spaces
0039         if (pos == std::string::npos) {
0040           // The rest of the comment cannot fit in the width or we
0041           // would have already printed it.  Break the line at the
0042           // end of the previous word if there is one and print the
0043           // first part.  Then print the rest whether it fits or not.
0044           if (startNextSearch != startLine) {
0045             os << std::setfill(' ') << std::setw(indent) << "";
0046             os << text.substr(startLine, startNextSearch - startLine);
0047             os << "\n";
0048             startLine = startNextSearch;
0049           }
0050           os << std::setfill(' ') << std::setw(indent) << "";
0051           os << text.substr(startLine);
0052           os << "\n";
0053           break;
0054         }
0055 
0056         if ((pos + 1U - startLine) > suggestedWidth) {
0057           // With this word the line is too long.  Print out to
0058           // the end of the previous word if there was one.
0059           // If there was not, then print to the end of this word
0060           // even though it exceeds the width.
0061           if (startNextSearch != startLine) {
0062             os << std::setfill(' ') << std::setw(indent) << "";
0063             os << text.substr(startLine, startNextSearch - startLine);
0064             os << "\n";
0065             startLine = startNextSearch;
0066           }
0067           if ((pos + 1U - startLine) > suggestedWidth) {
0068             os << std::setfill(' ') << std::setw(indent) << "";
0069             os << text.substr(startLine, pos + 1U - startLine);
0070             os << "\n";
0071             startLine = pos + 1U;
0072           }
0073         }
0074         startNextSearch = pos + 1U;
0075       }
0076       os.fill(oldFill);
0077     }
0078   }  // namespace
0079 
0080   // Print and wrap text to an output stream.
0081   // This function looks for new line chars in the text,
0082   // and calls wrapAndPrintLine() to output each line,
0083   // which wraps appropriately.  That function
0084   // inserts new lines to try to break the text to fit into
0085   // the suggested width.  At the beginning and after every
0086   // inserted newline this will print "indent" blank spaces.
0087   // The function will consider inserting a new line after
0088   // every blank space.  If the text to the next blank
0089   // space will exceed the "suggestedWidth" it inserts a
0090   // new line.  If the text between two blank spaces (a "word")
0091   // is longer than the suggested width, then it prints the whole
0092   // word anyway (exceeding the "suggestedWidth" for lines).
0093   // The output will look nice if the input has words separated
0094   // by a single blank space with no extra space in the input text,
0095   // otherwise the extra spaces and newlines get printed
0096   // making the output not nicely formatted ...
0097   void DocFormatHelper::wrapAndPrintText(std::ostream& os,
0098                                          std::string const& text,
0099                                          size_t indent,
0100                                          size_t suggestedWidth) {
0101     size_t pos = text.find_first_of('\n');
0102     if (pos == std::string::npos) {
0103       // no embedded newlines
0104       wrapAndPrintLine(os, text, indent, suggestedWidth);
0105     } else {
0106       // print the first line.
0107       wrapAndPrintLine(os, text.substr(0, pos), indent, suggestedWidth);
0108       // print all lines after the first.
0109       wrapAndPrintText(os, text.substr(pos + 1), indent, suggestedWidth);
0110     }
0111   }
0112 
0113   void DocFormatHelper::init() {
0114     section_ = std::string();
0115     pass_ = 0;
0116     column1_ = 0;
0117     column2_ = 0;
0118     column3_ = 0;
0119     counter_ = 0;
0120     parent_ = OTHER;
0121   }
0122 
0123   size_t DocFormatHelper::commentWidth() const {
0124     // Make the length of a comment at least 30 characters
0125     // per line, longer if there is more space available
0126     size_t width = 30U;
0127     if (lineWidth() > startColumn2() + 30U) {
0128       width = lineWidth() - startColumn2();
0129     }
0130     return width;
0131   }
0132 
0133   void DocFormatHelper::indent(std::ostream& os) const {
0134     char oldFill = os.fill();
0135     os << std::setfill(' ') << std::setw(indentation_) << "";
0136     os.fill(oldFill);
0137   }
0138 
0139   void DocFormatHelper::indent2(std::ostream& os) const {
0140     char oldFill = os.fill();
0141     os << std::setfill(' ') << std::setw(startColumn2_) << "";
0142     os.fill(oldFill);
0143   }
0144 }  // namespace edm