BDE 4.14.0 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
204 /// non-zero otherwise. The behavior is undefined unless an object is
205 /// associated with this parser. Upon successful completion, the parser
206 /// will be disassociated with the object.
207 int endParse();
208
209 /// Push the characters ranging from the specified `begin` up to (but
210 /// not including) the specified `end` into this parser. Return 0 if
211 /// successful and non-zero otherwise. The parameterized
212 /// `INPUT_ITERATOR` must be dereferenceable to a `char` value. The
213 /// behavior is undefined unless an object is associated with this
214 /// parser.
215 template <class INPUT_ITERATOR>
216 int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
217};
218
219 // =======================
220 // struct HexParser_Helper
221 // =======================
222
223/// Namespace for facilities that are used in the implementation of class
224/// `HexParser<TYPE>`. Only instances of `HexParser<TYPE>` can access the
225/// facilities in this class.
226///
227/// See @ref balxml_hexparser
229
230 template <class TYPE>
231 friend class HexParser;
232
233 // Table that maps from ASCII character value to hex value.
234 static const char s_hexValueTable[128];
235};
236
237// ============================================================================
238// INLINE DEFINITIONS
239// ============================================================================
240
241 // ---------------------
242 // class HexParser<TYPE>
243 // ---------------------
244
245// PRIVATE MANIPULATORS
246template <class TYPE>
247void HexParser<TYPE>::appendOctet(char firstDigit, char secondDigit)
248{
249 BSLS_ASSERT(bdlb::CharType::isXdigit((unsigned char) firstDigit));
250 BSLS_ASSERT(bdlb::CharType::isXdigit((unsigned char) secondDigit));
251
252 char value =
253 (char)((HexParser_Helper::s_hexValueTable[(int)firstDigit] << 4)
254 | (HexParser_Helper::s_hexValueTable[(int)secondDigit]));
255
256 d_object_p->push_back(value);
257}
258
259// CREATORS
260template <class TYPE>
262: d_firstDigit(0)
263, d_object_p(0)
264{
265}
266
267// MANIPULATORS
268template <class TYPE>
270{
271 BSLS_ASSERT(object);
272
273 enum { k_SUCCESS = 0 };
274
275 d_firstDigit = 0;
276 d_object_p = object;
277
279
280 return k_SUCCESS;
281}
282
283template <class TYPE>
285{
286 BSLS_ASSERT(d_object_p);
287
288 enum { k_SUCCESS = 0, k_FAILURE = -1 };
289
290 d_object_p = 0;
291
292 return 0 == d_firstDigit ? k_SUCCESS : k_FAILURE;
293}
294
295template <class TYPE>
296template <class INPUT_ITERATOR>
297int HexParser<TYPE>::pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end)
298{
299 BSLS_ASSERT(d_object_p);
300
301 enum { k_SUCCESS = 0, k_FAILURE = -1 };
302
303 while (begin != end) {
304 const char digit = *begin;
305
306 ++begin;
307
308 if (!bdlb::CharType::isSpace(digit)) {
309 if (!bdlb::CharType::isXdigit(digit)) {
310 return k_FAILURE; // RETURN
311 }
312
313 if (0 == d_firstDigit) {
314 d_firstDigit = digit;
315 }
316 else {
317 appendOctet(d_firstDigit, digit);
318
319 d_firstDigit = 0;
320 }
321 }
322 }
323
324 return k_SUCCESS;
325}
326
327} // close package namespace
328
329
330#endif
331
332// ----------------------------------------------------------------------------
333// Copyright 2015 Bloomberg Finance L.P.
334//
335// Licensed under the Apache License, Version 2.0 (the "License");
336// you may not use this file except in compliance with the License.
337// You may obtain a copy of the License at
338//
339// http://www.apache.org/licenses/LICENSE-2.0
340//
341// Unless required by applicable law or agreed to in writing, software
342// distributed under the License is distributed on an "AS IS" BASIS,
343// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
344// See the License for the specific language governing permissions and
345// limitations under the License.
346// ----------------------------- END-OF-FILE ----------------------------------
347
348/** @} */
349/** @} */
350/** @} */
Definition balxml_hexparser.h:228
Definition balxml_hexparser.h:169
int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end)
Definition balxml_hexparser.h:297
int endParse()
Definition balxml_hexparser.h:284
HexParser()
Create a parser for parsing Hex types.
Definition balxml_hexparser.h:261
int beginParse(TYPE *object)
Definition balxml_hexparser.h:269
#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.
static bool isSpace(char character)
Definition bdlb_chartype.h:842
static bool isXdigit(char character)
Definition bdlb_chartype.h:830