Esempio n. 1
0
File: uim-eb.c Progetto: NgoHuy/uim
void
uim_eb_open ()
{
  EB_Error_Code err;

  err = eb_initialize_library();
  if (err != EB_SUCCESS)
    uim_notify_fatal(_("eb: failed to initialize EB library : error = %s\n"),
	     eb_error_message(err));
}
Esempio n. 2
0
/*------------------------------------------------------------------------
-- Name: print_title_to_out_file
--
-- Description:
--   Print the title of the book to the output file.
--
-- Parameters:
--   None.
--
-- Returns:
--   None.
--
------------------------------------------------------------------------*/
static void print_title_to_out_file(void)
{
    EB_Error_Code error_code;
    char *status_conv = NULL;
    FILE *out_file = NULL;

    /* Get the title of the subbook */
    error_code = eb_subbook_title2(&book, subbook_index, title);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to get the title: %s\n", eb_error_message(error_code));
        die(1);
    }

    /* Convert title from EUC-JP to UTF-8 */
    status_conv = convert_encoding(conv_buf, MAXLEN_CONV, title, EB_MAX_TITLE_LENGTH, "UTF-8", "EUC-JP");

    if(status_conv == NULL)
    {
        fprintf(stderr, "Error: Something went wrong when trying to encode the title\n");
        die(1);
    }

    out_file = fopen(out_path, "w");

    if(out_file == NULL)
    {
        fprintf(stderr, "Error: Could not open output file, \"%s\"\n", out_path);
        die(1);
    }

    /* Output the text to file (in UTF-8) */
    fwrite(conv_buf, 1, strlen(conv_buf), out_file);

    fclose(out_file);

} /* print_title_to_out_file */
Esempio n. 3
0
/*
 * Uncompress files in `book' and output them under `out_top_path'.
 * If it succeeds, 0 is returned.  Otherwise -1 is returned.
 */
int
ebzip_unzip_book(const char *out_top_path, const char *book_path,
    char subbook_name_list[][EB_MAX_DIRECTORY_NAME_LENGTH + 1],
    int subbook_name_count)
{
    EB_Book book;
    EB_Error_Code error_code;
    EB_Subbook_Code subbook_list[EB_MAX_SUBBOOKS];
    EB_Subbook_Code subbook_code;
    int subbook_count = 0;
    int result;
    int i;

    eb_initialize_book(&book);

    /*
     * Bind a book.
     */
    error_code = eb_bind(&book, book_path);
    if (error_code != EB_SUCCESS) {
	fprintf(stderr, "%s: %s\n", invoked_name,
	    eb_error_message(error_code));
	fflush(stderr);
	return -1;
    }

    /*
     * For each targe subbook, convert a subbook-names to a subbook-codes.
     * If no subbook is specified by `--subbook'(`-S'), set all subbooks
     * as the target.
     */
    if (subbook_name_count == 0) {
	error_code = eb_subbook_list(&book, subbook_list, &subbook_count);
	if (error_code != EB_SUCCESS) {
	    fprintf(stderr, "%s: %s\n", invoked_name,
		eb_error_message(error_code));
	    fflush(stderr);
	    return -1;
	}
    } else {
	for (i = 0; i < subbook_name_count; i++) {
	    error_code = find_subbook(&book, subbook_name_list[i],
		&subbook_code);
	    if (error_code != EB_SUCCESS) {
		fprintf(stderr, _("%s: unknown subbook name `%s'\n"),
		    invoked_name, subbook_name_list[i]);
		return -1;
	    }
	    subbook_list[subbook_count++] = subbook_code;
	}
    }

    /*
     * Uncompress the book.
     */
    if (book.disc_code == EB_DISC_EB) {
	result = ebzip_unzip_book_eb(&book, out_top_path, book_path,
	    subbook_list, subbook_count);
    } else {
	result = ebzip_unzip_book_epwing(&book, out_top_path, book_path,
	    subbook_list, subbook_count);
    }

    eb_finalize_book(&book);

    return result;
}
Esempio n. 4
0
/*
 * Internal function for `unzip_book'.
 * This is used to compress an EPWING book.
 */
static int
ebzip_unzip_book_epwing(EB_Book *book, const char *out_top_path,
    const char *book_path, EB_Subbook_Code *subbook_list, int subbook_count)
{
    EB_Subbook *subbook;
    EB_Error_Code error_code;
    EB_Font *font;
    String_List string_list;
    char in_path_name[PATH_MAX + 1];
    char out_sub_path[PATH_MAX + 1];
    char out_path_name[PATH_MAX + 1];
    char catalogs_file_name[EB_MAX_FILE_NAME_LENGTH];
    mode_t out_directory_mode;
    Zio_Code in_zio_code;
    int i, j;

    /*
     * If `out_top_path' and/or `book_path' represents "/", replace it
     * to an empty string.
     */
    if (strcmp(out_top_path, "/") == 0)
	out_top_path++;
    if (strcmp(book_path, "/") == 0)
	book_path++;

    /*
     * Initialize variables.
     */
    out_directory_mode = 0777 ^ get_umask();
    string_list_initialize(&string_list);

    error_code = eb_load_all_subbooks(book);
    if (error_code != EB_SUCCESS) {
	fprintf(stderr, "%s: %s\n", invoked_name,
	    eb_error_message(error_code));
    }

    /*
     * Uncompress a book.
     */
    for (i = 0; i < subbook_count; i++) {
	subbook = book->subbooks + subbook_list[i];

	/*
	 * Make an output directory for the current subbook.
	 */
	eb_compose_path_name(out_top_path, subbook->directory_name,
	    out_sub_path);
	if (!ebzip_test_flag
	    && make_missing_directory(out_sub_path, out_directory_mode) < 0) {
	    fprintf(stderr, _("%s: failed to create a directory, %s: %s\n"),
		invoked_name, strerror(errno), out_sub_path);
	    goto failed;
	}

	/*
	 * Make `data' sub directory for the current subbook.
	 */
	eb_compose_path_name2(out_top_path, subbook->directory_name,
	    subbook->data_directory_name, out_sub_path);
	if (!ebzip_test_flag
	    && make_missing_directory(out_sub_path, out_directory_mode) < 0) {
	    fprintf(stderr, _("%s: failed to create a directory, %s: %s\n"),
		invoked_name, strerror(errno), out_sub_path);
	    goto failed;
	}

	/*
	 * Uncompress HONMON/HONMON2 file.
	 */
	in_zio_code = zio_mode(&subbook->text_zio);
	eb_compose_path_name3(book->path, subbook->directory_name,
	    subbook->data_directory_name, subbook->text_file_name,
	    in_path_name);
	eb_compose_path_name3(out_top_path, subbook->directory_name,
	    subbook->data_directory_name, subbook->text_file_name,
	    out_path_name);

	if (in_zio_code != ZIO_INVALID
	    && !string_list_find(&string_list, in_path_name)) {
	    if (strncasecmp(subbook->text_file_name, "honmon2", 7) == 0)
		eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_ORG);
	    else
		eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);
	    if (ebzip_unzip_file(out_path_name, in_path_name, in_zio_code) < 0)
		goto failed;
	    string_list_add(&string_list, in_path_name);
	}

	/*
	 * Uncompress HONMONS file.
	 */
	in_zio_code = zio_mode(&subbook->sound_zio);
	eb_compose_path_name3(book->path, subbook->directory_name,
	    subbook->data_directory_name, subbook->sound_file_name,
	    in_path_name);
	eb_compose_path_name3(out_top_path, subbook->directory_name,
	    subbook->data_directory_name, subbook->sound_file_name,
	    out_path_name);
	eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);

	if (!ebzip_skip_flag_sound
	    && in_zio_code != ZIO_INVALID
	    && !string_list_find(&string_list, in_path_name)) {
	    if (ebzip_unzip_file(out_path_name, in_path_name, in_zio_code) < 0)
		goto failed;
	    string_list_add(&string_list, in_path_name);
	}

	/*
	 * Uncompress HONMONG file.
	 */
	in_zio_code = zio_mode(&subbook->graphic_zio);
	eb_compose_path_name3(book->path, subbook->directory_name,
	    subbook->data_directory_name, subbook->graphic_file_name,
	    in_path_name);
	eb_compose_path_name3(out_top_path, subbook->directory_name,
	    subbook->data_directory_name, subbook->graphic_file_name,
	    out_path_name);
	eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);

	if (!ebzip_skip_flag_graphic
	    && in_zio_code != ZIO_INVALID
	    && !string_list_find(&string_list, in_path_name)) {
	    if (ebzip_unzip_file(out_path_name, in_path_name, in_zio_code) < 0)
		goto failed;
	    string_list_add(&string_list, in_path_name);
	}

	/*
	 * Make `gaiji' sub directory for the current subbook.
	 */
	if (!ebzip_skip_flag_font) {
	    eb_compose_path_name2(out_top_path, subbook->directory_name,
		subbook->gaiji_directory_name, out_sub_path);
	    if (!ebzip_test_flag
		&& make_missing_directory(out_sub_path, out_directory_mode)
		< 0) {
		fprintf(stderr,
		    _("%s: failed to create a directory, %s: %s\n"),
		    invoked_name, strerror(errno), out_sub_path);
		goto failed;
	    }

	    /*
	     * Uncompress narrow font files.
	     */
	    for (j = 0; j < EB_MAX_FONTS; j++) {
		font = subbook->narrow_fonts + j;
		if (font->font_code == EB_FONT_INVALID)
		    continue;

		in_zio_code = zio_mode(&font->zio);
		eb_compose_path_name3(book->path, subbook->directory_name,
		    subbook->gaiji_directory_name, font->file_name,
		    in_path_name);
		eb_compose_path_name3(out_top_path, subbook->directory_name,
		    subbook->gaiji_directory_name, font->file_name,
		    out_path_name);
		eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);

		if (in_zio_code != ZIO_INVALID
		    && !string_list_find(&string_list, in_path_name)) {
		    if (ebzip_unzip_file(out_path_name, in_path_name,
			in_zio_code) < 0)
			goto failed;
		    string_list_add(&string_list, in_path_name);
		}
	    }

	    /*
	     * Uncompress wide font files.
	     */
	    for (j = 0; j < EB_MAX_FONTS; j++) {
		font = subbook->wide_fonts + j;
		if (font->font_code == EB_FONT_INVALID)
		    continue;

		in_zio_code = zio_mode(&font->zio);
		eb_compose_path_name3(book->path, subbook->directory_name,
		    subbook->gaiji_directory_name,
		    font->file_name, in_path_name);
		eb_compose_path_name3(out_top_path, subbook->directory_name,
		    subbook->gaiji_directory_name, font->file_name,
		    out_path_name);
		eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);

		if (in_zio_code != ZIO_INVALID
		    && !string_list_find(&string_list, in_path_name)) {
		    if (ebzip_unzip_file(out_path_name, in_path_name,
			in_zio_code) < 0)
			goto failed;
		    string_list_add(&string_list, in_path_name);
		}
	    }
	}

	/*
	 * Copy movie files.
	 */
	if (!ebzip_skip_flag_movie) {
	    eb_compose_path_name2(book->path, subbook->directory_name,
		subbook->movie_directory_name, in_path_name);
	    eb_compose_path_name2(out_top_path, subbook->directory_name,
		subbook->movie_directory_name, out_path_name);
	    if (ebzip_copy_files_in_directory(out_path_name, in_path_name) < 0)
		goto failed;
	}
    }

    /*
     * Copy CATALOGS file.
     */
    if (eb_find_file_name(book->path, "catalogs", catalogs_file_name)
	== EB_SUCCESS) {
	eb_compose_path_name(book->path, catalogs_file_name, in_path_name);
	eb_compose_path_name(out_top_path, catalogs_file_name, out_path_name);
	if (ebzip_copy_file(out_path_name, in_path_name) < 0)
	    goto failed;
    }

    string_list_finalize(&string_list);
    return 0;

    /*
     * An error occurs...
     */
  failed:
    string_list_finalize(&string_list);
    return -1;
}
Esempio n. 5
0
/*
 * Internal function for `unzip_book'.
 * This is used to compress an EB book.
 */
static int
ebzip_unzip_book_eb(EB_Book *book, const char *out_top_path,
    const char *book_path, EB_Subbook_Code *subbook_list, int subbook_count)
{
    EB_Subbook *subbook;
    EB_Error_Code error_code;
    String_List string_list;
    char in_path_name[PATH_MAX + 1];
    char out_sub_path[PATH_MAX + 1];
    char out_path_name[PATH_MAX + 1];
    char catalog_file_name[EB_MAX_FILE_NAME_LENGTH];
    char language_file_name[EB_MAX_FILE_NAME_LENGTH];
    mode_t out_directory_mode;
    Zio_Code in_zio_code;
    int i;

    /*
     * If `out_top_path' and/or `book_path' represents "/", replace it
     * to an empty string.
     */
    if (strcmp(out_top_path, "/") == 0)
	out_top_path++;
    if (strcmp(book_path, "/") == 0)
	book_path++;

    /*
     * Initialize variables.
     */
    out_directory_mode = 0777 ^ get_umask();
    string_list_initialize(&string_list);

    error_code = eb_load_all_subbooks(book);
    if (error_code != EB_SUCCESS) {
	fprintf(stderr, "%s: %s\n", invoked_name,
	    eb_error_message(error_code));
    }

    /*
     * Uncompress a book.
     */
    for (i = 0; i < subbook_count; i++) {
	subbook = book->subbooks + subbook_list[i];

	/*
	 * Make an output directory for the current subbook.
	 */
	eb_compose_path_name(out_top_path, subbook->directory_name,
	    out_sub_path);
	if (!ebzip_test_flag
	    && make_missing_directory(out_sub_path, out_directory_mode) < 0) {
	    fprintf(stderr, _("%s: failed to create a directory, %s: %s\n"),
		invoked_name, strerror(errno), out_sub_path);
	    goto failed;
	}

	/*
	 * Uncompress START file.
	 */
	in_zio_code = zio_mode(&subbook->text_zio);
	eb_compose_path_name2(book->path, subbook->directory_name,
	    subbook->text_file_name, in_path_name);
	eb_compose_path_name2(out_top_path, subbook->directory_name,
	    subbook->text_file_name, out_path_name);
	eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);

	if (in_zio_code != ZIO_INVALID
	    && !string_list_find(&string_list, in_path_name)) {
	    if (ebzip_unzip_start_file(out_path_name, in_path_name,
		in_zio_code, subbook->index_page) < 0)
		goto failed;
	}

	if (!ebzip_test_flag
	    && rewrite_sebxa_start(out_path_name, subbook->index_page) < 0)
	    goto failed;
	string_list_add(&string_list, in_path_name);
    }

    /*
     * Uncompress a language file.
     */
    if (eb_find_file_name(book->path, "language", language_file_name)
	== EB_SUCCESS) {
	eb_compose_path_name(book->path, language_file_name, in_path_name);
	eb_compose_path_name(out_top_path, language_file_name, out_path_name);
	eb_path_name_zio_code(in_path_name, ZIO_PLAIN, &in_zio_code);
	eb_fix_path_name_suffix(out_path_name, EBZIP_SUFFIX_NONE);
	if (ebzip_unzip_file(out_path_name, in_path_name, in_zio_code) < 0)
	    goto failed;
    }

    /*
     * Copy CATALOG file.
     */
    if (eb_find_file_name(book->path, "catalog", catalog_file_name)
	== EB_SUCCESS) {
	eb_compose_path_name(book->path, catalog_file_name, in_path_name);
	eb_compose_path_name(out_top_path, catalog_file_name, out_path_name);
	if (ebzip_copy_file(out_path_name, in_path_name) < 0)
	    goto failed;
    }

    string_list_finalize(&string_list);
    return 0;

    /*
     * An error occurs...
     */
  failed:
    string_list_finalize(&string_list);
    return -1;
}
Esempio n. 6
0
File: font.c Progetto: jokester/eb
int
main(int argc, char *argv[])
{
    EB_Error_Code error_code;
    EB_Book book;
    EB_Subbook_Code subbook_list[EB_MAX_SUBBOOKS];
    int subbook_count;
    int subbook_index;
    int font_start;
    unsigned char bitmap[EB_SIZE_NARROW_FONT_16];
    int i, j;

    /* コマンド行引数をチェック。*/
    if (argc != 3) {
        fprintf(stderr, "Usage: %s book-path subbook-index\n",
            argv[0]);
        exit(1);
    }

    /* EB ライブラリと `book' を初期化。*/
    eb_initialize_library();
    eb_initialize_book(&book);

    /* 書籍を `book' に結び付ける。*/
    error_code = eb_bind(&book, argv[1]);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to bind the book, %s: %s\n",
            argv[0], eb_error_message(error_code), argv[1]);
        goto die;
    }

    /* 副本の一覧を取得。*/
    error_code = eb_subbook_list(&book, subbook_list, &subbook_count);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to get the subbbook list, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    /* 副本のインデックスを取得。*/
    subbook_index = atoi(argv[2]);

    /*「現在の副本 (current subbook)」を設定。*/
    error_code = eb_set_subbook(&book, subbook_list[subbook_index]);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to set the current subbook, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    /*「現在の外字の大きさ」を設定。*/
    if (eb_set_font(&book, EB_FONT_16) < 0) {
        fprintf(stderr, "%s: failed to set the font size, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    /* 外字の開始位置を取得。*/
    error_code = eb_narrow_font_start(&book, &font_start);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to get font information, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    i = font_start;
    for (;;) {
        /* 外字のビットマップデータを取得。*/
	error_code = eb_narrow_font_character_bitmap(&book, i,
	    (char *)bitmap);
	if (error_code != EB_SUCCESS) {
            fprintf(stderr, "%s: failed to get font data, %s\n",
                argv[0], eb_error_message(error_code));
            goto die;
        }

	/* ビットマップをアスキーアートにして出力。*/
	printf("code point=%04x\n", i);
	for (j = 0; j < 16; j++) {
	    fputc((bitmap[j] & 0x80) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x40) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x20) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x10) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x08) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x04) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x02) ? '*' : ' ', stdout);
	    fputc((bitmap[j] & 0x01) ? '*' : ' ', stdout);
	    fputc('\n', stdout);
	}
	fputs("--------\n", stdout);

        /* 外字の文字番号を一つ進める。*/
	error_code = eb_forward_narrow_font_character(&book, 1, &i);
	if (error_code != EB_SUCCESS)
	    break;
    }
        
    /* 書籍と EB ライブラリの利用を終了。*/
    eb_finalize_book(&book);
    eb_finalize_library();
    exit(0);

    /* エラー発生で終了するときの処理。*/
  die:
    eb_finalize_book(&book);
    eb_finalize_library();
    exit(1);
}
Esempio n. 7
0
int
main(int argc, char *argv[])
{
    EB_Error_Code error_code;
    EB_BookList bl;
    int book_count;
    char *name, *title;
    int i;

    /* コマンド行引数をチェック。*/
    if (argc != 2) {
        fprintf(stderr, "Usage: %s book-path remote-access-identifier\n",
            argv[0]);
        exit(1);
    }

    /* EB ライブラリと `bl' を初期化。*/
    eb_initialize_library();
    eb_initialize_booklist(&bl);

    /* EBNET サーバを `bl' に結び付ける。*/
    error_code = eb_bind_booklist(&bl, argv[1]);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to bind the EBNET server, %s: %s\n",
            argv[0], eb_error_message(error_code), argv[1]);
        goto die;
    }

    /* サーバ上の書籍、appendix の個数を取得。*/
    error_code = eb_booklist_book_count(&bl, &book_count);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to get the number of books, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    for (i = 0; i < book_count; i++) {
        /* 書籍、appendix の名称を取得。*/
        error_code = eb_booklist_book_name(&bl, i, &name);
	if (error_code != EB_SUCCESS) {
            fprintf(stderr, "%s: failed to get book name #%d, %s\n",
                argv[0], i, eb_error_message(error_code));
            goto die;
        }

        /* 書籍、appendix の題名を取得。*/
        error_code = eb_booklist_book_name(&bl, i, &title);
	if (error_code != EB_SUCCESS) {
            fprintf(stderr, "%s: failed to get book title #%d, %s\n",
                argv[0], i, eb_error_message(error_code));
            goto die;
        }

        printf("%-20s  %s\n", name, title);
    }
        
    /* `bl' と EB ライブラリの利用を終了。*/
    eb_finalize_booklist(&bl);
    eb_finalize_library();
    exit(0);

    /* エラー発生で終了するときの処理。*/
  die:
    eb_finalize_booklist(&bl);
    eb_finalize_library();
    exit(1);
}
Esempio n. 8
0
/*------------------------------------------------------------------------
-- Name: lookup_word
--
-- Description:
--   Lookup the input word and send the results to the output file.
--
-- Parameters:
--   None.
--
-- Returns:
--   None.
--
------------------------------------------------------------------------*/
static void lookup_word(void)
{
    char lookup_word_utf8[MAXLEN_LOOKUP_WORD + 1];
    char lookup_word_eucjp[MAXLEN_LOOKUP_WORD + 1];
    char *status_conv = NULL;
    EB_Error_Code error_code;
    EB_Hit hits[MAX_HITS];
    FILE *in_file = NULL;
    FILE *out_file = NULL;
    int hit_count;
    int heading_length;
    int text_length;
    int i;

    /* Get the word to lookup */
    in_file = fopen(in_path, "r");

    if(in_file == NULL)
    {
        fprintf(stderr, "Error: Could not open input file: \"%s\"", in_path);
        die(1);
    }

    if(fgets(lookup_word_utf8, MAXLEN_LOOKUP_WORD, in_file) == NULL)
    {
        fclose(in_file);
        fprintf(stderr, "Error: Could not read word from input file: \"%s\"", in_path);
        die(1);
    }

    fclose(in_file);

    /* Remove the final '\n' */
    if(lookup_word_utf8[strlen(lookup_word_utf8) - 1] == '\n')
    {
        lookup_word_utf8[strlen(lookup_word_utf8) - 1] = '\0';
    }

    /* Convert the lookup word from UTF-8 to EUC-JP */
    status_conv = convert_encoding(lookup_word_eucjp, MAXLEN_LOOKUP_WORD, lookup_word_utf8, strlen(lookup_word_utf8), "EUC-JP", "UTF-8");

    if(status_conv == NULL)
    {
        fprintf(stderr, "Error: Something went wront when trying to encode the lookup word\n");
        die(1);
    }

    /* Perform an exact search of the lookup word */
    error_code = eb_search_exactword(&book, lookup_word_eucjp);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to search for the word, %s: %s\n", eb_error_message(error_code), lookup_word_eucjp);
        die(1);
    }

    while(1)
    {
        /* Get the list of hits */
        error_code = eb_hit_list(&book, MAX_HITS, hits, &hit_count);

        if(error_code != EB_SUCCESS)
        {
            fprintf(stderr, "Error: Failed to get hit entries, %s\n", eb_error_message(error_code));
            die(1);
        }

        /* Are we done? */
        if(hit_count == 0)
        {
            break;
        }

        /* Create the output file */
        out_file = fopen(out_path, "w");

        if(out_file == NULL)
        {
            fprintf(stderr, "Error: Could not open output file, \"%s\"\n", out_path);
            die(1);
        }

        /* Output only the number of hits? */
        if(show_hit_count)
        {
            fprintf(out_file, "{HITS: %d}\n", hit_count);
        }

        /* Determine the max number of hits to output */
        hit_count = MIN(hit_count, max_hits_to_output);

        /* For each search hit, print the hit information to the output file */
        for(i = 0; i < hit_count; i++)
        {
            /* Did the user specify a particular hit index to output? */
            if(hit_to_output >= 0)
            {
                i = hit_to_output;
            }

            /* Output the hit number */
            if(print_hit_number && (hit_count > 1) && (hit_to_output == -1))
            {
                fprintf(out_file, "{ENTRY: %d}\n", i);
            }

            /* Print the heading of the hit to file */
            if(print_heading)
            {
                /* Seek to the heading */
                error_code = eb_seek_text(&book, &(hits[i].heading));

                if(error_code != EB_SUCCESS)
                {
                    fprintf(stderr, "Error: Failed to seek the subbook, %s\n", eb_error_message(error_code));
                    fclose(out_file);
                    die(1);
                }

                /* Read the heading */
                error_code = eb_read_heading(&book, NULL, &hookset, NULL, MAXLEN_HEADING, heading, &heading_length);

                if(error_code != EB_SUCCESS)
                {
                    fprintf(stderr, "Error: Failed to read the subbook, %s\n", eb_error_message(error_code));
                    fclose(out_file);
                    die(1);
                }

                /* Convert from EUC-JP to UTF-8 */
                status_conv = convert_encoding(conv_buf, MAXLEN_CONV, heading, heading_length, "UTF-8", "EUC-JP");

                if(status_conv == NULL)
                {
                    fprintf(stderr, "Error: Something went wrong when trying to encode the the heading\n");
                    fclose(out_file);
                    die(1);
                }

                /* Replace gaiji that have UTF-8 equivalents */
                replace_gaiji_with_utf8(final_buf, conv_buf);

                /* Output the header to file (in UTF-8) */
                fprintf(out_file, "%s\n", conv_buf);
            }

            /* Print the text of the hit to file */
            if(print_text)
            {
                /* Seek to the text */
                error_code = eb_seek_text(&book, &(hits[i].text));

                if(error_code != EB_SUCCESS)
                {
                    fprintf(stderr, "Error: Failed to seek the subbook, %s\n", eb_error_message(error_code));
                    fclose(out_file);
                    die(1);
                }

                /* Read the text*/
                error_code = eb_read_text(&book, NULL, &hookset, NULL, MAXLEN_TEXT, text, &text_length);

                if(error_code != EB_SUCCESS)
                {
                    fprintf(stderr, "Error: Failed to read the subbook, %s\n", eb_error_message(error_code));
                    fclose(out_file);
                    die(1);
                }
            }

            /* Convert from EUC-JP to UTF-8 */
            status_conv = convert_encoding(conv_buf, MAXLEN_CONV, text, text_length, "UTF-8", "EUC-JP");

            if(status_conv == NULL)
            {
                fprintf(stderr, "Error: Something went wrong when trying to encode the the text\n");
                fclose(out_file);
                die(1);
            }

            /* Replace gaiji that have UTF-8 equivalents */
            replace_gaiji_with_utf8(final_buf, conv_buf);

            /* Output the text to file (in UTF-8) */
            fwrite(final_buf, 1, strlen(final_buf), out_file);

            /* Since the user specified a hit index, don't display the other hits */
            if(hit_to_output >= 0)
            {
                break;
            }
        }

        fclose(out_file);
    }

} /* lookup_word */
Esempio n. 9
0
/*------------------------------------------------------------------------
-- Name: lookup_link
--
-- Description:
--   Lookup the input link and send the results to the output file.
--
-- Parameters:
--   None.
--
-- Returns:
--   None.
--
------------------------------------------------------------------------*/
static void lookup_link(void)
{
    EB_Error_Code error_code;
    EB_Position position;
    FILE *in_file = NULL;
    FILE *out_file = NULL;
    char link_text[MAXLEN_LOOKUP_WORD] = "";
    char *status_conv = NULL;
    int text_length;
    int parse_result;

    in_file = fopen(in_path, "r");

    if(in_file == NULL)
    {
        fprintf(stderr, "Error: Could not open input file: \"%s\"", in_path);
        die(1);
    }

    if(fgets(link_text, MAXLEN_LOOKUP_WORD, in_file) == NULL)
    {
        fclose(in_file);
        fprintf(stderr, "Error: Could not read word from input file: \"%s\"", in_path);
        die(1);
    }

    fclose(in_file);

    /* Parse the location of the link in the subbook */
    parse_result = sscanf(link_text, "%X %X", &position.page, &position.offset);

    /* If link was not parsed correctly (2 is the expected number of fields in the input file) */
    if(parse_result != 2)
    {
        fprintf(stderr, "Error: Could not parse link from input file, %d.\n", parse_result);
        die(1);
    }

    error_code = eb_seek_text(&book, &position);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to seek text, \"%s\"\n", eb_error_message(error_code));
        die(1);
    }

    error_code = eb_read_text(&book, NULL, &hookset, NULL, MAXLEN_TEXT, text, &text_length);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to read text, \"%s\"\n", eb_error_message(error_code));
        die(1);
    }

    /* Convert from EUC-JP to UTF-8 */
    status_conv = convert_encoding(conv_buf, MAXLEN_CONV, text, text_length, "UTF-8", "EUC-JP");

    if(status_conv == NULL)
    {
        fprintf(stderr, "Error: Something went wrong when trying to encode the the text\n");
        die(1);
    }

    /* Replace gaiji that have UTF-8 equivalents */
    replace_gaiji_with_utf8(final_buf, conv_buf);

    out_file = fopen(out_path, "w");

    /* Output the text to file (in UTF-8) */
    fwrite(final_buf, 1, strlen(final_buf), out_file);

    fclose(out_file);

} /* lookup_link */
Esempio n. 10
0
/*------------------------------------------------------------------------
-- Name: init
--
-- Description:
--   Initialize EB Lib, set hooks, open the desired subbook.
--
-- Parameters:
--   None.
--
-- Returns:
--   None.
--
------------------------------------------------------------------------*/
static void init(void)
{
    EB_Error_Code error_code;
    EB_Subbook_Code subbook_list[EB_MAX_SUBBOOKS];
    FILE *out_file = NULL;
    int subbook_count;

    /* Blank the output file */
    out_file = fopen(out_path, "w");

    if(out_file == NULL)
    {
        fprintf(stderr, "Error: Could not open output file, \"%s\"\n", out_path);
        die(1);
    }

    fclose(out_file);

    /* Init EB Lib */
    eb_initialize_library();
    eb_initialize_book(&book);
    eb_initialize_hookset(&hookset);

    /* Set hooks */
    eb_set_hook(&hookset, &hook_narrow_font);
    eb_set_hook(&hookset, &hook_wide_font);

    if(set_emphasis_hook)
    {
        eb_set_hook(&hookset, &hook_begin_emphasis);
        eb_set_hook(&hookset, &hook_end_emphasis);
    }

    if(set_keyword_hook)
    {
        eb_set_hook(&hookset, &hook_begin_keyword);
        eb_set_hook(&hookset, &hook_end_keyword);
    }

    if(set_reference_hook)
    {
        eb_set_hook(&hookset, &hook_begin_reference);
        eb_set_hook(&hookset, &hook_end_reference);
    }

    if(set_subscript_hook)
    {
        eb_set_hook(&hookset, &hook_begin_subscript);
        eb_set_hook(&hookset, &hook_end_subscript);
    }

    if(set_superscript_hook)
    {
        eb_set_hook(&hookset, &hook_begin_superscript);
        eb_set_hook(&hookset, &hook_end_superscript);
    }

    /* Open the EPWING book */
    error_code = eb_bind(&book, book_path);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to bind the book, %s: %s\n", eb_error_message(error_code), book_path);
        die(1);
    }

    /* Get the subbook list */
    error_code = eb_subbook_list(&book, subbook_list, &subbook_count);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to get the subbbook list, %s\n", eb_error_message(error_code));
        die(1);
    }

    /* Get the subbook */
    error_code = eb_set_subbook(&book, subbook_list[subbook_index]);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to set the current subbook, %s\n", eb_error_message(error_code));
        die(1);
    }

    /* Get the subbook directory (the name only, not the full path) */
    error_code = eb_subbook_directory(&book, subbook_directory);

    if(error_code != EB_SUCCESS)
    {
        fprintf(stderr, "Error: Failed to get the subbook directory: %s\n", eb_error_message(error_code));
        die(1);
    }

    /* Set the font */
    if (eb_set_font(&book, EB_FONT_16) < 0)
    {
        fprintf(stderr, "Error: Failed to set the font size: %s\n", eb_error_message(error_code));
        die(1);
    }

} /* init */
Esempio n. 11
0
int
main(int argc, char *argv[])
{
    EB_Error_Code error_code;
    EB_Appendix app;
    EB_Subbook_Code subbook_list[EB_MAX_SUBBOOKS];
    int subbook_count;
    int subbook_index;
    int alt_start;
    char text[EB_MAX_ALTERNATION_TEXT_LENGTH + 1];
    int i;

    /* コマンド行引数をチェック。*/
    if (argc != 3) {
        fprintf(stderr, "Usage: %s appendix-path subbook-index\n",
            argv[0]);
        exit(1);
    }

    /* EB ライブラリと `app' を初期化。*/
    eb_initialize_library();
    eb_initialize_appendix(&app);

    /* appendix を `app' に結び付ける。*/
    error_code = eb_bind_appendix(&app, argv[1]);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to bind the app, %s: %s\n",
            argv[0], eb_error_message(error_code), argv[1]);
        goto die;
    }

    /* 副本の一覧を取得。*/
    error_code = eb_appendix_subbook_list(&app, subbook_list,
	&subbook_count);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to get the subbook list, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    /* 副本のインデックスを取得。*/
    subbook_index = atoi(argv[2]);

    /*「現在の副本 (current subbook)」を設定。*/
    if (eb_set_appendix_subbook(&app, subbook_list[subbook_index])
	< 0) {
        fprintf(stderr, "%s: failed to set the current subbook, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    /* 外字の開始位置を取得。*/
    error_code = eb_narrow_alt_start(&app, &alt_start);
    if (error_code != EB_SUCCESS) {
        fprintf(stderr, "%s: failed to get font information, %s\n",
            argv[0], eb_error_message(error_code));
        goto die;
    }

    i = alt_start;
    for (;;) {
        /* 外字の代替文字列を取得。*/
	error_code = eb_narrow_alt_character_text(&app, i, text);
	if (error_code != EB_SUCCESS) {
            fprintf(stderr, "%s: failed to get font data, %s\n",
                argv[0], eb_error_message(error_code));
            goto die;
        }

	/* 取得した代替文字列を出力。*/
	printf("%04x: %s\n", i, text);

        /* 外字の文字番号を一つ進める。*/
	error_code = eb_forward_narrow_alt_character(&app, 1, &i);
	if (error_code != EB_SUCCESS)
	    break;
    }
        
    /* appendix と EB ライブラリの利用を終了。*/
    eb_finalize_appendix(&app);
    eb_finalize_library();
    exit(0);

    /* エラー発生で終了するときの処理。*/
  die:
    eb_finalize_appendix(&app);
    eb_finalize_library();
    exit(1);
}
Esempio n. 12
0
int
main(int argc, char *argv[])
{
    EB_Error_Code error_code;
    char out_path[PATH_MAX + 1];
    char book_path[PATH_MAX + 1];
    char subbook_name_list[EB_MAX_SUBBOOKS][EB_MAX_DIRECTORY_NAME_LENGTH + 1];
    int subbook_name_count = 0;
    int ch;

    invoked_name = argv[0];
    strcpy(out_path, DEFAULT_OUTPUT_DIRECTORY);

    /*
     * Initialize locale data.
     */
#ifdef ENABLE_NLS
#ifdef HAVE_SETLOCALE
    setlocale(LC_ALL, "");
#endif
    bindtextdomain(TEXT_DOMAIN_NAME, LOCALEDIR);
    textdomain(TEXT_DOMAIN_NAME);
#endif

    /*
     * Initialize `book'.
     */
    error_code = eb_initialize_library();
    if (error_code != EB_SUCCESS) {
	fprintf(stderr, "%s: %s\n", invoked_name,
	    eb_error_message(error_code));
	goto die;
    }

    /*
     * Parse command line options.
     */
    for (;;) {
        ch = getopt_long(argc, argv, short_options, long_options, NULL);
        if (ch == -1)
            break;
        switch (ch) {
        case 'h':
            /*
             * Option `-h'.  Display help message, then exit.
             */
            output_help();
            exit(0);

        case 'o':
            /*
             * Option `-o'.  Output files under DIRECOTRY.
	     * The length of the file name
	     *    "<out_path>/catalogs.bak;1"
	     * must not exceed PATH_MAX.
             */
            if (PATH_MAX < strlen(optarg)) {
                fprintf(stderr, _("%s: too long output directory path\n"),
                    invoked_name);
                exit(1);
            }
            strcpy(out_path, optarg);
	    canonicalize_path(out_path);
	    if (PATH_MAX
		< strlen(out_path) + (1 + EB_MAX_DIRECTORY_NAME_LENGTH + 6)) {
		fprintf(stderr, _("%s: too long output directory path\n"),
		    invoked_name);
		goto die;
	    }
	    break;

        case 'S':
            /*
             * Option `-S'.  Specify target subbooks.
             */
            if (parse_subbook_name_argument(invoked_name, optarg,
		subbook_name_list, &subbook_name_count) < 0)
                exit(1);
            break;

        case 'v':
            /*
             * Option `-v'.  Display version number, then exit.
             */
            output_version(program_name, program_version);
            exit(0);

        default:
            output_try_help(invoked_name);
	    goto die;
	}
    }

    /*
     * Check the number of rest arguments.
     */
    if (1 < argc - optind) {
        fprintf(stderr, _("%s: too many arguments\n"), invoked_name);
        output_try_help(invoked_name);
	goto die;
    }

    /*
     * Set a book path.
     */
    if (argc == optind)
        strcpy(book_path, DEFAULT_BOOK_DIRECTORY);
    else
        strcpy(book_path, argv[optind]);

    if (is_ebnet_url(book_path)) {
	fprintf(stderr, "%s: %s\n", invoked_name,
	    eb_error_message(EB_ERR_EBNET_UNSUPPORTED));
	goto die;
    }
    canonicalize_path(book_path);

    if (PATH_MAX
	< strlen(book_path) + (1 + EB_MAX_DIRECTORY_NAME_LENGTH + 6)) {
	fprintf(stderr, _("%s: too long book directory path\n"),
	    invoked_name);
	goto die;
    }

    /*
     * Set signals.
     */
#ifdef SIGHUP
    signal(SIGHUP, trap);
#endif
    signal(SIGINT, trap);
#ifdef SIGQUIT
    signal(SIGQUIT, trap);
#endif
#ifdef SIGTERM
    signal(SIGTERM, trap);
#endif

    /*
     * Refile a catalog.
     */
    if (refile_book(out_path, book_path, subbook_name_list,
	subbook_name_count) < 0)
	goto die;

    eb_finalize_library();

    return 0;

    /*
     * A critical error occurs...
     */
  die:
    eb_finalize_library();
    exit(1);
}