BitShares-Core  7.0.2
BitShares blockchain node software and command-line wallet software
fstream.cpp
Go to the documentation of this file.
1 
2 #include <fstream>
3 #include <sstream>
4 
5 #include <fc/filesystem.hpp>
7 #include <fc/io/fstream.hpp>
8 #include <fc/log/logger.hpp>
9 
10 #include <boost/filesystem/path.hpp>
11 #include <boost/filesystem/fstream.hpp>
12 
13 using namespace std;
14 
15 namespace fc {
17  public:
18  boost::filesystem::ofstream ofs;
19  };
21  public:
22  boost::filesystem::ifstream ifs;
23  };
24 
25  ofstream::ofstream()
26  :my( new impl() ){}
27 
28  ofstream::ofstream( const fc::path& file, std::ios_base::openmode m )
29  :my( new impl() ) { this->open( file, m ); }
31 
32  void ofstream::open( const fc::path& file, std::ios_base::openmode m ) {
33  const boost::filesystem::path& bfp = file;
34  my->ofs.open( bfp, std::ios_base::out | std::ios_base::binary | m );
35  }
36  size_t ofstream::writesome( const char* buf, size_t len ) {
37  my->ofs.write(buf,len);
38  return len;
39  }
40  size_t ofstream::writesome(const std::shared_ptr<const char>& buffer, size_t len, size_t offset)
41  {
42  return writesome(buffer.get() + offset, len);
43  }
44 
45  void ofstream::put( char c ) {
46  my->ofs.put(c);
47  }
48  void ofstream::close() {
49  my->ofs.close();
50  }
51  void ofstream::flush() {
52  my->ofs.flush();
53  }
54 
56  :my(new impl()){}
57  ifstream::ifstream( const fc::path& file, int m )
58  :my(new impl())
59  {
60  this->open( file, m );
61  }
63 
64  void ifstream::open( const fc::path& file, int m ) {
65  const boost::filesystem::path& bfp = file;
66  my->ifs.open( bfp, std::ios::binary );
67  }
68 
69  size_t ifstream::readsome( char* buf, size_t len ) {
70  auto s = size_t(my->ifs.readsome( buf, len ));
71  if( s <= 0 )
72  {
73  read( buf, 1 );
74  s = 1;
75  if (len > 1)
76  {
77  s += size_t(my->ifs.readsome( &buf[1], len - 1));
78  }
79  }
80  return s;
81  }
82  size_t ifstream::readsome(const std::shared_ptr<char>& buffer, size_t max, size_t offset)
83  {
84  return readsome(buffer.get() + offset, max);
85  }
86 
87  ifstream& ifstream::read( char* buf, size_t len ) {
88  if (eof())
89  FC_THROW_EXCEPTION( eof_exception , "");
90  my->ifs.read(buf,len);
91  if (my->ifs.gcount() < int64_t(len))
92  FC_THROW_EXCEPTION( eof_exception , "");
93  return *this;
94  }
95  ifstream& ifstream::seekg( size_t p, seekdir d ) {
96  switch( d ) {
97  case beg: my->ifs.seekg( p, std::ios_base::beg ); return *this;
98  case cur: my->ifs.seekg( p, std::ios_base::cur ); return *this;
99  case end: my->ifs.seekg( p, std::ios_base::end ); return *this;
100  }
101  return *this;
102  }
103  void ifstream::close() { return my->ifs.close(); }
104 
105  bool ifstream::eof()const { return !my->ifs.good(); }
106 
107  void read_file_contents( const fc::path& filename, std::string& result )
108  {
109  const boost::filesystem::path& bfp = filename;
110  boost::filesystem::ifstream f( bfp, std::ios::in | std::ios::binary );
111  // don't use fc::stringstream here as we need something with override for << rdbuf()
112  std::stringstream ss;
113  ss << f.rdbuf();
114  result = ss.str();
115  }
116 
117 } // namespace fc
fc::ifstream::open
void open(const fc::path &file, int m)
Definition: fstream.cpp:64
fc::ifstream::cur
@ cur
Definition: fstream.hpp:30
fc::ifstream::impl
Definition: fstream.cpp:20
fc
Definition: api.hpp:15
fc::ifstream::~ifstream
~ifstream()
Definition: fstream.cpp:62
fc::ifstream::beg
@ beg
Definition: fstream.hpp:30
fc::ofstream::put
void put(char c)
Definition: fstream.cpp:45
filesystem.hpp
fc::ifstream::impl::ifs
boost::filesystem::ifstream ifs
Definition: fstream.cpp:22
fc::ofstream::writesome
size_t writesome(const char *buf, size_t len)
Definition: fstream.cpp:36
fc::ofstream::open
void open(const fc::path &file, std::ios_base::openmode m=std::ios_base::out|std::ios_base::binary)
Definition: fstream.cpp:32
fc::ifstream::end
@ end
Definition: fstream.hpp:30
fc::ifstream
Definition: fstream.hpp:27
fc::ofstream::close
void close()
Definition: fstream.cpp:48
fc::path
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
fc::ifstream::read
ifstream & read(char *buf, size_t len)
Definition: fstream.cpp:87
fc::ifstream::ifstream
ifstream()
Definition: fstream.cpp:55
fc::ifstream::eof
bool eof() const
Definition: fstream.cpp:105
fc::read_file_contents
void read_file_contents(const fc::path &filename, std::string &result)
Definition: fstream.cpp:107
exception.hpp
Defines exception's used by fc.
std
Definition: zeroed_array.hpp:76
fc::ifstream::readsome
size_t readsome(char *buf, size_t len)
Definition: fstream.cpp:69
fc::ifstream::close
void close()
Definition: fstream.cpp:103
logger.hpp
fstream.hpp
fc::ofstream::flush
void flush()
Definition: fstream.cpp:51
fc::ifstream::seekg
ifstream & seekg(size_t p, seekdir d=beg)
Definition: fstream.cpp:95
fc::ofstream::impl
Definition: fstream.cpp:16
fc::ofstream::impl::ofs
boost::filesystem::ofstream ofs
Definition: fstream.cpp:18
fc::ofstream::~ofstream
~ofstream()
Definition: fstream.cpp:30
FC_THROW_EXCEPTION
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:379
fc::ifstream::seekdir
seekdir
Definition: fstream.hpp:30
fc::ofstream::ofstream
ofstream()
Definition: fstream.cpp:25