/* This file is part of cpp-ethereum. cpp-ethereum is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. cpp-ethereum is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with cpp-ethereum. If not, see . */ /** @file CommonIO.h * @author Gav Wood * @date 2014 * * File & stream I/O routines. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include #include "Common.h" #include "Base64.h" namespace dev { /// Retrieve and returns the contents of the given file. If the file doesn't exist or isn't readable, returns an empty bytes. bytes contents(std::string const& _file); std::string contentsString(std::string const& _file); /// Retrieve and returns the allocated contents of the given file. If the file doesn't exist or isn't readable, returns nullptr. Don't forget to delete [] when finished. bytesRef contentsNew(std::string const& _file); /// Write the given binary data into the given file, replacing the file if it pre-exists. void writeFile(std::string const& _file, bytesConstRef _data); /// Write the given binary data into the given file, replacing the file if it pre-exists. inline void writeFile(std::string const& _file, bytes const& _data) { writeFile(_file, bytesConstRef(&_data)); } inline void writeFile(std::string const& _file, std::string const& _data) { writeFile(_file, bytesConstRef(_data)); } /// Nicely renders the given bytes to a string, optionally as HTML. /// @a _bytes: bytes array to be rendered as string. @a _width of a bytes line. std::string memDump(bytes const& _bytes, unsigned _width = 8, bool _html = false); // Stream I/O functions. // Provides templated stream I/O for all STL collections so they can be shifted on to any iostream-like interface. template struct StreamOut { static S& bypass(S& _out, T const& _t) { _out << _t; return _out; } }; template struct StreamOut { static S& bypass(S& _out, uint8_t const& _t) { _out << (int)_t; return _out; } }; template inline std::ostream& operator<<(std::ostream& _out, std::vector const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::array const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::pair const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::list const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::tuple const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::map const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::unordered_map const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::set const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::unordered_set const& _e); template inline std::ostream& operator<<(std::ostream& _out, std::multimap const& _e); template _S& operator<<(_S& _out, std::shared_ptr<_T> const& _p); template inline std::string toString(std::chrono::time_point const& _e, std::string _format = "") { unsigned long milliSecondsSinceEpoch = std::chrono::duration_cast(_e.time_since_epoch()).count(); auto const durationSinceEpoch = std::chrono::milliseconds(milliSecondsSinceEpoch); std::chrono::time_point const tpAfterDuration(durationSinceEpoch); tm timeValue; auto time = std::chrono::system_clock::to_time_t(tpAfterDuration); #ifdef _WIN32 gmtime_s(&timeValue, &time); #else gmtime_r(&time, &timeValue); #endif unsigned const millisRemainder = milliSecondsSinceEpoch % 1000; char buffer[1024]; if (strftime(buffer, sizeof(buffer), _format.c_str(), &timeValue)) return std::string(buffer) + "." + (millisRemainder < 1 ? "000" : millisRemainder < 10 ? "00" : millisRemainder < 100 ? "0" : "") + std::to_string(millisRemainder) + "Z"; return std::string(); } template inline S& streamout(S& _out, std::vector const& _e) { _out << "["; if (!_e.empty()) { StreamOut::bypass(_out, _e.front()); for (auto i = ++_e.begin(); i != _e.end(); ++i) StreamOut::bypass(_out << ",", *i); } _out << "]"; return _out; } template inline std::ostream& operator<<(std::ostream& _out, std::vector const& _e) { streamout(_out, _e); return _out; } template inline S& streamout(S& _out, std::array const& _e) { _out << "["; if (!_e.empty()) { StreamOut::bypass(_out, _e.front()); auto i = _e.begin(); for (++i; i != _e.end(); ++i) StreamOut::bypass(_out << ",", *i); } _out << "]"; return _out; } template inline std::ostream& operator<<(std::ostream& _out, std::array const& _e) { streamout(_out, _e); return _out; } template inline S& streamout(S& _out, std::list const& _e) { _out << "["; if (!_e.empty()) { _out << _e.front(); for (auto i = ++_e.begin(); i != _e.end(); ++i) _out << "," << *i; } _out << "]"; return _out; } template inline std::ostream& operator<<(std::ostream& _out, std::list const& _e) { streamout(_out, _e); return _out; } template inline S& streamout(S& _out, std::pair const& _e) { _out << "(" << _e.first << "," << _e.second << ")"; return _out; } template inline std::ostream& operator<<(std::ostream& _out, std::pair const& _e) { streamout(_out, _e); return _out; } template inline S& streamout(S& _out, std::tuple const& _t) { _out << "(" << std::get<0>(_t) << "," << std::get<1>(_t) << "," << std::get<2>(_t) << ")"; return _out; } template inline std::ostream& operator<<(std::ostream& _out, std::tuple const& _e) { streamout(_out, _e); return _out; } template S& streamout(S& _out, std::map const& _v) { if (_v.empty()) return _out << "{}"; int i = 0; for (auto p: _v) _out << (!(i++) ? "{ " : "; ") << p.first << " => " << p.second; return _out << " }"; } template inline std::ostream& operator<<(std::ostream& _out, std::map const& _e) { streamout(_out, _e); return _out; } template S& streamout(S& _out, std::unordered_map const& _v) { if (_v.empty()) return _out << "{}"; int i = 0; for (auto p: _v) _out << (!(i++) ? "{ " : "; ") << p.first << " => " << p.second; return _out << " }"; } template inline std::ostream& operator<<(std::ostream& _out, std::unordered_map const& _e) { streamout(_out, _e); return _out; } template S& streamout(S& _out, std::set const& _v) { if (_v.empty()) return _out << "{}"; int i = 0; for (auto p: _v) _out << (!(i++) ? "{ " : ", ") << p; return _out << " }"; } template inline std::ostream& operator<<(std::ostream& _out, std::set const& _e) { streamout(_out, _e); return _out; } template S& streamout(S& _out, std::unordered_set const& _v) { if (_v.empty()) return _out << "{}"; int i = 0; for (auto p: _v) _out << (!(i++) ? "{ " : ", ") << p; return _out << " }"; } template inline std::ostream& operator<<(std::ostream& _out, std::unordered_set const& _e) { streamout(_out, _e); return _out; } template S& streamout(S& _out, std::multiset const& _v) { if (_v.empty()) return _out << "{}"; int i = 0; for (auto p: _v) _out << (!(i++) ? "{ " : ", ") << p; return _out << " }"; } template inline std::ostream& operator<<(std::ostream& _out, std::multiset const& _e) { streamout(_out, _e); return _out; } template S& streamout(S& _out, std::multimap const& _v) { if (_v.empty()) return _out << "{}"; T l; int i = 0; for (auto p: _v) if (!(i++)) _out << "{ " << (l = p.first) << " => " << p.second; else if (l == p.first) _out << ", " << p.second; else _out << "; " << (l = p.first) << " => " << p.second; return _out << " }"; } template inline std::ostream& operator<<(std::ostream& _out, std::multimap const& _e) { streamout(_out, _e); return _out; } template _S& operator<<(_S& _out, std::shared_ptr<_T> const& _p) { if (_p) _out << "@" << (*_p); else _out << "nullptr"; return _out; } // Functions that use streaming stuff. /// Converts arbitrary value to string representation using std::stringstream. template std::string toString(_T const& _t) { std::ostringstream o; o << _t; return o.str(); } }