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.
11 #include <yajl/yajl_gen.h>
15 #include "configuration.h"
17 yajl_gen_config yajl_config = {0, 0};
31 struct things * things_alloc() {
32 struct things *things = malloc(sizeof(struct things));
35 things->arr = malloc(sizeof(struct thing) * things->size);
39 void things_free(struct things *things) {
42 for (i = 0; i < things->len; i++) {
43 free(things->arr[i].name);
50 void things_add(struct things *things, time_t t, char *name) {
51 things->arr[things->len].t = t;
52 things->arr[things->len].len = strlen(name);
53 things->arr[things->len].name = malloc(things->arr[things->len].len + 1);
54 strcpy(things->arr[things->len].name, name);
56 if (things->len == things->size) {
58 things->arr = realloc(things->arr, sizeof(struct thing) * things->size);
62 int thing_compare(const void *a, const void *b) {
63 struct thing * aa = (struct thing *) a;
64 struct thing * bb = (struct thing *) b;
68 else if (aa->t > bb->t)
74 void things_sort(struct things *things) {
75 qsort(things->arr, things->len, sizeof(struct thing), thing_compare);
78 struct things * latest_things(const char *path, const char *file, int minlen) {
81 struct things * things = things_alloc();
88 fprintf(stderr, "Could not open %s: ", path);
92 while ((f = readdir(d)) != NULL) {
93 if (f->d_name[0] == '.') continue;
94 if (minlen > 0 && strlen(f->d_name) < minlen) continue;
96 snprintf(filename, 512, "%s/%s/%s", path, f->d_name, file);
98 snprintf(filename, 512, "%s/%s", path, f->d_name);
101 things_add(things, st.st_mtime, f->d_name);
110 void latest_tags(yajl_gen g) {
112 struct things * things = latest_things(blergconf.hash_tags_path, NULL, 3);
114 unsigned int count = (things->len >= 50 ? 50 : things->len);
115 yajl_gen_array_open(g);
116 for (i = 0; i < count; i++) {
117 yajl_gen_string(g, (unsigned char *)things->arr[i].name, things->arr[i].len);
119 yajl_gen_array_close(g);
124 void latest_records(yajl_gen g) {
126 struct things * things = latest_things(blergconf.data_path, "meta", 0);
128 unsigned int count = (things->len >= 50 ? 50 : things->len);
129 yajl_gen_array_open(g);
130 for (i = 0; i < count; i++) {
131 struct blerg * b = blerg_open(things->arr[i].name);
133 fprintf(stderr, "Could not open blerg for %s\n", things->arr[i].name);
136 uint64_t record_count = blerg_get_record_count(b);
137 if (record_count > 0)
138 json_generate_one_record(g, things->arr[i].name, b, record_count - 1, 1);
141 yajl_gen_array_close(g);
145 int main(int argc, char *argv[]) {
146 const unsigned char *buf;
152 yajl_gen g = yajl_gen_alloc(&yajl_config, NULL);
154 yajl_gen_map_open(g);
155 yajl_gen_string(g, (unsigned char *)"tags", 4);
159 yajl_gen_get_buf(g, &buf, &len);
160 fwrite(buf, len, 1, stdout);
163 yajl_gen_string(g, (unsigned char *)"records", 7);
167 yajl_gen_map_close(g);
168 yajl_gen_get_buf(g, &buf, &len);
169 fwrite(buf, len, 1, stdout);