Skip to content

Commit

Permalink
replace std::stringstream to fc_light::stringstream
Browse files Browse the repository at this point in the history
  • Loading branch information
sinev-valentine committed Oct 17, 2018
1 parent 4956838 commit 79377c2
Show file tree
Hide file tree
Showing 7 changed files with 355 additions and 38 deletions.
8 changes: 4 additions & 4 deletions libraries/fc_light/include/fc_light/io/fstream.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#pragma once
#include <fc_light/shared_ptr.hpp>
#include <fc_light/filesystem.hpp>
#include <iostream>
#include <fc_light/io/iostream.hpp>

namespace fc_light {
class path;
class ofstream : virtual public std::ostream {
class ofstream : virtual public ostream {
public:
enum mode { out, binary };
ofstream();
Expand All @@ -24,7 +24,7 @@ class ofstream : virtual public std::ostream {
fc_light::shared_ptr<impl> my;
};

class ifstream : virtual public std::istream {
class ifstream : virtual public istream {
public:
enum mode { in, binary };
enum seekdir { beg, cur, end };
Expand All @@ -38,7 +38,7 @@ class ifstream : virtual public std::istream {
size_t readsome(const std::shared_ptr<char>& buffer, size_t max, size_t offset);
ifstream& read( char* buf, size_t len );
ifstream& seekg( size_t p, seekdir d = beg );
using std::istream::get;
using istream::get;
void get( char& c ) { read( &c, 1 ); }
void close();
bool eof()const;
Expand Down
103 changes: 103 additions & 0 deletions libraries/fc_light/include/fc_light/io/iostream.hpp
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 );
}
8 changes: 4 additions & 4 deletions libraries/fc_light/include/fc_light/io/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ namespace fc_light
legacy_generator = 1
};

static std::ostream& to_stream( std::ostream& out, const fc_light::string&);
static std::ostream& to_stream( std::ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles );
static std::ostream& to_stream( std::ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles );
static std::ostream& to_stream( std::ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const fc_light::string&);
static ostream& to_stream( ostream& out, const variant& v, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const variants& v, output_formatting format = stringify_large_ints_and_doubles );
static ostream& to_stream( ostream& out, const variant_object& v, output_formatting format = stringify_large_ints_and_doubles );

// static variant from_stream( buffered_istream& in, parse_type ptype = legacy_parser );

Expand Down
4 changes: 2 additions & 2 deletions libraries/fc_light/include/fc_light/io/json_relaxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace fc_light { namespace json_relaxed
template<typename T>
fc_light::string tokenFromStream( T& in )
{
std::stringstream token;
fc_light::stringstream token;
try
{
char c = in.peek();
Expand Down Expand Up @@ -82,7 +82,7 @@ namespace fc_light { namespace json_relaxed
template<typename T, bool strict, bool allow_escape>
fc_light::string quoteStringFromStream( T& in )
{
std::stringstream token;
fc_light::stringstream token;
try
{
char q = in.get();
Expand Down
6 changes: 2 additions & 4 deletions libraries/fc_light/include/fc_light/io/sstream.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#pragma once
#include <iostream>
#include <fc_light/io/iostream.hpp>
#include <fc_light/fwd.hpp>
#include <fc_light/string.hpp>
#include <memory>

namespace fc_light {

class stringstream : virtual public std::iostream {
class stringstream : virtual public iostream {
public:
stringstream();
stringstream( fc_light::string& s);
Expand Down
215 changes: 215 additions & 0 deletions libraries/fc_light/src/io/iostream.cpp
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

0 comments on commit 79377c2

Please sign in to comment.