BDE 4.39.x 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 <span style="color: var(--deprecated-color-dark)">DEPRECATED:</span> 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 non-zero otherwise.
193 ///
194 /// \pre The behavior is undefined unless an object is
195 /// associated with this parser. Upon successful completion, the parser
196 /// will be disassociated with the object.
197 int endParse();
198
199 /// Push the characters ranging from the specified `begin` up to (but
200 /// not including) the specified `end` into this parser. Return 0 if
201 /// successful and non-zero otherwise. The parameterized
202 /// `INPUT_ITERATOR` must be dereferenceable to a `char` value.
203 ///
204 /// \pre The behavior is undefined unless an object is associated with this
205 /// parser.
206 template <class INPUT_ITERATOR>
207 int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
208};
209
210// ============================================================================
211// INLINE DEFINITIONS
212// ============================================================================
213
214 // ------------------------
215 // class Base64Parser<TYPE>
216 // ------------------------
217
218// CREATORS
219template <class TYPE>
221: d_base64Decoder(true) // 'true' indicates report errors
222, d_object_p(0)
223{
224}
225
226// MANIPULATORS
227template <class TYPE>
229{
230 BSLS_ASSERT(object);
231
232 enum { k_SUCCESS = 0 };
233
234 d_base64Decoder.resetState();
235 d_object_p = object;
236
238
239 return k_SUCCESS;
240}
241
242template <class TYPE>
244{
245 BSLS_ASSERT(d_object_p);
246
247 enum { k_SUCCESS = 0, k_FAILURE = -1 };
248
249 bsl::back_insert_iterator<TYPE> outputIterator(*d_object_p);
250
251 int status = d_base64Decoder.endConvert(outputIterator);
252
253 if (0 > status) {
254 return k_FAILURE; // RETURN
255 }
256
257 BSLS_ASSERT(0 == status); // nothing should be retained by decoder
258
259 d_object_p = 0;
260
261 return k_SUCCESS;
262}
263
264template <class TYPE>
265template <class INPUT_ITERATOR>
266int Base64Parser<TYPE>::pushCharacters(INPUT_ITERATOR begin,
267 INPUT_ITERATOR end)
268{
269 BSLS_ASSERT(d_object_p);
270
271 enum { k_SUCCESS = 0, k_FAILURE = -1 };
272
273 bsl::back_insert_iterator<TYPE> outputIterator(*d_object_p);
274
275 int status = d_base64Decoder.convert(outputIterator, begin, end);
276
277 if (0 > status) {
278 return k_FAILURE; // RETURN
279 }
280
281 BSLS_ASSERT(0 == status); // nothing should be retained by decoder
282
283 return k_SUCCESS;
284}
285
286} // close package namespace
287
288
289#endif
290
291// ----------------------------------------------------------------------------
292// Copyright 2015 Bloomberg Finance L.P.
293//
294// Licensed under the Apache License, Version 2.0 (the "License");
295// you may not use this file except in compliance with the License.
296// You may obtain a copy of the License at
297//
298// http://www.apache.org/licenses/LICENSE-2.0
299//
300// Unless required by applicable law or agreed to in writing, software
301// distributed under the License is distributed on an "AS IS" BASIS,
302// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
303// See the License for the specific language governing permissions and
304// limitations under the License.
305// ----------------------------- END-OF-FILE ----------------------------------
306
307/** @} */
308/** @} */
309/** @} */
Definition balxml_base64parser.h:161
int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition balxml_base64parser.h:266
int beginParse(TYPE *object)
Definition balxml_base64parser.h:228
int endParse()
Definition balxml_base64parser.h:243
Base64Parser()
Create a parser for parsing Base64 types.
Definition balxml_base64parser.h:220
Definition bdlde_base64decoder.h:417
#define BSLS_ASSERT(X)
Definition bsls_assert.h:1976
#define BSLS_IDENT(str)
BSLS_IDENT() - insert string into .comment binary segment (if supported)
Definition bsls_ident.h:238
Definition balxml_base64parser.h:150
void reset(TYPE *object)
Reset the value of the specified object to its default value.