ALPS Project

Header file parser/parser.h

This header contains a few helper functions to help reading parameter and XML files.

Synopsis

nameespace alps {

struct XMLTag
{ 
  typedef std::map<std::string,std::string> AttributeMap; 
  AttributeMap attributes;
  std::string name;
  enum {OPENING, CLOSING, SINGLE, COMMENT, PROCESSING} type;
  bool is_comment();
  bool is_processing();
  bool is_element();
};

std::string parse_identifier(std::istream& in);
std::string parse_parameter_name(std::istream& in);
std::string read_until(std::istream& in, char end);
void check_character(std::istream& in, char c, const std::string& error);

XMLTag parse_tag(std::istream& is, bool skip_comment);
std::string parse_content(std::istream& is);
void skip_element(std::istream& is, const XMLTag&);
void check_tag(std::istream& in, const std::string& name);

}

The XMLTag struct

stores information about an XML tag.
typedef std::map AttributeMap; 
AttributeMap attributes;
is a map to store attributes, indexed by their name.
std::string name;
stores the name of the tag. The trailing / in a single tag (opening and closing in one), such as <TAG/> is not included in the name whereas the leading / in a closing tag such as </TAG> is included.
enum {OPENING, CLOSING, SINGLE} type;
is the type of tag:
typenameexample
OPENINGTAG<TAG>
CLOSING/TAG</TAG>
SINGLETAG<TAG/>
COMMENT!<!-- comment --!/>
PROCESSING!<? processing instruction ?/>
bool is_comment();
returns true if the tag is a comment.
bool is_processing();
returns true if the tag is a processing instruction.
bool is_element();
returns true if the tag is neither a comment nor a processing instruction but the opening or closing tag of an element.

Functions

std::string parse_identifier(std::istream& in)
reads an identifier. An identifier is defined to start with a letter, follwoed by any number of alphanumeric characters, or the characters colon (:), underscore(_) and single quote (').
std::string parse_parameter_name(std::istream& in)
reads a paramter name. A parameter name is an identifier, where additionally any arbitrary sequence of characters can be contained within square brackets [], such as in MEASURE[Staggered Magnetization^2].
std::string read_until(std::istream& in, char end);
returns until the next occurence of the character end or until thye end of the stream is reached. The function returns a string containing the characters read (excluding leading and trailing whitespace and excluding the terminating character end.
void check_character(std::istream& in, char c, const std::string& error);
checks that the next character read from the stream (skipping whitespace characters) is the character passed as c and throws a std::runtime_error(error) otherwise.
XMLTag parse_tag(std::istream& is, bool skip_comment);
reads a tag from a std::istream and returns an XMLTag struct containing information about the tag. If the skip_comment argument is true andy processing instructions and comments are skipped over.
std::string parse_content(std::istream& is);
reads and returns the contents of an element up to the next tag, processing instruction or comment.
void skip_element(std::istream& is,const XMLTag& tag);
skips the element specified by the opening tag just read using parse_tag and all the elements it contains.
void check_tag(std::istream& in, const std::string& name);
checks if the next tag is called name and throws a std::runtime_error otherwise.

copyright (c) 1994-2010 by Matthias Troyer

Distributed under the Boost Software License, Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt)