better path handling code

This commit is contained in:
dborth 2009-06-12 07:43:19 +00:00
parent b9a9bf6aaa
commit 279333fd01
3 changed files with 46 additions and 17 deletions

View File

@ -17,9 +17,7 @@
#include <malloc.h>
#ifdef HW_RVL
extern "C" {
#include <di/di.h>
}
#endif
#include "vba.h"
@ -127,6 +125,28 @@ void ResetBrowser()
memset(browserList, 0, sizeof(BROWSERENTRY));
}
/****************************************************************************
* CleanupPath()
* Cleans up the filepath, removing double // and replacing \ with /
***************************************************************************/
static void CleanupPath(char * path)
{
int pathlen = strlen(path);
int j = 0;
for(int i=0; i < pathlen && i < MAXPATHLEN; i++)
{
if(path[i] == '\\')
path[i] = '/';
if(j == 0 || !(path[j-1] == '/' && path[i] == '/'))
path[j++] = path[i];
}
path[j] = 0;
if(strlen(path) == 0)
sprintf(path, "/");
}
/****************************************************************************
* UpdateDirName()
* Update curent directory name for file browser
@ -171,7 +191,7 @@ int UpdateDirName(int method)
if ((strlen(browser.dir)+1+strlen(browserList[browser.selIndex].filename)) < MAXPATHLEN)
{
/* update current directory name */
sprintf(browser.dir, "%s/%s",browser.dir, browserList[browser.selIndex].filename);
sprintf(browser.dir, "%s%s/",browser.dir, browserList[browser.selIndex].filename);
return 1;
}
else
@ -200,7 +220,7 @@ bool MakeFilePath(char filepath[], int type, int method, char * filename, int fi
}
else
{
sprintf(temppath, "%s/%s",browser.dir,browserList[browser.selIndex].filename);
sprintf(temppath, "%s%s",browser.dir,browserList[browser.selIndex].filename);
}
}
else
@ -269,10 +289,11 @@ bool MakeFilePath(char filepath[], int type, int method, char * filename, int fi
temppath[31] = 0; // truncate filename
break;
default:
sprintf (temppath, "%s/%s", folder, file);
sprintf (temppath, "/%s/%s", folder, file);
break;
}
}
CleanupPath(temppath); // cleanup path
strncpy(filepath, temppath, MAXPATHLEN);
return true;
}
@ -493,6 +514,8 @@ int BrowserChangeFolder(int method)
if(!UpdateDirName(method))
return -1;
CleanupPath(browser.dir);
switch (method)
{
case METHOD_DVD:
@ -534,7 +557,8 @@ OpenGameList ()
SwitchDVDFolder(GCSettings.LoadFolder); // switch to ROM folder
break;
default:
sprintf(browser.dir, "/%s", GCSettings.LoadFolder);
sprintf(browser.dir, "/%s/", GCSettings.LoadFolder);
CleanupPath(browser.dir);
ParseDirectory(method); // Parse root directory
break;
}

View File

@ -172,11 +172,11 @@ InitDeviceThread()
void UnmountAllFAT()
{
#ifdef HW_RVL
fatUnmount("sd:/");
fatUnmount("usb:/");
fatUnmount("sd:");
fatUnmount("usb:");
#else
fatUnmount("carda:/");
fatUnmount("cardb:/");
fatUnmount("carda:");
fatUnmount("cardb:");
#endif
}
@ -220,7 +220,7 @@ bool MountFAT(int method)
return false; // unknown device
}
sprintf(rootdir, "%s:/", name);
sprintf(rootdir, "%s:", name);
if(unmountRequired[method])
{
@ -283,27 +283,29 @@ bool ChangeInterface(int method, bool silent)
}
else if(method == METHOD_DVD)
{
rootdir[0] = 0;
mounted = MountDVD(silent);
}
#ifdef HW_RVL
else if(method == METHOD_SMB)
{
sprintf(rootdir, "smb:/");
mounted = ConnectShare(silent);
}
#endif
else if(method == METHOD_MC_SLOTA)
{
rootdir[0] = 0;
mounted = TestMC(CARD_SLOTA, silent);
}
else if(method == METHOD_MC_SLOTB)
{
rootdir[0] = 0;
mounted = TestMC(CARD_SLOTB, silent);
}
if(!mounted)
{
sprintf(browser.dir,"/");
rootdir[0] = 0;
}
return mounted;
}
@ -347,7 +349,8 @@ ParseDirectory(int method)
if(ChangeInterface(method, SILENT))
{
sprintf(browser.dir,"/");
dir = diropen(rootdir);
sprintf(fulldir, "%s%s", rootdir, browser.dir);
dir = diropen(fulldir);
if (dir == NULL)
{
sprintf(msg, "Error opening %s", rootdir);
@ -646,7 +649,7 @@ SaveFile (char * buffer, char *filepath, u32 datasize, int method, bool silent)
ResumeDeviceThread();
CancelAction();
return written;
return written;
}
u32 SaveFile(char * filepath, u32 datasize, int method, bool silent)

View File

@ -20,6 +20,7 @@
#include "menu.h"
#include "fileop.h"
#include "http.h"
#include "filebrowser.h"
static bool inNetworkInit = false;
static bool networkInit = false;
@ -278,6 +279,7 @@ ConnectShare (bool silent)
GCSettings.smbshare, GCSettings.smbip))
{
networkShareInit = true;
sprintf(rootdir, "smb:");
}
if(!silent)