Created
October 9, 2014 10:33
-
-
Save killgxlin/7a112e2e593e8f168453 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
int | |
write_json(int wfd, Document &doc) { | |
auto &al = doc.GetAllocator(); | |
GenericStringBuffer<UTF8<>, Document::AllocatorType> sb(&al); | |
PrettyWriter<GenericStringBuffer<UTF8<>, Document::AllocatorType>, UTF8<>, UTF8<>, Document::AllocatorType > sbwr(sb, &al); | |
doc.Accept(sbwr); | |
return strndup(sb.GetString(), sb.GetSize()); | |
uint32_t size = sb.GetSize(); | |
assert(write(wfd, &size, sizeof(size)) == sizeof(size)); | |
assert(write(wfd, sb.GetString(), size) == sb.GetSize()); | |
return 0; | |
} | |
int | |
read_json(int rfd, Document &doc) { | |
uint32_t size; | |
assert(read(rfd, &size, sizeof(size)) == sizeof(size)); | |
char* buffer = malloc(size); | |
assert(read(rfd, buffer, size) == size); | |
assert(!doc.Parse(buffer).HasParseError()); | |
return 0; | |
} | |
static void | |
my_test2() { | |
int fds[2]; | |
assert(pipe(fds) == 0); | |
Document doc; | |
assert(write_json(fds[1], doc) == 0); | |
assert(read_json(fds[0], doc) == 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment