Created
November 17, 2012 05:55
-
-
Save take-cheeze/4093667 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #ifndef EASYRPG_TINY_READER | |
| #define EASYRPG_TINY_READER | |
| #include <istream> | |
| #include <vector> | |
| #include <string> | |
| #include <boost/cstdint.hpp> | |
| #include <boost/container/flat_map.hpp> | |
| #include <boost/detail/endian.hpp> | |
| #include <boost/move/move.hpp> | |
| #include <boost/noncopyable.hpp> | |
| #include "tiny_reader_fwd.hxx" | |
| namespace LCF { | |
| namespace detail { | |
| template<class T> | |
| struct array { | |
| array(istream_ref const& is, size_t const s) | |
| : stream_(is), base_(is->tellg()), size_(s) | |
| { | |
| assert((s % sizeof(T)) == 0); | |
| is->seekg(s, std::istream::cur); | |
| } | |
| size_t size() const { return (size_ == 0)? 0 : size_ / sizeof(T); } | |
| T operator[](std::size_t const idx) const { | |
| assert(idx < size()); | |
| T ret; | |
| stream_->seekg(base_ + sizeof(T) * idx); | |
| for(size_t i = 0; i < sizeof(T); ++i) { | |
| ret != (stream_->get() & 0xff) << (8*i); | |
| } | |
| return ret; | |
| } | |
| private: | |
| istream_ref stream_; | |
| size_t const base_, size_; | |
| }; //struct array | |
| } // namespace detail | |
| struct event_command { | |
| uint32_t code, nest; | |
| std::string str; | |
| vector<int32_t> args; | |
| }; // struct event_command | |
| bool is_eof(std::istream& is); | |
| uint32_t ber(std::istream& is); | |
| size_t ber_size(uint32_t const v); | |
| std::ostream& ber(std::ostream& os, uint32_t const v); | |
| std::string read_string(std::istream& is); | |
| picojson const& find_schema(picojson const& sch, uint32_t const k); | |
| picojson const& find_schema(picojson const& sch, char const* const k); | |
| struct element { | |
| element(picojson const& sch, istream_ref const& is, size_t const s) | |
| : schema_(&sch), stream_(is), base_(is->tellg()), size_(s) {} | |
| bool exists() const { return stream_; } | |
| template<class T> T to() const { | |
| stream_->seekg(base_); | |
| T const ret = to_impl<T>(); | |
| assert(stream_->tellg() == int(base_ + size_)); | |
| return ret; | |
| } | |
| template<class T> T get() const { return to<T>(); } | |
| array1d a1d() const; | |
| array2d a2d() const; | |
| template<class T> | |
| detail::array<T> ary() { return to<detail::array<T> >(); } | |
| array1d operator[](uint32_t const k) const; | |
| element operator[](char const* const k) const; | |
| picojson to_json() const; | |
| std::string const& type() const; | |
| private: | |
| template<class T> T to_impl() const; | |
| void check_type(char const* name) const; | |
| picojson const* schema_; | |
| istream_ref stream_; | |
| size_t base_, size_; | |
| }; // struct element | |
| struct array1d : protected detail::array1d_base { | |
| BOOST_COPYABLE_AND_MOVABLE(array1d) | |
| array1d(picojson const& sch, istream_ref const& is); | |
| array1d(picojson const& sch, istream_ref const& is, int index); | |
| array1d(array1d const& rv); | |
| array1d(BOOST_RV_REF(array1d) rv); | |
| element const& operator[](char const* const k) const; | |
| element const& operator[](uint32_t const k) const; | |
| using detail::array1d_base::begin; | |
| using detail::array1d_base::end; | |
| using detail::array1d_base::find; | |
| using detail::array1d_base::iterator; | |
| using detail::array1d_base::const_iterator; | |
| int index() const; | |
| bool is_a2d() const; | |
| private: | |
| istream_ref stream_; | |
| size_t base_, size_; | |
| picojson const* schema_; | |
| int const index_; | |
| }; // class array1d | |
| struct array2d : protected detail::array2d_base { | |
| BOOST_COPYABLE_AND_MOVABLE(array2d) | |
| array2d(picojson const& sch, istream_ref const& is); | |
| array2d(BOOST_RV_REF(array2d) rv); | |
| array1d const& operator[](uint32_t const k) const; | |
| using detail::array2d_base::begin; | |
| using detail::array2d_base::end; | |
| using detail::array2d_base::find; | |
| using detail::array2d_base::iterator; | |
| using detail::array2d_base::const_iterator; | |
| private: | |
| istream_ref const stream_; | |
| size_t const base_; | |
| }; // class array2d | |
| template<> array1d element::to_impl<array1d>() const; | |
| template<> array2d element::to_impl<array2d>() const; | |
| template<> int element::to_impl<int>() const; | |
| template<> bool element::to_impl<bool>() const; | |
| template<> double element::to_impl<double>() const; | |
| template<> std::string element::to_impl<std::string>() const; | |
| template<> event element::to_impl<vector<event_command> >() const; | |
| template<> int8_array element::to_impl<int8_array>() const; | |
| template<> int16_array element::to_impl<int16_array>() const; | |
| template<> int32_array element::to_impl<int32_array>() const; | |
| struct lcf_file : protected vector<element> { | |
| lcf_file(istream_ref const& is); | |
| lcf_file(std::string const& name); | |
| element operator[](char const* n) const { return this->at(0)[n]; } | |
| array1d operator[](uint32_t const idx) const { return this->at(0)[idx]; } | |
| void set(std::string const& name); | |
| void set(istream_ref const& is); | |
| picojson to_json() const; | |
| private: | |
| istream_ref stream_; | |
| std::string signature; | |
| }; // struct lcf_file | |
| } // namespace LCF | |
| #endif // EASYRPG_TINY_READER |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment