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);
38 void things_free(struct things *things) {
41 for (i = 0; i < things->len; i++) {
42 free(things->arr[i].name);
49 void things_add(struct things *things, time_t t, char *name) {
50 things->arr[things->len].t = t;
51 things->arr[things->len].len = strlen(name);
52 things->arr[things->len].name = malloc(things->arr[things->len].len + 1);
53 strcpy(things->arr[things->len].name, name);
55 if (things->len == things->size) {
57 things->arr = realloc(things->arr, sizeof(struct thing) * things->size);
61 int thing_compare(const void *a, const void *b) {
62 struct thing * aa = (struct thing *) a;
63 struct thing * bb = (struct thing *) b;
67 else if (aa->t > bb->t)
73 void things_sort(struct things *things) {
74 qsort(things->arr, things->len, sizeof(struct thing), thing_compare);
77 struct things * latest_things(const char *path, const char *file, int minlen) {
80 struct things * things = things_alloc();
87 fprintf(stderr, "Could not open %s: ", path);
91 while (f = readdir(d)) {
92 if (f->d_name[0] == '.') continue;
93 if (minlen > 0 && strlen(f->d_name) < minlen) continue;
95 snprintf(filename, 512, "%s/%s/%s", path, f->d_name, file);
97 snprintf(filename, 512, "%s/%s", path, f->d_name);
100 things_add(things, st.st_mtime, f->d_name);
109 void latest_tags(yajl_gen g) {
111 struct things * things = latest_things(blergconf.hash_tags_path, NULL, 3);
113 unsigned int count = (things->len >= 50 ? 50 : things->len);
114 yajl_gen_array_open(g);
115 for (i = 0; i < count; i++) {
116 yajl_gen_string(g, things->arr[i].name, things->arr[i].len);
118 yajl_gen_array_close(g);
123 void latest_records(yajl_gen g) {
125 struct things * things = latest_things(blergconf.data_path, "meta", 0);
127 unsigned int count = (things->len >= 50 ? 50 : things->len);
128 yajl_gen_array_open(g);
129 for (i = 0; i < count; i++) {
130 struct blerg * b = blerg_open(things->arr[i].name);
132 fprintf(stderr, "Could not open blerg for %s\n", things->arr[i].name);
135 uint64_t record_count = blerg_get_record_count(b);
136 if (record_count > 0)
137 json_generate_one_record(g, things->arr[i].name, b, record_count - 1, 1);
140 yajl_gen_array_close(g);
144 int main(int argc, char *argv[]) {
145 const unsigned char *buf;
151 yajl_gen g = yajl_gen_alloc(&yajl_config, NULL);
153 yajl_gen_map_open(g);
154 yajl_gen_string(g, "tags", 4);
158 yajl_gen_get_buf(g, &buf, &len);
159 fwrite(buf, len, 1, stdout);
162 yajl_gen_string(g, "records", 7);
166 yajl_gen_map_close(g);
167 yajl_gen_get_buf(g, &buf, &len);
168 fwrite(buf, len, 1, stdout);