OW_Format.cpp

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

Generated on Thu Feb 9 08:47:59 2006 for openwbem by  doxygen 1.4.6