CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_0/src/FWCore/ParameterSet/src/DocFormatHelper.cc

Go to the documentation of this file.
00001 
00002 #include "FWCore/ParameterSet/interface/DocFormatHelper.h"
00003 
00004 #include <ostream>
00005 #include <iomanip>
00006 
00007 namespace edm {
00008 
00009   namespace {
00010     void wrapAndPrintLine(std::ostream& os, std::string const& text, size_t indent, size_t suggestedWidth) {
00011 
00012       char oldFill = os.fill();
00013 
00014       size_t length = text.size();
00015 
00016       // The position in the text where we start printing the next line
00017       size_t startLine = 0U;
00018 
00019       // The position in the text where we start looking for the next blank space
00020       size_t startNextSearch = 0U;
00021 
00022       // Loop over spaces in the text
00023       while (true) {
00024 
00025         // If the rest of the text fits on the current line,
00026         // then print it and we are done
00027         if ((length - startLine) <= suggestedWidth) {
00028           os << std::setfill(' ') << std::setw(indent) << "";
00029           if (startLine == 0) os << text;
00030           else os << text.substr(startLine);
00031           os << "\n";
00032           break;
00033         }
00034 
00035         // Look for next space
00036         size_t pos = text.find_first_of(' ', startNextSearch);
00037 
00038         // No more spaces
00039         if (pos == std::string::npos) {
00040           // The rest of the comment cannot fit in the width or we
00041           // would have already printed it.  Break the line at the
00042           // end of the previous word if there is one and print the
00043           // first part.  Then print the rest whether it fits or not.
00044           if (startNextSearch != startLine) {
00045             os << std::setfill(' ') << std::setw(indent) << "";
00046             os << text.substr(startLine, startNextSearch - startLine);
00047             os << "\n";
00048             startLine = startNextSearch;
00049           }
00050           os << std::setfill(' ') << std::setw(indent) << "";
00051           os << text.substr(startLine);
00052           os << "\n";
00053           break;
00054         }
00055 
00056         if ((pos + 1U - startLine) > suggestedWidth) {
00057 
00058           // With this word the line is too long.  Print out to
00059           // the end of the previous word if there was one.
00060           // If there was not, then print to the end of this word
00061           // even though it exceeds the width.
00062           if (startNextSearch != startLine) {
00063             os << std::setfill(' ') << std::setw(indent) << "";
00064             os << text.substr(startLine, startNextSearch - startLine);
00065             os << "\n";
00066             startLine = startNextSearch;
00067           }
00068           if ((pos + 1U - startLine) > suggestedWidth) {
00069             os << std::setfill(' ') << std::setw(indent) << "";
00070             os << text.substr(startLine, pos + 1U - startLine);
00071             os << "\n";
00072             startLine = pos + 1U;
00073           }
00074         }
00075         startNextSearch = pos + 1U;
00076       }
00077       os.fill(oldFill);
00078     }
00079   }
00080 
00081   // Print and wrap text to an output stream.
00082   // This function looks for new line chars in the text,
00083   // and calls wrapAndPrintLine() to output each line,
00084   // which wraps appropriately.  That function
00085   // inserts new lines to try to break the text to fit into
00086   // the suggested width.  At the beginning and after every
00087   // inserted newline this will print "indent" blank spaces.
00088   // The function will consider inserting a new line after
00089   // every blank space.  If the text to the next blank
00090   // space will exceed the "suggestedWidth" it inserts a
00091   // new line.  If the text between two blank spaces (a "word")
00092   // is longer than the suggested width, then it prints the whole
00093   // word anyway (exceeding the "suggestedWidth" for lines).
00094   // The output will look nice if the input has words separated
00095   // by a single blank space with no extra space in the input text,
00096   // otherwise the extra spaces and newlines get printed
00097   // making the output not nicely formatted ...
00098   void DocFormatHelper::wrapAndPrintText(std::ostream& os,
00099                                          std::string const& text,
00100                                          size_t indent,
00101                                          size_t suggestedWidth) {
00102 
00103     size_t pos = text.find_first_of('\n');
00104     if (pos == std::string::npos) {
00105       // no embedded newlines
00106       wrapAndPrintLine(os, text, indent, suggestedWidth);
00107     } else {
00108       // print the first line.
00109       wrapAndPrintLine(os, text.substr(0, pos), indent, suggestedWidth);
00110       // print all lines after the first.
00111       wrapAndPrintText(os, text.substr(pos + 1), indent, suggestedWidth);
00112     }
00113   }
00114 
00115   void DocFormatHelper::init() {
00116     section_ = std::string();
00117     pass_ = 0;
00118     column1_ = 0;
00119     column2_ = 0;
00120     column3_ = 0;
00121     counter_ = 0;
00122     parent_ = OTHER;
00123   }
00124 
00125   size_t DocFormatHelper::commentWidth() const {
00126 
00127     // Make the length of a comment at least 30 characters
00128     // per line, longer if there is more space available
00129     size_t width = 30U;
00130     if (lineWidth() > startColumn2() + 30U) {
00131       width = lineWidth() - startColumn2();
00132     }
00133     return width;
00134   }
00135 
00136   void DocFormatHelper::indent(std::ostream& os) const {
00137     char oldFill = os.fill();
00138     os << std::setfill(' ') << std::setw(indentation_) << "";
00139     os.fill(oldFill);
00140   }
00141 
00142   void DocFormatHelper::indent2(std::ostream& os) const {
00143     char oldFill = os.fill();
00144     os << std::setfill(' ') << std::setw(startColumn2_) << "";
00145     os.fill(oldFill);
00146   }
00147 }