Fabcoin Core  0.16.2
P2P Digital Currency
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_map>
29 #include <unordered_set>
30 #include <array>
31 #include <list>
32 #include <memory>
33 #include <vector>
34 #include <array>
35 #include <sstream>
36 #include <string>
37 #include <iostream>
38 #include <chrono>
39 #include "Common.h"
40 #include "CommonData.h"
41 #include "Base64.h"
42 
43 namespace dev
44 {
45 
47 std::string getPassword(std::string const& _prompt);
48 
51 bytes contents(std::string const& _file);
53 bytesSec contentsSec(std::string const& _file);
56 std::string contentsString(std::string const& _file);
59 bytesRef contentsNew(std::string const& _file, bytesRef _dest = bytesRef());
60 
65 void writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename = false);
67 inline void writeFile(std::string const& _file, bytes const& _data, bool _writeDeleteRename = false) { writeFile(_file, bytesConstRef(&_data), _writeDeleteRename); }
68 inline void writeFile(std::string const& _file, std::string const& _data, bool _writeDeleteRename = false) { writeFile(_file, bytesConstRef(_data), _writeDeleteRename); }
69 
72 std::string memDump(bytes const& _bytes, unsigned _width = 8, bool _html = false);
73 
74 // Stream I/O functions.
75 // Provides templated stream I/O for all STL collections so they can be shifted on to any iostream-like interface.
76 
77 template <class S, class T> struct StreamOut { static S& bypass(S& _out, T const& _t) { _out << _t; return _out; } };
78 template <class S> struct StreamOut<S, uint8_t> { static S& bypass(S& _out, uint8_t const& _t) { _out << (int)_t; return _out; } };
79 
80 inline std::ostream& operator<<(std::ostream& _out, bytes const& _e) { _out << toHex(_e, 2, HexPrefix::Add); return _out; }
81 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::vector<T> const& _e);
82 template <class T, std::size_t Z> inline std::ostream& operator<<(std::ostream& _out, std::array<T, Z> const& _e);
83 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::pair<T, U> const& _e);
84 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::list<T> const& _e);
85 template <class T1, class T2, class T3> inline std::ostream& operator<<(std::ostream& _out, std::tuple<T1, T2, T3> const& _e);
86 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::map<T, U> const& _e);
87 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::unordered_map<T, U> const& _e);
88 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::set<T, U> const& _e);
89 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::unordered_set<T, U> const& _e);
90 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::multimap<T, U> const& _e);
91 template <class _S, class _T> _S& operator<<(_S& _out, std::shared_ptr<_T> const& _p);
92 
93 #if defined(_WIN32)
94 template <class T> inline std::string toString(std::chrono::time_point<T> const& _e, std::string _format = "%Y-%m-%d %H:%M:%S")
95 #else
96 template <class T> inline std::string toString(std::chrono::time_point<T> const& _e, std::string _format = "%F %T")
97 #endif
98 {
99  unsigned long milliSecondsSinceEpoch = std::chrono::duration_cast<std::chrono::milliseconds>(_e.time_since_epoch()).count();
100  auto const durationSinceEpoch = std::chrono::milliseconds(milliSecondsSinceEpoch);
101  std::chrono::time_point<std::chrono::system_clock> const tpAfterDuration(durationSinceEpoch);
102 
103  tm timeValue;
104  auto time = std::chrono::system_clock::to_time_t(tpAfterDuration);
105 #if defined(_WIN32)
106  gmtime_s(&timeValue, &time);
107 #else
108  gmtime_r(&time, &timeValue);
109 #endif
110 
111  unsigned const millisRemainder = milliSecondsSinceEpoch % 1000;
112  char buffer[1024];
113  if (strftime(buffer, sizeof(buffer), _format.c_str(), &timeValue))
114  return std::string(buffer) + "." + (millisRemainder < 1 ? "000" : millisRemainder < 10 ? "00" : millisRemainder < 100 ? "0" : "") + std::to_string(millisRemainder) + "Z";
115  return std::string();
116 }
117 
118 template <class S, class T>
119 inline S& streamout(S& _out, std::vector<T> const& _e)
120 {
121  _out << "[";
122  if (!_e.empty())
123  {
124  StreamOut<S, T>::bypass(_out, _e.front());
125  for (auto i = ++_e.begin(); i != _e.end(); ++i)
126  StreamOut<S, T>::bypass(_out << ",", *i);
127  }
128  _out << "]";
129  return _out;
130 }
131 
132 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::vector<T> const& _e) { streamout(_out, _e); return _out; }
133 
134 template <class S, class T, std::size_t Z>
135 inline S& streamout(S& _out, std::array<T, Z> const& _e)
136 {
137  _out << "[";
138  if (!_e.empty())
139  {
140  StreamOut<S, T>::bypass(_out, _e.front());
141  auto i = _e.begin();
142  for (++i; i != _e.end(); ++i)
143  StreamOut<S, T>::bypass(_out << ",", *i);
144  }
145  _out << "]";
146  return _out;
147 }
148 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; }
149 
150 template <class S, class T>
151 inline S& streamout(S& _out, std::list<T> const& _e)
152 {
153  _out << "[";
154  if (!_e.empty())
155  {
156  _out << _e.front();
157  for (auto i = ++_e.begin(); i != _e.end(); ++i)
158  _out << "," << *i;
159  }
160  _out << "]";
161  return _out;
162 }
163 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::list<T> const& _e) { streamout(_out, _e); return _out; }
164 
165 template <class S, class T, class U>
166 inline S& streamout(S& _out, std::pair<T, U> const& _e)
167 {
168  _out << "(" << _e.first << "," << _e.second << ")";
169  return _out;
170 }
171 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::pair<T, U> const& _e) { streamout(_out, _e); return _out; }
172 
173 template <class S, class T1, class T2, class T3>
174 inline S& streamout(S& _out, std::tuple<T1, T2, T3> const& _t)
175 {
176  _out << "(" << std::get<0>(_t) << "," << std::get<1>(_t) << "," << std::get<2>(_t) << ")";
177  return _out;
178 }
179 template <class T1, class T2, class T3> inline std::ostream& operator<<(std::ostream& _out, std::tuple<T1, T2, T3> const& _e) { streamout(_out, _e); return _out; }
180 
181 template <class S, class T, class U>
182 S& streamout(S& _out, std::map<T, U> const& _v)
183 {
184  if (_v.empty())
185  return _out << "{}";
186  int i = 0;
187  for (auto p: _v)
188  _out << (!(i++) ? "{ " : "; ") << p.first << " => " << p.second;
189  return _out << " }";
190 }
191 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::map<T, U> const& _e) { streamout(_out, _e); return _out; }
192 
193 template <class S, class T, class U>
194 S& streamout(S& _out, std::unordered_map<T, U> const& _v)
195 {
196  if (_v.empty())
197  return _out << "{}";
198  int i = 0;
199  for (auto p: _v)
200  _out << (!(i++) ? "{ " : "; ") << p.first << " => " << p.second;
201  return _out << " }";
202 }
203 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::unordered_map<T, U> const& _e) { streamout(_out, _e); return _out; }
204 
205 template <class S, class T>
206 S& streamout(S& _out, std::set<T> const& _v)
207 {
208  if (_v.empty())
209  return _out << "{}";
210  int i = 0;
211  for (auto p: _v)
212  _out << (!(i++) ? "{ " : ", ") << p;
213  return _out << " }";
214 }
215 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::set<T> const& _e) { streamout(_out, _e); return _out; }
216 
217 template <class S, class T>
218 S& streamout(S& _out, std::unordered_set<T> const& _v)
219 {
220  if (_v.empty())
221  return _out << "{}";
222  int i = 0;
223  for (auto p: _v)
224  _out << (!(i++) ? "{ " : ", ") << p;
225  return _out << " }";
226 }
227 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::unordered_set<T> const& _e) { streamout(_out, _e); return _out; }
228 
229 template <class S, class T>
230 S& streamout(S& _out, std::multiset<T> const& _v)
231 {
232  if (_v.empty())
233  return _out << "{}";
234  int i = 0;
235  for (auto p: _v)
236  _out << (!(i++) ? "{ " : ", ") << p;
237  return _out << " }";
238 }
239 template <class T> inline std::ostream& operator<<(std::ostream& _out, std::multiset<T> const& _e) { streamout(_out, _e); return _out; }
240 
241 template <class S, class T, class U>
242 S& streamout(S& _out, std::multimap<T, U> const& _v)
243 {
244  if (_v.empty())
245  return _out << "{}";
246  T l;
247  int i = 0;
248  for (auto p: _v)
249  if (!(i++))
250  _out << "{ " << (l = p.first) << " => " << p.second;
251  else if (l == p.first)
252  _out << ", " << p.second;
253  else
254  _out << "; " << (l = p.first) << " => " << p.second;
255  return _out << " }";
256 }
257 template <class T, class U> inline std::ostream& operator<<(std::ostream& _out, std::multimap<T, U> const& _e) { streamout(_out, _e); return _out; }
258 
259 template <class _S, class _T> _S& operator<<(_S& _out, std::shared_ptr<_T> const& _p) { if (_p) _out << "@" << (*_p); else _out << "nullptr"; return _out; }
260 
261 // Functions that use streaming stuff.
262 
264 template <class _T>
265 std::string toString(_T const& _t)
266 {
267  std::ostringstream o;
268  o << _t;
269  return o.str();
270 }
271 
272 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
Definition: CommonData.h:54
S & streamout(S &_out, std::vector< T > const &_e)
Definition: CommonIO.h:119
void writeFile(std::string const &_file, bytesConstRef _data, bool _writeDeleteRename=false)
Write the given binary data into the given file, replacing the file if it pre-exists.
Definition: CommonIO.cpp:107
#define T(i, x)
size_t count
Definition: ExecStats.cpp:37
secure_vector< byte > bytesSec
Definition: Common.h:118
std::string contentsString(std::string const &_file)
Retrieve and returns the contents of the given file as a std::string.
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
Definition: CommonData.cpp:141
vector_ref< byte > bytesRef
Definition: Common.h:76
bytesSec contentsSec(std::string const &_file)
Secure variation.
std::vector< byte > bytes
Definition: Common.h:75
vector_ref< byte const > bytesConstRef
Definition: Common.h:77
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:80
static S & bypass(S &_out, T const &_t)
Definition: CommonIO.h:77
std::string memDump(bytes const &_bytes, unsigned _width=8, bool _html=false)
Nicely renders the given bytes to a string, optionally as HTML.
Definition: CommonIO.cpp:37
std::string getPassword(std::string const &_prompt)
Requests the user to enter a password on the console.
Definition: CommonIO.cpp:135
#define S(a)
Definition: mars.cpp:50
static S & bypass(S &_out, uint8_t const &_t)
Definition: CommonIO.h:78
bytesRef contentsNew(std::string const &_file, bytesRef _dest=bytesRef())
Retrieve and returns the allocated contents of the given file; if is given, don&#39;t allocate...
bytes contents(std::string const &_file)
Retrieve and returns the contents of the given file.