From 7d8ef1902ef3030991b50a9414116a0615ac49d9 Mon Sep 17 00:00:00 2001 From: Chip Black Date: Wed, 2 Feb 2011 17:17:39 -0600 Subject: [PATCH] Add brief mode of JSON generation for blerglatest --- cgi/cgi_blerg.c | 4 ++-- common/json.c | 12 +++++++++++- common/json.h | 2 +- http/http_blerg.c | 4 ++-- tools/blerglatest.c | 2 +- 5 files changed, 17 insertions(+), 7 deletions(-) diff --git a/cgi/cgi_blerg.c b/cgi/cgi_blerg.c index 737910c..9974513 100644 --- a/cgi/cgi_blerg.c +++ b/cgi/cgi_blerg.c @@ -33,7 +33,7 @@ void respond_for_range(struct blerg *b, uint64_t from, uint64_t to) { yajl_gen_array_open(g); for (i = to; i != from - 1; i--) { - json_generate_one_record(g, NULL, b, i); + json_generate_one_record(g, NULL, b, i, 0); yajl_gen_get_buf(g, &ybuf, &len); fwrite(ybuf, len, 1, stdout); yajl_gen_clear(g); @@ -61,7 +61,7 @@ void respond_taglist(struct tag *results, int i) { while (i >= 0) { b = blerg_open(results[i].author); if (b != NULL) { - json_generate_one_record(g, results[i].author, b, results[i].record); + json_generate_one_record(g, results[i].author, b, results[i].record, 0); blerg_close(b); } yajl_gen_get_buf(g, &ybuf, &len); diff --git a/common/json.c b/common/json.c index 5494e4a..eebea2c 100644 --- a/common/json.c +++ b/common/json.c @@ -8,7 +8,7 @@ #include "database.h" #include "json.h" -void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record) { +void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record, int brief) { char *data; char number[21]; int len; @@ -18,6 +18,16 @@ void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, u return; } + if (brief && len > 150) { + len = 150; + /* This is a bit of a hackish way to add an ellipsis, and will + * probably cause weird results if we overwrite part of a UTF-8 + * char */ + data[149] = '.'; + data[148] = '.'; + data[147] = '.'; + } + yajl_gen_map_open(g); if (author != NULL) { yajl_gen_string(g, "author", 6); diff --git a/common/json.h b/common/json.h index 78cb4d7..8a73e4c 100644 --- a/common/json.h +++ b/common/json.h @@ -4,6 +4,6 @@ #ifndef _JSON_H #define _JSON_H -void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record); +void json_generate_one_record(yajl_gen g, const char *author, struct blerg *b, uint64_t record, int brief); #endif /* _JSON_H */ diff --git a/http/http_blerg.c b/http/http_blerg.c index 9fc72a7..6fd8f11 100644 --- a/http/http_blerg.c +++ b/http/http_blerg.c @@ -75,7 +75,7 @@ ssize_t GET_generate_list(void *cls, uint64_t pos, char *buf, size_t max) { } /* Snarf one record */ - json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i]); + json_generate_one_record(gs->g, NULL, gs->b, gs->entries[gs->i], 0); if (gs->i == 0) { yajl_gen_array_close(gs->g); @@ -136,7 +136,7 @@ ssize_t GET_generate_taglist(void *cls, uint64_t pos, char *buf, size_t max) { /* Snarf one record */ b = blerg_open(ts->results[ts->i].author); if (b != NULL) { - json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record); + json_generate_one_record(ts->g, ts->results[ts->i].author, b, ts->results[ts->i].record, 0); blerg_close(b); } diff --git a/tools/blerglatest.c b/tools/blerglatest.c index a2d1c67..727eb18 100644 --- a/tools/blerglatest.c +++ b/tools/blerglatest.c @@ -124,7 +124,7 @@ void latest_records(yajl_gen g) { } uint64_t record_count = blerg_get_record_count(b); if (record_count > 0) - json_generate_one_record(g, things->arr[i].name, b, record_count - 1); + json_generate_one_record(g, things->arr[i].name, b, record_count - 1, 1); blerg_close(b); } yajl_gen_array_close(g); -- 2.25.1