Add RSS CGI, also quite a lot of refactoring
[blerg.git] / common / json.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <yajl/yajl_gen.h>
5 #include "database.h"
6 #include "json.h"
7
8 void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) {
9         char *data;
10         char number[21];
11         int len;
12
13         if (!blerg_fetch(b, record, &data, &len)) {
14                 fprintf(stderr, "Could not fetch record\n");
15                 return;
16         }
17
18         yajl_gen_map_open(g);
19         if (author != NULL) {
20                 yajl_gen_string(g, "author", 6);
21                 yajl_gen_string(g, author, strlen(author));
22         }
23         yajl_gen_string(g, "record", 6);
24         snprintf(number, 21, "%llu", record);
25         yajl_gen_string(g, number, strlen(number));
26         yajl_gen_string(g, "timestamp", 9);
27         yajl_gen_integer(g, blerg_get_timestamp(b, record));
28         yajl_gen_string(g, "data", 4);
29         yajl_gen_string(g, data, len);
30         yajl_gen_map_close(g);
31
32         free(data);
33 }
34