--- hatari/src/gui-sdl/dlgFileSelect.c 2019/04/01 07:14:59 1.1.1.5 +++ hatari/src/gui-sdl/dlgFileSelect.c 2019/04/09 08:48:52 1.1.1.8 @@ -6,7 +6,7 @@ A file selection dialog for the graphical user interface for Hatari. */ -const char DlgFileSelect_rcsid[] = "Hatari $Id: dlgFileSelect.c,v 1.1.1.5 2019/04/01 07:14:59 root Exp $"; +const char DlgFileSelect_fileid[] = "Hatari dlgFileSelect.c : " __DATE__ " " __TIME__; #include #include @@ -16,6 +16,7 @@ const char DlgFileSelect_rcsid[] = "Hata #include "scandir.h" #include "sdlgui.h" #include "file.h" +#include "paths.h" #include "zip.h" @@ -89,10 +90,10 @@ static int entries; /*-----------------------------------------------------------------------*/ -/* - Update the file name strings in the dialog. - Returns FALSE if it failed, TRUE on success. -*/ +/** + * Update the file name strings in the dialog. + * Returns false if it failed, true on success. + */ static int DlgFileSelect_RefreshEntries(struct dirent **files, char *path, bool browsingzip) { int i; @@ -101,7 +102,7 @@ static int DlgFileSelect_RefreshEntries( if (!tempstr) { perror("DlgFileSelect_RefreshEntries"); - return FALSE; + return false; } /* Copy entries to dialog: */ @@ -127,7 +128,7 @@ static int DlgFileSelect_RefreshEntries( { if( stat(tempstr, &filestat)==0 && S_ISDIR(filestat.st_mode) ) dlgfilenames[i][0] = SGFOLDER; /* Mark folders */ - if (ZIP_FileNameIsZIP(tempstr) && browsingzip == FALSE) + if (ZIP_FileNameIsZIP(tempstr) && browsingzip == false) dlgfilenames[i][0] = SGFOLDER; /* Mark .ZIP archives as folders */ } } @@ -136,15 +137,15 @@ static int DlgFileSelect_RefreshEntries( } free(tempstr); - return TRUE; + return true; } /*-----------------------------------------------------------------------*/ -/* - Remove all hidden files (files with file names that begin with a dot) from - the list. -*/ +/** + * Remove all hidden files (files with file names that begin with a dot) from + * the list. + */ static void DlgFileSelect_RemoveHiddenFiles(struct dirent **files) { int i; @@ -186,37 +187,37 @@ static void DlgFileSelect_RemoveHiddenFi /*-----------------------------------------------------------------------*/ -/* - Prepare to scroll up one entry. -*/ +/** + * Prepare to scroll up one entry. + */ static void DlgFileSelect_ScrollUp(void) { if (ypos > 0) { --ypos; - refreshentries = TRUE; + refreshentries = true; } } /*-----------------------------------------------------------------------*/ -/* - Prepare to scroll down one entry. -*/ +/** + * Prepare to scroll down one entry. + */ static void DlgFileSelect_ScrollDown(void) { if (ypos+SGFS_NUMENTRIES < entries) { ++ypos; - refreshentries = TRUE; + refreshentries = true; } } /*-----------------------------------------------------------------------*/ -/* - Handle SDL events. -*/ +/** + * Handle SDL events. + */ static void DlgFileSelect_HandleSdlEvents(SDL_Event *pEvent) { int oldypos = ypos; @@ -252,14 +253,14 @@ static void DlgFileSelect_HandleSdlEvent if (ypos < 0) ypos = 0; if (ypos != oldypos) - refreshentries = TRUE; + refreshentries = true; } /*-----------------------------------------------------------------------*/ -/* - Free file entries -*/ +/** + * Free file entries + */ static struct dirent **files_free(struct dirent **files) { int i; @@ -276,10 +277,10 @@ static struct dirent **files_free(struct /*-----------------------------------------------------------------------*/ -/* - Copy to dst src+add if they are below maxlen and return true, - otherwise return false -*/ +/** + * Copy to dst src+add if they are below maxlen and return true, + * otherwise return false + */ static int strcat_maxlen(char *dst, int maxlen, const char *src, const char *add) { int slen, alen; @@ -295,9 +296,9 @@ static int strcat_maxlen(char *dst, int } /*-----------------------------------------------------------------------*/ -/* - Create and return suitable path into zip file -*/ +/** + * Create and return suitable path into zip file + */ static char* zip_get_path(const char *zipdir, const char *zipfilename, int browsingzip) { if (browsingzip) @@ -311,7 +312,9 @@ static char* zip_get_path(const char *zi return strdup(""); } -/* string for zip root needs to be empty, check and correct if needed */ +/** + * string for zip root needs to be empty, check and correct if needed + */ static void correct_zip_root(char *zippath) { if (zippath[0] == PATHSEP && !zippath[1]) @@ -321,31 +324,32 @@ static void correct_zip_root(char *zippa } /*-----------------------------------------------------------------------*/ -/* - Show and process a file selection dialog. - Returns path/name user selected or NULL if user canceled - input: zip_path = pointer's pointer to buffer to contain file path - within a selected zip file, or NULL if browsing zip files is disallowed. - bAllowNew: TRUE if the user is allowed to insert new file names. -*/ +/** + * Show and process a file selection dialog. + * Returns path/name user selected or NULL if user canceled + * input: zip_path = pointer's pointer to buffer to contain file path + * within a selected zip file, or NULL if browsing zip files is disallowed. + * bAllowNew: true if the user is allowed to insert new file names. + */ char* SDLGui_FileSelect(const char *path_and_name, char **zip_path, bool bAllowNew) { struct dirent **files = NULL; char *pStringMem; char *retpath; - char *home, *path, *fname; /* The actual file and path names */ - bool reloaddir = TRUE; /* Do we have to reload the directory file list? */ + const char *home; + char *path, *fname; /* The actual file and path names */ + bool reloaddir = true; /* Do we have to reload the directory file list? */ int retbut; int oldcursorstate; int selection = -1; /* The actual selection, -1 if none selected */ char *zipfilename; /* Filename in zip file */ char *zipdir; - bool browsingzip = FALSE; /* Are we browsing an archive? */ + bool browsingzip = false; /* Are we browsing an archive? */ zip_dir *zipfiles = NULL; SDL_Event sdlEvent; ypos = 0; - refreshentries = TRUE; + refreshentries = true; entries = 0; /* Allocate memory for the file and path name strings: */ @@ -382,6 +386,7 @@ char* SDLGui_FileSelect(const char *path if (!(File_DirExists(path) || getcwd(path, FILENAME_MAX))) { perror("SDLGui_FileSelect: non-existing path and CWD failed"); + free(pStringMem); return NULL; } } @@ -405,6 +410,12 @@ char* SDLGui_FileSelect(const char *path if (browsingzip) { files = ZIP_GetFilesDir(zipfiles, zipdir, &entries); + if(!files) + { + fprintf(stderr, "SDLGui_FileSelect: ZIP_GetFilesDir error!\n"); + free(pStringMem); + return NULL; + } } else { @@ -426,8 +437,8 @@ char* SDLGui_FileSelect(const char *path } /* reload always implies refresh */ - reloaddir = FALSE; - refreshentries = TRUE; + reloaddir = false; + refreshentries = true; }/* reloaddir */ /* Update the file name strings in the dialog? */ @@ -438,7 +449,7 @@ char* SDLGui_FileSelect(const char *path free(pStringMem); return NULL; } - refreshentries = FALSE; + refreshentries = false; } /* Show dialog: */ @@ -457,7 +468,7 @@ char* SDLGui_FileSelect(const char *path return NULL; } - if (browsingzip == TRUE) + if (browsingzip == true) { if (!strcat_maxlen(tempstr, FILENAME_MAX, zipdir, files[retbut-SGFSDLG_ENTRY1+ypos]->d_name)) @@ -480,7 +491,7 @@ char* SDLGui_FileSelect(const char *path zipfiles = NULL; /* Copy the path name to the dialog */ File_ShrinkName(dlgpath, path, DLGPATH_SIZE); - browsingzip = FALSE; + browsingzip = false; } else { @@ -496,7 +507,7 @@ char* SDLGui_FileSelect(const char *path strcpy(zipdir, tempstr); File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE); } - reloaddir = TRUE; + reloaddir = true; /* Copy the path name to the dialog */ selection = -1; /* Remove old selection */ zipfilename[0] = '\0'; @@ -528,7 +539,7 @@ char* SDLGui_FileSelect(const char *path /* Copy the path name to the dialog */ File_ShrinkName(dlgpath, tempstr, DLGPATH_SIZE); strcpy(path, tempstr); - reloaddir = TRUE; + reloaddir = true; selection = -1; /* Remove old selection */ dlgfname[0] = 0; ypos = 0; @@ -537,15 +548,15 @@ char* SDLGui_FileSelect(const char *path { /* open a zip file */ zipfiles = ZIP_GetFiles(tempstr); - if (zipfiles != NULL && browsingzip == FALSE) + if (zipfiles != NULL && browsingzip == false) { selection = retbut-SGFSDLG_ENTRY1+ypos; strcpy(fname, files[selection]->d_name); File_ShrinkName(dlgfname, fname, DLGFNAME_SIZE); - browsingzip = TRUE; + browsingzip = true; zipdir[0] = '\0'; /* zip root */ File_ShrinkName(dlgpath, zipdir, DLGPATH_SIZE); - reloaddir = TRUE; + reloaddir = true; ypos = 0; } @@ -575,7 +586,7 @@ char* SDLGui_FileSelect(const char *path { /* free zip file entries */ ZIP_FreeZipDir(zipfiles); - browsingzip = FALSE; + browsingzip = false; zipfiles = NULL; File_ShrinkName(dlgpath, path, DLGPATH_SIZE); } @@ -593,24 +604,24 @@ char* SDLGui_FileSelect(const char *path File_PathShorten(path, 1); File_ShrinkName(dlgpath, path, DLGPATH_SIZE); } - reloaddir = TRUE; + reloaddir = true; break; case SGFSDLG_HOMEDIR: /* Change to home directory */ - home = getenv("HOME"); - if (home == NULL) + home = Paths_GetUserHome(); + if (home == NULL || !*home) break; if (browsingzip) { /* free zip file entries */ ZIP_FreeZipDir(zipfiles); zipfiles = NULL; - browsingzip = FALSE; + browsingzip = false; } strcpy(path, home); File_AddSlashToEndFileName(path); File_ShrinkName(dlgpath, path, DLGPATH_SIZE); - reloaddir = TRUE; + reloaddir = true; break; case SGFSDLG_ROOTDIR: /* Change to root directory */ @@ -619,11 +630,11 @@ char* SDLGui_FileSelect(const char *path /* free zip file entries */ ZIP_FreeZipDir(zipfiles); zipfiles = NULL; - browsingzip = FALSE; + browsingzip = false; } - strcpy(path, "/"); + path[0] = PATHSEP; path[1] = '\0'; strcpy(dlgpath, path); - reloaddir = TRUE; + reloaddir = true; break; case SGFSDLG_UP: /* Scroll up */ DlgFileSelect_ScrollUp(); @@ -637,7 +648,7 @@ char* SDLGui_FileSelect(const char *path strcpy(fname, dlgfname); break; case SGFSDLG_SHOWHIDDEN: /* Show/hide hidden files */ - reloaddir = TRUE; + reloaddir = true; ypos = 0; break; case SDLGUI_UNKNOWNEVENT: @@ -686,12 +697,13 @@ char* SDLGui_FileSelect(const char *path /*-----------------------------------------------------------------------*/ -/* Let user browse for a file, confname is used as default. +/** + * Let user browse for a file, confname is used as default. * If bAllowNew is true, user can select new files also. * * If no file is selected, or there's some problem with the file, - * return FALSE and clear dlgname & confname. - * Otherwise return TRUE, set dlgname & confname to the new file name + * return false and clear dlgname & confname. + * Otherwise return true, set dlgname & confname to the new file name * (dlgname is shrinked & limited to maxlen and confname is assumed * to have FILENAME_MAX amount of space). */ @@ -714,7 +726,7 @@ bool SDLGui_FileConfSelect(char *dlgname dlgname[0] = confname[0] = 0; } free(selname); - return TRUE; + return true; } - return FALSE; + return false; }