28 #include <unordered_map> 29 #include <unordered_set> 47 std::string
getPassword(std::string
const& _prompt);
68 inline void writeFile(std::string
const& _file, std::string
const& _data,
bool _writeDeleteRename =
false) {
writeFile(_file,
bytesConstRef(_data), _writeDeleteRename); }
72 std::string
memDump(
bytes const& _bytes,
unsigned _width = 8,
bool _html =
false);
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; } };
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);
94 template <
class T>
inline std::string
toString(std::chrono::time_point<T>
const& _e, std::string _format =
"%Y-%m-%d %H:%M:%S")
96 template <
class T>
inline std::string
toString(std::chrono::time_point<T>
const& _e, std::string _format =
"%F %T")
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);
104 auto time = std::chrono::system_clock::to_time_t(tpAfterDuration);
106 gmtime_s(&timeValue, &time);
108 gmtime_r(&time, &timeValue);
111 unsigned const millisRemainder = milliSecondsSinceEpoch % 1000;
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();
118 template <
class S,
class T>
125 for (
auto i = ++_e.begin(); i != _e.end(); ++i)
132 template <
class T>
inline std::ostream& operator<<(std::ostream& _out, std::vector<T>
const& _e) {
streamout(_out, _e);
return _out; }
134 template <
class S,
class T, std::
size_t Z>
142 for (++i; i != _e.end(); ++i)
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; }
150 template <
class S,
class T>
157 for (
auto i = ++_e.begin(); i != _e.end(); ++i)
163 template <
class T>
inline std::ostream& operator<<(std::ostream& _out, std::list<T>
const& _e) {
streamout(_out, _e);
return _out; }
165 template <
class S,
class T,
class U>
168 _out <<
"(" << _e.first <<
"," << _e.second <<
")";
171 template <
class T,
class U>
inline std::ostream& operator<<(std::ostream& _out, std::pair<T, U>
const& _e) {
streamout(_out, _e);
return _out; }
173 template <
class S,
class T1,
class T2,
class T3>
176 _out <<
"(" << std::get<0>(_t) <<
"," << std::get<1>(_t) <<
"," << std::get<2>(_t) <<
")";
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; }
181 template <
class S,
class T,
class U>
188 _out << (!(i++) ?
"{ " :
"; ") << p.first <<
" => " << p.second;
191 template <
class T,
class U>
inline std::ostream& operator<<(std::ostream& _out, std::map<T, U>
const& _e) {
streamout(_out, _e);
return _out; }
193 template <
class S,
class T,
class U>
200 _out << (!(i++) ?
"{ " :
"; ") << p.first <<
" => " << p.second;
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; }
205 template <
class S,
class T>
212 _out << (!(i++) ?
"{ " :
", ") << p;
215 template <
class T>
inline std::ostream& operator<<(std::ostream& _out, std::set<T>
const& _e) {
streamout(_out, _e);
return _out; }
217 template <
class S,
class T>
224 _out << (!(i++) ?
"{ " :
", ") << p;
227 template <
class T>
inline std::ostream& operator<<(std::ostream& _out, std::unordered_set<T>
const& _e) {
streamout(_out, _e);
return _out; }
229 template <
class S,
class T>
236 _out << (!(i++) ?
"{ " :
", ") << p;
239 template <
class T>
inline std::ostream& operator<<(std::ostream& _out, std::multiset<T>
const& _e) {
streamout(_out, _e);
return _out; }
241 template <
class S,
class T,
class U>
250 _out <<
"{ " << (l = p.first) <<
" => " << p.second;
251 else if (l == p.first)
252 _out <<
", " << p.second;
254 _out <<
"; " << (l = p.first) <<
" => " << p.second;
257 template <
class T,
class U>
inline std::ostream& operator<<(std::ostream& _out, std::multimap<T, U>
const& _e) {
streamout(_out, _e);
return _out; }
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; }
267 std::ostringstream o;
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
std::string toHex(T const &_data, int _w=2, HexPrefix _prefix=HexPrefix::DontAdd)
S & streamout(S &_out, std::vector< T > const &_e)
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.
secure_vector< byte > bytesSec
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.
vector_ref< byte > bytesRef
bytesSec contentsSec(std::string const &_file)
Secure variation.
std::vector< byte > bytes
vector_ref< byte const > bytesConstRef
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
static S & bypass(S &_out, T const &_t)
std::string memDump(bytes const &_bytes, unsigned _width=8, bool _html=false)
Nicely renders the given bytes to a string, optionally as HTML.
std::string getPassword(std::string const &_prompt)
Requests the user to enter a password on the console.
static S & bypass(S &_out, uint8_t const &_t)
bytesRef contentsNew(std::string const &_file, bytesRef _dest=bytesRef())
Retrieve and returns the allocated contents of the given file; if is given, don't allocate...
bytes contents(std::string const &_file)
Retrieve and returns the contents of the given file.