initial code repo
[stor4nfv.git] / src / ceph / src / json_spirit / json_spirit_reader_template.h
diff --git a/src/ceph/src/json_spirit/json_spirit_reader_template.h b/src/ceph/src/json_spirit/json_spirit_reader_template.h
new file mode 100644 (file)
index 0000000..c50f885
--- /dev/null
@@ -0,0 +1,666 @@
+#ifndef JSON_SPIRIT_READER_TEMPLATE\r
+#define JSON_SPIRIT_READER_TEMPLATE\r
+\r
+//          Copyright John W. Wilkinson 2007 - 2011\r
+// Distributed under the MIT License, see accompanying file LICENSE.txt\r
+\r
+// json spirit version 4.05\r
+\r
+#if defined(_MSC_VER) && (_MSC_VER >= 1020)\r
+# pragma once\r
+#endif\r
+\r
+#include "json_spirit_value.h"\r
+#include "json_spirit_error_position.h"\r
+\r
+#include "common/utf8.h"\r
+\r
+#define BOOST_SPIRIT_THREADSAFE  // uncomment for multithreaded use, requires linking to boost.thread\r
+\r
+#include <boost/bind.hpp>\r
+#include <boost/function.hpp>\r
+#include <boost/version.hpp>\r
+\r
+#if BOOST_VERSION >= 103800\r
+    #include <boost/spirit/include/classic_core.hpp>\r
+    #include <boost/spirit/include/classic_confix.hpp>\r
+    #include <boost/spirit/include/classic_escape_char.hpp>\r
+    #include <boost/spirit/include/classic_multi_pass.hpp>\r
+    #include <boost/spirit/include/classic_position_iterator.hpp>\r
+    #define spirit_namespace boost::spirit::classic\r
+#else\r
+    #include <boost/spirit/core.hpp>\r
+    #include <boost/spirit/utility/confix.hpp>\r
+    #include <boost/spirit/utility/escape_char.hpp>\r
+    #include <boost/spirit/iterator/multi_pass.hpp>\r
+    #include <boost/spirit/iterator/position_iterator.hpp>\r
+    #define spirit_namespace boost::spirit\r
+#endif\r
+\r
+namespace json_spirit\r
+{\r
+    const spirit_namespace::int_parser < boost::int64_t >  int64_p  = spirit_namespace::int_parser < boost::int64_t  >();\r
+    const spirit_namespace::uint_parser< boost::uint64_t > uint64_p = spirit_namespace::uint_parser< boost::uint64_t >();\r
+\r
+    template< class Iter_type >\r
+    bool is_eq( Iter_type first, Iter_type last, const char* c_str )\r
+    {\r
+        for( Iter_type i = first; i != last; ++i, ++c_str )\r
+        {\r
+            if( *c_str == 0 ) return false;\r
+\r
+            if( *i != *c_str ) return false;\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    template< class Char_type >\r
+    Char_type hex_to_num( const Char_type c )\r
+    {\r
+        if( ( c >= '0' ) && ( c <= '9' ) ) return c - '0';\r
+        if( ( c >= 'a' ) && ( c <= 'f' ) ) return c - 'a' + 10;\r
+        if( ( c >= 'A' ) && ( c <= 'F' ) ) return c - 'A' + 10;\r
+        return 0;\r
+    }\r
+\r
+    template< class Char_type, class Iter_type >\r
+    Char_type hex_str_to_char( Iter_type& begin )\r
+    {\r
+        const Char_type c1( *( ++begin ) );\r
+        const Char_type c2( *( ++begin ) );\r
+\r
+        return ( hex_to_num( c1 ) << 4 ) + hex_to_num( c2 );\r
+    }       \r
+\r
+    template< class String_type, class Iter_type >\r
+    String_type unicode_str_to_utf8( Iter_type& begin );\r
+\r
+    template<>\r
+    std::string unicode_str_to_utf8( std::string::const_iterator & begin )\r
+    {\r
+        typedef std::string::value_type Char_type;\r
+\r
+        const Char_type c1( *( ++begin ) );\r
+        const Char_type c2( *( ++begin ) );\r
+        const Char_type c3( *( ++begin ) );\r
+        const Char_type c4( *( ++begin ) );\r
+\r
+        unsigned long uc = ( hex_to_num( c1 ) << 12 ) + \r
+                           ( hex_to_num( c2 ) <<  8 ) + \r
+                           ( hex_to_num( c3 ) <<  4 ) + \r
+                           hex_to_num( c4 );\r
+\r
+        unsigned char buf[7];  // MAX_UTF8_SZ is 6 (see src/common/utf8.c)\r
+        int r = encode_utf8(uc, buf);\r
+        if (r >= 0) {\r
+            return std::string(reinterpret_cast<char *>(buf), r);\r
+        }\r
+        return std::string("_");\r
+    }\r
+\r
+    template< class String_type >\r
+    void append_esc_char_and_incr_iter( String_type& s, \r
+                                        typename String_type::const_iterator& begin, \r
+                                        typename String_type::const_iterator end )\r
+    {\r
+        typedef typename String_type::value_type Char_type;\r
+             \r
+        const Char_type c2( *begin );\r
+\r
+        switch( c2 )\r
+        {\r
+            case 't':  s += '\t'; break;\r
+            case 'b':  s += '\b'; break;\r
+            case 'f':  s += '\f'; break;\r
+            case 'n':  s += '\n'; break;\r
+            case 'r':  s += '\r'; break;\r
+            case '\\': s += '\\'; break;\r
+            case '/':  s += '/';  break;\r
+            case '"':  s += '"';  break;\r
+            case 'x':  \r
+            {\r
+                if( end - begin >= 3 )  //  expecting "xHH..."\r
+                {\r
+                    s += hex_str_to_char< Char_type >( begin );  \r
+                }\r
+                break;\r
+            }\r
+            case 'u':  \r
+            {\r
+                if( end - begin >= 5 )  //  expecting "uHHHH..."\r
+                {\r
+                    s += unicode_str_to_utf8< String_type >( begin );\r
+                }\r
+                break;\r
+            }\r
+        }\r
+    }\r
+\r
+    template< class String_type >\r
+    String_type substitute_esc_chars( typename String_type::const_iterator begin, \r
+                                   typename String_type::const_iterator end )\r
+    {\r
+        typedef typename String_type::const_iterator Iter_type;\r
+\r
+        if( end - begin < 2 ) return String_type( begin, end );\r
+\r
+        String_type result;\r
+        \r
+        result.reserve( end - begin );\r
+\r
+        const Iter_type end_minus_1( end - 1 );\r
+\r
+        Iter_type substr_start = begin;\r
+        Iter_type i = begin;\r
+\r
+        for( ; i < end_minus_1; ++i )\r
+        {\r
+            if( *i == '\\' )\r
+            {\r
+                result.append( substr_start, i );\r
+\r
+                ++i;  // skip the '\'\r
+             \r
+                append_esc_char_and_incr_iter( result, i, end );\r
+\r
+                substr_start = i + 1;\r
+            }\r
+        }\r
+\r
+        result.append( substr_start, end );\r
+\r
+        return result;\r
+    }\r
+\r
+    template< class String_type >\r
+    String_type get_str_( typename String_type::const_iterator begin, \r
+                       typename String_type::const_iterator end )\r
+    {\r
+        assert( end - begin >= 2 );\r
+\r
+        typedef typename String_type::const_iterator Iter_type;\r
+\r
+        Iter_type str_without_quotes( ++begin );\r
+        Iter_type end_without_quotes( --end );\r
+\r
+        return substitute_esc_chars< String_type >( str_without_quotes, end_without_quotes );\r
+    }\r
+\r
+    inline std::string get_str( std::string::const_iterator begin, std::string::const_iterator end )\r
+    {\r
+        return get_str_< std::string >( begin, end );\r
+    }\r
+\r
+// Need this guard else it tries to instantiate unicode_str_to_utf8 with a\r
+// std::wstring, which isn't presently implemented\r
+#if defined( JSON_SPIRIT_WMVALUE_ENABLED ) && !defined( BOOST_NO_STD_WSTRING )\r
+    inline std::wstring get_str( std::wstring::const_iterator begin, std::wstring::const_iterator end )\r
+    {\r
+        return get_str_< std::wstring >( begin, end );\r
+    }\r
+#endif\r
+\r
+    template< class String_type, class Iter_type >\r
+    String_type get_str( Iter_type begin, Iter_type end )\r
+    {\r
+        const String_type tmp( begin, end );  // convert multipass iterators to string iterators\r
+\r
+        return get_str( tmp.begin(), tmp.end() );\r
+    }\r
+\r
+    // this class's methods get called by the spirit parse resulting\r
+    // in the creation of a JSON object or array\r
+    //\r
+    // NB Iter_type could be a std::string iterator, wstring iterator, a position iterator or a multipass iterator\r
+    //\r
+    template< class Value_type, class Iter_type >\r
+    class Semantic_actions \r
+    {\r
+    public:\r
+\r
+        typedef typename Value_type::Config_type Config_type;\r
+        typedef typename Config_type::String_type String_type;\r
+        typedef typename Config_type::Object_type Object_type;\r
+        typedef typename Config_type::Array_type Array_type;\r
+        typedef typename String_type::value_type Char_type;\r
+\r
+        Semantic_actions( Value_type& value )\r
+        :   value_( value )\r
+        ,   current_p_( 0 )\r
+        {\r
+        }\r
+\r
+        void begin_obj( Char_type c )\r
+        {\r
+            assert( c == '{' );\r
+\r
+            begin_compound< Object_type >();\r
+        }\r
+\r
+        void end_obj( Char_type c )\r
+        {\r
+            assert( c == '}' );\r
+\r
+            end_compound();\r
+        }\r
+\r
+        void begin_array( Char_type c )\r
+        {\r
+            assert( c == '[' );\r
+     \r
+            begin_compound< Array_type >();\r
+        }\r
+\r
+        void end_array( Char_type c )\r
+        {\r
+            assert( c == ']' );\r
+\r
+            end_compound();\r
+        }\r
+\r
+        void new_name( Iter_type begin, Iter_type end )\r
+        {\r
+            assert( current_p_->type() == obj_type );\r
+\r
+            name_ = get_str< String_type >( begin, end );\r
+        }\r
+\r
+        void new_str( Iter_type begin, Iter_type end )\r
+        {\r
+            add_to_current( get_str< String_type >( begin, end ) );\r
+        }\r
+\r
+        void new_true( Iter_type begin, Iter_type end )\r
+        {\r
+            assert( is_eq( begin, end, "true" ) );\r
+\r
+            add_to_current( true );\r
+        }\r
+\r
+        void new_false( Iter_type begin, Iter_type end )\r
+        {\r
+            assert( is_eq( begin, end, "false" ) );\r
+\r
+            add_to_current( false );\r
+        }\r
+\r
+        void new_null( Iter_type begin, Iter_type end )\r
+        {\r
+            assert( is_eq( begin, end, "null" ) );\r
+\r
+            add_to_current( Value_type() );\r
+        }\r
+\r
+        void new_int( boost::int64_t i )\r
+        {\r
+            add_to_current( i );\r
+        }\r
+\r
+        void new_uint64( boost::uint64_t ui )\r
+        {\r
+            add_to_current( ui );\r
+        }\r
+\r
+        void new_real( double d )\r
+        {\r
+            add_to_current( d );\r
+        }\r
+\r
+    private:\r
+\r
+        Semantic_actions& operator=( const Semantic_actions& ); \r
+                                    // to prevent "assignment operator could not be generated" warning\r
+\r
+        Value_type* add_first( const Value_type& value )\r
+        {\r
+            assert( current_p_ == 0 );\r
+\r
+            value_ = value;\r
+            current_p_ = &value_;\r
+            return current_p_;\r
+        }\r
+\r
+        template< class Array_or_obj >\r
+        void begin_compound()\r
+        {\r
+            if( current_p_ == 0 )\r
+            {\r
+                add_first( Array_or_obj() );\r
+            }\r
+            else\r
+            {\r
+                stack_.push_back( current_p_ );\r
+\r
+                Array_or_obj new_array_or_obj;   // avoid copy by building new array or object in place\r
+\r
+                current_p_ = add_to_current( new_array_or_obj );\r
+            }\r
+        }\r
+\r
+        void end_compound()\r
+        {\r
+            if( current_p_ != &value_ )\r
+            {\r
+                current_p_ = stack_.back();\r
+                \r
+                stack_.pop_back();\r
+            }    \r
+        }\r
+\r
+        Value_type* add_to_current( const Value_type& value )\r
+        {\r
+            if( current_p_ == 0 )\r
+            {\r
+                return add_first( value );\r
+            }\r
+            else if( current_p_->type() == array_type )\r
+            {\r
+                current_p_->get_array().push_back( value );\r
+\r
+                return &current_p_->get_array().back(); \r
+            }\r
+            \r
+            assert( current_p_->type() == obj_type );\r
+\r
+            return &Config_type::add( current_p_->get_obj(), name_, value );\r
+        }\r
+\r
+        Value_type& value_;             // this is the object or array that is being created\r
+        Value_type* current_p_;         // the child object or array that is currently being constructed\r
+\r
+        std::vector< Value_type* > stack_;   // previous child objects and arrays\r
+\r
+        String_type name_;              // of current name/value pair\r
+    };\r
+\r
+    template< typename Iter_type >\r
+    void throw_error( spirit_namespace::position_iterator< Iter_type > i, const std::string& reason )\r
+    {\r
+        throw Error_position( i.get_position().line, i.get_position().column, reason );\r
+    }\r
+\r
+    template< typename Iter_type >\r
+    void throw_error( Iter_type i, const std::string& reason )\r
+    {\r
+       throw reason;\r
+    }\r
+\r
+    // the spirit grammer \r
+    //\r
+    template< class Value_type, class Iter_type >\r
+    class Json_grammer : public spirit_namespace::grammar< Json_grammer< Value_type, Iter_type > >\r
+    {\r
+    public:\r
+\r
+        typedef Semantic_actions< Value_type, Iter_type > Semantic_actions_t;\r
+\r
+        Json_grammer( Semantic_actions_t& semantic_actions )\r
+        :   actions_( semantic_actions )\r
+        {\r
+        }\r
+\r
+        static void throw_not_value( Iter_type begin, Iter_type end )\r
+        {\r
+           throw_error( begin, "not a value" );\r
+        }\r
+\r
+        static void throw_not_array( Iter_type begin, Iter_type end )\r
+        {\r
+           throw_error( begin, "not an array" );\r
+        }\r
+\r
+        static void throw_not_object( Iter_type begin, Iter_type end )\r
+        {\r
+           throw_error( begin, "not an object" );\r
+        }\r
+\r
+        static void throw_not_pair( Iter_type begin, Iter_type end )\r
+        {\r
+           throw_error( begin, "not a pair" );\r
+        }\r
+\r
+        static void throw_not_colon( Iter_type begin, Iter_type end )\r
+        {\r
+           throw_error( begin, "no colon in pair" );\r
+        }\r
+\r
+        static void throw_not_string( Iter_type begin, Iter_type end )\r
+        {\r
+           throw_error( begin, "not a string" );\r
+        }\r
+\r
+        template< typename ScannerT >\r
+        class definition\r
+        {\r
+        public:\r
+\r
+            definition( const Json_grammer& self )\r
+            {\r
+                using namespace spirit_namespace;\r
+\r
+                typedef typename Value_type::String_type::value_type Char_type;\r
+\r
+                // first we convert the semantic action class methods to functors with the \r
+                // parameter signature expected by spirit\r
+\r
+                typedef boost::function< void( Char_type )            > Char_action;\r
+                typedef boost::function< void( Iter_type, Iter_type ) > Str_action;\r
+                typedef boost::function< void( double )               > Real_action;\r
+                typedef boost::function< void( boost::int64_t )       > Int_action;\r
+                typedef boost::function< void( boost::uint64_t )      > Uint64_action;\r
+\r
+                Char_action   begin_obj  ( boost::bind( &Semantic_actions_t::begin_obj,   &self.actions_, _1 ) );\r
+                Char_action   end_obj    ( boost::bind( &Semantic_actions_t::end_obj,     &self.actions_, _1 ) );\r
+                Char_action   begin_array( boost::bind( &Semantic_actions_t::begin_array, &self.actions_, _1 ) );\r
+                Char_action   end_array  ( boost::bind( &Semantic_actions_t::end_array,   &self.actions_, _1 ) );\r
+                Str_action    new_name   ( boost::bind( &Semantic_actions_t::new_name,    &self.actions_, _1, _2 ) );\r
+                Str_action    new_str    ( boost::bind( &Semantic_actions_t::new_str,     &self.actions_, _1, _2 ) );\r
+                Str_action    new_true   ( boost::bind( &Semantic_actions_t::new_true,    &self.actions_, _1, _2 ) );\r
+                Str_action    new_false  ( boost::bind( &Semantic_actions_t::new_false,   &self.actions_, _1, _2 ) );\r
+                Str_action    new_null   ( boost::bind( &Semantic_actions_t::new_null,    &self.actions_, _1, _2 ) );\r
+                Real_action   new_real   ( boost::bind( &Semantic_actions_t::new_real,    &self.actions_, _1 ) );\r
+                Int_action    new_int    ( boost::bind( &Semantic_actions_t::new_int,     &self.actions_, _1 ) );\r
+                Uint64_action new_uint64 ( boost::bind( &Semantic_actions_t::new_uint64,  &self.actions_, _1 ) );\r
+\r
+                // actual grammer\r
+\r
+                json_\r
+                    = value_ | eps_p[ &throw_not_value ]\r
+                    ;\r
+\r
+                value_\r
+                    = string_[ new_str ] \r
+                    | number_ \r
+                    | object_ \r
+                    | array_ \r
+                    | str_p( "true" ) [ new_true  ] \r
+                    | str_p( "false" )[ new_false ] \r
+                    | str_p( "null" ) [ new_null  ]\r
+                    ;\r
+\r
+                object_ \r
+                    = ch_p('{')[ begin_obj ]\r
+                    >> !members_\r
+                    >> ( ch_p('}')[ end_obj ] | eps_p[ &throw_not_object ] )\r
+                    ;\r
+\r
+                members_\r
+                    = pair_ >> *( ',' >> pair_  | ch_p(',') )\r
+                    ;\r
+\r
+                pair_\r
+                    = string_[ new_name ]\r
+                    >> ( ':' | eps_p[ &throw_not_colon ] )\r
+                    >> ( value_ | eps_p[ &throw_not_value ] )\r
+                    ;\r
+\r
+                array_\r
+                    = ch_p('[')[ begin_array ]\r
+                    >> !elements_\r
+                    >> ( ch_p(']')[ end_array ] | eps_p[ &throw_not_array ] )\r
+                    ;\r
+\r
+                elements_\r
+                    = value_ >> *( ',' >> value_ | ch_p(',') )\r
+                    ;\r
+\r
+                string_ \r
+                    = lexeme_d // this causes white space inside a string to be retained\r
+                      [\r
+                          confix_p\r
+                          ( \r
+                              '"', \r
+                              *lex_escape_ch_p,\r
+                              '"'\r
+                          ) \r
+                      ]\r
+                    ;\r
+\r
+                number_\r
+                    = strict_real_p[ new_real   ] \r
+                    | int64_p      [ new_int    ]\r
+                    | uint64_p     [ new_uint64 ]\r
+                    ;\r
+            }\r
+\r
+            spirit_namespace::rule< ScannerT > json_, object_, members_, pair_, array_, elements_, value_, string_, number_;\r
+\r
+            const spirit_namespace::rule< ScannerT >& start() const { return json_; }\r
+        };\r
+\r
+    private:\r
+\r
+        Json_grammer& operator=( const Json_grammer& ); // to prevent "assignment operator could not be generated" warning\r
+\r
+        Semantic_actions_t& actions_;\r
+    };\r
+\r
+    template< class Iter_type, class Value_type >\r
+    void add_posn_iter_and_read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )\r
+    {\r
+        typedef spirit_namespace::position_iterator< Iter_type > Posn_iter_t;\r
+\r
+        const Posn_iter_t posn_begin( begin, end );\r
+        const Posn_iter_t posn_end( end, end );\r
+     \r
+        read_range_or_throw( posn_begin, posn_end, value );\r
+    }\r
+\r
+    template< class Istream_type >\r
+    struct Multi_pass_iters\r
+    {\r
+        typedef typename Istream_type::char_type Char_type;\r
+        typedef std::istream_iterator< Char_type, Char_type > istream_iter;\r
+        typedef spirit_namespace::multi_pass< istream_iter > Mp_iter;\r
+\r
+        Multi_pass_iters( Istream_type& is )\r
+        {\r
+            is.unsetf( std::ios::skipws );\r
+\r
+            begin_ = spirit_namespace::make_multi_pass( istream_iter( is ) );\r
+            end_   = spirit_namespace::make_multi_pass( istream_iter() );\r
+        }\r
+\r
+        Mp_iter begin_;\r
+        Mp_iter end_;\r
+    };\r
+\r
+    // reads a JSON Value from a pair of input iterators throwing an exception on invalid input, e.g.\r
+    //\r
+    // string::const_iterator start = str.begin();\r
+    // const string::const_iterator next = read_range_or_throw( str.begin(), str.end(), value );\r
+    //\r
+    // The iterator 'next' will point to the character past the \r
+    // last one read.\r
+    //\r
+    template< class Iter_type, class Value_type >\r
+    Iter_type read_range_or_throw( Iter_type begin, Iter_type end, Value_type& value )\r
+    {\r
+        Semantic_actions< Value_type, Iter_type > semantic_actions( value );\r
+     \r
+        const spirit_namespace::parse_info< Iter_type > info = \r
+                            spirit_namespace::parse( begin, end, \r
+                                                    Json_grammer< Value_type, Iter_type >( semantic_actions ), \r
+                                                    spirit_namespace::space_p );\r
+\r
+        if( !info.hit )\r
+        {\r
+            assert( false ); // in theory exception should already have been thrown\r
+            throw_error( info.stop, "error" );\r
+        }\r
+\r
+        return info.stop;\r
+    }\r
+\r
+    // reads a JSON Value from a pair of input iterators, e.g.\r
+    //\r
+    // string::const_iterator start = str.begin();\r
+    // const bool success = read_string( start, str.end(), value );\r
+    //\r
+    // The iterator 'start' will point to the character past the \r
+    // last one read.\r
+    //\r
+    template< class Iter_type, class Value_type >\r
+    bool read_range( Iter_type& begin, Iter_type end, Value_type& value )\r
+    {\r
+        try\r
+        {\r
+            begin = read_range_or_throw( begin, end, value );\r
+\r
+            return true;\r
+        }\r
+        catch( ... )\r
+        {\r
+            return false;\r
+        }\r
+    }\r
+\r
+    // reads a JSON Value from a string, e.g.\r
+    //\r
+    // const bool success = read_string( str, value );\r
+    //\r
+    template< class String_type, class Value_type >\r
+    bool read_string( const String_type& s, Value_type& value )\r
+    {\r
+        typename String_type::const_iterator begin = s.begin();\r
+\r
+        return read_range( begin, s.end(), value );\r
+    }\r
+\r
+    // reads a JSON Value from a string throwing an exception on invalid input, e.g.\r
+    //\r
+    // read_string_or_throw( is, value );\r
+    //\r
+    template< class String_type, class Value_type >\r
+    void read_string_or_throw( const String_type& s, Value_type& value )\r
+    {\r
+        add_posn_iter_and_read_range_or_throw( s.begin(), s.end(), value );\r
+    }\r
+\r
+    // reads a JSON Value from a stream, e.g.\r
+    //\r
+    // const bool success = read_stream( is, value );\r
+    //\r
+    template< class Istream_type, class Value_type >\r
+    bool read_stream( Istream_type& is, Value_type& value )\r
+    {\r
+        Multi_pass_iters< Istream_type > mp_iters( is );\r
+\r
+        return read_range( mp_iters.begin_, mp_iters.end_, value );\r
+    }\r
+\r
+    // reads a JSON Value from a stream throwing an exception on invalid input, e.g.\r
+    //\r
+    // read_stream_or_throw( is, value );\r
+    //\r
+    template< class Istream_type, class Value_type >\r
+    void read_stream_or_throw( Istream_type& is, Value_type& value )\r
+    {\r
+        const Multi_pass_iters< Istream_type > mp_iters( is );\r
+\r
+        add_posn_iter_and_read_range_or_throw( mp_iters.begin_, mp_iters.end_, value );\r
+    }\r
+}\r
+\r
+#endif\r