Quick Links:

bal | bbl | bdl | bsl

Namespaces

Component balxml_listparser
[Package balxml]

Provide push parser for lists. More...

Namespaces

namespace  balxml

Detailed Description

Outline
Purpose:
Provide push parser for lists.
Classes:
balxml::ListParser push parser for lists
See also:
Component bdlat_arrayfunctions
Description:
The balxml::ListParser class template provided by this component can be used to parse lists into an object that supports bdlat_ArrayFunctions.
This class template is a model of the PushParser concept, which contains the following methods:
  int beginParse(TYPE *object);
      // Prepare the parser to start parsing a new value and associate the
      // specified 'object' with the parser.  Return 0 if successful and
      // non-zero otherwise.

  int endParse();
      // Ends the parse operation and store the value parsed from the pushed
      // characters into the associated object.  Return 0 if successful and
      // non-zero otherwise.  The behavior is undefined unless an object is
      // associated with this parser.  Upon successful completion, the parser
      // will be disassociated with the object.

  template <typename INPUT_ITERATOR>
  int pushCharacters(INPUT_ITERATOR begin, INPUT_ITERATOR end);
      // Push the characters ranging from the specified 'begin' up to (but
      // not including) the specified 'end' into this parser.  Return 0 if
      // successful and non-zero otherwise.  The parameterized
      // 'INPUT_ITERATOR' must be dereferenceable to a 'char' value.  The
      // behavior is undefined unless an object is associated with this
      // parser.
Usage:
The following snippets of code illustrate the usage of this component. Suppose you had an input stream that contained a list of doubles. The following loadDoublesFromListStream function loads this data into an bsl::vector<double>:
  #include <balxml_listparser.h>

  #include <bdlt_date.h>

  #include <istream>
  #include <iterator>
  #include <vector>
  #include <sstream>
  #include <string>

  using namespace BloombergLP;

  int parseDouble(double *result, const char *data, int dataLength);

  int loadDoublesFromListStream(bsl::vector<double> *result,
                                bsl::istream&        stream)
  {
      enum { k_FAILURE = -1 };

      balxml::ListParser<bsl::vector<double> > parser(&parseDouble);

      if (0 != parser.beginParse(result)) {
          return k_FAILURE;
      }

      if (0 != parser.pushCharacters(bsl::istreambuf_iterator<char>(stream),
                                     bsl::istreambuf_iterator<char>())) {
          return k_FAILURE;
      }

      return parser.endParse();
  }
The parseDouble function is implemented as follows:
  int parseDouble(double *result, const char *data, int dataLength)
  {
      bsl::stringstream ss(bsl::string(data, dataLength));
      ss >> (*result);
      return 0;
  }
The following function demonstrates the loadDoublesFromListStream function:
  void usageExample()
  {
      const char INPUT[] = "1.5 2.0 3.8 1.0";

      bsl::vector<double> vec;
      bsl::istringstream  iss(INPUT);

      int result = loadDoublesFromListStream(&vec, iss);

      assert(0   == result);
      assert(4   == vec.size());
      assert(1.5 == vec[0]);
      assert(2.0 == vec[1]);
      assert(3.8 == vec[2]);
      assert(1.0 == vec[3]);
  }