Beispiel #1
0
QList <EB_Hit> QEb::hitList(int max_count)
{
    EB_Hit *harray = new EB_Hit[max_count];
    int cnt;
    QList <EB_Hit> hits;
    EB_Error_Code ecode = eb_hit_list(&book, max_count, harray, &cnt);
    if (ecode != EB_SUCCESS)
        dispError("eb_hit_list", ecode);
    else
        for (int i = 0; i < cnt; i++) 
            hits << harray[i];
    return hits;
}
Beispiel #2
0
char *
uim_eb_search_text (uim_eb *ueb, const char *key, const char *enc)
{
  char *text;
  int i;
  char *str = NULL;
  iconv_t cd;

  /* FIXME! check return value */

  cd = (iconv_t)uim_iconv->create("EUC-JP", enc);
  text = uim_iconv->convert(cd, key);
  uim_iconv->release(cd);

  if (!text)
	  return NULL;

  for (i = 0; i < ueb->subCount; i++) {
    EB_Hit hits[MAX_HITS];
    int hitCount;
    int j;

    /* specify subbook */
    if (eb_set_subbook(&ueb->book, ueb->subCodes[i]) != EB_SUCCESS) {
      uim_notify_fatal(N_("eb: eb_set_subbook() failed")); continue;
    }

    eb_search_word(&ueb->book, text);
    eb_hit_list(&ueb->book, MAX_HITS, hits, &hitCount);
    for (j = 0; j < hitCount; j++) {
      /*EB_Position headp = hits[j].heading;*/
      EB_Position textp = hits[j].text;

      go_text_eb(ueb, textp, &str, enc);
      uim_eb_strappend(&str, "\n", sizeof("\n"));
    }
  }

  free(text);

  return str;
}
Beispiel #3
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 */