diff --git a/include/fc/exception.hpp b/include/fc/exception.hpp index 77514bf..65f4752 100644 --- a/include/fc/exception.hpp +++ b/include/fc/exception.hpp @@ -88,6 +88,7 @@ namespace fc { throw_exception_( func, file, line, msg, to_string(fc::forward(a1) ), to_string( fc::forward(a2) ), to_string( fc::forward(a3) ) ); } + fc::string except_str(); } // namespace fc #define FC_THROW(X) throw X diff --git a/include/fc/filesystem.hpp b/include/fc/filesystem.hpp index ad659f1..1c33a38 100644 --- a/include/fc/filesystem.hpp +++ b/include/fc/filesystem.hpp @@ -6,6 +6,7 @@ namespace boost { namespace filesystem { class path; + class directory_iterator; } } @@ -31,15 +32,34 @@ namespace fc { operator boost::filesystem::path& (); operator const boost::filesystem::path& ()const; + fc::path stem()const; + fc::path extension()const; fc::path filename()const; + fc::path parent_path()const; fc::string string()const; private: fwd _p; }; + class directory_iterator { + public: + directory_iterator( const fc::path& p ); + directory_iterator(); + ~directory_iterator(); + + fc::path operator*()const; + directory_iterator& operator++(int); + directory_iterator& operator++(); + + friend bool operator==( const directory_iterator&, const directory_iterator& ); + friend bool operator!=( const directory_iterator&, const directory_iterator& ); + private: + fwd _p; + }; + bool exists( const path& p ); bool is_directory( const path& p ); - bool is_regular( const path& p ); + bool is_regular_file( const path& p ); void create_directories( const path& p ); uint64_t file_size( const path& p ); } diff --git a/include/fc/stream.hpp b/include/fc/stream.hpp index c4584fb..1235384 100644 --- a/include/fc/stream.hpp +++ b/include/fc/stream.hpp @@ -196,6 +196,7 @@ namespace fc { friend stringstream& operator<<( stringstream&, const bool& ); friend stringstream& operator<<( stringstream&, const char& ); friend stringstream& operator<<( stringstream&, const fc::string& ); + friend stringstream& operator<<( stringstream&, const char* ); private: class impl; fwd my; diff --git a/include/fc/string.hpp b/include/fc/string.hpp index d9e214c..4ad1b3c 100644 --- a/include/fc/string.hpp +++ b/include/fc/string.hpp @@ -56,6 +56,8 @@ namespace fc { friend string operator + ( const string&, const string& ); friend string operator + ( const string&, char c ); + + fc::string substr( int32_t start, int32_t len = 0x7fffffff ); private: void* my; diff --git a/include/fc/tcp_socket.hpp b/include/fc/tcp_socket.hpp index ad4bcf8..e90d555 100644 --- a/include/fc/tcp_socket.hpp +++ b/include/fc/tcp_socket.hpp @@ -16,6 +16,8 @@ namespace fc { size_t readsome( char* buffer, size_t max ); size_t read( char* buffer, size_t s ); + void flush(); + private: friend class tcp_server; class impl; @@ -24,11 +26,11 @@ namespace fc { class tcp_server { public: - tcp_server(); + tcp_server(uint16_t port=0); ~tcp_server(); bool accept( tcp_socket& s ); - void listen( uint16_t port ); +// void listen( uint16_t port ); private: class impl; diff --git a/src/asio.cpp b/src/asio.cpp index 9a2e0c2..90cbc14 100644 --- a/src/asio.cpp +++ b/src/asio.cpp @@ -1,5 +1,6 @@ #include #include +#include #include namespace fc { diff --git a/src/base58.cpp b/src/base58.cpp index 3d27f1c..724625b 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -534,6 +535,7 @@ inline std::string EncodeBase58(const unsigned char* pbegin, const unsigned char // Convert little endian std::string to big endian reverse(str.begin(), str.end()); +// slog( "Encode '%s'", str.c_str() ); return str; } @@ -563,8 +565,10 @@ inline bool DecodeBase58(const char* psz, std::vector& vchRet) { while (isspace(*p)) p++; - if (*p != '\0') + if (*p != '\0') { + slog( "%s '%c'", pszBase58,*p ); return false; + } break; } bnChar.setulong(p1 - pszBase58); @@ -609,6 +613,7 @@ fc::string to_base58( const char* d, uint32_t s ) { * @return the number of bytes decoded */ size_t from_base58( const fc::string& base58_str, char* out_data, size_t out_data_len ) { + //slog( "%s", base58_str.c_str() ); std::vector out; if( !DecodeBase58( base58_str.c_str(), out ) ) { FC_THROW_MSG( "Unable to decode base58 string '%s'", base58_str ); diff --git a/src/exception.cpp b/src/exception.cpp index 59fb3b8..332f463 100644 --- a/src/exception.cpp +++ b/src/exception.cpp @@ -102,5 +102,7 @@ namespace fc { const fc::string& a1, const fc::string& a2, const fc::string& a3, const fc::string& a4 ) { ::boost::exception_detail::throw_exception_(fc::generic_exception(msg),func, file, line ); } + fc::string except_str() { return fc::current_exception().diagnostic_information(); } } + diff --git a/src/filesystem.cpp b/src/filesystem.cpp index ab6fb29..385dfaf 100644 --- a/src/filesystem.cpp +++ b/src/filesystem.cpp @@ -56,11 +56,20 @@ namespace fc { fc::path path::filename()const { return _p->filename(); } + fc::path path::extension()const { + return _p->extension(); + } + fc::path path::stem()const { + return _p->stem(); + } + fc::path path::parent_path()const { + return _p->parent_path(); + } bool exists( const path& p ) { return boost::filesystem::exists(p); } void create_directories( const path& p ) { boost::filesystem::create_directories(p); } bool is_directory( const path& p ) { return boost::filesystem::is_directory(p); } - bool is_regular( const path& p ) { return boost::filesystem::is_regular(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); } } diff --git a/src/stream.cpp b/src/stream.cpp index 73710bc..bba12f1 100644 --- a/src/stream.cpp +++ b/src/stream.cpp @@ -233,16 +233,81 @@ namespace fc { public: impl( fc::string&s ) :ss( reinterpret_cast(s) ) - { - } + { } + impl(){} + std::stringstream ss; }; stringstream::stringstream( fc::string& s ) :my(s) { } + stringstream::stringstream(){} stringstream::~stringstream(){} + stringstream& operator<<( stringstream& s, const int64_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const uint64_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const int32_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const uint32_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const int16_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const uint16_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const int8_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const uint8_t& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const float& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const double& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const bool& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const char& v ){ + s.my->ss << v; + return s; + } + stringstream& operator<<( stringstream& s, const char* cs ) { + s.my->ss << cs; + return s; + } + stringstream& operator<<( stringstream& s, const fc::string& v ){ + s.my->ss <(v); + return s; + } + + fc::string stringstream::str(){ + //std::string st = my->ss.str(); + return my->ss.str().c_str();//*reinterpret_cast(&st); + } + + stringstream& operator>>( stringstream& s, int64_t& v ){ s.my->ss >> v; return s; diff --git a/src/string.cpp b/src/string.cpp index c5bbaf5..bb616d7 100644 --- a/src/string.cpp +++ b/src/string.cpp @@ -64,6 +64,10 @@ namespace fc { void string::clear() { return reinterpret_cast(this)->clear(); } void string::resize( uint64_t s ) { reinterpret_cast(this)->resize(s); } + + fc::string string::substr( int32_t start, int32_t len ) { + return reinterpret_cast(*this).substr(start,len).c_str(); + } const char* string::c_str()const { return reinterpret_cast(this)->c_str(); } bool string::operator == ( const char* s )const { diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 058fcd8..07abe9a 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include namespace fc { @@ -9,7 +10,7 @@ namespace fc { public: impl():_sock( fc::asio::default_io_service() ){} ~impl(){ - _sock.cancel(); + _sock.close(); } boost::asio::ip::tcp::socket _sock; @@ -24,12 +25,14 @@ namespace fc { boost::system::error_code ec; size_t w = my->_sock.write_some( boost::asio::buffer( buf, len ), ec ); + slog( "wrote %d ", w ); if( w < len ) { buf += w; len -= w; - } + } if( ec == boost::asio::error::would_block ) { + wlog( "world block" ); promise::ptr p(new promise("tcp_socket::write")); boost::asio::async_write( my->_sock, boost::asio::buffer(buf, len), [=]( const boost::system::error_code& ec, size_t bt ) { @@ -38,6 +41,7 @@ namespace fc { }); p->wait(); } else if( ec ) { + wlog( "throw" ); throw boost::system::system_error(ec); } } @@ -67,7 +71,7 @@ namespace fc { class tcp_server::impl { public: - impl():_accept( fc::asio::default_io_service() ){} + impl(uint16_t port):_accept( fc::asio::default_io_service(), boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port) ){} ~impl(){ _accept.cancel(); } @@ -75,26 +79,38 @@ namespace fc { boost::asio::ip::tcp::acceptor _accept; }; - tcp_server::tcp_server() { + tcp_server::tcp_server(uint16_t port) + :my(port) { } tcp_server::~tcp_server() { } bool tcp_server::accept( tcp_socket& s ) { - fc::promise::ptr p( - new promise("mace::cmt::asio::tcp::accept") ); - my->_accept.async_accept( s.my->_sock, - [=]( const boost::system::error_code& e ) { - p->set_value(e); - } ); + slog( "accept!" ); + fc::promise::ptr p( new promise("mace::cmt::asio::tcp::accept") ); + slog( "." ); + my->_accept.async_accept( s.my->_sock, [=]( const boost::system::error_code& e ) { + slog( "\aaccept!" ); + p->set_value(e); + } ); + slog( "." ); auto ec = p->wait(); + slog( "." ); if( !ec ) s.my->_sock.non_blocking(true); if( ec ) BOOST_THROW_EXCEPTION( boost::system::system_error(ec) ); return true; } + #if 0 void tcp_server::listen( uint16_t port ) { + /* + slog( "listen %d!", port ); + my->_accept.bind( + slog( "listen %d!", port ); my->_accept.listen(port); + slog( "listen %d!", port ); + */ } + #endif } // namespace fc