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_document.hpp"
18
19#include <sexp/parser.hpp>
20#include <sstream>
21
22#include "physfs/ifile_stream.hpp"
23#include "util/file_system.hpp"
24#include "util/log.hpp"
25
26ReaderDocument
27ReaderDocument::from_stream(std::istream& stream, const std::string& filename)
28{
29 sexp::Value sx = sexp::Parser::from_stream(stream, sexp::Parser::USE_ARRAYS);
30 return ReaderDocument(filename, std::move(sx));
31}
32
33ReaderDocument
34ReaderDocument::from_file(const std::string& filename)
35{
36 log_debug << "ReaderDocument::parse: " << filename << std::endl;
37
38 IFileStream in(filename);
39 if (!in.good()) {
40 std::stringstream msg;
41 msg << "Parser problem: Couldn't open file '" << filename << "'.";
42 throw std::runtime_error(msg.str());
43 } else {
44 return from_stream(in, filename);
45 }
46}
47
48ReaderDocument::ReaderDocument(const std::string& filename, sexp::Value sx) :
49 m_filename(filename),
50 m_sx(std::move(sx))
51{
52}
53
54ReaderObject
55ReaderDocument::get_root() const
56{
57 return ReaderObject(*this, m_sx);
58}
59
60std::string
61ReaderDocument::get_filename() const
62{
63 return m_filename;
64}
65
66std::string
67ReaderDocument::get_directory() const
68{
69 return FileSystem::dirname(m_filename);
70}
71
72/* EOF */
73