-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace std::stringstream to fc_light::stringstream
- Loading branch information
1 parent
4956838
commit 79377c2
Showing
7 changed files
with
355 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#pragma once | ||
#include <fc_light/utility.hpp> | ||
#include <fc_light/string.hpp> | ||
#include <memory> | ||
|
||
namespace fc_light { | ||
|
||
/** | ||
* Provides a fc::thread friendly cooperatively multi-tasked stream that | ||
* will block 'cooperatively' instead of hard blocking. | ||
*/ | ||
class istream | ||
{ | ||
public: | ||
virtual ~istream(){}; | ||
|
||
/** read at least 1 byte or throw, if no data is available | ||
* this method should block cooperatively until data is | ||
* available or fc::eof is thrown. | ||
* | ||
* @throws fc::eof if at least 1 byte cannot be read | ||
**/ | ||
virtual size_t readsome( char* buf, size_t len ) = 0; | ||
virtual size_t readsome( const std::shared_ptr<char>& buf, size_t len, size_t offset ) = 0; | ||
|
||
/** read len bytes or throw, this method is implemented | ||
* in terms of readsome. | ||
* | ||
* @throws fc::eof_exception if len bytes cannot be read | ||
**/ | ||
istream& read( char* buf, size_t len ); | ||
istream& read( const std::shared_ptr<char>& buf, size_t len, size_t offset = 0 ); | ||
virtual char get(); | ||
void get( char& c ) { c = get(); } | ||
}; | ||
typedef std::shared_ptr<istream> istream_ptr; | ||
|
||
/** | ||
* Provides a fc::thread friendly cooperatively multi-tasked stream that | ||
* will block 'cooperatively' instead of hard blocking. | ||
*/ | ||
class ostream | ||
{ | ||
public: | ||
virtual ~ostream(){}; | ||
virtual size_t writesome( const char* buf, size_t len ) = 0; | ||
virtual size_t writesome( const std::shared_ptr<const char>& buf, size_t len, size_t offset ) = 0; | ||
virtual void close() = 0; | ||
virtual void flush() = 0; | ||
|
||
void put( char c ) { write(&c,1); } | ||
|
||
/** implemented in terms of writesome, guarantees len bytes are sent | ||
* but not flushed. | ||
**/ | ||
ostream& write( const char* buf, size_t len ); | ||
ostream& write( const std::shared_ptr<const char>& buf, size_t len, size_t offset = 0 ); | ||
}; | ||
|
||
typedef std::shared_ptr<ostream> ostream_ptr; | ||
|
||
class iostream : public virtual ostream, public virtual istream {}; | ||
|
||
// fc_light::istream& getline( fc_light::istream&, fc_light::string&, char delim = '\n' ); | ||
|
||
template<size_t N> | ||
ostream& operator<<( ostream& o, char (&array)[N] ) | ||
{ | ||
return o.write( array, N ); | ||
} | ||
|
||
ostream& operator<<( ostream& o, char ); | ||
ostream& operator<<( ostream& o, const char* v ); | ||
ostream& operator<<( ostream& o, const std::string& v ); | ||
ostream& operator<<( ostream& o, const fc_light::string& v ); | ||
ostream& operator<<( ostream& o, const double& v ); | ||
ostream& operator<<( ostream& o, const float& v ); | ||
ostream& operator<<( ostream& o, const int64_t& v ); | ||
ostream& operator<<( ostream& o, const uint64_t& v ); | ||
ostream& operator<<( ostream& o, const int32_t& v ); | ||
ostream& operator<<( ostream& o, const uint32_t& v ); | ||
ostream& operator<<( ostream& o, const int16_t& v ); | ||
ostream& operator<<( ostream& o, const uint16_t& v ); | ||
ostream& operator<<( ostream& o, const int8_t& v ); | ||
ostream& operator<<( ostream& o, const uint8_t& v ); | ||
#ifndef _MSC_VER | ||
ostream& operator<<( ostream& o, const size_t& v ); | ||
#endif | ||
|
||
istream& operator>>( istream& o, std::string& v ); | ||
istream& operator>>( istream& o, fc_light::string& v ); | ||
istream& operator>>( istream& o, char& v ); | ||
istream& operator>>( istream& o, double& v ); | ||
istream& operator>>( istream& o, float& v ); | ||
istream& operator>>( istream& o, int64_t& v ); | ||
istream& operator>>( istream& o, uint64_t& v ); | ||
istream& operator>>( istream& o, int32_t& v ); | ||
istream& operator>>( istream& o, uint32_t& v ); | ||
istream& operator>>( istream& o, int16_t& v ); | ||
istream& operator>>( istream& o, uint16_t& v ); | ||
istream& operator>>( istream& o, int8_t& v ); | ||
istream& operator>>( istream& o, uint8_t& v ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
#include <fc_light/io/iostream.hpp> | ||
#include <fc_light/io/sstream.hpp> | ||
#include <iostream> | ||
#include <string.h> | ||
#include <string> | ||
#include <boost/lexical_cast.hpp> | ||
#include <boost/thread/mutex.hpp> | ||
//#include <fc_light/io/stdio.hpp> | ||
#include <fc_light/log/logger.hpp> | ||
|
||
namespace fc_light { | ||
|
||
ostream& operator<<( ostream& o, const char v ) | ||
{ | ||
o.write( &v, 1 ); | ||
return o; | ||
} | ||
ostream& operator<<( ostream& o, const char* v ) | ||
{ | ||
o.write( v, strlen(v) ); | ||
return o; | ||
} | ||
|
||
ostream& operator<<( ostream& o, const std::string& v ) | ||
{ | ||
o.write( v.c_str(), v.size() ); | ||
return o; | ||
} | ||
#ifdef USE_FC_STRING | ||
ostream& operator<<( ostream& o, const fc::string& v ) | ||
{ | ||
o.write( v.c_str(), v.size() ); | ||
return o; | ||
} | ||
#endif | ||
|
||
ostream& operator<<( ostream& o, const double& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const float& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const int64_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const uint64_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const int32_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const uint32_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const int16_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const uint16_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const int8_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
ostream& operator<<( ostream& o, const uint8_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
#ifdef __APPLE__ | ||
ostream& operator<<( ostream& o, const size_t& v ) | ||
{ | ||
return o << boost::lexical_cast<std::string>(v).c_str(); | ||
} | ||
|
||
#endif | ||
|
||
istream& operator>>( istream& o, std::string& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
#ifdef USE_FC_STRING | ||
istream& operator>>( istream& o, fc::string& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
#endif | ||
|
||
istream& operator>>( istream& o, char& v ) | ||
{ | ||
o.read(&v,1); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, double& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, float& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, int64_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, uint64_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, int32_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, uint32_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, int16_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, uint16_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, int8_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
istream& operator>>( istream& o, uint8_t& v ) | ||
{ | ||
assert(false && "not implemented"); | ||
return o; | ||
} | ||
|
||
|
||
char istream::get() | ||
{ | ||
char tmp; | ||
read(&tmp,1); | ||
return tmp; | ||
} | ||
|
||
istream& istream::read( char* buf, size_t len ) | ||
{ | ||
char* pos = buf; | ||
while( size_t(pos-buf) < len ) | ||
pos += readsome( pos, len - (pos - buf) ); | ||
return *this; | ||
} | ||
|
||
istream& istream::read( const std::shared_ptr<char>& buf, size_t len, size_t offset ) | ||
{ | ||
size_t bytes_read = 0; | ||
while( bytes_read < len ) | ||
bytes_read += readsome(buf, len - bytes_read, bytes_read + offset); | ||
return *this; | ||
} | ||
|
||
ostream& ostream::write( const char* buf, size_t len ) | ||
{ | ||
const char* pos = buf; | ||
while( size_t(pos-buf) < len ) | ||
pos += writesome( pos, len - (pos - buf) ); | ||
return *this; | ||
} | ||
|
||
ostream& ostream::write( const std::shared_ptr<const char>& buf, size_t len, size_t offset ) | ||
{ | ||
size_t bytes_written = 0; | ||
while( bytes_written < len ) | ||
bytes_written += writesome(buf, len - bytes_written, bytes_written + offset); | ||
return *this; | ||
} | ||
|
||
} // namespace fc |
Oops, something went wrong.