blocxx
Format.cpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (C) 2005, Vintela, Inc. All rights reserved.
3* Copyright (C) 2006, Novell, Inc. All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without
6* modification, are permitted provided that the following conditions are met:
7*
8* * Redistributions of source code must retain the above copyright notice,
9* this list of conditions and the following disclaimer.
10* * Redistributions in binary form must reproduce the above copyright
11* notice, this list of conditions and the following disclaimer in the
12* documentation and/or other materials provided with the distribution.
13* * Neither the name of
14* Vintela, Inc.,
15* nor Novell, Inc.,
16* nor the names of its contributors or employees may be used to
17* endorse or promote products derived from this software without
18* specific prior written permission.
19*
20* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30* POSSIBILITY OF SUCH DAMAGE.
31*******************************************************************************/
32
33
37
38#include "blocxx/BLOCXX_config.h"
39#include "blocxx/Format.hpp"
40
41namespace BLOCXX_NAMESPACE
42{
43
45Format::operator String() const
46{
47 return oss.toString();
48}
49
51{
52 return oss.toString();
53}
54
55const char* Format::c_str() const
56{
57 return oss.c_str();
58}
59
60char Format::process(String& str, char numArgs)
61{
62 int len(str.length());
63 char c(' ');
64 bool err = false;
65 int i = 0;
66 while (i < len && c == ' ' && !err)
67 {
68 switch (str[i])
69 {
70 case '%':
71 if (i + 1 < len)
72 {
73 ++i;
74 switch (str[i])
75 {
76 case '1': case '2': case '3': case '4': case '5':
77 case '6': case '7': case '8': case '9':
78 c = str[i];
79 break;
80 case '%':
81 oss << str[i];
82 break;
83 default:
84 err = true;
85 } // inner switch
86 } else err = true;
87 break;
88 default:
89 oss << str[i];
90 break;
91 } // outer switch
92 ++i;
93 } // for
94 if ( i <= len && c > numArgs )
95 {
96 oss << "\n*** Parameter specifier too large.";
97 err = true;
98 }
99 if (err)
100 {
101 oss << "\n*** Error in format string at \"" << str.substring(i-1) << "\".\n";
102 str.erase();
103 return '0';
104 }
105 str.erase(0, i);
106 return c;
107} // process
108
109std::ostream&
110operator<<(std::ostream& os, const Format& f)
111{
112 os.write(f.oss.c_str(), f.oss.length());
113 return os;
114}
115
116void Format::put(const String& t)
117{ // t is inserted into oss
118 if (!oss.good())
119 {
120 return;
121 }
122 oss << t;
123}
124
125#define BLOCXX_DEFINE_PUT(type) \
126void Format::put(type t) \
127{ \
128\
129 if (!oss.good()) \
130 { \
131 return; \
132 } \
133\
134 oss << t; \
135}
137BLOCXX_DEFINE_PUT(unsigned char);
139BLOCXX_DEFINE_PUT(unsigned short);
141BLOCXX_DEFINE_PUT(unsigned int);
143BLOCXX_DEFINE_PUT(unsigned long);
145BLOCXX_DEFINE_PUT(unsigned long long);
146#undef BLOCXX_DEFINE_PUT
147
148Format::Format(const char* ca, const String& a) : oss()
149{
150 String fmt(ca);
151 while (!fmt.empty())
152 {
153 switch (process(fmt, '1'))
154 {
155 case '1': put(a); break;
156 }
157 }
158}
159Format::Format(const char* ca, const String& a, const String& b) : oss()
160{
161 String fmt(ca);
162 while (!fmt.empty())
163 {
164 switch (process(fmt, '2'))
165 {
166 case '1': put(a); break;
167 case '2': put(b); break;
168 }
169 }
170}
171Format::Format(const char* ca, const String& a, const String& b, const String& c) : oss()
172{
173 String fmt(ca);
174 while (!fmt.empty())
175 {
176 switch (process(fmt, '3'))
177 {
178 case '1': put(a); break;
179 case '2': put(b); break;
180 case '3': put(c); break;
181 }
182 }
183}
184
185} // end namespace BLOCXX_NAMESPACE
186
#define BLOCXX_DEFINE_PUT(type)
Definition Format.cpp:125
OStringStream oss
Definition Format.hpp:80
char process(String &f, char c0)
Definition Format.cpp:60
const char * c_str() const
Definition Format.cpp:55
void put(const T &t)
Definition Format.hpp:100
Format(const char *ca, const A &a)
Definition Format.hpp:108
String toString() const
Definition Format.cpp:50
This String class is an abstract data type that represents as NULL terminated string of characters.
Definition String.hpp:67
String & erase()
Delete all the characters of the string.
Definition String.cpp:824
size_t length() const
Definition String.cpp:354
String substring(size_t beginIndex, size_t length=npos) const
Create another String object that is comprised of a substring of this String object.
Definition String.cpp:698
Taken from RFC 1321.
ostream & operator<<(ostream &ostrm, const Bool &arg)
Definition Bool.cpp:77