updates and fixes

This commit is contained in:
Daniel Larimer 2012-10-21 20:54:52 -04:00
parent 3d56a96d4e
commit f3680c3183
5 changed files with 51 additions and 27 deletions

View file

@ -52,7 +52,6 @@ namespace fc {
}; };
class iostream : public virtual ostream, public virtual istream {}; class iostream : public virtual ostream, public virtual istream {};
fc::istream& getline( fc::istream&, fc::string&, char delim = '\n' );
struct cout_t : virtual public ostream { struct cout_t : virtual public ostream {
virtual ostream& write( const char* buf, size_t len ); virtual ostream& write( const char* buf, size_t len );
@ -89,6 +88,8 @@ namespace fc {
virtual istream& read( char& ); virtual istream& read( char& );
virtual istream& read( fc::string& ); 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; extern cout_t cout;

View file

@ -16,6 +16,8 @@ namespace fc {
size_t readsome( char* buffer, size_t max ); size_t readsome( char* buffer, size_t max );
size_t read( char* buffer, size_t s ); size_t read( char* buffer, size_t s );
bool is_open()const;
void flush(); void flush();
private: private:

View file

@ -8,24 +8,37 @@
namespace fc { namespace fc {
fc::thread& cin_thread() { static fc::thread i("cin"); return i; } 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; fc::stringstream ss;
char c; char c;
if( i.readsome( &c, 1 ) != 1 ) { i.read( &c, 1 );
cin_thread().async([&](){ i.read(&c,1); } ).wait();
}
while( !i.eof() ) { while( !i.eof() ) {
if( c == delim ) { s = ss.str(); return i; } if( c == delim ) { s = ss.str(); return i; }
ss.write(&c,1); ss.write(&c,1);
i.read( &c, 1 );
if( i.readsome( &c, 1 ) != 1 ) {
cin_thread().async([&](){ i.read(&c,1); } ).wait();
}
} }
s = ss.str(); s = ss.str();
return i; 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; } ostream& cout_t::write( const char* buf, size_t len ) { std::cout.write(buf,len); return *this; }
void cout_t::close() {} void cout_t::close() {}
void cout_t::flush() { std::cout.flush(); } void cout_t::flush() { std::cout.flush(); }
@ -42,24 +55,28 @@ namespace fc {
return std::cin.readsome(buf,len); return std::cin.readsome(buf,len);
} }
istream& cin_t::read( char* buf, size_t 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); std::cin.read(buf,len);
return *this; return *this;
} }
bool cin_t::eof()const { return std::cin.eof(); } 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( int64_t& v) { slog(""); std::cin >> v; return *this; }
istream& cin_t::read( uint64_t& v) { 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) { 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) { 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) { 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) { 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) { 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) { std::cin >> v; return *this; } istream& cin_t::read( uint8_t& v) { slog(""); std::cin >> v; return *this; }
istream& cin_t::read( float& v) { std::cin >> v; return *this; } istream& cin_t::read( float& v) { slog(""); std::cin >> v; return *this; }
istream& cin_t::read( double& v) { std::cin >> v; return *this; } istream& cin_t::read( double& v) { slog(""); std::cin >> v; return *this; }
istream& cin_t::read( bool& v) { std::cin >> v; return *this; } istream& cin_t::read( bool& v) { slog(""); std::cin >> v; return *this; }
istream& cin_t::read( char& v) { std::cin >> v; return *this; } istream& cin_t::read( char& v) { slog(""); std::cin >> v; return *this; }
istream& cin_t::read( fc::string& v) { std::cin >> *reinterpret_cast<std::string*>(&v); return *this; } istream& cin_t::read( fc::string& v) { slog(""); std::cin >> *reinterpret_cast<std::string*>(&v); return *this; }
cout_t cout; cout_t cout;

View file

@ -1,5 +1,6 @@
#include <fc/sstream.hpp> #include <fc/sstream.hpp>
#include <fc/fwd_impl.hpp> #include <fc/fwd_impl.hpp>
#include <fc/log.hpp>
#include <sstream> #include <sstream>
namespace fc { namespace fc {
@ -32,14 +33,16 @@ namespace fc {
return *this; return *this;
} }
size_t stringstream::readsome( char* buf, size_t len ) { size_t stringstream::readsome( char* buf, size_t len ) {
slog("");
return my->ss.readsome(buf,len); return my->ss.readsome(buf,len);
} }
istream& stringstream::read( char* buf, size_t len ) { istream& stringstream::read( char* buf, size_t len ) {
slog("");
my->ss.read(buf,len); my->ss.read(buf,len);
return *this; return *this;
} }
void stringstream::close(){}; void stringstream::close(){ my->ss.flush(); };
void stringstream::flush(){}; void stringstream::flush(){ my->ss.flush(); };
istream& stringstream::read( int64_t& v ) { my->ss >> v; return *this; } istream& stringstream::read( int64_t& v ) { my->ss >> v; return *this; }
istream& stringstream::read( uint64_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<std::string*>(&v); return *this; } istream& stringstream::read( fc::string& v ) { my->ss >> *reinterpret_cast<std::string*>(&v); return *this; }
ostream& stringstream::write( const fc::string& s) { ostream& stringstream::write( const fc::string& s) {
my->ss << s.c_str(); my->ss.write( s.c_str(), s.size() );
return *this; return *this;
} }

View file

@ -16,6 +16,9 @@ namespace fc {
boost::asio::ip::tcp::socket _sock; boost::asio::ip::tcp::socket _sock;
}; };
bool tcp_socket::is_open()const {
return my->_sock.is_open();
}
tcp_socket::tcp_socket(){} tcp_socket::tcp_socket(){}
@ -26,14 +29,12 @@ namespace fc {
boost::system::error_code ec; boost::system::error_code ec;
size_t w = my->_sock.write_some( boost::asio::buffer( buf, len ), ec ); size_t w = my->_sock.write_some( boost::asio::buffer( buf, len ), ec );
slog( "wrote %d ", w );
if( w < len ) { if( w < len ) {
buf += w; buf += w;
len -= w; len -= w;
} }
if( ec == boost::asio::error::would_block ) { if( ec == boost::asio::error::would_block ) {
wlog( "world block" );
promise<size_t>::ptr p(new promise<size_t>("tcp_socket::write")); promise<size_t>::ptr p(new promise<size_t>("tcp_socket::write"));
boost::asio::async_write( my->_sock, boost::asio::buffer(buf, len), boost::asio::async_write( my->_sock, boost::asio::buffer(buf, len),
[=]( const boost::system::error_code& ec, size_t bt ) { [=]( const boost::system::error_code& ec, size_t bt ) {