BDE 4.14.0 Production release
Loading...
Searching...
No Matches
balxml_base64parser.h
Go to the documentation of this file.
1/// @file balxml_base64parser.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balxml_base64parser.h -*-C++-*-
8#ifndef INCLUDED_BALXML_BASE64PARSER
9#define INCLUDED_BALXML_BASE64PARSER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balxml_base64parser balxml_base64parser
15/// @brief Provide push parser for Base64 types.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balxml
19/// @{
20/// @addtogroup balxml_base64parser
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balxml_base64parser-purpose"> Purpose</a>
25/// * <a href="#balxml_base64parser-classes"> Classes </a>
26/// * <a href="#balxml_base64parser-description"> Description </a>
27/// * <a href="#balxml_base64parser-usage"> Usage </a>
28///
29/// # Purpose {#balxml_base64parser-purpose}
30/// Provide push parser for Base64 types.
31///
32/// @deprecated Use bdlde_base64decoder instead.
33///
34/// # Classes {#balxml_base64parser-classes}
35///
36/// - balxml::Base64Parser: push parser for Base64 types
37///
38/// # Description {#balxml_base64parser-description}
39/// The `balxml::Base64Parser` class template provided by this
40/// component can be used to parse Base64 characters into one of the supported
41/// Base64 types, which are `bsl::vector<char>` and `bsl::string`. The `TYPE`
42/// parameter can be one of these two types.
43///
44/// This class template is a model of the `PushParser` concept, which contains
45/// the following methods:
46/// @code
47/// int beginParse(TYPE *object);
48/// // Prepare the parser to start parsing a new value and associate the
49/// // specified 'object' with the parser. Return 0 if successful and
50/// // non-zero otherwise.
51///
52/// int endParse();
53/// // Ends the parse operation and store the value parsed from the pushed
54/// // characters into the associated object. Return 0 if successful and
55/// // non-zero otherwise. The behavior is undefined unless an object is
56/// // associated with this parser. Upon successful completion, the parser
57/// // will be disassociated with the object.
58///
59/// template <typename INPUT_ITERATOR>
60/// int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
61/// // Push the characters ranging from the specified 'begin' up to (but
62/// // not including) the specified 'end' into this parser. Return 0 if
63/// // successful and non-zero otherwise. The parameterized
64/// // 'INPUT_ITERATOR' must be dereferenceable to a 'char' value. The
65/// // behavior is undefined unless an object is associated with this
66/// // parser.
67/// @endcode
68///
69/// ## Usage {#balxml_base64parser-usage}
70///
71///
72/// The following snippets of code illustrate the usage of this component.
73/// Suppose you had an input stream that contained Base64 data. The following
74/// `loadFromBase64Stream` function loads this data into an `bsl::vector<char>`
75/// blob:
76/// @code
77/// #include <balxml_base64parser.h>
78///
79/// #include <istream>
80/// #include <iterator>
81/// #include <vector>
82///
83/// using namespace BloombergLP;
84///
85/// int loadFromBase64Stream(bsl::vector<char> *result, bsl::istream& stream)
86/// {
87/// enum { FAILURE = -1 };
88///
89/// balxml::Base64Parser<bsl::vector<char> > parser;
90///
91/// if (0 != parser.beginParse(result)) {
92/// return FAILURE;
93/// }
94///
95/// if (0 != parser.pushCharacters(bsl::istreambuf_iterator<char>(stream),
96/// bsl::istreambuf_iterator<char>())) {
97/// return FAILURE;
98/// }
99///
100/// return parser.endParse();
101/// }
102/// @endcode
103/// The following function demonstrates the `loadFromBase64Stream` function:
104/// @code
105/// #include <sstream>
106///
107/// void usageExample()
108/// {
109/// const char INPUT[] = "YWJjZA=="; // "abcd" in Base64
110///
111/// bsl::vector<char> vec;
112/// bsl::istringstream iss(INPUT);
113///
114/// int result = loadFromBase64Stream(&vec, iss);
115///
116/// assert(0 == result);
117/// assert(4 == vec.size());
118/// assert('a' == vec[0]);
119/// assert('b' == vec[1]);
120/// assert('c' == vec[2]);
121/// assert('d' == vec[3]);
122/// }
123/// @endcode
124/// @}
125/** @} */
126/** @} */
127
128/** @addtogroup bal
129 * @{
130 */
131/** @addtogroup balxml
132 * @{
133 */
134/** @addtogroup balxml_base64parser
135 * @{
136 */
137
138#include <balscm_version.h>
139
140#include <bdlde_base64decoder.h>
141
143
144#include <bsl_iterator.h>
145
146#include <bsls_assert.h>
147#include <bsls_review.h>
148
149
150namespace balxml {
151
152 // ========================
153 // class Base64Parser<TYPE>
154 // ========================
155
156/// This is a push parser for supported Base64 types (`bsl::vector<char>` or
157/// `bsl::string`).
158///
159/// See @ref balxml_base64parser
160template <class TYPE>
162
163 // PRIVATE DATA MEMBERS
164 bdlde::Base64Decoder d_base64Decoder; // decoder
165 TYPE *d_object_p; // associated object
166
167 private:
168 // NOT IMPLEMENTED
170 Base64Parser& operator=(const Base64Parser&);
171
172 public:
173 // CREATORS
174
175 /// Create a parser for parsing Base64 types.
176 Base64Parser();
177
178#ifdef DOXYGEN // Generated by compiler:
179
181 // Destroy this parser object.
182#endif
183
184 // MANIPULATORS
185
186 /// Prepare the parser to start parsing a new value and associate the
187 /// specified `object` with the parser. Return 0 if successful and
188 /// non-zero otherwise.
189 int beginParse(TYPE *object);
190
191 /// Ends the parse operation and store the value parsed from the pushed
192 /// characters into the associated object. Return 0 if successful and
193 /// non-zero otherwise. The behavior is undefined unless an object is
194 /// associated with this parser. Upon successful completion, the parser
195 /// will be disassociated with the object.
196 int endParse();
197
198 /// Push the characters ranging from the specified `begin` up to (but
199 /// not including) the specified `end` into this parser. Return 0 if
200 /// successful and non-zero otherwise. The parameterized
201 /// `INPUT_ITERATOR` must be dereferenceable to a `char` value. The
202 /// behavior is undefined unless an object is associated with this
203 /// parser.
204 template <class INPUT_ITERATOR>
205 int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
206};
207
208// ============================================================================
209// INLINE DEFINITIONS
210// ============================================================================
211
212 // ------------------------
213 // class Base64Parser<TYPE>
214 // ------------------------
215
216// CREATORS
217template <class TYPE>
219: d_base64Decoder(true) // 'true' indicates report errors
220, d_object_p(0)
221{
222}
223
224// MANIPULATORS
225template <class TYPE>
227{
228 BSLS_ASSERT(object);
229
230 enum { k_SUCCESS = 0 };
231
232 d_base64Decoder.resetState();
233 d_object_p = object;
234
236
237 return k_SUCCESS;
238}
239
240template <class TYPE>
242{
243 BSLS_ASSERT(d_object_p);
244
245 enum { k_SUCCESS = 0, k_FAILURE = -1 };
246
247 bsl::back_insert_iterator<TYPE> outputIterator(*d_object_p);
248
249 int status = d_base64Decoder.endConvert(outputIterator);
250
251 if (0 > status) {
252 return k_FAILURE; // RETURN
253 }
254
255 BSLS_ASSERT(0 == status); // nothing should be retained by decoder
256
257 d_object_p = 0;
258
259 return k_SUCCESS;
260}
261
262template <class TYPE>
263template <class INPUT_ITERATOR>
264int Base64Parser<TYPE>::pushCharacters(INPUT_ITERATOR begin,
265 INPUT_ITERATOR end)
266{
267 BSLS_ASSERT(d_object_p);
268
269 enum { k_SUCCESS = 0, k_FAILURE = -1 };
270
271 bsl::back_insert_iterator<TYPE> outputIterator(*d_object_p);
272
273 int status = d_base64Decoder.convert(outputIterator, begin, end);
274
275 if (0 > status) {
276 return k_FAILURE; // RETURN
277 }
278
279 BSLS_ASSERT(0 == status); // nothing should be retained by decoder
280
281 return k_SUCCESS;
282}
283
284} // close package namespace
285
286
287#endif
288
289// ----------------------------------------------------------------------------
290// Copyright 2015 Bloomberg Finance L.P.
291//
292// Licensed under the Apache License, Version 2.0 (the "License");
293// you may not use this file except in compliance with the License.
294// You may obtain a copy of the License at
295//
296// http://www.apache.org/licenses/LICENSE-2.0
297//
298// Unless required by applicable law or agreed to in writing, software
299// distributed under the License is distributed on an "AS IS" BASIS,
300// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
301// See the License for the specific language governing permissions and
302// limitations under the License.
303// ----------------------------- END-OF-FILE ----------------------------------
304
305/** @} */
306/** @} */
307/** @} */
Definition balxml_base64parser.h:161
int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition balxml_base64parser.h:264
int beginParse(TYPE *object)
Definition balxml_base64parser.h:226
int endParse()
Definition balxml_base64parser.h:241
Base64Parser()
Create a parser for parsing Base64 types.
Definition balxml_base64parser.h:218
Definition bdlde_base64decoder.h:512
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1804
#define BSLS_IDENT(str)
Definition bsls_ident.h:195
Definition balxml_base64parser.h:150
void reset(TYPE *object)
Reset the value of the specified object to its default value.