Browse Source

Some changes in libdevcore.

cl-refactor
chriseth 10 years ago
parent
commit
93003a1224
  1. 12
      ethkey/KeyAux.h
  2. 21
      libdevcore/Common.h
  3. 4
      libdevcore/CommonData.cpp
  4. 5
      libdevcore/CommonData.h
  5. 58
      libdevcore/CommonIO.cpp
  6. 6
      libdevcore/CommonIO.h
  7. 2
      liblll/CodeFragment.cpp
  8. 2
      libtestutils/Common.cpp
  9. 2
      lllc/main.cpp
  10. 2
      solc/CommandLineInterface.cpp
  11. 2
      test/TestHelper.cpp
  12. 2
      test/libdevcore/rlp.cpp
  13. 2
      test/libdevcrypto/SecretStore.cpp
  14. 2
      test/libdevcrypto/hexPrefix.cpp
  15. 6
      test/libdevcrypto/trie.cpp
  16. 2
      test/libethcore/dagger.cpp
  17. 2
      test/libethereum/genesis.cpp

12
ethkey/KeyAux.h

@ -221,26 +221,26 @@ public:
break;
}
case OperationMode::ImportBare:
for (string const& i: m_inputs)
for (string const& input: m_inputs)
{
h128 u;
bytes b;
b = fromHex(i);
b = fromHex(input);
if (b.size() != 32)
{
std::string s = contentsString(i);
std::string s = contentsString(input);
b = fromHex(s);
if (b.size() != 32)
u = store.importKey(i);
u = store.importKey(input);
}
if (!u && b.size() == 32)
u = store.importSecret(b, lockPassword(toAddress(Secret(b)).abridged()));
if (!u)
{
cerr << "Cannot import " << i << " not a file or secret." << endl;
cerr << "Cannot import " << input << " not a file or secret." << endl;
continue;
}
cout << "Successfully imported " << i << " as " << toUUID(u);
cout << "Successfully imported " << input << " as " << toUUID(u);
}
break;
case OperationMode::InspectBare:

21
libdevcore/Common.h

@ -113,25 +113,27 @@ static const u256 Invalid256 = ~(u256)0;
static const bytes NullBytes;
static const std::map<u256, u256> EmptyMapU256U256;
/// Interprets @a _u as a two's complement signed number and returns the resulting s256.
inline s256 u2s(u256 _u)
{
static const bigint c_end = (bigint)1 << 256;
static const u256 c_send = (u256)1 << 255;
if (_u < c_send)
return (s256)_u;
else
return (s256)-(c_end - _u);
static const bigint c_end = bigint(1) << 256;
if (boost::multiprecision::bit_test(_u, 255))
return s256(-(c_end - _u));
else
return s256(_u);
}
/// @returns the two's complement signed representation of the signed number _u.
inline u256 s2u(s256 _u)
{
static const bigint c_end = (bigint)1 << 256;
static const bigint c_end = bigint(1) << 256;
if (_u >= 0)
return (u256)_u;
return u256(_u);
else
return (u256)(c_end + _u);
return u256(c_end + _u);
}
/// @returns the smallest n >= 0 such that (1 << n) >= _x
inline unsigned int toLog2(u256 _x)
{
unsigned ret;
@ -139,6 +141,7 @@ inline unsigned int toLog2(u256 _x)
return ret;
}
/// @returns the absolute distance between _a and _b.
template <class N>
inline N diff(N const& _a, N const& _b)
{

4
libdevcore/CommonData.cpp

@ -93,7 +93,7 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw)
if (h != -1)
ret.push_back(h);
else if (_throw == WhenError::Throw)
throw BadHexCharacter();
BOOST_THROW_EXCEPTION(BadHexCharacter());
else
return bytes();
}
@ -104,7 +104,7 @@ bytes dev::fromHex(std::string const& _s, WhenError _throw)
if (h != -1 && l != -1)
ret.push_back((byte)(h * 16 + l));
else if (_throw == WhenError::Throw)
throw BadHexCharacter();
BOOST_THROW_EXCEPTION(BadHexCharacter());
else
return bytes();
}

5
libdevcore/CommonData.h

@ -68,11 +68,6 @@ int fromHex(char _i, WhenError _throw);
/// If _throw = ThrowType::DontThrow, it replaces bad hex characters with 0's, otherwise it will throw an exception.
bytes fromHex(std::string const& _s, WhenError _throw = WhenError::DontThrow);
#if 0
std::string toBase58(bytesConstRef _data);
bytes fromBase58(std::string const& _s);
#endif
/// Converts byte array to a string containing the same (binary) data. Unless
/// the byte array happens to contain ASCII data, this won't be printable.
inline std::string asString(bytes const& _b)

58
libdevcore/CommonIO.cpp

@ -64,59 +64,35 @@ string dev::memDump(bytes const& _bytes, unsigned _width, bool _html)
return ret.str();
}
// Don't forget to delete[] later.
bytesRef dev::contentsNew(std::string const& _file, bytesRef _dest)
template <typename _T>
inline _T contentsGeneric(std::string const& _file)
{
_T ret;
size_t const c_elementSize = sizeof(typename _T::value_type);
std::ifstream is(_file, std::ifstream::binary);
if (!is)
return bytesRef();
return ret;
// get length of file:
is.seekg (0, is.end);
is.seekg(0, is.end);
streamoff length = is.tellg();
if (length == 0) // return early, MSVC does not like reading 0 bytes
return bytesRef();
if (!_dest.empty() && _dest.size() != (unsigned)length)
return bytesRef();
is.seekg (0, is.beg);
bytesRef ret = _dest.empty() ? bytesRef(new byte[length], length) : _dest;
is.read((char*)ret.data(), length);
is.close();
if (length == 0)
return ret; // do not read empty file (MSVC does not like it)
is.seekg(0, is.beg);
ret.resize((length + c_elementSize - 1) / c_elementSize);
is.read(const_cast<char*>(reinterpret_cast<char const*>(ret.data())), length);
return ret;
}
bytes dev::contents(std::string const& _file)
bytes dev::contents(string const& _file)
{
std::ifstream is(_file, std::ifstream::binary);
if (!is)
return bytes();
// get length of file:
is.seekg (0, is.end);
streamoff length = is.tellg();
if (length == 0) // return early, MSVC does not like reading 0 bytes
return bytes();
is.seekg (0, is.beg);
bytes ret(length);
is.read((char*)ret.data(), length);
is.close();
return ret;
return contentsGeneric<bytes>(_file);
}
string dev::contentsString(std::string const& _file)
string dev::contentsString(string const& _file)
{
std::ifstream is(_file, std::ifstream::binary);
if (!is)
return string();
// get length of file:
is.seekg (0, is.end);
streamoff length = is.tellg();
if (length == 0) // return early, MSVC does not like reading 0 bytes
return string();
is.seekg (0, is.beg);
string ret;
ret.resize(length);
is.read((char*)ret.data(), length);
is.close();
return ret;
return contentsGeneric<string>(_file);
}
void dev::writeFile(std::string const& _file, bytesConstRef _data)

6
libdevcore/CommonIO.h

@ -42,10 +42,14 @@
namespace dev
{
/// Requests the user to enter a password on the console.
std::string getPassword(std::string const& _prompt);
/// Retrieve and returns the contents of the given file. If the file doesn't exist or isn't readable, returns an empty bytes.
/// Retrieve and returns the contents of the given file.
/// If the file doesn't exist or isn't readable, returns an empty container / bytes.
bytes contents(std::string const& _file);
/// Retrieve and returns the contents of the given file as a std::string.
/// If the file doesn't exist or isn't readable, returns an empty container / bytes.
std::string contentsString(std::string const& _file);
/// Retrieve and returns the allocated contents of the given file; if @_dest is given, don't allocate, use it directly.
/// If the file doesn't exist or isn't readable, returns bytesRef(). Don't forget to delete [] the returned value's data when finished.

2
liblll/CodeFragment.cpp

@ -196,7 +196,7 @@ void CodeFragment::constructOperation(sp::utree const& _t, CompilerState& _s)
{
if (_t.size() != 2)
error<IncorrectParameterCount>();
m_asm.append(CodeFragment::compile(asString(contents(firstAsString())), _s).m_asm);
m_asm.append(CodeFragment::compile(contentsString(firstAsString()), _s).m_asm);
}
else if (us == "SET")
{

2
libtestutils/Common.cpp

@ -59,7 +59,7 @@ Json::Value dev::test::loadJsonFromFile(std::string const& _path)
{
Json::Reader reader;
Json::Value result;
string s = asString(dev::contents(_path));
string s = dev::contentsString(_path);
if (!s.length())
ctest << "Contents of " + _path + " is empty. Have you cloned the 'tests' repo branch develop and set ETHEREUM_TEST_PATH to its path?";
else

2
lllc/main.cpp

@ -95,7 +95,7 @@ int main(int argc, char** argv)
}
}
else
src = asString(contents(infile));
src = contentsString(infile);
vector<string> errors;
if (src.empty())

2
solc/CommandLineInterface.cpp

@ -401,7 +401,7 @@ bool CommandLineInterface::processInput()
continue;
}
m_sourceCodes[infile] = asString(dev::contents(infile));
m_sourceCodes[infile] = dev::contentsString(infile);
}
m_compiler.reset(new CompilerStack(m_args["add-std"].as<bool>()));

2
test/TestHelper.cpp

@ -598,7 +598,7 @@ void userDefinedTest(std::function<void(json_spirit::mValue&, bool)> doTests)
{
cnote << "Testing user defined test: " << filename;
json_spirit::mValue v;
string s = asString(contents(filename));
string s = contentsString(filename);
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of " + filename + " is empty. ");
json_spirit::read_string(s, v);
json_spirit::mObject oSingleTest;

2
test/libdevcore/rlp.cpp

@ -67,7 +67,7 @@ namespace dev
string testPath = getTestPath();
testPath += "/BasicTests";
string s = asString(contents(testPath + "/rlptest.json"));
string s = contentsString(testPath + "/rlptest.json");
BOOST_REQUIRE_MESSAGE( s.length() > 0,
"Contents of 'rlptest.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);

2
test/libdevcrypto/SecretStore.cpp

@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(basic_tests)
cnote << "Testing Key Store...";
js::mValue v;
string s = asString(contents(testPath + "/basic_tests.json"));
string s = contentsString(testPath + "/basic_tests.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'KeyStoreTests/basic_tests.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);
for (auto& i: v.get_obj())

2
test/libdevcrypto/hexPrefix.cpp

@ -45,7 +45,7 @@ BOOST_AUTO_TEST_CASE(hexPrefix_test)
cnote << "Testing Hex-Prefix-Encode...";
js::mValue v;
string s = asString(contents(testPath + "/hexencodetest.json"));
string s = contentsString(testPath + "/hexencodetest.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Content from 'hexencodetest.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);
for (auto& i: v.get_obj())

6
test/libdevcrypto/trie.cpp

@ -82,7 +82,7 @@ BOOST_AUTO_TEST_CASE(hex_encoded_securetrie_test)
cnote << "Testing Secure Trie...";
js::mValue v;
string s = asString(contents(testPath + "/hex_encoded_securetrie_test.json"));
string s = contentsString(testPath + "/hex_encoded_securetrie_test.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'hex_encoded_securetrie_test.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);
for (auto& i: v.get_obj())
@ -149,7 +149,7 @@ BOOST_AUTO_TEST_CASE(trie_test_anyorder)
cnote << "Testing Trie...";
js::mValue v;
string s = asString(contents(testPath + "/trieanyorder.json"));
string s = contentsString(testPath + "/trieanyorder.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trieanyorder.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);
for (auto& i: v.get_obj())
@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(trie_tests_ordered)
cnote << "Testing Trie...";
js::mValue v;
string s = asString(contents(testPath + "/trietest.json"));
string s = contentsString(testPath + "/trietest.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'trietest.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);

2
test/libethcore/dagger.cpp

@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(basic_test)
cnote << "Testing Proof of Work...";
js::mValue v;
string s = asString(contents(testPath + "/ethash_tests.json"));
string s = contentsString(testPath + "/ethash_tests.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'ethash_tests.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);
for (auto& i: v.get_obj())

2
test/libethereum/genesis.cpp

@ -54,7 +54,7 @@ BOOST_AUTO_TEST_CASE(genesis_tests)
cnote << "Testing Genesis block...";
js::mValue v;
string s = asString(contents(testPath + "/genesishashestest.json"));
string s = contentsString(testPath + "/genesishashestest.json");
BOOST_REQUIRE_MESSAGE(s.length() > 0, "Contents of 'genesishashestest.json' is empty. Have you cloned the 'tests' repo branch develop?");
js::read_string(s, v);

Loading…
Cancel
Save