8 #include <yajl/yajl_gen.h>
13 yajl_gen_config yajl_config = {0, 0};
27 struct things * things_alloc() {
28 struct things *things = malloc(sizeof(struct things));
31 things->arr = malloc(sizeof(struct thing) * things->size);
34 void things_free(struct things *things) {
37 for (i = 0; i < things->len; i++) {
38 free(things->arr[i].name);
45 void things_add(struct things *things, time_t t, char *name) {
46 things->arr[things->len].t = t;
47 things->arr[things->len].len = strlen(name);
48 things->arr[things->len].name = malloc(things->arr[things->len].len + 1);
49 strcpy(things->arr[things->len].name, name);
51 if (things->len == things->size) {
53 things->arr = realloc(things->arr, sizeof(struct thing) * things->size);
57 int thing_compare(const void *a, const void *b) {
58 struct thing * aa = (struct thing *) a;
59 struct thing * bb = (struct thing *) b;
63 else if (aa->t > bb->t)
69 void things_sort(struct things *things) {
70 qsort(things->arr, things->len, sizeof(struct thing), thing_compare);
73 struct things * latest_things(const char *path, const char *file) {
76 struct things * things = things_alloc();
82 while (f = readdir(d)) {
83 if (f->d_name[0] == '.') continue;
85 snprintf(filename, 512, "%s/%s/%s", path, f->d_name, file);
87 snprintf(filename, 512, "%s/%s", path, f->d_name);
90 things_add(things, st.st_mtime, f->d_name);
99 void latest_tags(yajl_gen g) {
101 struct things * things = latest_things(HASH_TAGS_PATH, NULL);
103 unsigned int count = (things->len >= 50 ? 50 : things->len);
104 yajl_gen_array_open(g);
105 for (i = 0; i < count; i++) {
106 yajl_gen_string(g, things->arr[i].name, things->arr[i].len);
108 yajl_gen_array_close(g);
113 void latest_records(yajl_gen g) {
115 struct things * things = latest_things(DATA_PATH, "meta");
117 unsigned int count = (things->len >= 50 ? 50 : things->len);
118 yajl_gen_array_open(g);
119 for (i = 0; i < count; i++) {
120 struct blerg * b = blerg_open(things->arr[i].name);
122 fprintf(stderr, "Could not open blerg for %s\n", things->arr[i].name);
125 uint64_t record_count = blerg_get_record_count(b);
126 if (record_count > 0)
127 json_generate_one_record(g, things->arr[i].name, b, record_count - 1);
130 yajl_gen_array_close(g);
134 int main(int argc, char *argv[]) {
135 const unsigned char *buf;
138 yajl_gen g = yajl_gen_alloc(&yajl_config, NULL);
140 yajl_gen_map_open(g);
141 yajl_gen_string(g, "tags", 4);
145 yajl_gen_get_buf(g, &buf, &len);
146 fwrite(buf, len, 1, stdout);
149 yajl_gen_string(g, "records", 7);
153 yajl_gen_map_close(g);
154 yajl_gen_get_buf(g, &buf, &len);
155 fwrite(buf, len, 1, stdout);