2015-08-06 5 views

Odpowiedz

2

W tym przypadku znajduje się avro_writer_memory(), przyjmuje on wskaźnik i długość bufora jako parametry i daje avro_writer_t, który może być używany w zwykłych funkcjach zapisu. Możesz znaleźć jego zastosowanie w testach, takich jak this lub this. Minimalna przykładem będzie coś takiego (wpisywanie wartości zakodowanego na stderr, więc lepiej przekierować że do jakiegoś pliku i zbadać go po przebiegu programu):

#include <avro.h> 
#include <stdio.h> 
#include <unistd.h> 

static const char json_schema[] = "{ \"type\": \"string\" }"; 

int main(void) 
{ 
    char buf[1024]; 
    avro_writer_t writer; 
    avro_schema_t schema; 
    avro_value_iface_t* iface; 
    avro_value_t val; 
    size_t len; 

    if (avro_schema_from_json_literal(json_schema, &schema) != 0) { 
     printf("failed to initialize schema\n"); 
     goto out; 
    } 
    if ((writer = avro_writer_memory(buf, sizeof(buf))) == NULL) { 
     printf("failed to initialize writer\n"); 
     goto out_schema; 
    } 
    if ((iface = avro_generic_class_from_schema(schema)) == NULL) { 
     printf("failed to get class from schema\n"); 
     goto out_writer; 
    } 
    if (avro_generic_value_new(iface, &val) != 0) { 
     printf("failed to create new value\n"); 
     goto out_iface; 
    } 
    if (avro_value_reset(&val) != 0) { 
     printf("failed to reset value\n"); 
     goto out_val; 
    } 
    if (avro_value_set_string(&val, "some string wrapped by avro") != 0) { 
     printf("failed to set value string\n"); 
     goto out_val; 
    } 
    if (avro_value_write(writer, &val) != 0) { 
     printf("failed to write value into the buffer\n"); 
     goto out_val; 
    } 
    len = avro_writer_tell(writer); 
    printf("got %lu bytes\n", (unsigned long)len); 
    if (write(STDERR_FILENO, buf, len) != len) { 
     printf("failed to write to stderr, oops\n"); 
     goto out_val; 
    } 
out_val: 
    avro_value_decref(&val); 
out_iface: 
    avro_value_iface_decref(iface); 
out_writer: 
    avro_writer_free(writer); 
out_schema: 
    avro_schema_decref(schema); 
out: 
    return 0; 
} 

Ponadto, istnieje avro_writer_memory_set_dest() który pozwala ustawić nowy bufor do użycia przez istniejącego autora.

Powiązane problemy