Created
November 16, 2014 04:50
-
-
Save dooglus/863daab1a8c6431b6338 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <boost/spirit/include/classic_core.hpp> | |
#include <iostream> | |
#include <string> | |
#include <stdio.h> | |
#include <stdint.h> | |
using namespace std; | |
using namespace boost::spirit::classic; | |
struct count_satoshis | |
{ | |
count_satoshis(uint64_t& val_) : val(val_) {} | |
void operator()(const char* a, const char* b) const | |
{ | |
val = 0; | |
int i, c; | |
for (i = 0; i < 8; i++) { | |
if (a + i < b) | |
c = a[i] - '0'; | |
else | |
c = 0; | |
val *= 10; | |
val += c; | |
} | |
} | |
uint64_t& val; | |
}; | |
uint_parser<uint64_t, 10, 1, -1> uint_left; | |
uint_parser<uint64_t, 10, 1, 1> digit; | |
bool parse_number(char const* str, int64_t& n) | |
{ | |
bool is_negative = false; | |
uint64_t left = 0, right = 0; | |
count_satoshis save_right(right); | |
rule<> | |
left_ = uint_left[assign_a(left)], | |
right_ = (+digit)[count_satoshis(right)], | |
number_ = !sign_p[assign_a(is_negative)] >> ((!left_) >> ch_p('.') >> right_ | |
| left_ >> !ch_p('.')); | |
bool ret = parse(str, number_).full; | |
if (ret) | |
n = (is_negative ? -1 : 1) * (left * 100000000 + right); | |
return ret; | |
} | |
int main() | |
{ | |
string str; | |
int64_t n; | |
while (getline(cin, str)) | |
if (parse_number(str.c_str(), n)) | |
cout << str << " --> " << n << endl; | |
else | |
cout << "failed" << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Some inputs and outputs: