Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add roundtrip tool and needed functions (some are stubs)
- Loading branch information
Showing
5 changed files
with
344 additions
and
3 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* hipack-misc.c | ||
* Copyright (C) 2015 Adrian Perez <aperez@igalia.com> | ||
* | ||
* Distributed under terms of the MIT license. | ||
*/ | ||
|
||
#include "hipack.h" | ||
#include <math.h> | ||
|
||
|
||
bool | ||
hipack_value_equal (const hipack_value_t *a, | ||
const hipack_value_t *b) | ||
{ | ||
assert (a); | ||
assert (b); | ||
|
||
if (a->type != b->type) | ||
return false; | ||
|
||
switch (a->type) { | ||
case HIPACK_INTEGER: | ||
return a->v_integer == b->v_integer; | ||
case HIPACK_BOOL: | ||
return a->v_bool == b->v_bool; | ||
case HIPACK_FLOAT: | ||
return fabs(b->v_float - a->v_float) < 1e-15; | ||
case HIPACK_STRING: | ||
return hipack_string_equal (a->v_string, b->v_string); | ||
case HIPACK_LIST: | ||
return hipack_list_equal (a->v_list, b->v_list); | ||
case HIPACK_DICT: | ||
return hipack_dict_equal (a->v_dict, b->v_dict); | ||
default: | ||
assert (false); // Unreachable. | ||
} | ||
} | ||
|
||
|
||
bool | ||
hipack_list_equal (const hipack_list_t *a, | ||
const hipack_list_t *b) | ||
{ | ||
assert (a); | ||
assert (b); | ||
|
||
if (a->size != b->size) | ||
return false; | ||
|
||
for (uint32_t i = 0; i < a->size; i++) | ||
if (!hipack_value_equal (&a->data[i], &b->data[i])) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
|
||
bool | ||
hipack_dict_equal (const hipack_dict_t *a, | ||
const hipack_dict_t *b) | ||
{ | ||
assert (a); | ||
assert (b); | ||
|
||
if (a->count != b->count) | ||
return false; | ||
|
||
return false; | ||
} | ||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* hipack-writer.c | ||
* Copyright (C) 2015 Adrian Perez <aperez@igalia.com> | ||
* | ||
* Distributed under terms of the MIT license. | ||
*/ | ||
|
||
#include "hipack.h" | ||
|
||
|
||
static inline bool | ||
writechar (hipack_writer_t *writer, int ch) | ||
{ | ||
assert (ch != HIPACK_IO_ERROR); | ||
assert (ch != HIPACK_IO_EOF); | ||
|
||
assert (writer->putchar); | ||
|
||
int ret = (*writer->putchar) (writer->putchar_data, ch); | ||
assert (ret != HIPACK_IO_EOF); | ||
return ret == HIPACK_IO_ERROR; | ||
} | ||
|
||
|
||
#define CHECK_IO(statement) \ | ||
do { \ | ||
if (statement) return true; \ | ||
} while (0) | ||
|
||
|
||
static inline bool | ||
writedata (hipack_writer_t *writer, | ||
const char *data, | ||
uint32_t length) | ||
{ | ||
if (length) { | ||
assert (data); | ||
while (length--) { | ||
CHECK_IO (writechar (writer, *data++)); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
|
||
static inline int | ||
mapdigit (unsigned n) | ||
{ | ||
if (n < 10) { | ||
return '0' + n; | ||
} | ||
if (n < 36) { | ||
return 'A' + (n - 10); | ||
} | ||
assert (false); | ||
return '?'; | ||
} | ||
|
||
|
||
static inline bool | ||
formatint (hipack_writer_t *writer, | ||
int value, | ||
uint8_t base) | ||
{ | ||
assert (writer); | ||
if (value >= base) { | ||
CHECK_IO (formatint (writer, value / base, base)); | ||
} | ||
CHECK_IO (writechar (writer, mapdigit (value % base))); | ||
return false; | ||
} | ||
|
||
|
||
bool | ||
hipack_write_bool (hipack_writer_t *writer, | ||
const bool value) | ||
{ | ||
assert (writer); | ||
if (value) { | ||
return writedata (writer, "True", 4); | ||
} else { | ||
return writedata (writer, "False", 5); | ||
} | ||
} | ||
|
||
|
||
bool | ||
hipack_write_string (hipack_writer_t *writer, | ||
const hipack_string_t *hstr) | ||
{ | ||
assert (writer); | ||
assert (hstr); | ||
CHECK_IO (writechar (writer, '"')); | ||
for (uint32_t i = 0; i < hstr->size; i++) { | ||
switch (hstr->data[i]) { | ||
case 0x09: /* Horizontal tab. */ | ||
CHECK_IO (writechar (writer, '\\')); | ||
CHECK_IO (writechar (writer, 't')); | ||
break; | ||
case 0x0A: /* New line. */ | ||
CHECK_IO (writechar (writer, '\\')); | ||
CHECK_IO (writechar (writer, 'n')); | ||
break; | ||
case 0x0D: /* Carriage return. */ | ||
CHECK_IO (writechar (writer, '\\')); | ||
CHECK_IO (writechar (writer, 'r')); | ||
break; | ||
case 0x22: /* Double quote. */ | ||
CHECK_IO (writechar (writer, '\\')); | ||
CHECK_IO (writechar (writer, '"')); | ||
break; | ||
case 0x5C: /* Backslash. */ | ||
CHECK_IO (writechar (writer, '\\')); | ||
CHECK_IO (writechar (writer, '\\')); | ||
break; | ||
default: | ||
if (hstr->data[i] < 0x20) { | ||
/* ASCII non-printable character. */ | ||
CHECK_IO (writechar (writer, '\\')); | ||
CHECK_IO (formatint (writer, (uint8_t) hstr->data[i], 16)); | ||
} else { | ||
CHECK_IO (writechar (writer, hstr->data[i])); | ||
} | ||
} | ||
} | ||
CHECK_IO (writechar (writer, '"')); | ||
return false; | ||
} | ||
|
||
|
||
bool | ||
hipack_write (hipack_writer_t *writer, | ||
const hipack_dict_t *message) | ||
{ | ||
assert (writer); | ||
assert (message); | ||
} | ||
|
||
|
||
int | ||
hipack_stdio_putchar (void* fp, int ch) | ||
{ | ||
assert (fp); | ||
int ret = fputc (ch, (FILE*) fp); | ||
return (ret == EOF) ? HIPACK_IO_ERROR : ch; | ||
} |
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
Oops, something went wrong.