1
This commit is contained in:
Alex Yatskov 2016-11-28 21:16:43 -08:00
parent 71524395f6
commit 49b9497087
7 changed files with 56 additions and 56 deletions

View File

@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.5)
project(zero-epwing)
include_directories(eb)
link_directories(eb/eb/.libs jansson/lib)
add_executable(zero-epwing main.c book.c gaiji.c convert.c hooks.c)
add_executable(zero-epwing main.c book.c font.c convert.c hooks.c)
target_link_libraries(zero-epwing libeb.a libz.a libjansson.a)

14
book.c
View File

@ -47,7 +47,7 @@ static char* book_read(
EB_Hookset* hookset,
const EB_Position* position,
Book_Mode mode,
const Gaiji_Table* table
const Font_Table* table
) {
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
@ -93,7 +93,7 @@ static char* book_read(
return NULL;
}
gaiji_stub_decode(result, strlen(result) + 1, result);
font_stub_decode(result, strlen(result) + 1, result);
return result;
}
@ -139,7 +139,7 @@ static void book_encode(Book* book, json_t* book_json) {
json_decref(subbook_json_array);
}
static void subbook_entries_export(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, const Gaiji_Table* table) {
static void subbook_entries_export(Book_Subbook* subbook, EB_Book* eb_book, EB_Hookset* eb_hookset, const Font_Table* table) {
if (subbook->entry_capacity == 0) {
subbook->entry_capacity = 16384;
subbook->entries = malloc(subbook->entry_capacity * sizeof(Book_Entry));
@ -169,12 +169,12 @@ static void subbook_entries_export(Book_Subbook* subbook, EB_Book* eb_book, EB_H
while (hit_count > 0);
}
static void subbook_export(Book_Subbook* subbook, const Gaiji_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) {
const Gaiji_Table* table = NULL;
static void subbook_export(Book_Subbook* subbook, const Font_Context* context, EB_Book* eb_book, EB_Hookset* eb_hookset) {
const Font_Table* table = NULL;
char title[EB_MAX_TITLE_LENGTH + 1];
if (eb_subbook_title(eb_book, title) == EB_SUCCESS) {
subbook->title = eucjp_to_utf8(title);
table = gaiji_table_select(context, subbook->title);
table = font_table_select(context, subbook->title);
}
if (eb_have_copyright(eb_book)) {
@ -236,7 +236,7 @@ bool book_dump(Book* book, bool pretty_print, FILE* fp) {
}
bool book_export(Book* book, const Gaiji_Context* context, const char path[], bool markup) {
bool book_export(Book* book, const Font_Context* context, const char path[], bool markup) {
EB_Error_Code error;
if ((error = eb_initialize_library()) != EB_SUCCESS) {
fprintf(stderr, "Failed to initialize library: %s\n", eb_error_message(error));

4
book.h
View File

@ -22,7 +22,7 @@
#include <stdbool.h>
#include <stdio.h>
#include "gaiji.h"
#include "font.h"
/*
* Types
@ -54,7 +54,7 @@ typedef struct {
void book_init(Book* book);
void book_free(Book* book);
bool book_export(Book* book, const Gaiji_Context* context, const char path[], bool markup);
bool book_export(Book* book, const Font_Context* context, const char path[], bool markup);
bool book_dump(Book* book, bool pretty_print, FILE* fp);
#endif /* BOOK_H */

View File

@ -24,7 +24,7 @@
#include "jansson/include/jansson.h"
#include "util.h"
#include "gaiji.h"
#include "font.h"
/*
* Local functions
@ -82,7 +82,7 @@ static void encode_sequence(char output[], int size, const char utf8[]) {
strncat(output, "}", size);
}
static void parse_entry(Gaiji_Entry* entry, const json_t* entry_json) {
static void parse_entry(Font_Entry* entry, const json_t* entry_json) {
entry->code = json_integer_value(json_array_get(entry_json, 0));
const char* utf8 = json_string_value(json_array_get(entry_json, 1));
if (utf8 == NULL) {
@ -94,20 +94,20 @@ static void parse_entry(Gaiji_Entry* entry, const json_t* entry_json) {
}
}
static void parse_entries(Gaiji_Entry** entries, int* count, const json_t* entry_array_json) {
static void parse_entries(Font_Entry** entries, int* count, const json_t* entry_array_json) {
*count = json_array_size(entry_array_json);
if (*count == 0) {
*entries = NULL;
}
else {
*entries = malloc(sizeof(Gaiji_Entry) * *count);
*entries = malloc(sizeof(Font_Entry) * *count);
for (int i = 0; i < *count; ++i) {
parse_entry(*entries + i, json_array_get(entry_array_json, i));
}
}
}
static void parse_table(Gaiji_Table* table, const json_t* table_json) {
static void parse_table(Font_Table* table, const json_t* table_json) {
const char* name = json_string_value(json_object_get(table_json, "name"));
if (name == NULL) {
*table->name = 0;
@ -118,25 +118,25 @@ static void parse_table(Gaiji_Table* table, const json_t* table_json) {
}
parse_entries(
(Gaiji_Entry**)&table->table_wide,
(Font_Entry**)&table->table_wide,
&table->table_wide_size,
json_object_get(table_json, "wide")
);
parse_entries(
(Gaiji_Entry**)&table->table_narrow,
(Font_Entry**)&table->table_narrow,
&table->table_narrow_size,
json_object_get(table_json, "narrow")
);
}
static void parse_table_array(Gaiji_Context* context, const json_t* table_array_json) {
static void parse_table_array(Font_Context* context, const json_t* table_array_json) {
context->table_count = json_array_size(table_array_json);
if (context->table_count == 0) {
context->tables = NULL;
}
else {
context->tables = malloc(sizeof(Gaiji_Table) * context->table_count);
context->tables = malloc(sizeof(Font_Table) * context->table_count);
for (int i = 0; i < context->table_count; ++i) {
parse_table(context->tables + i, json_array_get(table_array_json, i));
}
@ -147,9 +147,9 @@ static void parse_table_array(Gaiji_Context* context, const json_t* table_array_
* Exported functions
*/
const Gaiji_Table* gaiji_table_select(const Gaiji_Context* context, const char name[]) {
const Font_Table* font_table_select(const Font_Context* context, const char name[]) {
for (int i = 0; i < context->table_count; ++i) {
const Gaiji_Table* table = context->tables + i;
const Font_Table* table = context->tables + i;
if (strstr(name, table->name) != NULL) {
return table;
}
@ -158,21 +158,21 @@ const Gaiji_Table* gaiji_table_select(const Gaiji_Context* context, const char n
return NULL;
}
void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* table, Gaiji_Width width) {
void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width) {
do {
if (table == NULL) {
break;
}
const Gaiji_Entry* entries = NULL;
const Font_Entry* entries = NULL;
int count = 0;
switch (width) {
case GAIJI_WIDTH_WIDE:
case FONT_WIDTH_WIDE:
entries = table->table_wide;
count = table->table_wide_size;
break;
case GAIJI_WIDTH_NARROW:
case FONT_WIDTH_NARROW:
entries = table->table_narrow;
count = table->table_narrow_size;
break;
@ -181,7 +181,7 @@ void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* tab
assert(entries != NULL);
for (int i = 0; i < count; ++i) {
const Gaiji_Entry* entry = entries + i;
const Font_Entry* entry = entries + i;
if (entry->code == code) {
encode_sequence(output, size, entry->utf8);
return;
@ -195,7 +195,7 @@ void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* tab
output[size - 1] = 0;
}
void gaiji_stub_decode(char output[], int size, const char input[]) {
void font_stub_decode(char output[], int size, const char input[]) {
const char* ptr_in = input;
char* ptr_out = output;
bool decode = false;
@ -235,7 +235,7 @@ void gaiji_stub_decode(char output[], int size, const char input[]) {
*ptr_out = 0;
}
bool gaiji_context_init(Gaiji_Context* context, const char path[]) {
bool font_context_init(Font_Context* context, const char path[]) {
context->table_count = 0;
context->tables = NULL;
@ -254,11 +254,11 @@ bool gaiji_context_init(Gaiji_Context* context, const char path[]) {
return true;
}
void gaiji_context_destroy(Gaiji_Context* context) {
void font_context_destroy(Font_Context* context) {
for (int i = 0; i < context->table_count; ++i) {
Gaiji_Table* table = context->tables + i;
free((Gaiji_Entry*)table->table_wide);
free((Gaiji_Entry*)table->table_narrow);
Font_Table* table = context->tables + i;
free((Font_Entry*)table->table_wide);
free((Font_Entry*)table->table_narrow);
}
free(context->tables);

View File

@ -16,8 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef GAIJI_H
#define GAIJI_H
#ifndef FONT_H
#define FONT_H
#include <stdbool.h>
@ -35,36 +35,36 @@
typedef struct {
int code;
char utf8[MAX_UTF8_BYTES];
} Gaiji_Entry;
} Font_Entry;
typedef struct {
char name[256];
const Gaiji_Entry* table_wide;
const Font_Entry* table_wide;
int table_wide_size;
const Gaiji_Entry* table_narrow;
const Font_Entry* table_narrow;
int table_narrow_size;
} Gaiji_Table;
} Font_Table;
typedef struct {
Gaiji_Table* tables;
Font_Table* tables;
int table_count;
} Gaiji_Context;
} Font_Context;
typedef enum {
GAIJI_WIDTH_WIDE,
GAIJI_WIDTH_NARROW,
} Gaiji_Width;
FONT_WIDTH_WIDE,
FONT_WIDTH_NARROW,
} Font_Width;
/*
* Functions
*/
bool gaiji_context_init(Gaiji_Context* context, const char path[]);
void gaiji_context_destroy(Gaiji_Context* context);
bool font_context_init(Font_Context* context, const char path[]);
void font_context_destroy(Font_Context* context);
const Gaiji_Table* gaiji_table_select(const Gaiji_Context* context, const char name[]);
const Font_Table* font_table_select(const Font_Context* context, const char name[]);
void gaiji_stub_encode(char output[], int size, int code, const Gaiji_Table* table, Gaiji_Width width);
void gaiji_stub_decode(char output[], int size, const char input[]);
void font_stub_encode(char output[], int size, int code, const Font_Table* table, Font_Width width);
void font_stub_decode(char output[], int size, const char input[]);
#endif /* GAIJI_H */
#endif /* FONT_H */

View File

@ -19,7 +19,7 @@
#include <assert.h>
#include "hooks.h"
#include "gaiji.h"
#include "font.h"
#include "util.h"
#include "eb/eb/eb.h"
@ -114,7 +114,7 @@ static EB_Error_Code hook_narrow_font( /* EB_HOOK_NARROW_FONT */
assert(argc > 0);
char stub[MAX_STUB_BYTES];
gaiji_stub_encode(stub, ARRSIZE(stub), argv[0], container, GAIJI_WIDTH_NARROW);
font_stub_encode(stub, ARRSIZE(stub), argv[0], container, FONT_WIDTH_NARROW);
eb_write_text_string(book, stub);
return 0;
@ -133,7 +133,7 @@ static EB_Error_Code hook_wide_font( /* EB_HOOK_WIDE_FONT */
assert(argc > 0);
char stub[MAX_STUB_BYTES];
gaiji_stub_encode(stub, ARRSIZE(stub), argv[0], container, GAIJI_WIDTH_WIDE);
font_stub_encode(stub, ARRSIZE(stub), argv[0], container, FONT_WIDTH_WIDE);
eb_write_text_string(book, stub);
return 0;

8
main.c
View File

@ -22,7 +22,7 @@
#include "util.h"
#include "book.h"
#include "gaiji.h"
#include "font.h"
/*
* Local types
@ -99,8 +99,8 @@ int main(int argc, char *argv[]) {
Options options = { };
argp_parse(&argp, argc, argv, 0, 0, &options);
Gaiji_Context context;
if (!gaiji_context_init(&context, *options.font_path == 0 ? NULL : options.font_path)) {
Font_Context context;
if (!font_context_init(&context, *options.font_path == 0 ? NULL : options.font_path)) {
return 1;
}
@ -113,6 +113,6 @@ int main(int argc, char *argv[]) {
book_free(&book);
gaiji_context_destroy(&context);
font_context_destroy(&context);
return success ? 0 : 1;
}