From 74bdfc547ad0f29d17dd6457e025c957c70bf510 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Wed, 2 Nov 2016 20:06:40 -0700 Subject: [PATCH] Json export --- .gitmodules | 3 +++ .ycm_extra_conf.py | 1 + CMakeLists.txt | 4 +-- jansson | 1 + main.c | 21 ++++++++++----- util.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++ util.h | 6 ++++- 7 files changed, 92 insertions(+), 10 deletions(-) create mode 160000 jansson diff --git a/.gitmodules b/.gitmodules index f47b583..9472fac 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "eb"] path = eb url = https://github.com/FooSoft/eb.git +[submodule "jansson"] + path = jansson + url = https://github.com/akheron/jansson.git diff --git a/.ycm_extra_conf.py b/.ycm_extra_conf.py index 9f4b23c..99f7196 100644 --- a/.ycm_extra_conf.py +++ b/.ycm_extra_conf.py @@ -5,6 +5,7 @@ def FlagsForFile(filename, **kwargs): '-Werror', '-std=gnu11', '-I/mnt/projects/zero-epwing/eb', + '-I/mnt/projects/zero-epwing/jansson/include', '-x', 'c', ] diff --git a/CMakeLists.txt b/CMakeLists.txt index f9b84ad..51b7aa1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) project(zero-epwing) include_directories(eb) -link_directories(eb/eb/.libs) +link_directories(eb/eb/.libs jansson/lib) add_executable(zero-epwing main.c util.c convert.c) -target_link_libraries(zero-epwing libeb.a libz.a) +target_link_libraries(zero-epwing libeb.a libz.a libjansson.a) diff --git a/jansson b/jansson new file mode 160000 index 0000000..bc5741f --- /dev/null +++ b/jansson @@ -0,0 +1 @@ +Subproject commit bc5741fb1ac730ead24e9bd08977fc6c248e04b0 diff --git a/main.c b/main.c index 9681bab..7bf6f2a 100644 --- a/main.c +++ b/main.c @@ -119,6 +119,9 @@ static void export_book(const char path[], Book* book) { case EB_CHARCODE_JISX0208_GB2312: strcpy(book->character_code, "jisx0208/gb2312"); break; + default: + strcpy(book->character_code, "invalid"); + break; } } @@ -131,6 +134,9 @@ static void export_book(const char path[], Book* book) { case EB_DISC_EPWING: strcpy(book->disc_code, "epwing"); break; + default: + strcpy(book->disc_code, "invalid"); + break; } } @@ -161,13 +167,14 @@ static void export_book(const char path[], Book* book) { int main(int argc, char *argv[]) { if (argc != 2) { - fprintf(stderr, "usage: %s path\n", argv[0]); + fprintf(stderr, "usage: %s dictionary_path\n", argv[0]); return 2; } - - Book book = {}; - export_book(argv[1], &book); - free_book(&book); - - return 1; + else { + Book book = {}; + export_book(argv[1], &book); + dump_book(&book, stdout); + free_book(&book); + return 1; + } } diff --git a/util.c b/util.c index a776ff1..f9b17fc 100644 --- a/util.c +++ b/util.c @@ -17,6 +17,7 @@ */ #include +#include #include #include "util.h" @@ -26,6 +27,8 @@ #include "eb/eb/error.h" #include "eb/eb/text.h" +#include "jansson/include/jansson.h" + #define MAX_TEXT 1024 char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode) { @@ -82,3 +85,66 @@ void free_book(Book* book) { free(subbook->entries); } } + +static void encode_entry(Entry* entry, json_t* entry_json) { + json_object_set_new(entry_json, "heading", json_string(entry->heading)); + json_object_set_new(entry_json, "text", json_string(entry->text)); +} + +static void encode_subbook(Subbook* subbook, json_t* subbook_json) { + if (subbook->title != NULL) { + json_object_set_new(subbook_json, "title", json_string(subbook->title)); + } + + if (subbook->copyright != NULL) { + json_object_set_new(subbook_json, "copyright", json_string(subbook->copyright)); + } + + if (strlen(subbook->error) > 0) { + json_object_set_new(subbook_json, "error", json_string(subbook->error)); + } + + json_t* entry_json_array = json_array(); + for (int i = 0; i < subbook->entry_count; ++i) { + json_t* entry_json = json_object(); + encode_entry(subbook->entries + i, entry_json); + json_array_append(entry_json_array, entry_json); + json_decref(entry_json); + } + + json_object_set(subbook_json, "entries", entry_json_array); + json_decref(entry_json_array); +} + +static void encode_book(Book* book, json_t* book_json) { + json_object_set_new(book_json, "characterCode", json_string(book->character_code)); + json_object_set_new(book_json, "discCode", json_string(book->disc_code)); + + if (strlen(book->error) > 0) { + json_object_set_new(book_json, "error", json_string(book->error)); + } + + json_t* subbook_json_array = json_array(); + for (int i = 0; i < book->subbook_count; ++i) { + json_t* subbook_json = json_object(); + encode_subbook(book->subbooks + i, subbook_json); + json_array_append(subbook_json_array, subbook_json); + json_decref(subbook_json); + } + + json_object_set(book_json, "subbooks", subbook_json_array); + json_decref(subbook_json_array); +} + +void dump_book(Book* book, FILE* fp) { + json_t* book_json = json_object(); + encode_book(book, book_json); + + char* output = json_dumps(book_json, JSON_COMPACT); + if (output != NULL) { + fputs(output, fp); + } + free(output); + + json_decref(book_json); +} diff --git a/util.h b/util.h index 1265a8e..09490bc 100644 --- a/util.h +++ b/util.h @@ -20,6 +20,7 @@ #define UTIL_H #include +#include #include "eb/eb/eb.h" /* @@ -74,6 +75,9 @@ typedef enum { } ReadMode; char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode); -void free_book(Book* book); + +void free_book(Book* book); +void dump_book(Book* book, FILE* fp); + #endif