1
This commit is contained in:
Alex Yatskov 2016-11-02 09:14:14 -07:00
parent 0f7bd08570
commit ff679358a9
3 changed files with 75 additions and 66 deletions

67
main.c
View File

@ -16,10 +16,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _BSD_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include "convert.h"
@ -27,54 +24,8 @@
#include "eb/eb/eb.h"
#include "eb/eb/error.h"
#include "eb/eb/text.h"
#define MAX_HITS 128
#define MAX_TEXT 1024
typedef enum {
READ_MODE_TEXT,
READ_MODE_HEADING,
} ReadMode;
static char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode) {
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
char data[MAX_TEXT];
ssize_t data_length = 0;
EB_Error_Code error;
switch (mode) {
case READ_MODE_TEXT:
error = eb_read_text(
book,
NULL,
NULL,
NULL,
MAX_TEXT - 1,
data,
&data_length
);
break;
case READ_MODE_HEADING:
error = eb_read_heading(
book,
NULL,
NULL,
NULL,
MAX_TEXT - 1,
data,
&data_length
);
break;
default:
return NULL;
}
return error == EB_SUCCESS ? eucjp_to_utf8(data) : NULL;
}
#define MAX_HITS 256
static void export_subbook_entries(EB_Book* eb_book, Subbook* subbook) {
if (subbook->entry_cap == 0) {
@ -211,22 +162,6 @@ static void export_book(const char path[], Book* book) {
while(0);
}
static void free_book(Book* book) {
for (int i = 0; i < book->subbook_count; ++i) {
Subbook* subbook = book->subbooks + i;
free(subbook->title);
free(subbook->copyright);
for (int j = 0; j < subbook->entry_count; ++j) {
Entry* entry = subbook->entries + j;
free(entry->heading);
free(entry->text);
}
free(subbook->entries);
}
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "usage: %s path\n", argv[0]);

62
util.c
View File

@ -17,3 +17,65 @@
*/
#include "util.h"
#include "convert.h"
#include "eb/eb/eb.h"
#include "eb/eb/error.h"
#include "eb/eb/text.h"
#define MAX_TEXT 1024
char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode) {
if (eb_seek_text(book, position) != EB_SUCCESS) {
return NULL;
}
char data[MAX_TEXT];
ssize_t data_length = 0;
EB_Error_Code error;
switch (mode) {
case READ_MODE_TEXT:
error = eb_read_text(
book,
NULL,
NULL,
NULL,
MAX_TEXT - 1,
data,
&data_length
);
break;
case READ_MODE_HEADING:
error = eb_read_heading(
book,
NULL,
NULL,
NULL,
MAX_TEXT - 1,
data,
&data_length
);
break;
default:
return NULL;
}
return error == EB_SUCCESS ? eucjp_to_utf8(data) : NULL;
}
void free_book(Book* book) {
for (int i = 0; i < book->subbook_count; ++i) {
Subbook* subbook = book->subbooks + i;
free(subbook->title);
free(subbook->copyright);
for (int j = 0; j < subbook->entry_count; ++j) {
Entry* entry = subbook->entries + j;
free(entry->heading);
free(entry->text);
}
free(subbook->entries);
}
}

12
util.h
View File

@ -64,4 +64,16 @@ typedef struct {
char error[MAX_ERROR];
} Book;
/*
Book helpers
*/
typedef enum {
READ_MODE_TEXT,
READ_MODE_HEADING,
} ReadMode;
char* read_book_data(EB_Book* book, const EB_Position* position, ReadMode mode);
void free_book(Book* book);
#endif