32 #include <boost/filesystem.hpp> 41 ret <<
"<pre style=\"font-family: Monospace,Lucida Console,Courier,Courier New,sans-serif; font-size: small\">";
42 for (
unsigned i = 0; i < _bytes.size(); i += _width)
44 ret << hex << setw(4) << setfill(
'0') << i <<
" ";
45 for (
unsigned j = i; j < i + _width; ++j)
46 if (j < _bytes.size())
47 if (_bytes[j] >= 32 && _bytes[j] < 127)
48 if ((
char)_bytes[j] ==
'<' && _html)
50 else if ((
char)_bytes[j] ==
'&' && _html)
53 ret << (char)_bytes[j];
59 for (
unsigned j = i; j < i + _width && j < _bytes.size(); ++j)
60 ret << setfill(
'0') << setw(2) << hex << (unsigned)_bytes[j] <<
" ";
68 template <
typename _T>
72 size_t const c_elementSize =
sizeof(
typename _T::value_type);
73 std::ifstream is(_file, std::ifstream::binary);
79 streamoff length = is.tellg();
84 ret.resize((length + c_elementSize - 1) / c_elementSize);
85 is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
91 return contentsGeneric<bytes>(_file);
96 bytes b = contentsGeneric<bytes>(_file);
104 return contentsGeneric<string>(_file);
109 namespace fs = boost::filesystem;
110 if (_writeDeleteRename)
112 fs::path tempPath = fs::unique_path(_file +
"-%%%%%%");
113 writeFile(tempPath.string(), _data,
false);
115 fs::rename(tempPath, _file);
121 if (!fs::exists(p.parent_path()))
123 fs::create_directories(p.parent_path());
127 ofstream s(_file, ios::trunc | ios::binary);
128 s.write(reinterpret_cast<char const*>(_data.
data()), _data.
size());
130 BOOST_THROW_EXCEPTION(FileError() <<
errinfo_comment(
"Could not write to file: " + _file));
138 cout << _prompt << flush;
141 DWORD fdwSaveOldMode;
142 if ((hStdin = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
144 if (!GetConsoleMode(hStdin, &fdwSaveOldMode))
147 if (!SetConsoleMode(hStdin, fdwSaveOldMode & (~ENABLE_ECHO_INPUT)))
151 std::getline(cin, ret);
153 if (!SetConsoleMode(hStdin, fdwSaveOldMode))
157 struct termios oflags;
158 struct termios nflags;
162 tcgetattr(fileno(stdin), &oflags);
164 nflags.c_lflag &= ~ECHO;
165 nflags.c_lflag |= ECHONL;
167 if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
170 printf(
"%s", _prompt.c_str());
171 if (!fgets(password,
sizeof(password), stdin))
173 password[strlen(password) - 1] = 0;
176 if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
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.
std::hash for asio::adress
std::string contentsString(std::string const &_file)
Retrieve and returns the contents of the given file as a std::string.
vector_ref< byte > bytesRef
bytesSec contentsSec(std::string const &_file)
Secure variation.
std::vector< byte > bytes
boost::error_info< struct tag_comment, std::string > errinfo_comment
_T contentsGeneric(std::string const &_file)
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.
bytes contents(std::string const &_file)
Retrieve and returns the contents of the given file.
#define DEV_IGNORE_EXCEPTIONS(X)