Commit d20e0353 authored by Kirill Smelkov's avatar Kirill Smelkov

.

parent d1c36492
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
#include <golang/libgolang.h> #include <golang/libgolang.h>
using namespace golang; using namespace golang;
#include <inttypes.h>
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
...@@ -222,3 +224,23 @@ vector<string> split(const string &s, char sep) { ...@@ -222,3 +224,23 @@ vector<string> split(const string &s, char sep) {
} }
} // strings:: } // strings::
// xstrconv:: (strconv-like)
namespace xstrconv {
// parseHex64 decodes 16-character-wide hex-encoded string into uint64.
tuple<uint64_t, error> parseHex64(const string& s) {
if (s.size() != 16)
return make_tuple(0, fmt::errorf("hex64 %s invalid", s.c_str()));
uint64_t v;
int n = sscanf(s.c_str(), "%16" SCNx64, &v); // XXX verify
if (n != 1)
return make_tuple(0, fmt::errorf("hex64 %s invalid", s.c_str()));
return make_tuple(v, nil);
}
} // xstrconv::
...@@ -196,4 +196,12 @@ struct set : std::unordered_set<Key> { ...@@ -196,4 +196,12 @@ struct set : std::unordered_set<Key> {
}; };
// xstrconv::
namespace xstrconv {
tuple<uint64_t, error> parseHex64(const string& s);
tuple<int64_t, error> parseInt(const string& s);
} // xstrconv
#endif #endif
...@@ -701,13 +701,13 @@ static error _parsePinReq(PinReq *pin, const rxPkt *pkt) { ...@@ -701,13 +701,13 @@ static error _parsePinReq(PinReq *pin, const rxPkt *pkt) {
return fmt::errorf("expected 3 arguments, got %zd", argv.size()); return fmt::errorf("expected 3 arguments, got %zd", argv.size());
error err; error err;
tie(pin->foid, err) = _parseHex64(argv[0]); tie(pin->foid, err) = xstrconv::parseHex64(argv[0]);
if (err != nil) if (err != nil)
return fmt::errorf("invalid foid"); return fmt::errorf("invalid foid");
if (!strings::has_prefix(argv[1], '#')) if (!strings::has_prefix(argv[1], '#'))
return fmt::errorf("invalid blk"); return fmt::errorf("invalid blk");
tie(pin->blk, err) = _parseInt(argv[1].substr(1)); tie(pin->blk, err) = xstrconv::parseInt(argv[1].substr(1));
if (err != nil) if (err != nil)
return fmt::errorf("invalid blk"); return fmt::errorf("invalid blk");
...@@ -717,7 +717,7 @@ static error _parsePinReq(PinReq *pin, const rxPkt *pkt) { ...@@ -717,7 +717,7 @@ static error _parsePinReq(PinReq *pin, const rxPkt *pkt) {
if (at == "head") { if (at == "head") {
pin->at = TidHead; pin->at = TidHead;
} else { } else {
tie(pin->at, err) = _parseHex64(at); tie(pin->at, err) = xstrconv::parseHex64(at);
if (err != nil) if (err != nil)
return fmt::errorf("invalid at"); return fmt::errorf("invalid at");
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment