void wordwrap_wrap_z_ucs(WORDWRAP *wrapper, z_ucs *input) { size_t len, chars_to_copy, space_in_buffer; len = z_ucs_len(input); while (len > 0) { space_in_buffer = wrapper->input_buffer_size - 1 - wrapper->input_index; chars_to_copy = len > space_in_buffer ? space_in_buffer : len; TRACE_LOG("chars_to_copy: %d, len:%d, space_in_buffer:%d.\n", chars_to_copy, len, space_in_buffer); z_ucs_ncpy( wrapper->input_buffer + wrapper->input_index, input, chars_to_copy); wrapper->input_index += chars_to_copy; wrapper->input_buffer[wrapper->input_index] = 0; input += chars_to_copy; len -= chars_to_copy; TRACE_LOG("chars copied: %d, chars left: %ld.\n",chars_to_copy,(long)len); if ( (wrapper->input_index == wrapper->input_buffer_size - 1) || ( (wrapper->flush_after_newline == true) && ( (wrapper->input_index + wrapper->chars_already_on_line > wrapper->line_length) || (z_ucs_chr(wrapper->input_buffer, Z_UCS_NEWLINE) != NULL) ) ) ) { flush_input_buffer(wrapper, false); //FIXME: Increase buffer size in case flush not possible. } } }
// Returns malloc()ed string which needs to be free()d later. static char *get_path_for_locale(z_ucs *locale_name) { z_dir *dir; char *search_path; z_ucs *search_path_zucs, *path_ptr, *colon_index, *zucs_ptr; z_ucs *zucs_buf = NULL; size_t bufsize = 0, len, dirname_len; struct z_dir_ent z_dir_entry; char *dirname; char *locale_name_utf8, *locale_dir_name = NULL; search_path = locale_search_path == NULL ? default_search_path : locale_search_path; search_path_zucs = dup_utf8_string_to_zucs_string(search_path); path_ptr = search_path_zucs; TRACE_LOG("Search path: \""); TRACE_LOG_Z_UCS(search_path_zucs); TRACE_LOG("'.\n"); // open-resource: locale_name_utf8 = dup_zucs_string_to_utf8_string(locale_name); while (*path_ptr != 0) { colon_index = z_ucs_chr(path_ptr, Z_UCS_COLON); len = colon_index == NULL ? z_ucs_len(path_ptr) : (size_t)(colon_index - path_ptr); TRACE_LOG("len: %ld\n", (long)len); if (len > 0) { if (bufsize < len + 1) { TRACE_LOG("realloc buf for %ld chars / %ld bytes.\n", (long)len + 1, (long)sizeof(z_ucs) * (len + 1)); // open-resource: if ((zucs_ptr = realloc(zucs_buf, sizeof(z_ucs) * (len + 1))) == NULL) { // exit-point: TRACE_LOG("realloc() returned NULL.\n"); free(zucs_buf); free(locale_name_utf8); free(path_ptr); return NULL; } bufsize = len + 1; zucs_buf = zucs_ptr; } z_ucs_ncpy(zucs_buf, path_ptr, len); zucs_buf[len] = 0; // open-resource: if ((dirname = dup_zucs_string_to_utf8_string(zucs_buf)) == NULL) { // exit-point: TRACE_LOG("dup_zucs_string_to_utf8_string() returned NULL.\n"); free(zucs_buf); free(locale_name_utf8); free(path_ptr); return NULL; } TRACE_LOG("Path: '%s'\n", dirname); // open-resource: if ((dir = fsi->open_dir(dirname)) != NULL) { while (fsi->read_dir(&z_dir_entry, dir) == 0) { TRACE_LOG("Processing \"%s\".\n", z_dir_entry.d_name); if (strcasecmp(locale_name_utf8, z_dir_entry.d_name) == 0) { dirname_len = strlen(dirname) + strlen(z_dir_entry.d_name); TRACE_LOG("dirname_len: %ld.\n", (long)dirname_len); // open-resource: if ((locale_dir_name = malloc(dirname_len + 2)) == NULL) { // exit-point: TRACE_LOG("malloc() returned NULL.\n"); fsi->close_dir(dir); free(dirname); free(zucs_buf); free(locale_name_utf8); free(path_ptr); return NULL; } strcpy(locale_dir_name, dirname); strcat(locale_dir_name, "/"); strcat(locale_dir_name, z_dir_entry.d_name); break; } } // close-resource: fsi->close_dir(dir); } // close-resource: free(dirname); } if (locale_dir_name != NULL) break; path_ptr += len + (colon_index != NULL ? 1 : 0); } free(search_path_zucs); // close-resource: free(locale_name_utf8); // close-resource: free(zucs_buf); //TRACE_LOG("res:'"); //TRACE_LOG_Z_UCS(locale_dir_name); //TRACE_LOG("\n"); return locale_dir_name; }