Improve Print_current_directory()

This commit is contained in:
Thomas Bernard 2019-02-18 09:48:30 +01:00
parent 9886872c8a
commit 6e83791dde
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C

View File

@ -1248,32 +1248,15 @@ static void Display_bookmark(T_Dropdown_button * Button, int bookmark_number)
//------------------------ Chargements et sauvegardes ------------------------ //------------------------ Chargements et sauvegardes ------------------------
void Print_current_directory(void) /// Shows Selector->Directory on 37 chars
// static void Print_current_directory(void)
// Shows Selector->Directory on 37 chars
//
{ {
char converted_name[MAX_PATH_CHARACTERS]; size_t length; // length du répertoire courant
int length; // length du répertoire courant size_t index; // index de parcours de la chaine complète
int index; // index de parcours de la chaine complète
strncpy(converted_name,Selector->Directory,sizeof(converted_name));
converted_name[sizeof(converted_name)-1] = '\0';
#ifdef ENABLE_FILENAMES_ICONV
{
/* convert file name from UTF8 to ANSI */
char * input = Selector->Directory;
size_t inbytesleft = strlen(input);
char * output = converted_name;
size_t outbytesleft = sizeof(converted_name)-1;
if(cd != (iconv_t)-1 && (ssize_t)iconv(cd, &input, &inbytesleft, &output, &outbytesleft) >= 0)
*output = '\0';
}
#endif /* ENABLE_FILENAMES_ICONV */
Window_rectangle(10,84,37*8,8,MC_Light); Window_rectangle(10,84,37*8,8,MC_Light);
if (Selector->Directory_unicode[0] != 0) if (Selector->Directory_unicode != NULL && Selector->Directory_unicode[0] != 0)
{ {
length=Unicode_strlen(Selector->Directory_unicode); length=Unicode_strlen(Selector->Directory_unicode);
if (length>MAX_DISPLAYABLE_PATH) if (length>MAX_DISPLAYABLE_PATH)
@ -1301,6 +1284,23 @@ void Print_current_directory(void)
} }
else else
{ {
char * converted_name;
#ifdef ENABLE_FILENAMES_ICONV
/* convert file name from UTF8 to ANSI */
char * input = Selector->Directory;
size_t inbytesleft = strlen(input);
char * output;
size_t outbytesleft = inbytesleft + 1;
converted_name = malloc(outbytesleft);
output = converted_name;
if(cd != (iconv_t)-1 && (ssize_t)iconv(cd, &input, &inbytesleft, &output, &outbytesleft) >= 0)
*output = '\0';
else
strcpy(converted_name, Selector->Directory);
#else
converted_name = strdup(Selector->Directory);
#endif /* ENABLE_FILENAMES_ICONV */
length = strlen(converted_name); length = strlen(converted_name);
if (length>MAX_DISPLAYABLE_PATH) if (length>MAX_DISPLAYABLE_PATH)
{ // We need to truncate the directory { // We need to truncate the directory
@ -1318,7 +1318,7 @@ void Print_current_directory(void)
(length-index<=MAX_DISPLAYABLE_PATH-4) ) (length-index<=MAX_DISPLAYABLE_PATH-4) )
{ {
// we found the place ! // we found the place !
strcpy(temp_name+4,converted_name+index); memcpy(temp_name + 4, converted_name + index, length - index + 1);
break; break;
} }
@ -1327,6 +1327,7 @@ void Print_current_directory(void)
} }
else // The string is short enough else // The string is short enough
Print_in_window(10,84,converted_name,MC_Black,MC_Light); Print_in_window(10,84,converted_name,MC_Black,MC_Light);
free(converted_name);
} }
Update_window_area(10,84,37*8,8); Update_window_area(10,84,37*8,8);