2009-09-12 01:03:44 +00:00
|
|
|
/* PCSX2 - PS2 Emulator for PCs
|
2010-05-03 14:08:02 +00:00
|
|
|
* Copyright (C) 2002-2010 PCSX2 Dev Team
|
2009-10-04 09:00:07 +00:00
|
|
|
*
|
2009-09-12 01:03:44 +00:00
|
|
|
* PCSX2 is free software: you can redistribute it and/or modify it under the terms
|
|
|
|
* of the GNU Lesser General Public License as published by the Free Software Found-
|
|
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* PCSX2 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 PCSX2.
|
|
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-09-01 20:31:46 +00:00
|
|
|
#include "common/Path.h"
|
2009-09-12 01:03:44 +00:00
|
|
|
|
2009-09-13 17:11:35 +00:00
|
|
|
#include <wx/file.h>
|
2009-11-14 08:36:57 +00:00
|
|
|
#include <wx/utils.h>
|
2009-09-13 17:11:35 +00:00
|
|
|
|
2009-09-12 01:03:44 +00:00
|
|
|
// ---------------------------------------------------------------------------------
|
2010-06-06 23:26:07 +00:00
|
|
|
// wxDirName (implementations)
|
2009-09-12 01:03:44 +00:00
|
|
|
// ---------------------------------------------------------------------------------
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxFileName wxDirName::Combine(const wxFileName &right) const
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssertMsg(IsDir(), L"Warning: Malformed directory name detected during wxDirName concatenation.");
|
|
|
|
if (right.IsAbsolute())
|
|
|
|
return right;
|
2009-09-12 01:03:44 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
// Append any directory parts from right, and then set the filename.
|
|
|
|
// Except we can't do that because our m_members are private (argh!) and there is no API
|
|
|
|
// for getting each component of the path. So instead let's use Normalize:
|
2009-09-12 01:03:44 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxFileName result(right);
|
|
|
|
result.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE, GetPath());
|
|
|
|
return result;
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxDirName wxDirName::Combine(const wxDirName &right) const
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssertMsg(IsDir() && right.IsDir(), L"Warning: Malformed directory name detected during wDirName concatenation.");
|
2009-09-12 01:03:44 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxDirName result(right);
|
|
|
|
result.Normalize(wxPATH_NORM_ENV_VARS | wxPATH_NORM_DOTS | wxPATH_NORM_ABSOLUTE, GetPath());
|
|
|
|
return result;
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxDirName &wxDirName::Normalize(int flags, const wxString &cwd)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssertMsg(IsDir(), L"Warning: Malformed directory name detected during wDirName normalization.");
|
|
|
|
if (!wxFileName::Normalize(flags, cwd))
|
|
|
|
throw Exception::ParseError().SetDiagMsg(L"wxDirName::Normalize operation failed.");
|
|
|
|
return *this;
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxDirName &wxDirName::MakeRelativeTo(const wxString &pathBase)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssertMsg(IsDir(), L"Warning: Malformed directory name detected during wDirName normalization.");
|
|
|
|
if (!wxFileName::MakeRelativeTo(pathBase))
|
|
|
|
throw Exception::ParseError().SetDiagMsg(L"wxDirName::MakeRelativeTo operation failed.");
|
|
|
|
return *this;
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxDirName &wxDirName::MakeAbsolute(const wxString &cwd)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxAssertMsg(IsDir(), L"Warning: Malformed directory name detected during wDirName normalization.");
|
|
|
|
if (!wxFileName::MakeAbsolute(cwd))
|
|
|
|
throw Exception::ParseError().SetDiagMsg(L"wxDirName::MakeAbsolute operation failed.");
|
|
|
|
return *this;
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void wxDirName::Rmdir()
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
if (!Exists())
|
|
|
|
return;
|
|
|
|
wxFileName::Rmdir();
|
|
|
|
// TODO : Throw exception if operation failed? Do we care?
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool wxDirName::Mkdir()
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
// wxWidgets recurses directory creation for us.
|
2009-09-12 01:03:44 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
// only exist in wx2.9 and above
|
2011-02-20 16:57:36 +00:00
|
|
|
#ifndef wxS_DIR_DEFAULT
|
|
|
|
#define wxS_DIR_DEFAULT 0777
|
|
|
|
#endif
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
if (Exists())
|
|
|
|
return true;
|
|
|
|
return wxFileName::Mkdir(wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL);
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------------
|
2009-09-13 17:11:35 +00:00
|
|
|
// Path namespace (wxFileName helpers)
|
2009-09-12 01:03:44 +00:00
|
|
|
// ---------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
bool Path::IsRelative(const wxString &path)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return wxDirName(path).IsRelative();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2009-09-13 17:11:35 +00:00
|
|
|
// Returns -1 if the file does not exist.
|
2016-11-12 15:28:37 +00:00
|
|
|
s64 Path::GetFileSize(const wxString &path)
|
2009-09-13 17:11:35 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
if (!wxFile::Exists(path.c_str()))
|
|
|
|
return -1;
|
|
|
|
return (s64)wxFileName::GetSize(path).GetValue();
|
2009-09-13 17:11:35 +00:00
|
|
|
}
|
|
|
|
|
2009-11-24 08:32:39 +00:00
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::Normalize(const wxString &src)
|
2009-11-24 08:32:39 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
wxFileName normalize(src);
|
|
|
|
normalize.Normalize();
|
|
|
|
return normalize.GetFullPath();
|
2009-11-24 08:32:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::Normalize(const wxDirName &src)
|
2009-11-24 08:32:39 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return wxDirName(src).Normalize().ToString();
|
2009-11-24 08:32:39 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::MakeAbsolute(const wxString &src)
|
2010-10-27 15:16:14 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
wxFileName absolute(src);
|
|
|
|
absolute.MakeAbsolute();
|
|
|
|
return absolute.GetFullPath();
|
2010-10-27 15:16:14 +00:00
|
|
|
}
|
|
|
|
|
2009-09-12 01:03:44 +00:00
|
|
|
// Concatenates two pathnames together, inserting delimiters (backslash on win32)
|
|
|
|
// as needed! Assumes the 'dest' is allocated to at least g_MaxPath length.
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::Combine(const wxString &srcPath, const wxString &srcFile)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return (wxDirName(srcPath) + srcFile).GetFullPath();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::Combine(const wxDirName &srcPath, const wxFileName &srcFile)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return (srcPath + srcFile).GetFullPath();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::Combine(const wxString &srcPath, const wxDirName &srcFile)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return (wxDirName(srcPath) + srcFile).ToString();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replaces the extension of the file with the one given.
|
|
|
|
// This function works for path names as well as file names.
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::ReplaceExtension(const wxString &src, const wxString &ext)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
wxFileName jojo(src);
|
|
|
|
jojo.SetExt(ext);
|
|
|
|
return jojo.GetFullPath();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::ReplaceFilename(const wxString &src, const wxString &newfilename)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
wxFileName jojo(src);
|
|
|
|
jojo.SetFullName(newfilename);
|
|
|
|
return jojo.GetFullPath();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::GetFilename(const wxString &src)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return wxFileName(src).GetFullName();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::GetFilenameWithoutExt(const wxString &src)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return wxFileName(src).GetName();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::GetDirectory(const wxString &src)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
return wxFileName(src).GetPath();
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// returns the base/root directory of the given path.
|
|
|
|
// Example /this/that/something.txt -> dest == "/"
|
2016-11-12 15:28:37 +00:00
|
|
|
wxString Path::GetRootDirectory(const wxString &src)
|
2009-09-12 01:03:44 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
size_t pos = src.find_first_of(wxFileName::GetPathSeparators());
|
|
|
|
if (pos == wxString::npos)
|
|
|
|
return wxString();
|
|
|
|
else
|
|
|
|
return wxString(src.begin(), src.begin() + pos);
|
2009-09-12 01:03:44 +00:00
|
|
|
}
|
|
|
|
|
2009-11-14 08:36:57 +00:00
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Launches the specified file according to its mime type
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
void pxLaunch(const wxString &filename)
|
2009-11-14 08:36:57 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
wxLaunchDefaultBrowser(filename);
|
2009-11-14 08:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pxLaunch(const char *filename)
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxLaunch(fromUTF8(filename));
|
2009-11-14 08:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------
|
|
|
|
// Launches a file explorer window on the specified path. If the given path is not
|
|
|
|
// a qualified URI (with a prefix:// ), file:// is automatically prepended. This
|
|
|
|
// bypasses wxWidgets internal filename checking, which can end up launching things
|
|
|
|
// through browser more often than desired.
|
|
|
|
//
|
2016-11-12 15:28:37 +00:00
|
|
|
void pxExplore(const wxString &path)
|
2009-11-14 08:36:57 +00:00
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
wxLaunchDefaultBrowser(!path.Contains(L"://") ? L"file://" + path : path);
|
2009-11-14 08:36:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void pxExplore(const char *path)
|
|
|
|
{
|
2016-11-12 15:28:37 +00:00
|
|
|
pxExplore(fromUTF8(path));
|
2009-11-14 08:36:57 +00:00
|
|
|
}
|