Last active
February 2, 2017 18:49
-
-
Save alexsnaps/1296470ed59618d7a12db607aaddc3f2 to your computer and use it in GitHub Desktop.
msgpack EXT issue
This file contains 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 <iostream> | |
#include <msgpack.hpp> | |
#include <unistd.h> | |
static const std::size_t MSGPACK_CHUNK_SIZE = 8 * 1024; // 64KB works fine! | |
void assert_key_exists(const msgpack::object &object, const char string[6]); | |
bool equal_to_symbol_p(const msgpack::object &object, const std::string &string); | |
void dump_map(const msgpack::object &object); | |
static const int SYMBOL_EXT_CODE = 0x00; | |
int main() { | |
// auto src = "simple_no_ext.mp"; | |
auto src = "simple.mp"; | |
auto file = fopen(src, "r"); | |
if (file == NULL) { | |
perror(src); | |
exit(1); | |
} | |
auto fd = fileno(file); | |
msgpack::unpacker unpacker; | |
msgpack::object_handle result; | |
size_t in = 0; | |
for (;;) { | |
unpacker.reserve_buffer(MSGPACK_CHUNK_SIZE); | |
auto input_size = read(fd, unpacker.buffer(), unpacker.buffer_capacity()); | |
if (input_size < 0) { | |
exit(1); | |
} | |
in += input_size; | |
unpacker.buffer_consumed(static_cast<std::size_t>(input_size)); | |
if (unpacker.next(result)) { | |
assert_key_exists(result.get(), "input"); | |
assert_key_exists(result.get(), "sources"); | |
break; | |
} | |
} | |
fprintf(stdout, "Read %zu bytes\n", in); | |
dump_map(result.get()); | |
} | |
void dump_map(const msgpack::object &object) { | |
fprintf(stdout, "\n\nThe map has %d elements: \n", object.via.map.size); | |
for (int i = 0; i < object.via.map.size; i++) { | |
auto ext = object.via.map.ptr[i].key.via.ext; | |
fprintf(stdout, " + key '%s'\n", std::string(ext.data(), ext.size).c_str()); | |
} | |
} | |
void assert_key_exists(const msgpack::object &object, const char *key) { | |
if (object.type == msgpack::type::MAP) { | |
for (auto &element : object.via.map) { | |
if (equal_to_symbol_p(element.key, key)) { | |
fprintf(stdout, "Did find key '%s'\n", key); | |
return; | |
} | |
} | |
} | |
fprintf(stderr, "Did NOT find key '%s'\n", key); | |
} | |
bool equal_to_symbol_p(const msgpack::object &object, const std::string &string) { | |
// if (object.type != msgpack::type::STR) { | |
if (object.type != msgpack::type::EXT) { | |
return false; | |
} | |
// auto ext = object.via.str; | |
msgpack::object_ext ext = object.via.ext; | |
if (ext.type() != SYMBOL_EXT_CODE) { | |
return false; | |
} | |
// return ext.size == string.length() && std::equal(ext.ptr, ext.ptr + ext.size, string.begin()); | |
return ext.size == string.length() && std::equal(ext.data(), ext.data() + ext.size, string.begin()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment