~ubuntu-branches/ubuntu/wily/aegisub/wily-proposed

« back to all changes in this revision

Viewing changes to libaegisub/common/json.cpp

  • Committer: Package Import Robot
  • Author(s): Sebastian Reichel, Pascal De Vuyst, Juan Picca, Sebastian Reichel
  • Date: 2015-08-04 21:40:50 UTC
  • mfrom: (5.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150804214050-y2aghm9vdksoc8t7
Tags: 3.2.2+dfsg-1
[ Pascal De Vuyst ]
* Fix Typo in package description (Closes: #739219)

[ Juan Picca ]
* Add patch to fix reproducible build (Closes: #789728)

[ Sebastian Reichel ]
* New upstream release
 - remove vendor directory from orig tarball
* Update Debian Standards Version to 3.9.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
/// @brief Parse JSON files and return json::UnknownElement
17
17
/// @ingroup libaegisub io
18
18
 
19
 
#include "../config.h"
20
 
 
21
19
#include "libaegisub/json.h"
22
20
 
23
 
#include <fstream>
24
 
#include <memory>
25
 
#include <sstream>
26
 
 
 
21
#include "libaegisub/cajun/reader.h"
27
22
#include "libaegisub/fs.h"
28
23
#include "libaegisub/io.h"
29
24
#include "libaegisub/log.h"
30
 
#include "libaegisub/util.h"
31
 
 
32
 
namespace agi {
33
 
        namespace json_util {
34
 
 
35
 
json::UnknownElement parse(std::unique_ptr<std::istream> stream) {
 
25
 
 
26
#include <boost/interprocess/streams/bufferstream.hpp>
 
27
 
 
28
namespace agi { namespace json_util {
 
29
 
 
30
json::UnknownElement parse(std::istream &stream) {
36
31
        try {
37
32
                json::UnknownElement root;
38
 
                json::Reader::Read(root, *stream);
 
33
                json::Reader::Read(root, stream);
39
34
                return root;
40
35
        } catch (json::Reader::ParseException& e) {
41
36
                LOG_E("json/parse") << "json::ParseException: " << e.what() << ", Line/offset: " << e.m_locTokenBegin.m_nLine + 1 << '/' << e.m_locTokenBegin.m_nLineOffset + 1;
46
41
        }
47
42
}
48
43
 
49
 
json::UnknownElement file(agi::fs::path const& file) {
50
 
        return parse(io::Open(file));
51
 
}
52
 
 
53
 
json::UnknownElement file(agi::fs::path const& file, const std::string &default_config) {
 
44
json::UnknownElement file(agi::fs::path const& file, std::pair<const char *, size_t> default_config) {
54
45
        try {
55
 
                return parse(io::Open(file));
 
46
                if (fs::FileExists(file))
 
47
                        return parse(*io::Open(file));
56
48
        }
57
49
        catch (fs::FileNotFound const&) {
58
50
                // Not an error
59
 
                return parse(util::make_unique<std::istringstream>(default_config));
60
51
        }
61
52
        catch (json::Exception&) {
62
53
                // Already logged in parse
63
 
                return parse(util::make_unique<std::istringstream>(default_config));
64
54
        }
65
55
        catch (agi::Exception& e) {
66
56
                LOG_E("json/file") << "Unexpected error when reading config file " << file << ": " << e.GetMessage();
67
 
                return parse(util::make_unique<std::istringstream>(default_config));
68
57
        }
 
58
        boost::interprocess::ibufferstream stream(default_config.first, default_config.second);
 
59
        return parse(stream);
69
60
}
70
61
 
71
 
        } // namespace json_util
72
 
} // namespace agi
 
62
} }