Fabcoin Core  0.16.2
P2P Digital Currency
CommonIO.cpp
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 */
22 #include "CommonIO.h"
23 #include <iostream>
24 #include <cstdlib>
25 #include <fstream>
26 #include <stdio.h>
27 #if defined(_WIN32)
28 #include <windows.h>
29 #else
30 #include <termios.h>
31 #endif
32 #include <boost/filesystem.hpp>
33 #include "Exceptions.h"
34 using namespace std;
35 using namespace dev;
36 
37 string dev::memDump(bytes const& _bytes, unsigned _width, bool _html)
38 {
39  stringstream ret;
40  if (_html)
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)
43  {
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)
49  ret << "&lt;";
50  else if ((char)_bytes[j] == '&' && _html)
51  ret << "&amp;";
52  else
53  ret << (char)_bytes[j];
54  else
55  ret << '?';
56  else
57  ret << ' ';
58  ret << " ";
59  for (unsigned j = i; j < i + _width && j < _bytes.size(); ++j)
60  ret << setfill('0') << setw(2) << hex << (unsigned)_bytes[j] << " ";
61  ret << "\n";
62  }
63  if (_html)
64  ret << "</pre>";
65  return ret.str();
66 }
67 
68 template <typename _T>
69 inline _T contentsGeneric(std::string const& _file)
70 {
71  _T ret;
72  size_t const c_elementSize = sizeof(typename _T::value_type);
73  std::ifstream is(_file, std::ifstream::binary);
74  if (!is)
75  return ret;
76 
77  // get length of file:
78  is.seekg(0, is.end);
79  streamoff length = is.tellg();
80  if (length == 0)
81  return ret; // do not read empty file (MSVC does not like it)
82  is.seekg(0, is.beg);
83 
84  ret.resize((length + c_elementSize - 1) / c_elementSize);
85  is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
86  return ret;
87 }
88 
89 bytes dev::contents(string const& _file)
90 {
91  return contentsGeneric<bytes>(_file);
92 }
93 
94 bytesSec dev::contentsSec(string const& _file)
95 {
96  bytes b = contentsGeneric<bytes>(_file);
97  bytesSec ret(b);
98  bytesRef(&b).cleanse();
99  return ret;
100 }
101 
102 string dev::contentsString(string const& _file)
103 {
104  return contentsGeneric<string>(_file);
105 }
106 
107 void dev::writeFile(std::string const& _file, bytesConstRef _data, bool _writeDeleteRename)
108 {
109  namespace fs = boost::filesystem;
110  if (_writeDeleteRename)
111  {
112  fs::path tempPath = fs::unique_path(_file + "-%%%%%%");
113  writeFile(tempPath.string(), _data, false);
114  // will delete _file if it exists
115  fs::rename(tempPath, _file);
116  }
117  else
118  {
119  // create directory if not existent
120  fs::path p(_file);
121  if (!fs::exists(p.parent_path()))
122  {
123  fs::create_directories(p.parent_path());
124  DEV_IGNORE_EXCEPTIONS(fs::permissions(p.parent_path(), fs::owner_all));
125  }
126 
127  ofstream s(_file, ios::trunc | ios::binary);
128  s.write(reinterpret_cast<char const*>(_data.data()), _data.size());
129  if (!s)
130  BOOST_THROW_EXCEPTION(FileError() << errinfo_comment("Could not write to file: " + _file));
131  DEV_IGNORE_EXCEPTIONS(fs::permissions(_file, fs::owner_read|fs::owner_write));
132  }
133 }
134 
135 std::string dev::getPassword(std::string const& _prompt)
136 {
137 #if defined(_WIN32)
138  cout << _prompt << flush;
139  // Get current Console input flags
140  HANDLE hStdin;
141  DWORD fdwSaveOldMode;
142  if ((hStdin = GetStdHandle(STD_INPUT_HANDLE)) == INVALID_HANDLE_VALUE)
143  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("GetStdHandle"));
144  if (!GetConsoleMode(hStdin, &fdwSaveOldMode))
145  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("GetConsoleMode"));
146  // Set console flags to no echo
147  if (!SetConsoleMode(hStdin, fdwSaveOldMode & (~ENABLE_ECHO_INPUT)))
148  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("SetConsoleMode"));
149  // Read the string
150  std::string ret;
151  std::getline(cin, ret);
152  // Restore old input mode
153  if (!SetConsoleMode(hStdin, fdwSaveOldMode))
154  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("SetConsoleMode"));
155  return ret;
156 #else
157  struct termios oflags;
158  struct termios nflags;
159  char password[256];
160 
161  // disable echo in the terminal
162  tcgetattr(fileno(stdin), &oflags);
163  nflags = oflags;
164  nflags.c_lflag &= ~ECHO;
165  nflags.c_lflag |= ECHONL;
166 
167  if (tcsetattr(fileno(stdin), TCSANOW, &nflags) != 0)
168  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("tcsetattr"));
169 
170  printf("%s", _prompt.c_str());
171  if (!fgets(password, sizeof(password), stdin))
172  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("fgets"));
173  password[strlen(password) - 1] = 0;
174 
175  // restore terminal
176  if (tcsetattr(fileno(stdin), TCSANOW, &oflags) != 0)
177  BOOST_THROW_EXCEPTION(ExternalFunctionFailure("tcsetattr"));
178 
179 
180  return password;
181 #endif
182 }
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void printf(const char *fmt, const Args &...args)
Format list of arguments to std::cout, according to the given format string.
Definition: tinyformat.h:972
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
std::hash for asio::adress
Definition: Common.h:323
std::string contentsString(std::string const &_file)
Retrieve and returns the contents of the given file as a std::string.
vector_ref< byte > bytesRef
Definition: Common.h:76
bytesSec contentsSec(std::string const &_file)
Secure variation.
std::vector< byte > bytes
Definition: Common.h:75
#define b(i, j)
size_t size() const
Definition: vector_ref.h:55
_T * data() const
Definition: vector_ref.h:51
boost::error_info< struct tag_comment, std::string > errinfo_comment
Definition: Assertions.h:78
_T contentsGeneric(std::string const &_file)
Definition: CommonIO.cpp:69
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
bytes contents(std::string const &_file)
Retrieve and returns the contents of the given file.
#define DEV_IGNORE_EXCEPTIONS(X)
Definition: Common.h:63