move some functions from filesel.c to fileseltools.c

This commit is contained in:
Thomas Bernard 2020-02-08 01:03:43 +01:00
parent 47e1ee496e
commit eb0effaf3f
No known key found for this signature in database
GPG Key ID: 0FF11B67A5C0863C
8 changed files with 289 additions and 219 deletions

View File

@ -804,7 +804,8 @@ endif
# This is the list of the objects we want to build. Dependancies are built by "make depend" automatically.
OBJS = main.o init.o graph.o $(APIOBJ) misc.o special.o \
buttons.o palette.o help.o operatio.o pages.o \
readline.o engine.o filesel.o op_c.o readini.o saveini.o \
readline.o engine.o filesel.o fileseltools.o \
op_c.o readini.o saveini.o \
shade.o keyboard.o io.o version.o text.o SFont.o setup.o \
pxsimple.o pxtall.o pxwide.o pxdouble.o pxtriple.o \
pxtall2.o pxtall3.o pxwide2.o pxquad.o \
@ -827,7 +828,7 @@ TESTSOBJS = $(patsubst %.c,%.o,$(wildcard tests/*.c)) \
pngformat.o motoformats.o stformats.o c64formats.o cpcformats.o \
ifformat.o msxformats.o giformat.o \
op_c.o colorred.o \
unicode.o \
unicode.o fileseltools.o \
io.o realpath.o version.o pversion.o \
gfx2surface.o \
gfx2log.o gfx2mem.o

View File

@ -55,6 +55,7 @@
#include "engine.h"
#include "readline.h"
#include "filesel.h"
#include "fileseltools.h"
#include "loadsave.h"
#include "init.h"
#include "buttons.h"

View File

@ -31,6 +31,7 @@
#include "engine.h"
#include "errors.h"
#include "filesel.h" // Get_item_by_index
#include "fileseltools.h"
#include "global.h"
#include "graph.h"
#include "io.h" // find_last_separator

View File

@ -70,6 +70,7 @@
#include "help.h"
#include "unicode.h"
#include "filesel.h"
#include "fileseltools.h"
#define NORMAL_FILE_COLOR MC_Light // color du texte pour une ligne de
// fichier non sélectionné
@ -223,202 +224,6 @@ void Free_fileselector_list(T_Fileselector *list)
Recount_files(list);
}
word * Format_filename_unicode(const word * fname, word max_length, int type)
{
static word result[40];
int c;
int other_cursor;
int pos_last_dot;
// safety
if (max_length>40)
max_length=40;
if (Unicode_char_strcmp(fname,PARENT_DIR)==0)
{
Unicode_char_strlcpy(result, "\x11 PARENT DIRECTORY", 40);
// Append spaces
for (c=18; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
}
else if (fname[0]=='.' || type==1 || type==2)
{
// Files ".something" or drives or directories: Aligned left on (max_length-1) chars max
// Initialize as all spaces
for (c=0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
for (c=0;fname[c]!='\0' && c < max_length-1;c++)
result[c]=fname[c];
// A special character indicates the filename is truncated
if (c >= max_length-1)
result[max_length-2]=(byte)ELLIPSIS_CHARACTER;
}
else
{
// Initialize as all spaces
for (c = 0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
result[max_length-5]='.';
// Look for the last dot in filename
pos_last_dot = Position_last_dot_unicode(fname);
// Copy the part before the dot
for (c=0; c!=pos_last_dot && fname[c]!='\0'; c++)
{
if (c > max_length-6)
{
result[max_length-6]=(byte)ELLIPSIS_CHARACTER;
break;
}
result[c]=fname[c];
}
// Ensuite on recopie la partie qui suit le point (si nécessaire):
if (pos_last_dot != -1)
{
for (c = pos_last_dot+1,other_cursor=max_length-4;fname[c]!='\0' && other_cursor < max_length-1;c++,other_cursor++)
result[other_cursor]=fname[c];
}
}
return result;
}
char * Format_filename(const char * fname, word max_length, int type)
{
static char result[40];
int c;
int other_cursor;
int pos_last_dot;
#ifdef ENABLE_FILENAMES_ICONV
/* convert file name from UTF8 to ANSI */
char * converted_fname = NULL;
{
char * input = (char *)fname;
size_t inbytesleft = strlen(fname);
char * output = converted_fname;
size_t outbytesleft = inbytesleft;
converted_fname = malloc(outbytesleft + 1);
output = converted_fname;
if(cd != (iconv_t)-1 && (ssize_t)iconv(cd, &input, &inbytesleft, &output, &outbytesleft) >= 0)
{
*output = '\0';
fname = converted_fname;
}
}
#endif /* ENABLE_FILENAMES_ICONV */
// safety
if (max_length>40)
max_length=40;
if (strcmp(fname,PARENT_DIR)==0)
{
strcpy(result,"\x11 PARENT DIRECTORY");
// Append spaces
for (c=18; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
}
else if (fname[0]=='.' || type==1 || type==2)
{
// Files ".something" or drives or directories: Aligned left on (max_length-1) chars max
// Initialize as all spaces
for (c=0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
for (c=0;fname[c]!='\0' && c < max_length-1;c++)
result[c]=fname[c];
// A special character indicates the filename is truncated
if (c >= max_length-1)
result[max_length-2]=ELLIPSIS_CHARACTER;
}
else
{
// Initialize as all spaces
for (c = 0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
result[max_length-5]='.';
// Look for the last dot in filename
pos_last_dot = Position_last_dot(fname);
// Copy the part before the dot
for (c=0; c!=pos_last_dot && fname[c]!='\0'; c++)
{
if (c > max_length-6)
{
result[max_length-6]=ELLIPSIS_CHARACTER;
break;
}
result[c]=fname[c];
}
// Ensuite on recopie la partie qui suit le point (si nécessaire):
if (pos_last_dot != -1)
{
for (c = pos_last_dot+1,other_cursor=max_length-4;fname[c]!='\0' && other_cursor < max_length-1;c++,other_cursor++)
result[other_cursor]=fname[c];
}
}
#ifdef ENABLE_FILENAMES_ICONV
free(converted_fname);
#endif
return result;
}
/**
* Add an item to the file selector linked list
* @param list the linked list
* @param full_name the file name
* @param short_name the file name truncated to display in the file selector
* @param type the type of the item : 0 = File, 1 = Directory, 2 = Drive
* @param icon the icon for the item
* @return a pointer to the newly added item
* @return NULL in case of error
*/
T_Fileselector_item * Add_element_to_list(T_Fileselector *list, const char * full_name, const char *short_name, enum FSOBJECT_TYPE type, enum ICON_TYPES icon)
{
// Working element
T_Fileselector_item * temp_item;
size_t full_name_len, short_name_len;
full_name_len = strlen(full_name) + 1;
short_name_len = strlen(short_name) + 1;
// Allocate enough room for one struct + the visible label
temp_item=(T_Fileselector_item *)malloc(sizeof(T_Fileselector_item)+full_name_len);
if (temp_item == NULL) // memory allocation error
return NULL;
memset(temp_item, 0, sizeof(T_Fileselector_item));
if (short_name_len > sizeof(temp_item->Short_name))
short_name_len = sizeof(temp_item->Short_name) - 1; // without terminating 0
// Initialize element
memcpy(temp_item->Short_name,short_name,short_name_len);
memcpy(temp_item->Full_name,full_name,full_name_len);
temp_item->Type = type;
temp_item->Icon = icon;
// Doubly-linked
temp_item->Next =list->First;
temp_item->Previous=NULL;
if (list->First!=NULL)
list->First->Previous=temp_item;
// Put new element at the beginning
list->First=temp_item;
return temp_item;
}
///
/// Checks if a file has the requested file extension.

View File

@ -38,26 +38,6 @@
/// @return 1 if the user want to load/save a file
byte Button_Load_or_Save(T_Selector_settings *settings, byte load, T_IO_Context *context);
T_Fileselector_item * Add_element_to_list(T_Fileselector *list, const char * full_name, const char *short_name, enum FSOBJECT_TYPE type, enum ICON_TYPES icon);
///
/// Formats a display name for a file, directory, or similar name (drive, volume).
/// @param fname full file name from the file system
/// @param max_length numbers of characters to display
/// @param type 0 for file, 1 for directory, 2 for drive
/// @return a pointer to a single static buffer of maximum 40 characters
/// including the '\\0'.
char * Format_filename(const char * fname, word max_length, int type);
///
/// Formats a display name for a file, directory, or similar name (drive, volume).
/// @param fname full file name from the file system
/// @param max_length numbers of characters to display
/// @param type 0 for file, 1 for directory, 2 for drive
/// @return a pointer to a single static buffer of maximum 40 wide characters
/// including the '\\0'.
word * Format_filename_unicode(const word * fname, word max_length, int type);
void Free_fileselector_list(T_Fileselector *list);
void Sort_list_of_files(T_Fileselector *list);

232
src/fileseltools.c Normal file
View File

@ -0,0 +1,232 @@
/* vim:expandtab:ts=2 sw=2:
*/
/* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2018-2020 Thomas Bernard
Copyright 2014 Sergii Pylypenko
Copyright 2011 Pawel Góralski
Copyright 2009 Franck Charlet
Copyright 2008 Peter Gordon
Copyright 2008 Yves Rizoud
Copyright 2007 Adrien Destugues
Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
Grafx2 is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.
Grafx2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
*/
#include <string.h>
#include <stdlib.h>
#include "struct.h"
#include "global.h"
#include "unicode.h"
#include "io.h"
#include "fileseltools.h"
/**
* Add an item to the file selector linked list
* @param list the linked list
* @param full_name the file name
* @param short_name the file name truncated to display in the file selector
* @param type the type of the item : 0 = File, 1 = Directory, 2 = Drive
* @param icon the icon for the item
* @return a pointer to the newly added item
* @return NULL in case of error
*/
T_Fileselector_item * Add_element_to_list(T_Fileselector *list, const char * full_name, const char *short_name, enum FSOBJECT_TYPE type, enum ICON_TYPES icon)
{
// Working element
T_Fileselector_item * temp_item;
size_t full_name_len, short_name_len;
full_name_len = strlen(full_name) + 1;
short_name_len = strlen(short_name) + 1;
// Allocate enough room for one struct + the visible label
temp_item=(T_Fileselector_item *)malloc(sizeof(T_Fileselector_item)+full_name_len);
if (temp_item == NULL) // memory allocation error
return NULL;
memset(temp_item, 0, sizeof(T_Fileselector_item));
if (short_name_len > sizeof(temp_item->Short_name))
short_name_len = sizeof(temp_item->Short_name) - 1; // without terminating 0
// Initialize element
memcpy(temp_item->Short_name,short_name,short_name_len);
memcpy(temp_item->Full_name,full_name,full_name_len);
temp_item->Type = type;
temp_item->Icon = icon;
// Doubly-linked
temp_item->Next =list->First;
temp_item->Previous=NULL;
if (list->First!=NULL)
list->First->Previous=temp_item;
// Put new element at the beginning
list->First=temp_item;
return temp_item;
}
word * Format_filename_unicode(const word * fname, word max_length, int type)
{
static word result[40];
int c;
int other_cursor;
int pos_last_dot;
// safety
if (max_length>40)
max_length=40;
if (Unicode_char_strcmp(fname,PARENT_DIR)==0)
{
Unicode_char_strlcpy(result, "\x11 PARENT DIRECTORY", 40);
// Append spaces
for (c=18; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
}
else if (fname[0]=='.' || type==1 || type==2)
{
// Files ".something" or drives or directories: Aligned left on (max_length-1) chars max
// Initialize as all spaces
for (c=0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
for (c=0;fname[c]!='\0' && c < max_length-1;c++)
result[c]=fname[c];
// A special character indicates the filename is truncated
if (c >= max_length-1)
result[max_length-2]=(byte)ELLIPSIS_CHARACTER;
}
else
{
// Initialize as all spaces
for (c = 0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
result[max_length-5]='.';
// Look for the last dot in filename
pos_last_dot = Position_last_dot_unicode(fname);
// Copy the part before the dot
for (c=0; c!=pos_last_dot && fname[c]!='\0'; c++)
{
if (c > max_length-6)
{
result[max_length-6]=(byte)ELLIPSIS_CHARACTER;
break;
}
result[c]=fname[c];
}
// Ensuite on recopie la partie qui suit le point (si nécessaire):
if (pos_last_dot != -1)
{
for (c = pos_last_dot+1,other_cursor=max_length-4;fname[c]!='\0' && other_cursor < max_length-1;c++,other_cursor++)
result[other_cursor]=fname[c];
}
}
return result;
}
char * Format_filename(const char * fname, word max_length, int type)
{
static char result[40];
int c;
int other_cursor;
int pos_last_dot;
#ifdef ENABLE_FILENAMES_ICONV
/* convert file name from UTF8 to ANSI */
char * converted_fname = NULL;
{
char * input = (char *)fname;
size_t inbytesleft = strlen(fname);
char * output = converted_fname;
size_t outbytesleft = inbytesleft;
converted_fname = malloc(outbytesleft + 1);
output = converted_fname;
if(cd != (iconv_t)-1 && (ssize_t)iconv(cd, &input, &inbytesleft, &output, &outbytesleft) >= 0)
{
*output = '\0';
fname = converted_fname;
}
}
#endif /* ENABLE_FILENAMES_ICONV */
// safety
if (max_length>40)
max_length=40;
if (strcmp(fname,PARENT_DIR)==0)
{
strcpy(result,"\x11 PARENT DIRECTORY");
// Append spaces
for (c=18; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
}
else if (fname[0]=='.' || type==1 || type==2)
{
// Files ".something" or drives or directories: Aligned left on (max_length-1) chars max
// Initialize as all spaces
for (c=0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
for (c=0;fname[c]!='\0' && c < max_length-1;c++)
result[c]=fname[c];
// A special character indicates the filename is truncated
if (c >= max_length-1)
result[max_length-2]=ELLIPSIS_CHARACTER;
}
else
{
// Initialize as all spaces
for (c = 0; c<max_length-1; c++)
result[c]=' ';
result[c]='\0';
result[max_length-5]='.';
// Look for the last dot in filename
pos_last_dot = Position_last_dot(fname);
// Copy the part before the dot
for (c=0; c!=pos_last_dot && fname[c]!='\0'; c++)
{
if (c > max_length-6)
{
result[max_length-6]=ELLIPSIS_CHARACTER;
break;
}
result[c]=fname[c];
}
// Ensuite on recopie la partie qui suit le point (si nécessaire):
if (pos_last_dot != -1)
{
for (c = pos_last_dot+1,other_cursor=max_length-4;fname[c]!='\0' && other_cursor < max_length-1;c++,other_cursor++)
result[other_cursor]=fname[c];
}
}
#ifdef ENABLE_FILENAMES_ICONV
free(converted_fname);
#endif
return result;
}

50
src/fileseltools.h Normal file
View File

@ -0,0 +1,50 @@
/* vim:expandtab:ts=2 sw=2:
*/
/* Grafx2 - The Ultimate 256-color bitmap paint program
Copyright 2018-2020 Thomas Bernard
Copyright 2007 Adrien Destugues
Copyright 1996-2001 Sunset Design (Guillaume Dorme & Karl Maritaud)
Grafx2 is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; version 2
of the License.
Grafx2 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Grafx2; if not, see <http://www.gnu.org/licenses/>
*/
//////////////////////////////////////////////////////////////////////////////
///@file fileseltools.h
/// Utility functions for the Fileselector window
//////////////////////////////////////////////////////////////////////////////
#ifndef FILESELTOOLS_H_INCLUDED
#define FILESELTOOLS_H_INCLUDED
T_Fileselector_item * Add_element_to_list(T_Fileselector *list, const char * full_name, const char *short_name, enum FSOBJECT_TYPE type, enum ICON_TYPES icon);
///
/// Formats a display name for a file, directory, or similar name (drive, volume).
/// @param fname full file name from the file system
/// @param max_length numbers of characters to display
/// @param type 0 for file, 1 for directory, 2 for drive
/// @return a pointer to a single static buffer of maximum 40 characters
/// including the '\\0'.
char * Format_filename(const char * fname, word max_length, int type);
///
/// Formats a display name for a file, directory, or similar name (drive, volume).
/// @param fname full file name from the file system
/// @param max_length numbers of characters to display
/// @param type 0 for file, 1 for directory, 2 for drive
/// @return a pointer to a single static buffer of maximum 40 wide characters
/// including the '\\0'.
word * Format_filename_unicode(const word * fname, word max_length, int type);
#endif

View File

@ -71,7 +71,7 @@
#include "gfx2log.h"
#include "gfx2mem.h"
// for the network browse
#include "filesel.h"
#include "fileseltools.h"
// Lit un octet
// Renvoie -1 si OK, 0 en cas d'erreur