diff --git a/src/main.c b/src/main.c index 26203531..54abf8f2 100644 --- a/src/main.c +++ b/src/main.c @@ -546,6 +546,69 @@ CT_ASSERT(sizeof(T_Components)==3); // when sizeof(T_Palette) is not 768. CT_ASSERT(sizeof(T_Palette)==768); +/** + * Convert a file name to unicode characters + * + * @param filename_unicode the output buffer of MAX_PATH_CHARACTERS wide characters + * @param filename the input file name + * @param directory the input file directory + * @return 0 if no conversion has taken place. + * @return 1 if the unicode filename has been retrieved + */ +static int Get_Unicode_Filename(word * filename_unicode, const char * filename, const char * directory) +{ +#if defined(WIN32) + int i = 0, j = 0; + WCHAR shortPath[MAX_PATH_CHARACTERS]; + WCHAR longPath[MAX_PATH_CHARACTERS]; + + // copy the full path to a wide character buffer : + while (directory[0] != '\0') + shortPath[i++] = *directory++; + if (i > 0 && shortPath[i-1] != '\\') // add path separator only if it is missing + shortPath[i++] = '\\'; + while (filename[0] != '\0') + shortPath[i++] = *filename++; + shortPath[i++] = 0; + if (GetLongPathNameW(shortPath, longPath, MAX_PATH_CHARACTERS) == 0) + return 0; + i = 0; + while (longPath[j] != 0) + { + if (longPath[j] == '\\') + i = 0; + else + filename_unicode[i++] = longPath[j]; + j++; + } + filename_unicode[i++] = 0; + return 1; +#elif defined(ENABLE_FILENAMES_ICONV) + char * input = filename; + size_t inbytesleft = strlen(filename); + char * output = (char *)filename_unicode; + size_t outbytesleft = (MAX_PATH_CHARACTERS - 1) * 2; + (void)directory; // unused + if (cd_utf16 != (iconv_t)-1) + { + size_t r = iconv(cd_utf16, &input, &inbytesleft, &output, &outbytesleft); + if (r != (size_t)-1) + { + output[0] = '\0'; + output[1] = '\0'; + return 1; + } + } + return 0; +#else + (void)filename_unicode; + (void)filename; + (void)directory; + // not implemented + return 0; +#endif +} + // ------------------------ Initialiser le programme ------------------------- // Returns 0 on fail int Init_program(int argc,char * argv[]) @@ -558,12 +621,12 @@ int Init_program(int argc,char * argv[]) int file_in_command_line; T_Gradient_array initial_gradients; static char main_filename [MAX_PATH_CHARACTERS]; -#ifdef ENABLE_FILENAMES_ICONV - static word main_filename_unicode[MAX_PATH_CHARACTERS]; -#endif static char main_directory[MAX_PATH_CHARACTERS]; static char spare_filename [MAX_PATH_CHARACTERS]; static char spare_directory[MAX_PATH_CHARACTERS]; +#if defined(ENABLE_FILENAMES_ICONV) || defined(WIN32) + static word filename_unicode[MAX_PATH_CHARACTERS]; +#endif #if defined(__MINT__) printf("===============================\n"); @@ -1022,6 +1085,8 @@ int Init_program(int argc,char * argv[]) case 2: // Load this file Init_context_layered_image(&context, spare_filename, spare_directory); + if (Get_Unicode_Filename(filename_unicode, spare_filename, spare_directory)) + context.File_name_unicode = filename_unicode; Load_image(&context); Destroy_context(&context); Redraw_layered_image(); @@ -1031,24 +1096,8 @@ int Init_program(int argc,char * argv[]) // no break ! proceed with the other file now case 1: Init_context_layered_image(&context, main_filename, main_directory); -#ifdef ENABLE_FILENAMES_ICONV - { - char * input = main_filename; - size_t inbytesleft = strlen(main_filename); - char * output = (char *)main_filename_unicode; - size_t outbytesleft = sizeof(main_filename_unicode) - 2; - if (cd_utf16 != (iconv_t)-1) - { - size_t r = iconv(cd_utf16, &input, &inbytesleft, &output, &outbytesleft); - if (r != (size_t)-1) - { - output[0] = '\0'; - output[1] = '\0'; - context.File_name_unicode = main_filename_unicode; - } - } - } -#endif + if (Get_Unicode_Filename(filename_unicode, main_filename, main_directory)) + context.File_name_unicode = filename_unicode; Load_image(&context); Destroy_context(&context); Redraw_layered_image();