Add license notifications to all source files
[blerg.git] / common / json.c
1 /* Blerg is (C) 2011 The Dominion of Awesome, and is distributed under a
2  * BSD-style license.  Please see the COPYING file for details.
3  */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
7 #include <yajl/yajl_gen.h>
8 #include "database.h"
9 #include "json.h"
10
11 void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
12         char *data;
13         char number[21];
14         int len;
15
16         if (!blerg_fetch(b, record, &data, &len)) {
17                 fprintf(stderr, "Could not fetch record\n");
18                 return;
19         }
20
21         yajl_gen_map_open(g);
22         if (author != NULL) {
23                 yajl_gen_string(g, "author", 6);
24                 yajl_gen_string(g, author, strlen(author));
25         }
26         yajl_gen_string(g, "record", 6);
27         snprintf(number, 21, "%llu", record);
28         yajl_gen_string(g, number, strlen(number));
29         yajl_gen_string(g, "timestamp", 9);
30         yajl_gen_integer(g, blerg_get_timestamp(b, record));
31         yajl_gen_string(g, "data", 4);
32         yajl_gen_string(g, data, len);
33         yajl_gen_map_close(g);
34
35         free(data);
36 }
37