From 6bf25ae8e75df986140e4f3d1347dd8f62d2144b Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 15 Nov 2012 11:55:36 -0500 Subject: [PATCH 1/3] fix json_rpc_errorobj --- include/fc/filesystem.hpp | 2 ++ include/fc/fstream.hpp | 15 ++++++++------- include/fc/json_rpc_error_object.hpp | 3 +++ include/fc/sha1.hpp | 2 ++ include/fc/value.hpp | 8 ++++++++ src/filesystem.cpp | 2 ++ src/json_rpc_error_object.cpp | 2 +- src/value.cpp | 19 ++++++++++++------- 8 files changed, 38 insertions(+), 15 deletions(-) diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index b7e5235..551dc42 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -64,6 +64,8 @@ namespace fc { void create_directories( const path& p ); path canonical( const path& p ); uint64_t file_size( const path& p ); + bool remove( const path& p ); + void copy( const path& from, const path& to ); } #endif // _FC_FILESYSTEM_HPP_ diff --git a/include/fc/fstream.hpp b/include/fc/fstream.hpp index 349d6f9..890d637 100644 --- a/include/fc/fstream.hpp +++ b/include/fc/fstream.hpp @@ -1,16 +1,17 @@ #pragma once #include +#include namespace fc { - - class ofstream //: virtual public ostream { + class path; + class ofstream : virtual public ostream { public: enum mode { out, binary }; ofstream(); - ofstream( const fc::string& file, int m ); + ofstream( const fc::path& file, int m = binary ); ~ofstream(); - void open( const fc::string& file, int m ); + void open( const fc::path& file, int m = binary ); ofstream& write( const char* buf, size_t len ); void put( char c ); void close(); @@ -21,14 +22,14 @@ namespace fc { fwd my; }; - class ifstream //: virtual public istream { + class ifstream : virtual public istream { public: enum mode { in, binary }; ifstream(); - ifstream( const fc::string& file, int m ); + ifstream( const fc::path& file, int m ); ~ifstream(); - void open( const fc::string& file, int m ); + void open( const fc::path& file, int m ); ifstream& read( char* buf, size_t len ); void close(); void flush(); diff --git a/include/fc/json_rpc_error_object.hpp b/include/fc/json_rpc_error_object.hpp index 656c150..6c6e999 100644 --- a/include/fc/json_rpc_error_object.hpp +++ b/include/fc/json_rpc_error_object.hpp @@ -1,4 +1,7 @@ #pragma once +#include +#include +#include namespace fc { namespace json { diff --git a/include/fc/sha1.hpp b/include/fc/sha1.hpp index cef850f..7d95e5b 100644 --- a/include/fc/sha1.hpp +++ b/include/fc/sha1.hpp @@ -4,6 +4,7 @@ #include namespace fc { + class path; class sha1 { public: @@ -18,6 +19,7 @@ namespace fc { static sha1 hash( const char* d, uint32_t dlen ); static sha1 hash( const fc::string& ); + static sha1 hash( const fc::path& ); template static sha1 hash( const T& t ) { sha1::encoder e; e << t; return e.result(); } diff --git a/include/fc/value.hpp b/include/fc/value.hpp index 11d1354..120d9f8 100644 --- a/include/fc/value.hpp +++ b/include/fc/value.hpp @@ -127,6 +127,14 @@ namespace fc { bool is_null()const; void visit( const_visitor&& v )const; + + /* sets the subkey key with v and return *this */ + value& set( const char* key, fc::value v ); + value& set( const fc::string& key, fc::value v ); + + template + value& set( S&& key, T&& v ) { return set( fc::forward(key), fc::value( fc::forward(v) ) ); } + private: /** throws exceptions on errors * diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 10236b8..cbd9983 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -75,4 +75,6 @@ namespace fc { bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); } bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); } uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); } + void copy( const path& f, const path& t ) { boost::filesystem::copy( f, t ); } + void remove( const path& f ) { boost::filesystem::remove( f, t ); } } diff --git a/src/json_rpc_error_object.cpp b/src/json_rpc_error_object.cpp index 3fc186d..944d5f1 100644 --- a/src/json_rpc_error_object.cpp +++ b/src/json_rpc_error_object.cpp @@ -1,6 +1,6 @@ #include namespace fc { namespace json { - error_object::error_object( const fc::string& msg, fc::value v, int c ) + error_object::error_object( const fc::string& m, fc::value v, int64_t c ) :code(c),message(m),data(fc::move(v)){} }} diff --git a/src/value.cpp b/src/value.cpp index a1c8a18..892ca12 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -48,6 +48,7 @@ namespace fc { struct value_holder_impl : value_holder { value_holder_impl(){}; // typedef void_t T; + /* virtual const char* type()const { return "void"; } virtual void visit( value::const_visitor&& v )const{ v(); } virtual void visit( value_visitor&& v ) { v(); } @@ -56,6 +57,7 @@ namespace fc { virtual value_holder* move_helper( char* c ){ return new(c) value_holder_impl(); } virtual value_holder* copy_helper( char* c )const{ return new(c) value_holder_impl();} + */ }; @@ -121,7 +123,10 @@ namespace fc { }; value_holder::~value_holder(){} - const char* value_holder::type()const { return "null"; } + const char* value_holder::type()const { return "void"; } + value_holder* value_holder::move_helper( char* c ) { return new(c) value_holder(); } + value_holder* value_holder::copy_helper( char* c )const { return new(c) value_holder(); } + void value_holder::visit( value::const_visitor&& v )const { v(); } void value_holder::visit( value_visitor&& v ) { v(); } @@ -133,8 +138,8 @@ namespace fc { const value& value_holder::at( size_t )const { FC_THROW_MSG("value type '%s' not an array", type()); return *((const value*)0); } void value_holder::push_back( value&& v ) { FC_THROW_MSG("value type '%s' not an array", type()); } - value_holder* value_holder::move_helper( char* c ) { return new(c) value_holder(); } - value_holder* value_holder::copy_helper( char* c )const{ return new(c) value_holder(); } + // value_holder* value_holder::move_helper( char* c ) = 0; + // value_holder* value_holder::copy_helper( char* c )const = 0; void value_holder_impl::resize( size_t s ) { val.fields.resize(s); } void value_holder_impl::reserve( size_t s ) { val.fields.reserve(s); } @@ -168,7 +173,7 @@ static const detail::value_holder* gh( const aligned<24>& h ) { } value::value() { - new (holder) detail::value_holder(); + new (holder) detail::value_holder_impl(); } value::value( value&& m ) { gh(m.holder)->move_helper(holder._store._data); @@ -180,7 +185,7 @@ value::value( char* c ) { new (holder) detail::value_holder_impl( c ); } value::~value() { - gh(holder)->~value_holder(); + gh(holder)->~value_holder_impl(); } value::value( int8_t v){ static_assert( sizeof(holder) >= sizeof( detail::value_holder_impl ), "size check" ); @@ -271,7 +276,7 @@ value& value::operator=( const value& v ){ return *this; } bool value::is_null()const { - return strcmp(gh(holder)->type(), "null") == 0; + return strcmp(gh(holder)->type(), "void") == 0; } @@ -314,7 +319,7 @@ value& value::operator[]( const char* key ) { } o->val.fields.push_back( key_val(key) ); return o->val.fields.back().val; - } else if (strcmp(gh(holder)->type(), "null" ) == 0 ) { + } else if (strcmp(gh(holder)->type(), "void" ) == 0 ) { new (holder) detail::value_holder_impl(value::object()); return (*this)[key]; } From 53bb959c5d3d5351f7058f7694a920f045836222 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 15 Nov 2012 12:08:31 -0500 Subject: [PATCH 2/3] expanded filesystem api + value api --- src/filesystem.cpp | 2 +- src/value.cpp | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/filesystem.cpp b/src/filesystem.cpp index cbd9983..8d3ffc6 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -76,5 +76,5 @@ namespace fc { bool is_regular_file( const path& p ) { return boost::filesystem::is_regular_file(p); } uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); } void copy( const path& f, const path& t ) { boost::filesystem::copy( f, t ); } - void remove( const path& f ) { boost::filesystem::remove( f, t ); } + bool remove( const path& f ) { return boost::filesystem::remove( f ); } } diff --git a/src/value.cpp b/src/value.cpp index 892ca12..c29ac2a 100644 --- a/src/value.cpp +++ b/src/value.cpp @@ -185,7 +185,7 @@ value::value( char* c ) { new (holder) detail::value_holder_impl( c ); } value::~value() { - gh(holder)->~value_holder_impl(); + gh(holder)->~value_holder(); } value::value( int8_t v){ static_assert( sizeof(holder) >= sizeof( detail::value_holder_impl ), "size check" ); @@ -364,5 +364,14 @@ void value::visit( value::const_visitor&& v )const { auto h = ((detail::value_holder*)&holder[0]); h->visit( fc::move(v) ); } +/* sets the subkey key with v and return *this */ +value& value::set( const char* key, fc::value v ) { + (*this)[key] = fc::move(v); + return *this; +} +value& value::set( const fc::string& key, fc::value v ) { + (*this)[key.c_str()] = fc::move(v); + return *this; +} } // namepace fc From ba4eb96d8888d5c15c50870a201eb4e7a296eea8 Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Thu, 15 Nov 2012 12:37:56 -0500 Subject: [PATCH 3/3] adding fstream, fix bugs --- CMakeLists.txt | 1 + include/fc/fstream.hpp | 9 ++--- include/fc/json_rpc_connection.hpp | 2 +- src/filesystem.cpp | 1 + src/fstream.cpp | 60 ++++++++++++++++++++++++++++++ src/sha1.cpp | 10 +++++ 6 files changed, 76 insertions(+), 7 deletions(-) create mode 100644 src/fstream.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index a217780..fdf7fc3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -67,6 +67,7 @@ set( sources src/log.cpp src/time.cpp src/iostream.cpp + src/fstream.cpp src/sstream.cpp src/exception.cpp src/thread.cpp diff --git a/include/fc/fstream.hpp b/include/fc/fstream.hpp index 890d637..1704e1e 100644 --- a/include/fc/fstream.hpp +++ b/include/fc/fstream.hpp @@ -1,5 +1,5 @@ #pragma once -#include +#include #include namespace fc { @@ -19,7 +19,7 @@ namespace fc { private: class impl; - fwd my; + fc::shared_ptr my; }; class ifstream : virtual public istream { @@ -32,13 +32,10 @@ namespace fc { void open( const fc::path& file, int m ); ifstream& read( char* buf, size_t len ); void close(); - void flush(); - bool eof()const; - private: class impl; - fwd my; + fc::shared_ptr my; }; } // namespace fc diff --git a/include/fc/json_rpc_connection.hpp b/include/fc/json_rpc_connection.hpp index a2d4ff0..f8369f3 100644 --- a/include/fc/json_rpc_connection.hpp +++ b/include/fc/json_rpc_connection.hpp @@ -115,7 +115,7 @@ namespace fc { namespace json { template void add_interface( const fc::ptr& it ) { - it->template visit( detail::add_method_visitor( it, *this ) ); + it->template visit( detail::add_method_visitor( it, *this ) ); } void add_method( const fc::string& name, const fc::json::rpc_server_method::ptr& func ); diff --git a/src/filesystem.cpp b/src/filesystem.cpp index 8d3ffc6..d56fa0e 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -77,4 +77,5 @@ namespace fc { uint64_t file_size( const path& p ) { return boost::filesystem::file_size(p); } void copy( const path& f, const path& t ) { boost::filesystem::copy( f, t ); } bool remove( const path& f ) { return boost::filesystem::remove( f ); } + fc::path canonical( const fc::path& p ) { return boost::filesystem::canonical(p); } } diff --git a/src/fstream.cpp b/src/fstream.cpp new file mode 100644 index 0000000..3295fdc --- /dev/null +++ b/src/fstream.cpp @@ -0,0 +1,60 @@ +#include +#include +#include + +namespace fc { + class ofstream::impl : public fc::retainable { + public: + std::ofstream ofs; + }; + class ifstream::impl : public fc::retainable { + public: + std::ifstream ifs; + }; + + ofstream::ofstream() + :my( new impl() ){} + + ofstream::ofstream( const fc::path& file, int m ) + :my( new impl() ) { this->open( file, m ); } + ofstream::~ofstream(){} + + void ofstream::open( const fc::path& file, int m ) { + my->ofs.open( file.string().c_str(), std::ios::binary ); + } + ofstream& ofstream::write( const char* buf, size_t len ) { + my->ofs.write(buf,len); + return *this; + } + void ofstream::put( char c ) { + my->ofs.put(c); + } + void ofstream::close() { + my->ofs.close(); + } + void ofstream::flush() { + my->ofs.flush(); + } + + ifstream::ifstream() + :my(new impl()){} + ifstream::ifstream( const fc::path& file, int m ) + :my(new impl()) + { + this->open( file, m ); + } + ifstream::~ifstream(){} + + void ifstream::open( const fc::path& file, int m ) { + my->ifs.open( file.string().c_str(), std::ios::binary ); + } + ifstream& ifstream::read( char* buf, size_t len ) { + my->ifs.read(buf,len); + return *this; + } + void ifstream::close() { return my->ifs.close(); } + + bool ifstream::eof()const { return my->ifs.eof(); } + + +} // namespace fc diff --git a/src/sha1.cpp b/src/sha1.cpp index 613e7e7..74ba66b 100644 --- a/src/sha1.cpp +++ b/src/sha1.cpp @@ -3,6 +3,8 @@ #include #include #include +#include +#include namespace fc { @@ -36,6 +38,14 @@ namespace fc { sha1 sha1::hash( const fc::string& s ) { return hash( s.c_str(), s.size() ); } + sha1 sha1::hash( const fc::path& s ) { + file_mapping fmap( s.string().c_str(), read_only ); + size_t fsize = file_size(s); + mapped_region mr( fmap, fc::read_only, 0, fsize ); + + const char* pos = reinterpret_cast(mr.get_address()); + return hash( pos, fsize ); + } void sha1::encoder::write( const char* d, uint32_t dlen ) { SHA1_Update( &my->ctx, d, dlen);