Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:01

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/json
0009 //
0010 
0011 #ifndef BOOST_JSON_IMPL_PARSE_IPP
0012 #define BOOST_JSON_IMPL_PARSE_IPP
0013 
0014 #include <boost/json/parse.hpp>
0015 #include <boost/json/parser.hpp>
0016 #include <boost/json/detail/except.hpp>
0017 
0018 #include <istream>
0019 
0020 namespace boost {
0021 namespace json {
0022 
0023 value
0024 parse(
0025     string_view s,
0026     error_code& ec,
0027     storage_ptr sp,
0028     const parse_options& opt)
0029 {
0030     unsigned char temp[
0031         BOOST_JSON_STACK_BUFFER_SIZE];
0032     parser p(storage_ptr(), opt, temp);
0033     p.reset(std::move(sp));
0034     p.write(s, ec);
0035     if(ec)
0036         return nullptr;
0037     return p.release();
0038 }
0039 
0040 value
0041 parse(
0042     string_view s,
0043     std::error_code& ec,
0044     storage_ptr sp,
0045     parse_options const& opt)
0046 {
0047     error_code jec;
0048     value result = parse(s, jec, std::move(sp), opt);
0049     ec = jec;
0050     return result;
0051 }
0052 
0053 value
0054 parse(
0055     string_view s,
0056     storage_ptr sp,
0057     const parse_options& opt)
0058 {
0059     error_code ec;
0060     auto jv = parse(
0061         s, ec, std::move(sp), opt);
0062     if(ec)
0063         detail::throw_system_error( ec );
0064     return jv;
0065 }
0066 
0067 value
0068 parse(
0069     std::istream& is,
0070     error_code& ec,
0071     storage_ptr sp,
0072     parse_options const& opt)
0073 {
0074     unsigned char parser_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
0075     stream_parser p(storage_ptr(), opt, parser_buffer);
0076     p.reset(std::move(sp));
0077 
0078     char read_buffer[BOOST_JSON_STACK_BUFFER_SIZE / 2];
0079     do
0080     {
0081         if( is.eof() )
0082         {
0083             p.finish(ec);
0084             break;
0085         }
0086 
0087         if( !is )
0088         {
0089             BOOST_JSON_FAIL( ec, error::input_error );
0090             break;
0091         }
0092 
0093         is.read(read_buffer, sizeof(read_buffer));
0094         auto const consumed = is.gcount();
0095 
0096         p.write( read_buffer, static_cast<std::size_t>(consumed), ec );
0097     }
0098     while( !ec.failed() );
0099 
0100     if( ec.failed() )
0101         return nullptr;
0102 
0103     return p.release();
0104 }
0105 
0106 value
0107 parse(
0108     std::istream& is,
0109     std::error_code& ec,
0110     storage_ptr sp,
0111     parse_options const& opt)
0112 {
0113     error_code jec;
0114     value result = parse(is, jec, std::move(sp), opt);
0115     ec = jec;
0116     return result;
0117 }
0118 
0119 value
0120 parse(
0121     std::istream& is,
0122     storage_ptr sp,
0123     parse_options const& opt)
0124 {
0125     error_code ec;
0126     auto jv = parse(
0127         is, ec, std::move(sp), opt);
0128     if(ec)
0129         detail::throw_system_error( ec );
0130     return jv;
0131 }
0132 
0133 } // namespace json
0134 } // namespace boost
0135 
0136 #endif