From eb0effaf3f2a270c8c62bb69f3a33ccdedc2f765 Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Sat, 8 Feb 2020 01:03:43 +0100 Subject: [PATCH] move some functions from filesel.c to fileseltools.c --- src/Makefile | 5 +- src/buttons.c | 1 + src/factory.c | 1 + src/filesel.c | 197 +------------------------------------- src/filesel.h | 20 ---- src/fileseltools.c | 232 +++++++++++++++++++++++++++++++++++++++++++++ src/fileseltools.h | 50 ++++++++++ src/io.c | 2 +- 8 files changed, 289 insertions(+), 219 deletions(-) create mode 100644 src/fileseltools.c create mode 100644 src/fileseltools.h diff --git a/src/Makefile b/src/Makefile index d4124768..373f4d24 100644 --- a/src/Makefile +++ b/src/Makefile @@ -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 diff --git a/src/buttons.c b/src/buttons.c index e6ee7265..5104b519 100644 --- a/src/buttons.c +++ b/src/buttons.c @@ -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" diff --git a/src/factory.c b/src/factory.c index 317464cc..8d435487 100644 --- a/src/factory.c +++ b/src/factory.c @@ -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 diff --git a/src/filesel.c b/src/filesel.c index e2099359..7055173e 100644 --- a/src/filesel.c +++ b/src/filesel.c @@ -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) - result[max_length-2]=(byte)ELLIPSIS_CHARACTER; - } - else - { - // Initialize as all spaces - for (c = 0; 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) - result[max_length-2]=ELLIPSIS_CHARACTER; - } - else - { - // Initialize as all spaces - for (c = 0; 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. diff --git a/src/filesel.h b/src/filesel.h index 8f9e8f09..b2eb912f 100644 --- a/src/filesel.h +++ b/src/filesel.h @@ -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); diff --git a/src/fileseltools.c b/src/fileseltools.c new file mode 100644 index 00000000..b8f6b37a --- /dev/null +++ b/src/fileseltools.c @@ -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 +*/ + +#include +#include +#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) + result[max_length-2]=(byte)ELLIPSIS_CHARACTER; + } + else + { + // Initialize as all spaces + for (c = 0; 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) + result[max_length-2]=ELLIPSIS_CHARACTER; + } + else + { + // Initialize as all spaces + for (c = 0; 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; +} + + diff --git a/src/fileseltools.h b/src/fileseltools.h new file mode 100644 index 00000000..aea5e8a3 --- /dev/null +++ b/src/fileseltools.h @@ -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 +*/ + +////////////////////////////////////////////////////////////////////////////// +///@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 diff --git a/src/io.c b/src/io.c index 6d08b843..70b1de52 100644 --- a/src/io.c +++ b/src/io.c @@ -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