Merge branch 'master' of github.com:bytemaster/fc

This commit is contained in:
Daniel Larimer 2012-11-15 19:15:19 -05:00
commit cc4e1f1b52
10 changed files with 118 additions and 20 deletions

View file

@ -67,6 +67,7 @@ set( sources
src/log.cpp
src/time.cpp
src/iostream.cpp
src/fstream.cpp
src/sstream.cpp
src/exception.cpp
src/thread.cpp

View file

@ -64,6 +64,8 @@ namespace fc {
void create_directories( const path& p );
path canonical( const path& p );
uint64_t file_size( const path& p );
bool remove( const path& p );
void copy( const path& from, const path& to );
}
#endif // _FC_FILESYSTEM_HPP_

View file

@ -1,16 +1,17 @@
#pragma once
#include <fc/fwd.hpp>
#include <fc/shared_ptr.hpp>
#include <fc/iostream.hpp>
namespace fc {
class ofstream //: virtual public ostream {
class path;
class ofstream : virtual public ostream {
public:
enum mode { out, binary };
ofstream();
ofstream( const fc::string& file, int m );
ofstream( const fc::path& file, int m = binary );
~ofstream();
void open( const fc::string& file, int m );
void open( const fc::path& file, int m = binary );
ofstream& write( const char* buf, size_t len );
void put( char c );
void close();
@ -18,26 +19,23 @@ namespace fc {
private:
class impl;
fwd<impl,896> my;
fc::shared_ptr<impl> my;
};
class ifstream //: virtual public istream {
class ifstream : virtual public istream {
public:
enum mode { in, binary };
ifstream();
ifstream( const fc::string& file, int m );
ifstream( const fc::path& file, int m );
~ifstream();
void open( const fc::string& file, int m );
void open( const fc::path& file, int m );
ifstream& read( char* buf, size_t len );
void close();
void flush();
bool eof()const;
private:
class impl;
fwd<impl,904> my;
fc::shared_ptr<impl> my;
};
} // namespace fc

View file

@ -115,7 +115,7 @@ namespace fc { namespace json {
template<typename InterfaceType>
void add_interface( const fc::ptr<InterfaceType>& it ) {
it->template visit<InterfaceType>( detail::add_method_visitor<InterfaceType>( it, *this ) );
it->template visit( detail::add_method_visitor<InterfaceType>( it, *this ) );
}
void add_method( const fc::string& name, const fc::json::rpc_server_method::ptr& func );

View file

@ -4,6 +4,7 @@
#include <fc/reflect.hpp>
namespace fc {
class path;
class sha1 {
public:
@ -18,6 +19,7 @@ namespace fc {
static sha1 hash( const char* d, uint32_t dlen );
static sha1 hash( const fc::string& );
static sha1 hash( const fc::path& );
template<typename T>
static sha1 hash( const T& t ) { sha1::encoder e; e << t; return e.result(); }

View file

@ -127,6 +127,14 @@ namespace fc {
bool is_null()const;
void visit( const_visitor&& v )const;
/* sets the subkey key with v and return *this */
value& set( const char* key, fc::value v );
value& set( const fc::string& key, fc::value v );
template<typename S, typename T>
value& set( S&& key, T&& v ) { return set( fc::forward<S>(key), fc::value( fc::forward<T>(v) ) ); }
private:
/** throws exceptions on errors
*

View file

@ -75,4 +75,7 @@ namespace fc {
bool is_directory( const path& p ) { return boost::filesystem::is_directory(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); }
void copy( const path& f, const path& t ) { boost::filesystem::copy( f, t ); }
bool remove( const path& f ) { return boost::filesystem::remove( f ); }
fc::path canonical( const fc::path& p ) { return boost::filesystem::canonical(p); }
}

60
src/fstream.cpp Normal file
View file

@ -0,0 +1,60 @@
#include <fc/fstream.hpp>
#include <fc/filesystem.hpp>
#include <fstream>
namespace fc {
class ofstream::impl : public fc::retainable {
public:
std::ofstream ofs;
};
class ifstream::impl : public fc::retainable {
public:
std::ifstream ifs;
};
ofstream::ofstream()
:my( new impl() ){}
ofstream::ofstream( const fc::path& file, int m )
:my( new impl() ) { this->open( file, m ); }
ofstream::~ofstream(){}
void ofstream::open( const fc::path& file, int m ) {
my->ofs.open( file.string().c_str(), std::ios::binary );
}
ofstream& ofstream::write( const char* buf, size_t len ) {
my->ofs.write(buf,len);
return *this;
}
void ofstream::put( char c ) {
my->ofs.put(c);
}
void ofstream::close() {
my->ofs.close();
}
void ofstream::flush() {
my->ofs.flush();
}
ifstream::ifstream()
:my(new impl()){}
ifstream::ifstream( const fc::path& file, int m )
:my(new impl())
{
this->open( file, m );
}
ifstream::~ifstream(){}
void ifstream::open( const fc::path& file, int m ) {
my->ifs.open( file.string().c_str(), std::ios::binary );
}
ifstream& ifstream::read( char* buf, size_t len ) {
my->ifs.read(buf,len);
return *this;
}
void ifstream::close() { return my->ifs.close(); }
bool ifstream::eof()const { return my->ifs.eof(); }
} // namespace fc

View file

@ -3,6 +3,8 @@
#include <fc/fwd_impl.hpp>
#include <openssl/sha.h>
#include <string.h>
#include <fc/filesystem.hpp>
#include <fc/interprocess/file_mapping.hpp>
namespace fc {
@ -36,6 +38,14 @@ namespace fc {
sha1 sha1::hash( const fc::string& s ) {
return hash( s.c_str(), s.size() );
}
sha1 sha1::hash( const fc::path& s ) {
file_mapping fmap( s.string().c_str(), read_only );
size_t fsize = file_size(s);
mapped_region mr( fmap, fc::read_only, 0, fsize );
const char* pos = reinterpret_cast<const char*>(mr.get_address());
return hash( pos, fsize );
}
void sha1::encoder::write( const char* d, uint32_t dlen ) {
SHA1_Update( &my->ctx, d, dlen);

View file

@ -48,6 +48,7 @@ namespace fc {
struct value_holder_impl<void> : value_holder {
value_holder_impl(){};
// typedef void_t T;
/*
virtual const char* type()const { return "void"; }
virtual void visit( value::const_visitor&& v )const{ v(); }
virtual void visit( value_visitor&& v ) { v(); }
@ -56,6 +57,7 @@ namespace fc {
virtual value_holder* move_helper( char* c ){ return new(c) value_holder_impl(); }
virtual value_holder* copy_helper( char* c )const{ return new(c) value_holder_impl();}
*/
};
@ -121,7 +123,10 @@ namespace fc {
};
value_holder::~value_holder(){}
const char* value_holder::type()const { return "null"; }
const char* value_holder::type()const { return "void"; }
value_holder* value_holder::move_helper( char* c ) { return new(c) value_holder(); }
value_holder* value_holder::copy_helper( char* c )const { return new(c) value_holder(); }
void value_holder::visit( value::const_visitor&& v )const { v(); }
void value_holder::visit( value_visitor&& v ) { v(); }
@ -133,8 +138,8 @@ namespace fc {
const value& value_holder::at( size_t )const { FC_THROW_MSG("value type '%s' not an array", type()); return *((const value*)0); }
void value_holder::push_back( value&& v ) { FC_THROW_MSG("value type '%s' not an array", type()); }
value_holder* value_holder::move_helper( char* c ) { return new(c) value_holder(); }
value_holder* value_holder::copy_helper( char* c )const{ return new(c) value_holder(); }
// value_holder* value_holder::move_helper( char* c ) = 0;
// value_holder* value_holder::copy_helper( char* c )const = 0;
void value_holder_impl<value::array>::resize( size_t s ) { val.fields.resize(s); }
void value_holder_impl<value::array>::reserve( size_t s ) { val.fields.reserve(s); }
@ -168,7 +173,7 @@ static const detail::value_holder* gh( const aligned<24>& h ) {
}
value::value() {
new (holder) detail::value_holder();
new (holder) detail::value_holder_impl<void>();
}
value::value( value&& m ) {
gh(m.holder)->move_helper(holder._store._data);
@ -271,7 +276,7 @@ value& value::operator=( const value& v ){
return *this;
}
bool value::is_null()const {
return strcmp(gh(holder)->type(), "null") == 0;
return strcmp(gh(holder)->type(), "void") == 0;
}
@ -314,7 +319,7 @@ value& value::operator[]( const char* key ) {
}
o->val.fields.push_back( key_val(key) );
return o->val.fields.back().val;
} else if (strcmp(gh(holder)->type(), "null" ) == 0 ) {
} else if (strcmp(gh(holder)->type(), "void" ) == 0 ) {
new (holder) detail::value_holder_impl<value::object>(value::object());
return (*this)[key];
}
@ -359,5 +364,14 @@ void value::visit( value::const_visitor&& v )const {
auto h = ((detail::value_holder*)&holder[0]);
h->visit( fc::move(v) );
}
/* sets the subkey key with v and return *this */
value& value::set( const char* key, fc::value v ) {
(*this)[key] = fc::move(v);
return *this;
}
value& value::set( const fc::string& key, fc::value v ) {
(*this)[key.c_str()] = fc::move(v);
return *this;
}
} // namepace fc