Ethereum  PoC-8
The C++ Implementation of Ethereum
CommonIO.h
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
24 #pragma once
25 
26 #include <map>
27 #include <set>
28 #include <unordered_set>
29 #include <array>
30 #include <list>
31 #include <vector>
32 #include <sstream>
33 #include <string>
34 #include <iosfwd>
35 #include <chrono>
36 #include "Common.h"
37 #include "CommonData.h"
38 #include <boost/filesystem.hpp>
39 
40 namespace dev
41 {
42 
44 std::string getPassword(std::string const& _prompt);
45 
48 bytes contents(boost::filesystem::path const& _file);
50 bytesSec contentsSec(boost::filesystem::path const& _file);
53 std::string contentsString(boost::filesystem::path const& _file);
54 
59 void writeFile(boost::filesystem::path const& _file, bytesConstRef _data, bool _writeDeleteRename = false);
61 inline void writeFile(boost::filesystem::path const& _file, bytes const& _data, bool _writeDeleteRename = false) { writeFile(_file, bytesConstRef(&_data), _writeDeleteRename); }
62 
65 void copyDirectory(boost::filesystem::path const& _srcDir, boost::filesystem::path const& _dstDir);
66 
69 std::string memDump(bytes const& _bytes, unsigned _width = 8, bool _html = false);
70 
71 // Stream I/O functions.
72 // Provides templated stream I/O for all STL collections so they can be shifted on to any iostream-like interface.
73 
74 template <class T> struct StreamOut { static std::ostream& bypass(std::ostream& _out, T const& _t) { _out << _t; return _out; } };
75 template <> struct StreamOut<uint8_t> { static std::ostream& bypass(std::ostream& _out, uint8_t const& _t) { _out << (int)_t; return _out; } };
76 
77 inline std::ostream& operator<<(std::ostream& _out, bytes const& _e) { _out << toHexPrefixed(_e); return _out; }
78 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::vector<T> const& _e);
79 template <class T, std::size_t Z> inline std::ostream& operator<<(std::ostream& _out, std::array<T, Z> const& _e);
80 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::set<T, U> const& _e);
81 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::unordered_set<T, U> const& _e);
82 
83 #if defined(_WIN32)
84 template <class T> inline std::string toString(std::chrono::time_point<T> const& _e, std::string const& _format = "%Y-%m-%d %H:%M:%S")
85 #else
86 template <class T> inline std::string toString(std::chrono::time_point<T> const& _e, std::string const& _format = "%F %T")
87 #endif
88 {
89  unsigned long milliSecondsSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(_e.time_since_epoch()).count();
90  auto const durationSinceEpoch = std::chrono::milliseconds(milliSecondsSinceEpoch);
91  std::chrono::time_point<std::chrono::system_clock> const tpAfterDuration(durationSinceEpoch);
92 
93  tm timeValue;
94  auto time = std::chrono::system_clock::to_time_t(tpAfterDuration);
95 #if defined(_WIN32)
96  gmtime_s(&timeValue, &time);
97 #else
98  gmtime_r(&time, &timeValue);
99 #endif
100 
101  unsigned const millisRemainder = milliSecondsSinceEpoch % 1000;
102  char buffer[1024];
103  if (strftime(buffer, sizeof(buffer), _format.c_str(), &timeValue))
104  return std::string(buffer) + "." + (millisRemainder < 1 ? "000" : millisRemainder < 10 ? "00" : millisRemainder < 100 ? "0" : "") + std::to_string(millisRemainder) + "Z";
105  return std::string();
106 }
107 
108 template <class T>
109 inline std::ostream& streamout(std::ostream& _out, std::vector<T> const& _e)
110 {
111  _out << "[";
112  if (!_e.empty())
113  {
114  StreamOut<T>::bypass(_out, _e.front());
115  for (auto i = ++_e.begin(); i != _e.end(); ++i)
116  StreamOut<T>::bypass(_out << ",", *i);
117  }
118  _out << "]";
119  return _out;
120 }
121 
122 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::vector<T> const& _e) { streamout(_out, _e); return _out; } // Used in CommonJS.h
123 
124 template <class T, std::size_t Z>
125 inline std::ostream& streamout(std::ostream& _out, std::array<T, Z> const& _e) //used somewhere?
126 {
127  _out << "[";
128  if (!_e.empty())
129  {
130  StreamOut<T>::bypass(_out, _e.front());
131  auto i = _e.begin();
132  for (++i; i != _e.end(); ++i)
133  StreamOut<T>::bypass(_out << ",", *i);
134  }
135  _out << "]";
136  return _out;
137 }
138 template <class T, std::size_t Z> inline std::ostream& operator<<(std::ostream& _out, std::array<T, Z> const& _e) { streamout(_out, _e); return _out; }
139 
140 template <class T>
141 std::ostream& streamout(std::ostream& _out, std::set<T> const& _v)
142 {
143  if (_v.empty())
144  return _out << "{}";
145  int i = 0;
146  for (auto p: _v)
147  _out << (!(i++) ? "{ " : ", ") << p;
148  return _out << " }";
149 }
150 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::set<T> const& _e) { streamout(_out, _e); return _out; }
151 
152 template <class T>
153 std::ostream& streamout(std::ostream& _out, std::unordered_set<T> const& _v)
154 {
155  if (_v.empty())
156  return _out << "{}";
157  int i = 0;
158  for (auto p: _v)
159  _out << (!(i++) ? "{ " : ", ") << p;
160  return _out << " }";
161 }
162 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::unordered_set<T> const& _e) { streamout(_out, _e); return _out; }
163 
164 // Functions that use streaming stuff.
165 
167 template <class _T>
168 inline std::string toString(_T const& _t)
169 {
170  std::ostringstream o;
171  o << _t;
172  return o.str();
173 }
174 
175 template <>
176 inline std::string toString<std::string>(std::string const& _s)
177 {
178  return _s;
179 }
180 
181 template <>
182 inline std::string toString<uint8_t>(uint8_t const& _u)
183 {
184  std::ostringstream o;
185  o << static_cast<uint16_t>(_u);
186  return o.str();
187 }
188 }
dev::contents
bytes contents(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:107
dev::toHexPrefixed
std::string toHexPrefixed(T const &_data)
Definition: CommonData.h:72
dev::StreamOut::bypass
static std::ostream & bypass(std::ostream &_out, T const &_t)
Definition: CommonIO.h:74
dev::copyDirectory
void copyDirectory(boost::filesystem::path const &_srcDir, boost::filesystem::path const &_dstDir)
Definition: CommonIO.cpp:146
dev::toString
std::string toString(std::chrono::time_point< T > const &_e, std::string const &_format="%F %T")
Definition: CommonIO.h:86
dev::contentsString
string contentsString(boost::filesystem::path const &_file)
Definition: CommonIO.cpp:120
dev::operator<<
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:77
dev::streamout
std::ostream & streamout(std::ostream &_out, std::vector< T > const &_e)
Definition: CommonIO.h:109
Common.h
dev::getPassword
std::string getPassword(std::string const &_prompt)
Requests the user to enter a password on the console.
Definition: CommonIO.cpp:154
dev::StreamOut
Definition: CommonIO.h:74
dev::toString< uint8_t >
std::string toString< uint8_t >(uint8_t const &_u)
Definition: CommonIO.h:182
dev::StreamOut< uint8_t >::bypass
static std::ostream & bypass(std::ostream &_out, uint8_t const &_t)
Definition: CommonIO.h:75
dev::bytes
std::vector< byte > bytes
Definition: Common.h:72
dev::bytesConstRef
vector_ref< byte const > bytesConstRef
Definition: Common.h:74
CommonData.h
dev::bytesSec
secure_vector< byte > bytesSec
Definition: Common.h:115
dev::contentsSec
bytesSec contentsSec(boost::filesystem::path const &_file)
Secure variation.
Definition: CommonIO.cpp:112
dev::memDump
string memDump(bytes const &_bytes, unsigned _width, bool _html)
Definition: CommonIO.cpp:55
dev
Definition: Address.cpp:21
dev::writeFile
void writeFile(boost::filesystem::path const &_file, bytesConstRef _data, bool _writeDeleteRename)
Definition: CommonIO.cpp:125