Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

Tools.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // Copyright (C) 2000
00004 // Ralf Westram
00005 // (Coded@ReallySoft.de)
00006 //
00007 // Permission to use, copy, modify, distribute and sell this software
00008 // and its documentation for any purpose is hereby granted without fee,
00009 // provided that the above copyright notice appear in all copies and
00010 // that both that copyright notice and this permission notice appear
00011 // in supporting documentation.  Ralf Westram makes no
00012 // representations about the suitability of this software for any
00013 // purpose.  It is provided "as is" without express or implied warranty.
00014 //
00015 // This code is part of my library.
00016 // You may find a more recent version at http://www.reallysoft.de/
00017 //
00019 
00020 #include "Tools.h"
00021 #include "Err.h"
00022 #include <cstdio>
00023 #include <new>
00024 
00025 #ifdef __STRICT_ANSI__
00026 #error Do not define __STRICT_ANSI__ - I need vsnprintf
00027 #endif
00028 
00029 using namespace std;
00030 
00031 namespace rs {
00032 
00033     namespace str {
00034 
00035         // --------------------------------------------------------------------------------
00036         //     string vstrf(const char *format, va_list argPtr)
00037         // --------------------------------------------------------------------------------
00038         string vstrf(const char *format, va_list argPtr) {
00039             static size_t  buf_size = 256;
00040             static char   *buffer   = new char[buf_size];
00041 
00042             size_t length;
00043             while (1) {
00044                 if (!buffer) {
00045                     assert(buffer); // to stop when debugging
00046                     throw std::bad_alloc();
00047                 }
00048 
00049                 length = vsnprintf(buffer, buf_size, format, argPtr);
00050                 if (length < buf_size) break; // string fits into current buffer
00051 
00052                 // otherwise resize buffer :
00053                 buf_size += buf_size/2;
00054 //                 cerr << "Reallocate vstrf-buffer to size=" << buf_size << endl;
00055                 delete [] buffer;
00056                 buffer    = new char[buf_size];
00057             }
00058 
00059             return string(buffer, length);
00060         }
00061 
00062         //  ---------------------------------------------
00063         //      string strf(const char *format, ...)
00064         //  ---------------------------------------------
00065         string strf(const char *format, ...) {
00066             va_list argPtr;
00067             va_start(argPtr, format);
00068             string result = vstrf(format, argPtr);
00069             va_end(argPtr);
00070 
00071             return result;
00072         }
00073 
00074     };
00075 
00076     namespace output {
00077 
00078         // --------------------------------------------------------------------------------
00079         //         void outf(ostream& out, const char *format, ...)
00080         // --------------------------------------------------------------------------------
00081         void outf(ostream& out, const char *format, ...) {
00082             va_list argPtr;
00083             va_start(argPtr, format);
00084             out << str::vstrf(format, argPtr);
00085             va_end(argPtr);
00086         }
00087 
00088 //         // ----------------------------------------------------------------------------------------
00089 //         //      void fixed_width_output(std::ostream& out, const std::string& s, size_t width)
00090 //         // ----------------------------------------------------------------------------------------
00091 //         void fixed_width_output(std::ostream& out, const std::string& s, size_t width) {
00092 //             int rest = width-s.length();
00093 
00094 //             if (rest>0) {
00095 //                 out << string(rest, ' ');
00096 //             }
00097 //             out << s;
00098 //         }
00099 
00100         // -----------------------------------------------------------------------------------------------------------------------
00101         //      void printIndented(std::ostream& out, unsigned indent, unsigned border, unsigned width, const std::string& s)
00102         // -----------------------------------------------------------------------------------------------------------------------
00103         void printIndented(std::ostream& out, unsigned indent, unsigned border, unsigned width, const std::string& s) {
00104             unsigned line_feed = s.find('\n');
00105             if (line_feed!=string::npos) {
00106                 printIndented(out, indent, border, width, s.substr(0, line_feed));
00107                 out << string(border+indent, ' ');
00108                 printIndented(out, indent, border, width, s.substr(line_feed+1, string::npos));
00109                 return;
00110             }
00111 
00112             unsigned rest_len = width-indent;
00113             unsigned len = s.length();
00114             unsigned start = 0;
00115             bool first = true;
00116 
00117             while (len>rest_len) {
00118                 if (!first) {
00119                     out << string(border+indent, ' ');
00120                 }
00121 
00122                 unsigned end_pos = start+rest_len;
00123                 unsigned last_space = s.rfind(' ', end_pos);
00124                 unsigned last_pos;
00125 
00126                 if (last_space==string::npos || last_space<=start) { // no space found
00127                     last_pos = end_pos;
00128                     end_pos++;
00129                 }
00130                 else {
00131                     last_pos = last_space-1;
00132                     end_pos = last_space+1;
00133                 }
00134 
00135                 out << s.substr(start, last_pos-start+1) << "\n";
00136                 len -= (end_pos-start);
00137                 start = end_pos;
00138                 first = false;
00139             }
00140 
00141             if (!first) {
00142                 out << string(border+indent, ' ');
00143             }
00144             out << s.substr(start, string::npos) << "\n";
00145         }
00146 
00147         // ------------------------------------------------------------------------------------------------------------------
00148         //      void printIndented(std::ostream& out, unsigned width, const std::string& left, const std::string& right)
00149         // ------------------------------------------------------------------------------------------------------------------
00150         void printIndented(std::ostream& out, unsigned width, const std::string& left, const std::string& right) {
00151             out << left << ' ';
00152             unsigned left_len = left.length()+1;
00153             printIndented(out, left_len, 0, width-left_len, right);
00154         }
00155 
00156     };
00157 };

Contact me in case of errors or questions.
This documentation is powered by Doxygen.
(C) 2000-2002 Doxygen