Format.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 Novell, Inc. All rights reserved.
00004 *
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 *
00008 *  - Redistributions of source code must retain the above copyright notice,
00009 *    this list of conditions and the following disclaimer.
00010 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00037 #include "blocxx/BLOCXX_config.h"
00038 #include "blocxx/Format.hpp"
00039 
00040 namespace BLOCXX_NAMESPACE
00041 {
00042 
00044 Format::operator String() const
00045 {
00046    return oss.toString();
00047 }
00049 String Format::toString() const
00050 {
00051    return oss.toString();
00052 }
00054 const char* Format::c_str() const
00055 {
00056    return oss.c_str();
00057 }
00059 char Format::process(String& str, char numArgs)
00060 {
00061    int len(str.length());
00062    char c(' ');
00063    bool err = false;
00064    int i = 0;
00065    while (i < len && c == ' ' && !err)
00066    {
00067       switch (str[i])
00068       {
00069          case '%':
00070             if (i + 1 < len)
00071             {
00072                ++i;
00073                switch (str[i])
00074                {
00075                   case '1': case '2': case '3': case '4': case '5': 
00076                   case '6': case '7': case '8': case '9':
00077                      c = str[i]; 
00078                      break;
00079                   case '%': 
00080                      oss << str[i]; 
00081                      break;
00082                   default: 
00083                      err = true;
00084                } // inner switch
00085             } else err = true; 
00086             break;
00087          default: 
00088             oss << str[i];
00089             break;
00090       } // outer switch
00091       ++i;
00092    } // for
00093    if ( i <= len && c > numArgs )
00094    {
00095       oss << "\n*** Parameter specifier too large.";
00096       err = true;
00097    }
00098    if (err)
00099    {
00100       oss << "\n*** Error in format string at \"" << str.substring(i-1) << "\".\n";
00101       str.erase();
00102       return '0';
00103    }
00104    str.erase(0, i);
00105    return c;
00106 } // process
00108 std::ostream&
00109 operator<<(std::ostream& os, const Format& f)
00110 {
00111    os.write(f.oss.c_str(), f.oss.length());
00112    return os;
00113 }
00115 void Format::put(const String& t)
00116 { // t is inserted into oss
00117    if (!oss.good())
00118    {
00119       return;
00120    }
00121    oss << t;
00122 }
00124 #define BLOCXX_DEFINE_PUT(type) \
00125 void Format::put(type t) \
00126 { \
00127 \
00128    if (!oss.good()) \
00129    { \
00130       return; \
00131    } \
00132 \
00133    oss << t; \
00134 }
00135 BLOCXX_DEFINE_PUT(char);
00136 BLOCXX_DEFINE_PUT(unsigned char);
00137 BLOCXX_DEFINE_PUT(short);
00138 BLOCXX_DEFINE_PUT(unsigned short);
00139 BLOCXX_DEFINE_PUT(int);
00140 BLOCXX_DEFINE_PUT(unsigned int);
00141 BLOCXX_DEFINE_PUT(long);
00142 BLOCXX_DEFINE_PUT(unsigned long);
00143 BLOCXX_DEFINE_PUT(long long);
00144 BLOCXX_DEFINE_PUT(unsigned long long);
00145 #undef BLOCXX_DEFINE_PUT
00146 
00147 Format::Format(const char* ca, const String& a) : oss()
00148 {
00149    String fmt(ca);
00150    while (!fmt.empty())
00151    {
00152       switch (process(fmt, '1'))
00153       {
00154          case '1': put(a); break;
00155       }
00156    }
00157 }
00158 Format::Format(const char* ca, const String& a, const String& b) : oss()
00159 {
00160    String fmt(ca);
00161    while (!fmt.empty())
00162    {
00163       switch (process(fmt, '2'))
00164       {
00165          case '1': put(a); break;
00166          case '2': put(b); break;
00167       }
00168    }
00169 }
00170 Format::Format(const char* ca, const String& a, const String& b, const String& c) : oss()
00171 {
00172    String fmt(ca);
00173    while (!fmt.empty())
00174    {
00175       switch (process(fmt, '3'))
00176       {
00177          case '1': put(a); break;
00178          case '2': put(b); break;
00179          case '3': put(c); break;
00180       }
00181    }
00182 }
00183 
00184 } // end namespace BLOCXX_NAMESPACE
00185 

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6