blocxx
Main Page
Namespaces
Classes
Files
File List
File Members
src
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
38
#include "blocxx/BLOCXX_config.h"
39
#include "
blocxx/Format.hpp
"
40
41
namespace
BLOCXX_NAMESPACE
42
{
43
45
Format::operator
String
()
const
46
{
47
return
oss.
toString
();
48
}
50
String
Format::toString
()
const
51
{
52
return
oss
.
toString
();
53
}
55
const
char
*
Format::c_str
()
const
56
{
57
return
oss
.
c_str
();
58
}
60
char
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
109
std::ostream&
110
operator<<
(std::ostream& os,
const
Format
& f)
111
{
112
os.write(f.
oss
.
c_str
(), f.
oss
.
length
());
113
return
os;
114
}
116
void
Format::put
(
const
String
& t)
117
{
// t is inserted into oss
118
if
(!
oss
.good())
119
{
120
return
;
121
}
122
oss
<< t;
123
}
125
#define BLOCXX_DEFINE_PUT(type) \
126
void Format::put(type t) \
127
{ \
128
\
129
if (!oss.good()) \
130
{ \
131
return; \
132
} \
133
\
134
oss << t; \
135
}
136
BLOCXX_DEFINE_PUT
(
char
);
137
BLOCXX_DEFINE_PUT
(
unsigned
char
);
138
BLOCXX_DEFINE_PUT
(
short
);
139
BLOCXX_DEFINE_PUT
(
unsigned
short
);
140
BLOCXX_DEFINE_PUT
(
int
);
141
BLOCXX_DEFINE_PUT
(
unsigned
int
);
142
BLOCXX_DEFINE_PUT
(
long
);
143
BLOCXX_DEFINE_PUT
(
unsigned
long
);
144
BLOCXX_DEFINE_PUT
(
long
long
);
145
BLOCXX_DEFINE_PUT
(
unsigned
long
long
);
146
#undef BLOCXX_DEFINE_PUT
147
148
Format::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
}
159
Format::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
}
171
Format::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
Generated by
1.8.2