BitShares-Core  4.0.0
BitShares blockchain implementation and command-line interface 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
void open(const fc::path &file, int m)
Definition: fstream.cpp:64
size_t writesome(const char *buf, size_t len)
Definition: fstream.cpp:36
void flush()
Definition: fstream.cpp:51
void read_file_contents(const fc::path &filename, std::string &result)
Definition: fstream.cpp:107
ifstream & seekg(size_t p, seekdir d=beg)
Definition: fstream.cpp:95
boost::filesystem::ofstream ofs
Definition: fstream.cpp:18
#define FC_THROW_EXCEPTION(EXCEPTION, FORMAT,...)
Definition: exception.hpp:378
Defines exception&#39;s used by fc.
bool eof() const
Definition: fstream.cpp:105
void open(const fc::path &file, std::ios_base::openmode m=std::ios_base::out|std::ios_base::binary)
Definition: fstream.cpp:32
Definition: api.hpp:15
void close()
Definition: fstream.cpp:103
size_t readsome(char *buf, size_t len)
Definition: fstream.cpp:69
boost::filesystem::ifstream ifs
Definition: fstream.cpp:22
ifstream & read(char *buf, size_t len)
Definition: fstream.cpp:87
wraps boost::filesystem::path to provide platform independent path manipulation.
Definition: filesystem.hpp:28
void put(char c)
Definition: fstream.cpp:45
void close()
Definition: fstream.cpp:48