--- hatari/src/gui-sdl/dlgFileSelect.c 2019/04/01 07:13:50 1.1.1.4 +++ 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.4 2019/04/01 07:13:50 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" @@ -76,7 +77,7 @@ static SGOBJ fsdlg[] = { SGTEXT, SG_EXIT, 0, 2,21, DLGFILENAMES_SIZE,1, dlgfilenames[15] }, { SGBUTTON, SG_TOUCHEXIT, 0, 62,6, 1,1, "\x01" }, /* Arrow up */ { SGBUTTON, SG_TOUCHEXIT, 0, 62,21, 1,1, "\x02" }, /* Arrow down */ - { SGCHECKBOX, SG_EXIT, SG_SELECTED, 2,23, 18,1, "Show hidden files" }, + { SGCHECKBOX, SG_EXIT, 0, 2,23, 18,1, "Show hidden files" }, { SGBUTTON, SG_DEFAULT, 0, 32,23, 8,1, "Okay" }, { SGBUTTON, SG_CANCEL, 0, 50,23, 8,1, "Cancel" }, { -1, 0, 0, 0,0, 0,0, NULL } @@ -84,16 +85,16 @@ static SGOBJ fsdlg[] = static int ypos; /* First entry number to be displayed */ -static BOOL refreshentries; /* Do we have to update the file names in the dialog? */ +static bool refreshentries; /* Do we have to update the file names in the dialog? */ static int entries; /* How many files are in the actual directory? */ /*-----------------------------------------------------------------------*/ -/* - 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) +/** + * 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; char *tempstr = malloc(FILENAME_MAX); @@ -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,32 +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. -*/ -char* SDLGui_FileSelect(const char *path_and_name, char **zip_path, BOOL bAllowNew) +/** + * 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; - struct stat filestat; ypos = 0; - refreshentries = TRUE; + refreshentries = true; entries = 0; /* Allocate memory for the file and path name strings: */ @@ -356,6 +359,8 @@ char* SDLGui_FileSelect(const char *path zipdir = pStringMem + 2 * FILENAME_MAX; zipfilename = pStringMem + 3 * FILENAME_MAX; zipfilename[0] = 0; + fname[0] = 0; + path[0] = 0; SDLGui_CenterDlg(fsdlg); if (bAllowNew) @@ -375,21 +380,17 @@ char* SDLGui_FileSelect(const char *path strncpy(path, path_and_name, FILENAME_MAX); path[FILENAME_MAX-1] = '\0'; } - else + if (!File_DirExists(path)) { - if (!getcwd(path, FILENAME_MAX)) + File_SplitPath(path, path, fname, NULL); + if (!(File_DirExists(path) || getcwd(path, FILENAME_MAX))) { - perror("SDLGui_FileSelect"); + perror("SDLGui_FileSelect: non-existing path and CWD failed"); + free(pStringMem); return NULL; } } - if (stat(path, &filestat) == 0 && S_ISDIR(filestat.st_mode)) - { - File_AddSlashToEndFileName(path); - fname[0] = 0; - } - else - File_SplitPath(path, path, fname, NULL); + File_MakeAbsoluteName(path); File_MakeValidPathName(path); File_ShrinkName(dlgpath, path, DLGPATH_SIZE); @@ -409,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,12 +433,12 @@ char* SDLGui_FileSelect(const char *path { fprintf(stderr, "SDLGui_FileSelect: Path not found.\n"); free(pStringMem); - return FALSE; + return NULL; } /* reload always implies refresh */ - reloaddir = FALSE; - refreshentries = TRUE; + reloaddir = false; + refreshentries = true; }/* reloaddir */ /* Update the file name strings in the dialog? */ @@ -440,9 +447,9 @@ char* SDLGui_FileSelect(const char *path if (!DlgFileSelect_RefreshEntries(files, path, browsingzip)) { free(pStringMem); - return FALSE; + return NULL; } - refreshentries = FALSE; + refreshentries = false; } /* Show dialog: */ @@ -458,17 +465,17 @@ char* SDLGui_FileSelect(const char *path { perror("Error while allocating temporary memory in SDLGui_FileSelect()"); free(pStringMem); - return FALSE; + return NULL; } - if (browsingzip == TRUE) + if (browsingzip == true) { if (!strcat_maxlen(tempstr, FILENAME_MAX, zipdir, files[retbut-SGFSDLG_ENTRY1+ypos]->d_name)) { fprintf(stderr, "SDLGui_FileSelect: Path name too long!\n"); free(pStringMem); - return FALSE; + return NULL; } /* directory? */ if (File_DoesFileNameEndWithSlash(tempstr)) @@ -484,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 { @@ -500,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'; @@ -523,16 +530,16 @@ char* SDLGui_FileSelect(const char *path { fprintf(stderr, "SDLGui_FileSelect: Path name too long!\n"); free(pStringMem); - return FALSE; + return NULL; } - if (stat(tempstr, &filestat) == 0 && S_ISDIR(filestat.st_mode)) + if (File_DirExists(tempstr)) { File_HandleDotDirs(tempstr); File_AddSlashToEndFileName(tempstr); /* 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; @@ -541,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; } @@ -579,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); } @@ -597,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 */ @@ -623,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(); @@ -641,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: @@ -690,16 +697,17 @@ 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). */ -BOOL SDLGui_FileConfSelect(char *dlgname, char *confname, int maxlen, BOOL bAllowNew) +bool SDLGui_FileConfSelect(char *dlgname, char *confname, int maxlen, bool bAllowNew) { char *selname; @@ -718,7 +726,7 @@ BOOL SDLGui_FileConfSelect(char *dlgname dlgname[0] = confname[0] = 0; } free(selname); - return TRUE; + return true; } - return FALSE; + return false; }