view C++/XmlParser.h @ 212:35e34b0b80d4

Utf8: remove namespace
author David Demelier <markand@malikania.fr>
date Sat, 22 Mar 2014 22:05:58 +0100
parents 617459270743
children
line wrap: on
line source

/*
 * XmlParser.h -- C++ wrapper around libexpat
 *
 * Copyright (c) 2013 David Demelier <markand@malikania.fr>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#ifndef _XML_PARSER_H_
#define _XML_PARSER_H_

#include <memory>
#include <string>
#include <unordered_map>

#include <expat.h>

/**
 * @class XmlParser
 * @brief Wrapper around libexpat
 */
class XmlParser {
public:
	using Attrs	= std::unordered_map<std::string, std::string>;

private:
	struct Deleter {
		void operator()(XML_Parser *p)
		{
			XML_ParserFree(*p);

			delete p;
		}
	};

	using Ptr	= std::unique_ptr<XML_Parser, Deleter>;

	Ptr		m_handle;
	std::string	m_path;

public:
	/**
	 * Constructor and open a file.
	 *
	 * @param path the path
	 */
	XmlParser(const std::string &path);

	/**
	 * Read the file.
	 */
	void open();

	/**
	 * Handler when a tag occurs.
	 *
	 * @param name the tag name
	 * @param attributes the optional attributes
	 */
	virtual void startElementHandler(const std::string &name,
					 const Attrs &attributes) { }

	/**
	 * Handler when a tag ends.
	 *
	 * @param name the tag name
	 */
	virtual void endElementHandler(const std::string &name) { }

	/**
	 * Handler when data occurs. The data may have leading and
	 * trailing spaces.
	 *
	 * @param data the data
	 */
	virtual void characterDataHandler(const std::string &data) { }
};


#endif // !_XML_PARSER_H_