Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve core #1116 by removing fc::[io]fstream #179

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ set( fc_sources
src/io/iostream.cpp
src/io/datastream.cpp
src/io/buffered_iostream.cpp
src/io/fstream.cpp
src/io/sstream.cpp
src/io/json.cpp
src/io/varint.cpp
Expand Down
57 changes: 0 additions & 57 deletions include/fc/io/fstream.hpp

This file was deleted.

18 changes: 13 additions & 5 deletions include/fc/io/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <fc/variant.hpp>
#include <fc/filesystem.hpp>

#include <iostream>

#define DEFAULT_MAX_RECURSION_DEPTH 200

namespace fc
Expand Down Expand Up @@ -33,12 +35,18 @@ namespace fc
legacy_generator = 1
};

static ostream& to_stream( ostream& out, const std::string& );
static ostream& to_stream( ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static ostream& to_stream( ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static ostream& to_stream( ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static std::ostream& to_stream( std::ostream& out, const std::string& );
static std::ostream& to_stream( std::ostream& out, const variant& v,
output_formatting format = stringify_large_ints_and_doubles,
uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static std::ostream& to_stream( std::ostream& out, const variants& v,
output_formatting format = stringify_large_ints_and_doubles,
uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static std::ostream& to_stream( std::ostream& out, const variant_object& v,
output_formatting format = stringify_large_ints_and_doubles,
uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );

static variant from_stream( buffered_istream& in, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static variant from_stream( std::istream& in, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );

static variant from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
static variants variants_from_string( const string& utf8_str, parse_type ptype = legacy_parser, uint32_t max_depth = DEFAULT_MAX_RECURSION_DEPTH );
Expand Down
85 changes: 43 additions & 42 deletions include/fc/io/json_relaxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,21 @@

#include <fc/io/json.hpp>
#include <fc/exception/exception.hpp>
#include <fc/io/iostream.hpp>
#include <fc/io/buffered_iostream.hpp>
#include <fc/io/fstream.hpp>
#include <fc/io/sstream.hpp>
#include <fc/log/logger.hpp>
//#include <utfcpp/utf8.h>
#include <iostream>
#include <fstream>
#include <sstream>

#include <boost/filesystem/fstream.hpp>
#include <sstream>

namespace fc { namespace json_relaxed
{
template<typename T, bool strict>
variant variant_from_stream( T& in, uint32_t max_depth );
template<bool strict>
variant variant_from_stream( std::istream& in, uint32_t max_depth );

template<typename T>
std::string tokenFromStream( T& in )
std::string tokenFromStream( std::istream& in )
{
fc::stringstream token;
try
{
char c = in.peek();

char c;
while( true )
{
switch( c = in.peek() )
Expand Down Expand Up @@ -60,6 +50,8 @@ namespace fc { namespace json_relaxed
token << c;
in.get();
break;
case EOF:
if( in.eof() ) FC_THROW_EXCEPTION( eof_exception, "unexpected end of file" );
default:
return token.str();
}
Expand All @@ -79,8 +71,8 @@ namespace fc { namespace json_relaxed
("token", token.str() ) );
}

template<typename T, bool strict, bool allow_escape>
std::string quoteStringFromStream( T& in )
template<bool strict, bool allow_escape>
std::string quoteStringFromStream( std::istream& in )
{
fc::stringstream token;
try
Expand Down Expand Up @@ -147,6 +139,7 @@ namespace fc { namespace json_relaxed
("token", token.str() ) );
else if( allow_escape && (c == '\\') )
token << parseEscape( in );
else if( c == EOF && in.eof() ) FC_THROW_EXCEPTION( eof_exception, "unexpected end of file" );
else
{
in.get();
Expand All @@ -159,7 +152,6 @@ namespace fc { namespace json_relaxed
while( true )
{
char c = in.peek();

if( c == q )
{
in.get();
Expand All @@ -173,6 +165,7 @@ namespace fc { namespace json_relaxed
else if( (c == '\r') | (c == '\n') )
FC_THROW_EXCEPTION( parse_error_exception, "unexpected EOL in string '${token}'",
("token", token.str() ) );
else if( c == EOF && in.eof() ) FC_THROW_EXCEPTION( eof_exception, "unexpected end of file" );
else
{
in.get();
Expand All @@ -184,8 +177,8 @@ namespace fc { namespace json_relaxed
("token", token.str() ) );
}

template<typename T, bool strict>
std::string stringFromStream( T& in )
template<bool strict>
std::string stringFromStream( std::istream& in )
{
try
{
Expand All @@ -198,7 +191,7 @@ namespace fc { namespace json_relaxed
FC_THROW_EXCEPTION( parse_error_exception, "expected: '\"' at beginning of string, got '\''" );
// falls through
case '"':
return quoteStringFromStream<T, strict, true>( in );
return quoteStringFromStream<strict, true>( in );
case 'r':
if( strict )
FC_THROW_EXCEPTION( parse_error_exception, "raw strings not supported in strict mode" );
Expand All @@ -211,7 +204,9 @@ namespace fc { namespace json_relaxed
case '\'':
if( strict )
FC_THROW_EXCEPTION( parse_error_exception, "raw strings not supported in strict mode" );
return quoteStringFromStream<T, strict, false>( in );
return quoteStringFromStream<strict, false>( in );
case EOF:
if( in.eof() ) FC_THROW_EXCEPTION( eof_exception, "unexpected end of file" );
default:
if( strict )
FC_THROW_EXCEPTION( parse_error_exception, "unquoted strings not supported in strict mode" );
Expand Down Expand Up @@ -569,23 +564,29 @@ namespace fc { namespace json_relaxed
}
} FC_CAPTURE_AND_RETHROW( (token) ) }

template<typename T, bool strict>
variant_object objectFromStream( T& in, uint32_t max_depth )
template<bool strict>
variant_object objectFromStream( std::istream& in, uint32_t max_depth )
{
std::function<std::string(T&)> get_key = []( T& in ){ return json_relaxed::stringFromStream<T, strict>( in ); };
std::function<variant(T&)> get_value = [max_depth]( T& in ){ return json_relaxed::variant_from_stream<T, strict>( in, max_depth ); };
return objectFromStreamBase<T>( in, get_key, get_value );
std::function<std::string(std::istream&)> get_key = []( std::istream& in ){
return json_relaxed::stringFromStream<strict>( in );
};
std::function<variant(std::istream&)> get_value = [max_depth]( std::istream& in ){
return json_relaxed::variant_from_stream<strict>( in, max_depth );
};
return objectFromStreamBase( in, get_key, get_value );
}

template<typename T, bool strict>
variants arrayFromStream( T& in, uint32_t max_depth )
template<bool strict>
variants arrayFromStream( std::istream& in, uint32_t max_depth )
{
std::function<variant(T&)> get_value = [max_depth]( T& in ){ return json_relaxed::variant_from_stream<T, strict>( in, max_depth ); };
return arrayFromStreamBase<T>( in, get_value );
std::function<variant(std::istream&)> get_value = [max_depth]( std::istream& in ){
return json_relaxed::variant_from_stream<strict>( in, max_depth );
};
return arrayFromStreamBase( in, get_value );
}

template<typename T, bool strict>
variant numberFromStream( T& in )
template<bool strict>
variant numberFromStream( std::istream& in )
{ try {
std::string token = tokenFromStream(in);
variant result = json_relaxed::parseNumberOrStr<strict>( token );
Expand All @@ -594,8 +595,8 @@ namespace fc { namespace json_relaxed
return result;
} FC_CAPTURE_AND_RETHROW() }

template<typename T, bool strict>
variant wordFromStream( T& in )
template<bool strict>
variant wordFromStream( std::istream& in )
{
std::string token = tokenFromStream(in);

Expand Down Expand Up @@ -625,8 +626,8 @@ namespace fc { namespace json_relaxed
FC_THROW_EXCEPTION( parse_error_exception, "expected: null|true|false" );
}

template<typename T, bool strict>
variant variant_from_stream( T& in, uint32_t max_depth )
template<bool strict>
variant variant_from_stream( std::istream& in, uint32_t max_depth )
{
if( max_depth == 0 )
FC_THROW_EXCEPTION( parse_error_exception, "Too many nested items in JSON input!" );
Expand All @@ -635,11 +636,11 @@ namespace fc { namespace json_relaxed
switch( c )
{
case '"':
return json_relaxed::stringFromStream<T, strict>( in );
return json_relaxed::stringFromStream<strict>( in );
case '{':
return json_relaxed::objectFromStream<T, strict>( in, max_depth - 1 );
return json_relaxed::objectFromStream<strict>( in, max_depth - 1 );
case '[':
return json_relaxed::arrayFromStream<T, strict>( in, max_depth - 1 );
return json_relaxed::arrayFromStream<strict>( in, max_depth - 1 );
case '-':
case '+':
case '.':
Expand All @@ -653,7 +654,7 @@ namespace fc { namespace json_relaxed
case '7':
case '8':
case '9':
return json_relaxed::numberFromStream<T, strict>( in );
return json_relaxed::numberFromStream<strict>( in );
// null, true, false, or 'warning' / string
case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': case 'h':
case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': case 'o': case 'p':
Expand All @@ -664,7 +665,7 @@ namespace fc { namespace json_relaxed
case 'Q': case 'R': case 'S': case 'T': case 'U': case 'V': case 'W': case 'X':
case 'Y': case 'Z':
case '_': case '/':
return json_relaxed::wordFromStream<T, strict>( in );
return json_relaxed::wordFromStream<strict>( in );
case 0x04: // ^D end of transmission
case EOF:
FC_THROW_EXCEPTION( eof_exception, "unexpected end of file" );
Expand Down
11 changes: 7 additions & 4 deletions src/crypto/aes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
#include <fc/exception/exception.hpp>
#include <fc/fwd_impl.hpp>

#include <fc/io/fstream.hpp>

#include <fc/log/logger.hpp>

#include <fc/thread/thread.hpp>
Expand All @@ -17,6 +15,9 @@
#endif
#include <openssl/crypto.h>

#include <fstream>
#include <ios>

#if defined(_WIN32)
# include <windows.h>
#endif
Expand Down Expand Up @@ -358,7 +359,8 @@ void aes_save( const fc::path& file, const fc::sha512& key, std::ve
fc::raw::pack( check_enc, cipher );
auto check = check_enc.result();

fc::ofstream out(file);
std::ofstream out( file.string(), std::ios_base::binary );
FC_ASSERT( !out.fail() && !out.bad(), "Failed to open file '${f}'", ("f",file.string()) );
fc::raw::pack( out, check );
fc::raw::pack( out, cipher );
} FC_RETHROW_EXCEPTIONS( warn, "", ("file",file) ) }
Expand All @@ -370,7 +372,8 @@ std::vector<char> aes_load( const fc::path& file, const fc::sha512& key )
{ try {
FC_ASSERT( fc::exists( file ) );

fc::ifstream in( file, fc::ifstream::binary );
std::ifstream in( file.string(), std::ios_base::binary );
FC_ASSERT( !in.fail() && !in.bad(), "Failed to open file '${f}'", ("f",file.string()) );
fc::sha512 check;
std::vector<char> cipher;

Expand Down
6 changes: 4 additions & 2 deletions src/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
#include <fc/filesystem.hpp>
#include <fc/exception/exception.hpp>
#include <fc/fwd_impl.hpp>
#include <fc/io/fstream.hpp>

#include <fc/utf8.hpp>
#include <fc/variant.hpp>

#include <boost/config.hpp>
#include <boost/filesystem.hpp>

#include <fstream>

#ifdef _WIN32
# include <windows.h>
# include <userenv.h>
Expand Down Expand Up @@ -370,7 +371,8 @@ namespace fc {
}
if (create)
{
fc::ofstream ofs(*_path, std::ios_base::out | std::ios_base::binary);
std::ofstream ofs( _path->string(), std::ios_base::binary);
FC_ASSERT( !ofs.fail() && !ofs.bad(), "Failed to open file '${f}'", ("f",_path->string()) );
ofs.close();
}
}
Expand Down
Loading