Make saving of unicode named files work under Win32

the "Long" name is converted to short (DOS 8.3) name
as soon as possible.
This commit is contained in:
Thomas Bernard 2018-02-16 12:45:07 +01:00
parent 74818299a3
commit ad219c84d2
5 changed files with 47 additions and 1 deletions

View File

@ -3507,6 +3507,7 @@ void Save_picture(enum CONTEXT_TYPE type)
Main.image_is_modified=0;
Main.fileformat=save_context.Format;
strcpy(Main.backups->Pages->Filename, save_context.File_name);
Unicode_strlcpy(Main.backups->Pages->Filename_unicode, save_context.File_name_unicode, MAX_PATH_CHARACTERS);
strcpy(Main.backups->Pages->File_directory, save_context.File_directory);
}
if (type == CONTEXT_BRUSH)

View File

@ -1939,7 +1939,13 @@ byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context
WCHAR temp_str[MAX_PATH_CHARACTERS];
if (GetShortPathNameW((WCHAR *)filename_unicode, temp_str, MAX_PATH_CHARACTERS) == 0)
{
strcpy(filename_ansi, "*TEMP*");
// generate a temporary ansi name
int i;
for (i = 0; i < MAX_PATH_CHARACTERS - 1 && filename_unicode[i] != 0; i++)
{
filename_ansi[i] = (filename_unicode[i] < 256) ? (byte)filename_unicode[i] : '_';
}
filename_ansi[i] = '\0';
}
else
{

View File

@ -1754,7 +1754,34 @@ void Delete_safety_backups(void)
FILE * Open_file_write(T_IO_Context *context)
{
char filename[MAX_PATH_CHARACTERS]; // filename with full path
#if defined(WIN32)
WCHAR filename_unicode[MAX_PATH_CHARACTERS];
FILE * f;
if (context->File_name_unicode != NULL)
{
Unicode_char_strlcpy((word *)filename_unicode, context->File_directory, MAX_PATH_CHARACTERS);
Unicode_char_strlcat((word *)filename_unicode, PATH_SEPARATOR, MAX_PATH_CHARACTERS);
Unicode_strlcat((word *)filename_unicode, context->File_name_unicode, MAX_PATH_CHARACTERS);
f = _wfopen(filename_unicode, L"wb");
if (f != NULL)
{
// Now the file has been created, retrieve its short (ASCII) name
WCHAR shortpath[MAX_PATH_CHARACTERS];
DWORD len = GetShortPathNameW(filename_unicode, shortpath, MAX_PATH_CHARACTERS);
if (len > 0 && len < MAX_PATH_CHARACTERS)
{
DWORD start, index;
for (start = len; start > 0 && shortpath[start-1] != '\\'; start--);
for (index = 0; index < MAX_PATH_CHARACTERS - 1 && index < len - start; index++)
context->File_name[index] = shortpath[start + index];
context->File_name[index] = '\0';
}
}
return f;
}
#endif
Get_full_filename(filename, context->File_name, context->File_directory);
return fopen(filename, "wb");

View File

@ -64,6 +64,15 @@ void Unicode_strlcpy(word * dst, const word * src, size_t len)
*dst = 0;
}
/// concatenate unicode string
void Unicode_strlcat(word * dst, const word * src, size_t len)
{
size_t dst_len = Unicode_strlen(dst);
if (dst_len >= len)
return;
Unicode_strlcpy(dst + dst_len, src, len - dst_len);
}
/// Compare an unicode string with a regular Latin1 string
int Unicode_char_strcmp(const word * s1, const char * s2)
{

View File

@ -33,6 +33,9 @@ word * Unicode_strdup(const word * str);
/// Copy unicode string
void Unicode_strlcpy(word * dst, const word * src, size_t len);
/// Append unicode string to another
void Unicode_strlcat(word * dst, const word * src, size_t len);
/// Compare an unicode string with a regular Latin1 string
int Unicode_char_strcmp(const word * s1, const char * s2);