/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see .
*/
/** @file Utility.cpp
* @author Gav Wood
* @date 2014
*/
#include "Utility.h"
#include
#include
using namespace std;
using namespace dev;
using namespace dev::eth;
bytes dev::eth::parseData(string const& _args)
{
bytes m_data;
boost::smatch what;
static const boost::regex r("(@|\\$)?\"([^\"]*)\"(\\s.*)?");
static const boost::regex d("(@|\\$)?([0-9]+)(\\s*(ether)|(finney)|(szabo))?(\\s.*)?");
static const boost::regex h("(@|\\$)?(0x)?(([a-fA-F0-9])+)(\\s.*)?");
string s = _args;
while (s.size())
if (boost::regex_match(s, what, d))
{
u256 v((string)what[2]);
if (what[6] == "szabo")
v *= dev::eth::szabo;
else if (what[5] == "finney")
v *= dev::eth::finney;
else if (what[4] == "ether")
v *= dev::eth::ether;
bytes bs = dev::toCompactBigEndian(v);
if (what[1] != "$")
for (auto i = bs.size(); i < 32; ++i)
m_data.push_back(0);
for (auto b: bs)
m_data.push_back(b);
s = what[7];
}
else if (boost::regex_match(s, what, h))
{
bytes bs = fromHex(((what[3].length() & 1) ? "0" : "") + what[3]);
if (what[1] != "$")
for (auto i = bs.size(); i < 32; ++i)
m_data.push_back(0);
for (auto b: bs)
m_data.push_back(b);
s = what[5];
}
else if (boost::regex_match(s, what, r))
{
for (auto i: (string)what[2])
m_data.push_back((byte)i);
if (what[1] != "$")
for (int i = what[2].length(); i < 32; ++i)
m_data.push_back(0);
else
m_data.push_back(0);
s = what[3];
}
else
s = s.substr(1);
return m_data;
}