PrintSensibly.h

Go to the documentation of this file.
00001 
00002 /****************************************************************************** 
00003  * 
00004  *  @file  PrintSensibly.h
00005  * 
00006  *  Copyright (c) 2004, Michael E. Smoot .
00007  *  Copyright (c) 2004, Michael E. Smoot, Daniel Aarno.
00008  *  All rights reverved.
00009  * 
00010  *  See the file COPYING in the top directory of this distribution for
00011  *  more information.
00012  *  
00013  *  THE SOFTWARE IS PROVIDED _AS IS_, WITHOUT WARRANTY OF ANY KIND, EXPRESS 
00014  *  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
00015  *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
00016  *  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
00017  *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
00018  *  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
00019  *  DEALINGS IN THE SOFTWARE.  
00020  *  
00021  *****************************************************************************/ 
00022 
00023 #ifndef TCLAP_PRINTSENSIBLY_H
00024 #define TCLAP_PRINTSENSIBLY_H
00025 
00026 #include <iostream>
00027 #include <algorithm>
00028 #include <string>
00029 
00030 namespace TCLAP {
00031 
00042 inline void spacePrint( std::ostream& os, 
00043                               const std::string& s, 
00044                               int maxWidth, 
00045                               int indentSpaces=0, 
00046                               int secondLineOffset=0 )
00047 {
00048      int len = (int)(s.length());
00049 
00050      if ( (len + indentSpaces > maxWidth) && maxWidth > 0 )
00051      {
00052           int allowedLen = maxWidth - indentSpaces;
00053           int start = 0;
00054           while ( start < len )
00055           {
00056                // find the substring length
00057                int stringLen = std::min( len - start, allowedLen );
00058 
00059                // trim the length so it doesn't end in middle of a word
00060                if ( stringLen == allowedLen )
00061                     while ( s[stringLen+start] != ' ' && 
00062                             s[stringLen+start] != ',' &&
00063                             s[stringLen+start] != '|' )
00064                          stringLen--;
00065                
00066                // check for newlines
00067                for ( int i = 0; i < stringLen; i++ )
00068                     if ( s[start+i] == '\n' )
00069                          stringLen = i+1;
00070 
00071                // print the indent 
00072                for ( int i = 0; i < indentSpaces; i++ )
00073                     os << " ";
00074 
00075                if ( start == 0 )
00076                {
00077                     // handle second line offsets
00078                     indentSpaces += secondLineOffset;
00079 
00080                     // adjust allowed len
00081                     allowedLen -= secondLineOffset;
00082                }
00083                
00084                os << s.substr(start,stringLen) << std::endl;
00085 
00086                // so we don't start a line with a space
00087                if ( s[stringLen+start] == ' ' )
00088                     start++;
00089                
00090                start += stringLen;
00091           }
00092      }
00093      else
00094      {
00095           for ( int i = 0; i < indentSpaces; i++ )
00096                     os << " ";
00097           os << s << std::endl;
00098      }
00099 }
00100 
00101 } //namespace TCLAP
00102 
00103 #endif

Generated on Sun Mar 11 15:33:24 2007 for MetalWarriors by  doxygen 1.4.7