QString QEb::readHeading(void *para, bool hook_flag) { char buff[1024+1]; ssize_t len; QByteArray b; for(;;) { EB_Error_Code ecode; if (hook_flag) ecode = eb_read_heading(&book, &appendix, &hookset, para, 1024, buff, &len); else ecode = eb_read_heading(&book, &appendix, NULL, para, 1024, buff, &len); if (ecode != EB_SUCCESS) { dispError("eb_read_heading", ecode); break; } if (len > 0) b += QByteArray(buff, (int)len); if (isTextStopped()) break; //if (len < 1024) // break; } return eucToUtf(b); }
/*------------------------------------------------------------------------ -- 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 */