diff --git a/include/fc/iostream.hpp b/include/fc/iostream.hpp index 68d35ed..0a4165f 100644 --- a/include/fc/iostream.hpp +++ b/include/fc/iostream.hpp @@ -52,7 +52,6 @@ namespace fc { }; class iostream : public virtual ostream, public virtual istream {}; - fc::istream& getline( fc::istream&, fc::string&, char delim = '\n' ); struct cout_t : virtual public ostream { virtual ostream& write( const char* buf, size_t len ); @@ -89,6 +88,8 @@ namespace fc { virtual istream& read( char& ); virtual istream& read( fc::string& ); }; + fc::istream& getline( fc::istream&, fc::string&, char delim = '\n' ); + fc::cin_t& getline( fc::cin_t&, fc::string&, char delim = '\n' ); extern cout_t cout; diff --git a/include/fc/tcp_socket.hpp b/include/fc/tcp_socket.hpp index bb5fddb..dac3206 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 ); + bool is_open()const; + void flush(); private: diff --git a/src/iostream.cpp b/src/iostream.cpp index 77a3900..6494efb 100644 --- a/src/iostream.cpp +++ b/src/iostream.cpp @@ -8,24 +8,37 @@ namespace fc { fc::thread& cin_thread() { static fc::thread i("cin"); return i; } - fc::istream& getline( fc::istream& i, fc::string& s, char delim ) { + fc::cin_t& getline( fc::cin_t& i, fc::string& s, char delim ) { + if( !cin_thread().is_current() ) { + cin_thread().async([&](){ getline(i,s,delim); } ).wait(); + return i; + } fc::stringstream ss; char c; - if( i.readsome( &c, 1 ) != 1 ) { - cin_thread().async([&](){ i.read(&c,1); } ).wait(); - } + i.read( &c, 1 ); while( !i.eof() ) { if( c == delim ) { s = ss.str(); return i; } ss.write(&c,1); - - if( i.readsome( &c, 1 ) != 1 ) { - cin_thread().async([&](){ i.read(&c,1); } ).wait(); - } + i.read( &c, 1 ); } s = ss.str(); return i; } + fc::istream& getline( fc::istream& i, fc::string& s, char delim ) { + fc::stringstream ss; + char c; + i.read( &c, 1 ); + while( !i.eof() ) { + if( c == delim ) { s = ss.str(); return i; } + ss.write(&c,1); + i.read( &c, 1 ); + } + s = ss.str(); + return i; + } + + ostream& cout_t::write( const char* buf, size_t len ) { std::cout.write(buf,len); return *this; } void cout_t::close() {} void cout_t::flush() { std::cout.flush(); } @@ -42,24 +55,28 @@ namespace fc { return std::cin.readsome(buf,len); } istream& cin_t::read( char* buf, size_t len ) { + if( !cin_thread().is_current() ) { + cin_thread().async( [=](){ this->read(buf,len); } ).wait(); + return *this; + } std::cin.read(buf,len); return *this; } bool cin_t::eof()const { return std::cin.eof(); } - istream& cin_t::read( int64_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( uint64_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( int32_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( uint32_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( int16_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( uint16_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( int8_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( uint8_t& v) { std::cin >> v; return *this; } - istream& cin_t::read( float& v) { std::cin >> v; return *this; } - istream& cin_t::read( double& v) { std::cin >> v; return *this; } - istream& cin_t::read( bool& v) { std::cin >> v; return *this; } - istream& cin_t::read( char& v) { std::cin >> v; return *this; } - istream& cin_t::read( fc::string& v) { std::cin >> *reinterpret_cast(&v); return *this; } + istream& cin_t::read( int64_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( uint64_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( int32_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( uint32_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( int16_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( uint16_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( int8_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( uint8_t& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( float& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( double& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( bool& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( char& v) { slog(""); std::cin >> v; return *this; } + istream& cin_t::read( fc::string& v) { slog(""); std::cin >> *reinterpret_cast(&v); return *this; } cout_t cout; diff --git a/src/sstream.cpp b/src/sstream.cpp index f1f7a8b..ad2cc17 100644 --- a/src/sstream.cpp +++ b/src/sstream.cpp @@ -1,5 +1,6 @@ #include #include +#include #include namespace fc { @@ -32,14 +33,16 @@ namespace fc { return *this; } size_t stringstream::readsome( char* buf, size_t len ) { + slog(""); return my->ss.readsome(buf,len); } istream& stringstream::read( char* buf, size_t len ) { + slog(""); my->ss.read(buf,len); return *this; } - void stringstream::close(){}; - void stringstream::flush(){}; + void stringstream::close(){ my->ss.flush(); }; + void stringstream::flush(){ my->ss.flush(); }; istream& stringstream::read( int64_t& v ) { my->ss >> v; return *this; } istream& stringstream::read( uint64_t& v ) { my->ss >> v; return *this; } @@ -56,7 +59,7 @@ namespace fc { istream& stringstream::read( fc::string& v ) { my->ss >> *reinterpret_cast(&v); return *this; } ostream& stringstream::write( const fc::string& s) { - my->ss << s.c_str(); + my->ss.write( s.c_str(), s.size() ); return *this; } diff --git a/src/tcp_socket.cpp b/src/tcp_socket.cpp index 5f645f5..8dec3c4 100644 --- a/src/tcp_socket.cpp +++ b/src/tcp_socket.cpp @@ -16,6 +16,9 @@ namespace fc { boost::asio::ip::tcp::socket _sock; }; + bool tcp_socket::is_open()const { + return my->_sock.is_open(); + } tcp_socket::tcp_socket(){} @@ -26,14 +29,12 @@ 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 ) {