BDE 4.39.x Production Release
Loading...
Searching...
No Matches
balxml_hexparser.h
Go to the documentation of this file.
1/// @file balxml_hexparser.h
2///
3/// The content of this file has been pre-processed for Doxygen.
4///
5
6
7// balxml_hexparser.h -*-C++-*-
8#ifndef INCLUDED_BALXML_HEXPARSER
9#define INCLUDED_BALXML_HEXPARSER
10
11#include <bsls_ident.h>
12BSLS_IDENT("$Id: $")
13
14/// @defgroup balxml_hexparser balxml_hexparser
15/// @brief Provide push parser for hex types.
16/// @addtogroup bal
17/// @{
18/// @addtogroup balxml
19/// @{
20/// @addtogroup balxml_hexparser
21/// @{
22///
23/// <h1> Outline </h1>
24/// * <a href="#balxml_hexparser-purpose"> Purpose</a>
25/// * <a href="#balxml_hexparser-classes"> Classes </a>
26/// * <a href="#balxml_hexparser-description"> Description </a>
27/// * <a href="#balxml_hexparser-usage"> Usage </a>
28/// * <a href="#balxml_hexparser-example-1-basic-usage"> Example 1: Basic Usage </a>
29///
30/// # Purpose {#balxml_hexparser-purpose}
31/// Provide push parser for hex types.
32///
33/// # Classes {#balxml_hexparser-classes}
34///
35/// - balxml::HexParser: push parser for hex types
36///
37/// @see
38///
39/// # Description {#balxml_hexparser-description}
40/// The `balxml::HexParser` class template provided by this
41/// component can be used to parse Hex characters into one of the supported Hex
42/// types, which are `bsl::vector<char>` and `bsl::string`. The `TYPE`
43/// parameter can be one of these two types.
44///
45/// Note that if you need a way to encode binary data into ASCII, the
46/// @ref bdlde_base64encoder and @ref bdlde_base64decoder components are likely a more
47/// efficient solution.
48///
49/// This class template is a model of the `PushParser` concept, which contains
50/// the following methods:
51/// @code
52/// int beginParse(TYPE *object);
53/// // Prepare the parser to start parsing a new value and associate the
54/// // specified 'object' with the parser. Return 0 if successful and
55/// // non-zero otherwise.
56///
57/// int endParse();
58/// // Ends the parse operation and store the value parsed from the pushed
59/// // characters into the associated object. Return 0 if successful and
60/// // non-zero otherwise. The behavior is undefined unless an object is
61/// // associated with this parser. Upon successful completion, the parser
62/// // will be disassociated with the object.
63///
64/// template <typename INPUT_ITERATOR>
65/// int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
66/// // Push the characters ranging from the specified 'begin' up to (but
67/// // not including) the specified 'end' into this parser. Return 0 if
68/// // successful and non-zero otherwise. The parameterized
69/// // 'INPUT_ITERATOR' must be dereferenceable to a 'char' value. The
70/// // behavior is undefined unless an object is associated with this
71/// // parser.
72/// @endcode
73///
74/// ## Usage {#balxml_hexparser-usage}
75///
76///
77/// This section illustrates intended use of this component.
78///
79/// ### Example 1: Basic Usage {#balxml_hexparser-example-1-basic-usage}
80///
81///
82/// The following snippets of code illustrate the usage of this component.
83/// Suppose you had an input stream that contained Hex data. The following
84/// `loadFromHexStream` function loads this data into an `bsl::vector<char>`
85/// blob:
86/// @code
87/// #include <balxml_hexparser.h>
88///
89/// #include <bsl_istream.h>
90/// #include <bsl_iterator.h>
91/// #include <bsl_vector.h>
92///
93/// using namespace BloombergLP;
94///
95/// int loadFromHexStream(bsl::vector<char> *result, bsl::istream& stream)
96/// {
97/// enum { k_FAILURE = -1 };
98///
99/// balxml::HexParser<bsl::vector<char> > parser;
100///
101/// if (0 != parser.beginParse(result)) {
102/// return k_FAILURE;
103/// }
104///
105/// if (0 != parser.pushCharacters(bsl::istreambuf_iterator<char>(stream),
106/// bsl::istreambuf_iterator<char>())) {
107/// return k_FAILURE;
108/// }
109///
110/// return parser.endParse();
111/// }
112/// @endcode
113/// The following function demonstrates the `loadFromHexStream` function:
114/// @code
115/// #include <sstream>
116///
117/// void usageExample()
118/// {
119/// const char INPUT[] = "0F3B296A";
120///
121/// bsl::vector<char> vec;
122/// bsl::istringstream iss(INPUT);
123///
124/// int result = loadFromHexStream(&vec, iss);
125///
126/// assert(0 == result);
127/// assert(4 == vec.size());
128/// assert(0x0F == vec[0]);
129/// assert(0x3B == vec[1]);
130/// assert(0x29 == vec[2]);
131/// assert(0x6A == vec[3]);
132/// }
133/// @endcode
134/// @}
135/** @} */
136/** @} */
137
138/** @addtogroup bal
139 * @{
140 */
141/** @addtogroup balxml
142 * @{
143 */
144/** @addtogroup balxml_hexparser
145 * @{
146 */
147
148#include <balscm_version.h>
149
151
152#include <bdlb_chartype.h>
153
154#include <bsls_assert.h>
155#include <bsls_review.h>
156
157
158namespace balxml {
159
160 // =====================
161 // class HexParser<TYPE>
162 // =====================
163
164/// This is a push parser for supported Hex types (`bsl::vector<char>` or
165/// `bsl::string`).
166///
167/// See @ref balxml_hexparser
168template <class TYPE>
170
171 // PRIVATE DATA MEMBERS
172 char d_firstDigit; // buffer for first digit
173 TYPE *d_object_p; // associated object
174
175 private:
176 // NOT IMPLEMENTED
177 HexParser(const HexParser&);
178 HexParser& operator=(const HexParser&);
179
180 // PRIVATE MANIPULATORS
181
182 /// Append an octet generated from the specified `firstDigit` and the
183 /// specified `secondDigit` to the associated object.
184 void appendOctet(char firstDigit, char secondDigit);
185
186 public:
187 // CREATORS
188
189 /// Create a parser for parsing Hex types.
190 HexParser();
191
192 // Generated by compiler:
193 // ~HexParser();
194
195 // MANIPULATORS
196
197 /// Prepare the parser to start parsing a new value and associate the
198 /// specified `object` with the parser. Return 0 if successful and
199 /// non-zero otherwise.
200 int beginParse(TYPE *object);
201
202 /// Ends the parse operation and store the value parsed from the pushed
203 /// characters into the associated object. Return 0 if successful and non-zero otherwise.
204 ///
205 /// \pre The behavior is undefined unless an object is
206 /// associated with this parser. Upon successful completion, the parser
207 /// will be disassociated with the object.
208 int endParse();
209
210 /// Push the characters ranging from the specified `begin` up to (but
211 /// not including) the specified `end` into this parser. Return 0 if
212 /// successful and non-zero otherwise. The parameterized
213 /// `INPUT_ITERATOR` must be dereferenceable to a `char` value.
214 ///
215 /// \pre The behavior is undefined unless an object is associated with this
216 /// parser.
217 template <class INPUT_ITERATOR>
218 int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
219};
220
221 // =======================
222 // struct HexParser_Helper
223 // =======================
224
225/// Namespace for facilities that are used in the implementation of class
226/// `HexParser<TYPE>`. Only instances of `HexParser<TYPE>` can access the
227/// facilities in this class.
228///
229/// See @ref balxml_hexparser
231
232 template <class TYPE>
233 friend class HexParser;
234
235 // Table that maps from ASCII character value to hex value.
236 static const char s_hexValueTable[128];
237};
238
239// ============================================================================
240// INLINE DEFINITIONS
241// ============================================================================
242
243 // ---------------------
244 // class HexParser<TYPE>
245 // ---------------------
246
247// PRIVATE MANIPULATORS
248template <class TYPE>
249void HexParser<TYPE>::appendOctet(char firstDigit, char secondDigit)
250{
251 BSLS_ASSERT(bdlb::CharType::isXdigit((unsigned char) firstDigit));
252 BSLS_ASSERT(bdlb::CharType::isXdigit((unsigned char) secondDigit));
253
254 char value =
255 (char)((HexParser_Helper::s_hexValueTable[(int)firstDigit] << 4)
256 | (HexParser_Helper::s_hexValueTable[(int)secondDigit]));
257
258 d_object_p->push_back(value);
259}
260
261// CREATORS
262template <class TYPE>
264: d_firstDigit(0)
265, d_object_p(0)
266{
267}
268
269// MANIPULATORS
270template <class TYPE>
272{
273 BSLS_ASSERT(object);
274
275 enum { k_SUCCESS = 0 };
276
277 d_firstDigit = 0;
278 d_object_p = object;
279
281
282 return k_SUCCESS;
283}
284
285template <class TYPE>
287{
288 BSLS_ASSERT(d_object_p);
289
290 enum { k_SUCCESS = 0, k_FAILURE = -1 };
291
292 d_object_p = 0;
293
294 return 0 == d_firstDigit ? k_SUCCESS : k_FAILURE;
295}
296
297template <class TYPE>
298template <class INPUT_ITERATOR>
299int HexParser<TYPE>::pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end)
300{
301 BSLS_ASSERT(d_object_p);
302
303 enum { k_SUCCESS = 0, k_FAILURE = -1 };
304
305 while (begin != end) {
306 const char digit = *begin;
307
308 ++begin;
309
310 if (!bdlb::CharType::isSpace(digit)) {
311 if (!bdlb::CharType::isXdigit(digit)) {
312 return k_FAILURE; // RETURN
313 }
314
315 if (0 == d_firstDigit) {
316 d_firstDigit = digit;
317 }
318 else {
319 appendOctet(d_firstDigit, digit);
320
321 d_firstDigit = 0;
322 }
323 }
324 }
325
326 return k_SUCCESS;
327}
328
329} // close package namespace
330
331
332#endif
333
334// ----------------------------------------------------------------------------
335// Copyright 2015 Bloomberg Finance L.P.
336//
337// Licensed under the Apache License, Version 2.0 (the "License");
338// you may not use this file except in compliance with the License.
339// You may obtain a copy of the License at
340//
341// http://www.apache.org/licenses/LICENSE-2.0
342//
343// Unless required by applicable law or agreed to in writing, software
344// distributed under the License is distributed on an "AS IS" BASIS,
345// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
346// See the License for the specific language governing permissions and
347// limitations under the License.
348// ----------------------------- END-OF-FILE ----------------------------------
349
350/** @} */
351/** @} */
352/** @} */
Definition balxml_hexparser.h:230
Definition balxml_hexparser.h:169
int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition balxml_hexparser.h:299
int endParse()
Definition balxml_hexparser.h:286
HexParser()
Create a parser for parsing Hex types.
Definition balxml_hexparser.h:263
int beginParse(TYPE *object)
Definition balxml_hexparser.h:271
#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.
static bool isSpace(char character)
Definition bdlb_chartype.h:814
static bool isXdigit(char character)
Definition bdlb_chartype.h:802