1// SuperTux
2// Copyright (C) 2015 Ingo Ruhnke <grumbel@gmail.com>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17#include "util/reader_iterator.hpp"
18
19#include <sexp/io.hpp>
20#include <sstream>
21
22#include "util/reader_error.hpp"
23#include "util/reader_document.hpp"
24#include "util/reader_mapping.hpp"
25
26ReaderIterator::ReaderIterator(const ReaderDocument& doc, const sexp::Value& sx) :
27 m_doc(doc),
28 m_arr(sx.as_array()),
29 m_idx(0)
30{
31}
32
33bool
34ReaderIterator::next()
35{
36 m_idx += 1;
37 return m_idx < m_arr.size();
38}
39
40bool
41ReaderIterator::is_string()
42{
43 return m_arr[m_idx].is_string();
44}
45
46bool
47ReaderIterator::is_pair()
48{
49 return m_arr[m_idx].is_array();
50}
51
52std::string
53ReaderIterator::as_string_item()
54{
55 assert_is_string(m_doc, m_arr[m_idx]);
56
57 return m_arr[m_idx].as_string();
58}
59
60std::string
61ReaderIterator::get_key() const
62{
63 assert_is_array(m_doc, m_arr[m_idx]);
64 assert_array_size_ge(m_doc, m_arr[m_idx], 1);
65
66 return m_arr[m_idx].as_array()[0].as_string();
67}
68
69void
70ReaderIterator::get(bool& value) const
71{
72 assert_is_array(m_doc, m_arr[m_idx]);
73 assert_array_size_eq(m_doc, m_arr[m_idx], 2);
74 assert_is_boolean(m_doc, m_arr[m_idx].as_array()[1]);
75
76 value = m_arr[m_idx].as_array()[1].as_bool();
77}
78
79void
80ReaderIterator::get(int& value) const
81{
82 assert_is_array(m_doc, m_arr[m_idx]);
83 assert_array_size_eq(m_doc, m_arr[m_idx], 2);
84 assert_is_integer(m_doc, m_arr[m_idx].as_array()[1]);
85
86 value = m_arr[m_idx].as_array()[1].as_int();
87}
88
89void
90ReaderIterator::get(float& value) const
91{
92 assert_is_array(m_doc, m_arr[m_idx]);
93 assert_array_size_eq(m_doc, m_arr[m_idx], 2);
94 assert_is_real(m_doc, m_arr[m_idx].as_array()[1]);
95
96 value = m_arr[m_idx].as_array()[1].as_float();
97}
98
99void
100ReaderIterator::get(std::string& value) const
101{
102 assert_is_array(m_doc, m_arr[m_idx]);
103 assert_array_size_eq(m_doc, m_arr[m_idx], 2);
104 assert_is_string(m_doc, m_arr[m_idx].as_array()[1]);
105
106 value = m_arr[m_idx].as_array()[1].as_string();
107}
108
109ReaderMapping
110ReaderIterator::as_mapping() const
111{
112 return ReaderMapping(m_doc, m_arr[m_idx]);
113}
114
115const sexp::Value&
116ReaderIterator::get_sexp() const
117{
118 return m_arr[m_idx];
119}
120
121/* EOF */
122