-
Notifications
You must be signed in to change notification settings - Fork 0
/
brick-xml
133 lines (112 loc) · 4.13 KB
/
brick-xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*-
/*
* Copyright (C) 2019 Petr Rockai <code@fixp.eu>
*
* Permission to use, copy, modify, and 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.
*/
#pragma once
#include "brick-trace"
#include <string_view>
#include <vector>
#include <map>
#include <string>
#include <expat.h>
namespace brq
{
struct xml_parser
{
XML_Parser _parser;
virtual void start( std::string_view, std::vector< std::string_view > ) {}
virtual void end( std::string_view ) {}
virtual void text( std::string_view ) {}
xml_parser()
{
_parser = XML_ParserCreate( nullptr );
XML_SetElementHandler( _parser, _start, _end );
XML_SetCharacterDataHandler( _parser, _text );
XML_SetUserData( _parser, this );
}
void parse( std::string_view data )
{
XML_Parse( _parser, data.begin(), data.size(), false );
}
void done()
{
XML_Parse( _parser, nullptr, 0, true );
}
static void _start( void *instance, const char *name, const char **attrs )
{
xml_parser *i = static_cast< xml_parser * >( instance );
std::vector< std::string_view > attr_vec;
for ( ; *attrs ; ++attrs )
attr_vec.emplace_back( *attrs );
i->start( name, attr_vec );
}
static void _end( void *instance, const char *name )
{
xml_parser *i = static_cast< xml_parser * >( instance );
i->end( name );
}
static void _text( void *instance, const char *data, int len )
{
xml_parser *i = static_cast< xml_parser * >( instance );
return i->text( std::string_view( data, len ) );
}
};
struct xml_stack : xml_parser
{
using attrs = std::map< std::string, std::string >;
using tag = std::pair< std::string, attrs >;
std::vector< tag > _stack;
std::vector< std::string > _text;
xml_stack() { _text.emplace_back(); }
virtual std::string event( std::string_view, std::string_view text, attrs a )
{
event( text, a );
return "";
}
virtual void event( std::string_view text, attrs ) { event( text ); }
virtual void event( std::string_view ) {}
virtual void start( std::string_view n, std::vector< std::string_view > attr )
{
TRACE( "open", std::string( _stack.size() * 2, ' ' ), n, attr );
attrs a;
for ( auto i = attr.begin(); i != attr.end(); std::advance( i, 2 ) )
a.emplace( i[ 0 ], i[ 1 ] );
_stack.emplace_back( n, a );
_text.emplace_back();
}
virtual void end( std::string_view n )
{
auto replace = event( n, _text.back(), _stack.back().second );
_stack.pop_back();
_text.pop_back();
_text.back() += replace;
}
virtual void text( std::string_view txt )
{
_text.back() += txt;
}
template< typename... args >
bool in_context( args... ctx )
{
auto il = { std::string_view( ctx )... };
auto i = _stack.rbegin();
auto j = std::rbegin( il );
while ( i != _stack.rend() && j != std::rend( il ) )
if ( i++->first != *j++ )
return false;
return j == std::rend( il );
}
};
}