static void * test_allocator(void *ud, void *ptr, size_t osize, size_t nsize) { AVRO_UNUSED(ud); AVRO_UNUSED(osize); if (nsize == 0) { size_t *size = ((size_t *) ptr) - 1; if (osize != *size) { fprintf(stderr, "Error freeing %p:\n" "Size passed to avro_free (%" PRIsz ") " "doesn't match size passed to " "avro_malloc (%" PRIsz ")\n", ptr, osize, *size); abort(); //exit(EXIT_FAILURE); } free(size); return NULL; } else { size_t real_size = nsize + sizeof(size_t); size_t *old_size = ptr? ((size_t *) ptr)-1: NULL; size_t *size = (size_t *) realloc(old_size, real_size); *size = nsize; return (size + 1); } }
static int avro_memoize_free_key(avro_memoize_key_t *key, void *result, void *dummy) { AVRO_UNUSED(result); AVRO_UNUSED(dummy); avro_freet(avro_memoize_key_t, key); return ST_CONTINUE; }
static int union_free_foreach(int i, avro_schema_t schema, void *arg) { AVRO_UNUSED(i); AVRO_UNUSED(arg); avro_schema_decref(schema); return ST_DELETE; }
static int enum_free_foreach(int i, char *sym, void *arg) { AVRO_UNUSED(i); AVRO_UNUSED(arg); avro_str_free(sym); return ST_DELETE; }
static int avro_datum_value_set_string_len(const avro_value_iface_t *iface, void *vself, const char *str, size_t size) { AVRO_UNUSED(iface); AVRO_UNUSED(size); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); return avro_string_set(self, str); }
static int record_free_foreach(int i, struct avro_record_field_t *field, void *arg) { AVRO_UNUSED(i); AVRO_UNUSED(arg); avro_str_free(field->name); avro_schema_decref(field->type); avro_freet(struct avro_record_field_t, field); return ST_DELETE; }
static void * avro_default_allocator(void *ud, void *ptr, size_t osize, size_t nsize) { AVRO_UNUSED(ud); AVRO_UNUSED(osize); if (nsize == 0) { free(ptr); return NULL; } else { return realloc(ptr, nsize); } }
static int avro_datum_value_append(const avro_value_iface_t *iface, void *vself, avro_value_t *child_out, size_t *new_index) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (!is_avro_array(self)) { avro_set_error("Can only append to array"); return EINVAL; } int rval; avro_schema_t array_schema = avro_datum_get_schema(self); avro_schema_t child_schema = avro_schema_array_items(array_schema); avro_datum_t child_datum = avro_datum_from_schema(child_schema); if (child_datum == NULL) { return ENOMEM; } rval = avro_array_append_datum(self, child_datum); avro_datum_decref(child_datum); if (rval != 0) { return rval; } if (new_index != NULL) { *new_index = avro_array_size(self) - 1; } return avro_datum_as_child_value(child_out, child_datum); }
static int read_record(avro_reader_t reader, const avro_encoding_t * enc, avro_consumer_t *consumer, void *ud) { int rval; size_t num_fields; unsigned int i; AVRO_UNUSED(enc); check(rval, avro_consumer_call(consumer, record_start, ud)); num_fields = avro_schema_record_size(consumer->schema); for (i = 0; i < num_fields; i++) { avro_consumer_t *field_consumer = NULL; void *field_ud = NULL; check(rval, avro_consumer_call(consumer, record_field, i, &field_consumer, &field_ud, ud)); if (field_consumer) { check(rval, avro_consume_binary(reader, field_consumer, field_ud)); } else { avro_schema_t field_schema = avro_schema_record_field_get_by_index(consumer->schema, i); check(rval, avro_skip_data(reader, field_schema)); } } return 0; }
static int avro_datum_value_get_size(const avro_value_iface_t *iface, const void *vself, size_t *size) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (is_avro_array(self)) { *size = avro_array_size(self); return 0; } if (is_avro_map(self)) { *size = avro_map_size(self); return 0; } if (is_avro_record(self)) { avro_schema_t schema = avro_datum_get_schema(self); *size = avro_schema_record_size(schema); return 0; } avro_set_error("Can only get size of array, map, or record, %d", avro_typeof(self)); return EINVAL; }
static avro_schema_t avro_datum_value_get_schema(const avro_value_iface_t *iface, const void *vself) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(NULL, self, "datum instance"); return avro_datum_get_schema(self); }
static int avro_datum_value_set_null(const avro_value_iface_t *iface, void *vself) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, is_avro_null(self), "datum instance"); return 0; }
static int write_enum(avro_writer_t writer, const avro_encoding_t * enc, struct avro_enum_schema_t *enump, struct avro_enum_datum_t *datum) { AVRO_UNUSED(enump); return enc->write_long(writer, datum->value); }
static int avro_datum_value_reset(const avro_value_iface_t *iface, void *vself) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); return avro_datum_reset(self); }
static int avro_datum_value_set_long(const avro_value_iface_t *iface, void *vself, int64_t val) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); return avro_int64_set(self, val); }
static int avro_datum_value_set_fixed(const avro_value_iface_t *iface, void *vself, void *buf, size_t size) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); return avro_fixed_set(self, (const char *) buf, size); }
static int avro_datum_value_get_enum(const avro_value_iface_t *iface, const void *vself, int *out) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, is_avro_enum(self), "datum instance"); *out = avro_enum_get(self); return 0; }
static int avro_datum_value_add(const avro_value_iface_t *iface, void *vself, const char *key, avro_value_t *child, size_t *index, int *is_new) { AVRO_UNUSED(iface); avro_datum_t self = (avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (!is_avro_map(self)) { avro_set_error("Can only add to map"); return EINVAL; } int rval; avro_datum_t child_datum; if (avro_map_get(self, key, &child_datum) == 0) { /* key already exists */ if (is_new != NULL) { *is_new = 0; } if (index != NULL) { int real_index; avro_map_get_index(self, key, &real_index); *index = real_index; } return avro_datum_as_child_value(child, child_datum); } /* key is new */ avro_schema_t map_schema = avro_datum_get_schema(self); avro_schema_t child_schema = avro_schema_map_values(map_schema); child_datum = avro_datum_from_schema(child_schema); if (child_datum == NULL) { return ENOMEM; } rval = avro_map_set(self, key, child_datum); avro_datum_decref(child_datum); if (rval != 0) { return rval; } if (is_new != NULL) { *is_new = 1; } if (index != NULL) { *index = avro_map_size(self) - 1; } return avro_datum_as_child_value(child, child_datum); }
int avro_schema_from_json(const char *jsontext, const int32_t len, avro_schema_t *schema, avro_schema_error_t *e) { check_param(EINVAL, jsontext, "JSON text"); check_param(EINVAL, schema, "schema pointer"); json_t *root; json_error_t json_error; AVRO_UNUSED(len); AVRO_UNUSED(e); root = json_loads(jsontext, 0, &json_error); if (!root) { avro_set_error("Error parsing JSON: %s", json_error.text); return EINVAL; } return avro_schema_from_json_root(root, schema); }
int main(int argc, char **argv) { AVRO_UNUSED(argc); AVRO_UNUSED(argv); avro_value_t v1; avro_value_t v2; try(avro_generic_string_new(&v1, "test string a"), "Cannot create string value"); try(avro_generic_string_new(&v2, "test string b"), "Cannot create string value"); if (avro_value_equal(&v1, &v2)) { fprintf(stderr, "Unexpected avro_value_equal\n"); return EXIT_FAILURE; } if (avro_value_equal_fast(&v1, &v2)) { fprintf(stderr, "Unexpected avro_value_equal_fast\n"); return EXIT_FAILURE; } if (avro_value_cmp(&v1, &v2) >= 0) { fprintf(stderr, "Unexpected avro_value_cmp\n"); return EXIT_FAILURE; } if (avro_value_cmp_fast(&v1, &v2) >= 0) { fprintf(stderr, "Unexpected avro_value_cmp_fast\n"); return EXIT_FAILURE; } avro_value_decref(&v1); avro_value_decref(&v2); return 0; }
static int avro_datum_value_get_long(const avro_value_iface_t *iface, const void *vself, int64_t *out) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; int64_t value; check(rval, avro_int64_get(self, &value)); *out = value; return 0; }
static int avro_datum_value_get_discriminant(const avro_value_iface_t *iface, const void *vself, int *out) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (!is_avro_union(self)) { avro_set_error("Can only get discriminant of union"); return EINVAL; } *out = avro_union_discriminant(self); return 0; }
static int avro_datum_value_get_current_branch(const avro_value_iface_t *iface, const void *vself, avro_value_t *branch) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (!is_avro_union(self)) { avro_set_error("Can only get current branch of union"); return EINVAL; } avro_datum_t child_datum = avro_union_current_branch(self); return avro_datum_as_child_value(branch, child_datum); }
static int avro_datum_value_grab_fixed(const avro_value_iface_t *iface, const void *vself, avro_wrapped_buffer_t *dest) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; char *bytes; int64_t sz; check(rval, avro_fixed_get(self, &bytes, &sz)); /* nothing clever, just make a copy */ return avro_wrapped_buffer_new_copy(dest, bytes, sz); }
static int avro_datum_value_grab_string(const avro_value_iface_t *iface, const void *vself, avro_wrapped_buffer_t *dest) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; char *str; size_t sz; check(rval, avro_string_get(self, &str)); sz = strlen(str); /* nothing clever, just make a copy */ /* sz doesn't contain NUL terminator */ return avro_wrapped_buffer_new_copy(dest, str, sz+1); }
static int avro_datum_value_get_by_index(const avro_value_iface_t *iface, const void *vself, size_t index, avro_value_t *child, const char **name) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; avro_datum_t child_datum; if (is_avro_array(self)) { check(rval, avro_array_get(self, index, &child_datum)); return avro_datum_as_child_value(child, child_datum); } if (is_avro_map(self)) { const char *real_key; check(rval, avro_map_get_key(self, index, &real_key)); if (name != NULL) { *name = real_key; } check(rval, avro_map_get(self, real_key, &child_datum)); return avro_datum_as_child_value(child, child_datum); } if (is_avro_record(self)) { avro_schema_t schema = avro_datum_get_schema(self); const char *field_name = avro_schema_record_field_name(schema, index); if (field_name == NULL) { return EINVAL; } if (name != NULL) { *name = field_name; } check(rval, avro_record_get(self, field_name, &child_datum)); return avro_datum_as_child_value(child, child_datum); } avro_set_error("Can only get by index from array, map, or record"); return EINVAL; }
static int avro_datum_value_get_string(const avro_value_iface_t *iface, const void *vself, const char **str, size_t *size) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; char *value; check(rval, avro_string_get(self, &value)); if (str != NULL) { *str = (const char *) value; } if (size != NULL) { *size = strlen(value)+1; } return 0; }
static int avro_datum_value_set_branch(const avro_value_iface_t *iface, void *vself, int discriminant, avro_value_t *branch) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); if (!is_avro_union(self)) { avro_set_error("Can only set branch of union"); return EINVAL; } int rval; avro_datum_t child_datum; check(rval, avro_union_set_discriminant(self, discriminant, &child_datum)); return avro_datum_as_child_value(branch, child_datum); }
static int avro_datum_value_get_fixed(const avro_value_iface_t *iface, const void *vself, const void **buf, size_t *size) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; check_param(EINVAL, self, "datum instance"); int rval; char *bytes; int64_t sz; check(rval, avro_fixed_get(self, &bytes, &sz)); if (buf != NULL) { *buf = (const void *) bytes; } if (size != NULL) { *size = sz; } return 0; }
static avro_type_t avro_datum_value_get_type(const avro_value_iface_t *iface, const void *vself) { AVRO_UNUSED(iface); const avro_datum_t self = (const avro_datum_t) vself; #ifdef _WIN32 #pragma message("#warning: Bug: EINVAL is not of type avro_type_t.") #else //#warning "Bug: EINVAL is not of type avro_type_t." #endif /* We shouldn't use EINVAL as the return value to * check_param(), because EINVAL (= 22) is not a valid enum * avro_type_t. This is a structural issue -- we would need a * different interface on all the get_type functions to fix * this. For now, suppressing the error by casting EINVAL to * (avro_type_t) so the code compiles under C++. */ check_param((avro_type_t) EINVAL, self, "datum instance"); return avro_typeof(self); }