File: deb822.hpp

package info (click to toggle)
libparser%2B%2B 0.2.3-2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, squeeze, stretch, wheezy
  • size: 44 kB
  • ctags: 66
  • sloc: cpp: 237; makefile: 22
file content (201 lines) | stat: -rw-r--r-- 4,454 bytes parent folder | download
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
/* Copyright 2007, 2008, 2009 Simon Richter <Simon.Richter@hogyros.de>
 *
 * Released under the Boost Software Licence 1.0
 */

#ifndef util_deb822_hpp_
#define util_deb822_hpp_ 1

#include "parser.hpp"

#include <istream>
#include <string>

#include <iostream>

namespace util {
namespace deb822 {

template<
	typename CharT_ = char,
	typename Traits_ = std::char_traits<CharT_> >
class key_string :
	public std::basic_string<CharT_, Traits_>
{
public:
	typedef CharT_ char_type;
	typedef Traits_ traits_type;
	typedef std::basic_string<char_type, traits_type> string_type;

	key_string(void) : string_type() { }
	key_string(string_type const &k) : string_type(k) { }
};

template<
	typename CharT_ = char,
	typename Traits_ = std::char_traits<CharT_> >
class value_string :
	public std::basic_string<CharT_, Traits_>
{
public:
	typedef CharT_ char_type;
	typedef Traits_ traits_type;
	typedef std::basic_string<char_type, traits_type> string_type;

	value_string(void) : string_type() { }
	value_string(string_type const &k) : string_type(k) { }
};

template<
	typename CharT_ = char,
	typename Traits_ = std::char_traits<CharT_> >
class pair
{
public:
	typedef key_string<CharT_, Traits_> key_type;
	typedef value_string<CharT_, Traits_> value_type;

	key_type key;
	value_type value;
};

}

template<
	typename CharT_ = char,
	typename Traits_ = std::char_traits<CharT_> >
class deb822_parser_impl :
	public parser<deb822::pair<CharT_, Traits_> >::impl
{
public:
	typedef CharT_ char_type;
	typedef Traits_ traits_type;

	typedef deb822::pair<char_type, traits_type> value_type;

	typedef std::basic_string<char_type, traits_type> string_type;
	typedef std::basic_istream<char_type, traits_type> stream_type;

	deb822_parser_impl(stream_type &);
	virtual ~deb822_parser_impl(void) throw();

	typedef value_type const *pointer;
	typedef value_type const &reference;

	virtual reference operator*(void) const;
	virtual pointer operator->(void) const;
	virtual deb822_parser_impl &operator++(void);
	virtual bool operator!(void) const throw();

private:
	stream_type &stream;
	value_type obj;
	bool fake_failure;
};

template<typename CharT_, typename Traits_>
deb822_parser_impl<CharT_, Traits_>::deb822_parser_impl(
		std::basic_istream<char_type, traits_type> &stream) :
	stream(stream),
	fake_failure(false)
{
	stream >> std::noskipws;
	++*this;
	return;
}

template<typename CharT_, typename Traits_>
deb822_parser_impl<CharT_, Traits_>::~deb822_parser_impl(void) throw()
{
	if(fake_failure)
		stream.clear(stream.rdstate() & ~std::ios::failbit);
	return;
}

template<typename CharT_, typename Traits_>
typename deb822_parser_impl<CharT_, Traits_>::reference
deb822_parser_impl<CharT_, Traits_>::operator*(void) const
{
	return obj;
}

template<typename CharT_, typename Traits_>
typename deb822_parser_impl<CharT_, Traits_>::pointer
deb822_parser_impl<CharT_, Traits_>::operator->(void) const
{
	return &obj;
}

template<typename CharT_, typename Traits_>
deb822_parser_impl<CharT_, Traits_> &
deb822_parser_impl<CharT_, Traits_>::operator++(void)
{
	typename stream_type::sentry sentry(stream);
	if(!sentry)
		return *this;

	string_type line;
	if(!std::getline(stream, line))
		return *this;
	
	if(line.empty())
	{
		stream.setstate(std::ios::failbit);
		fake_failure = true;
		return *this;
	}

	typedef typename string_type::size_type pos_type;

	pos_type const colon = line.find(':');
	if(colon == string_type::npos)
	{
		stream.setstate(std::ios::failbit|std::ios::badbit);
		return *this;
	}

	bool const first_line_is_empty = (line.size() == (colon + 1));

	typename value_type::key_type key = line.substr(0, colon);
	typename value_type::value_type value = first_line_is_empty?
			typename value_type::value_type():
			line.substr(
				line.find_first_not_of(' ', colon + 1));

	while(stream.peek() == traits_type::to_int_type(' '))
	{
		if(!std::getline(stream, line))
			return *this;
		if(line.empty())
		{
			stream.setstate(std::ios::failbit);
			fake_failure = true;
			return *this;
		}

		char_type const space_dot[] = {' ', '.'};
		if(line == string_type(space_dot, 2))
			line.resize(1);

		line[0] = '\n';
		value += line;
	}

	obj.key = key;
	obj.value = value;

	return *this;
}

template<typename CharT_, typename Traits_>
bool deb822_parser_impl<CharT_, Traits_>::operator!(void) const throw()
{
	return !stream;
}

typedef deb822_parser_impl<> deb822_parser;
typedef deb822_parser_impl<wchar_t> deb822_wparser;

}

#endif