1
Fork 0

Json export

This commit is contained in:
Alex Yatskov 2016-11-02 20:06:40 -07:00
parent 631a420278
commit 74bdfc547a
7 changed files with 92 additions and 10 deletions

3
.gitmodules vendored
View File

@ -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

View File

@ -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',
]

View File

@ -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)

1
jansson Submodule

@ -0,0 +1 @@
Subproject commit bc5741fb1ac730ead24e9bd08977fc6c248e04b0

21
main.c
View File

@ -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;
}
}

66
util.c
View File

@ -17,6 +17,7 @@
*/
#include <stdbool.h>
#include <string.h>
#include <stdio.h>
#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);
}

6
util.h
View File

@ -20,6 +20,7 @@
#define UTIL_H
#include <stdlib.h>
#include <stdio.h>
#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