2010-02-08 17:23:39 +01:00
|
|
|
/* Copyright (c) 2010 Wildfire Games
|
2009-04-18 19:00:33 +02:00
|
|
|
*
|
2010-02-08 17:23:39 +01:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
* in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 19:00:33 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-18 19:51:05 +02:00
|
|
|
/*
|
|
|
|
* helper functions for path strings.
|
2006-04-24 01:14:18 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
// notes:
|
2007-12-20 21:14:21 +01:00
|
|
|
// - this module is independent of lib/file so that it can be used from
|
2006-04-24 01:14:18 +02:00
|
|
|
// other code without pulling in the entire file manager.
|
|
|
|
// - there is no restriction on buffer lengths except the underlying OS.
|
|
|
|
// input buffers must not exceed PATH_MAX chars, while outputs
|
|
|
|
// must hold at least that much.
|
|
|
|
// - unless otherwise mentioned, all functions are intended to work with
|
2009-08-01 21:37:38 +02:00
|
|
|
// native and VFS paths.
|
2006-11-18 18:48:49 +01:00
|
|
|
// when reading, both '/' and SYS_DIR_SEP are accepted; '/' is written.
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#ifndef INCLUDED_PATH_UTIL
|
|
|
|
#define INCLUDED_PATH_UTIL
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
#include "lib/native_path.h"
|
|
|
|
#include "lib/posix/posix_filesystem.h"
|
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
namespace ERR
|
|
|
|
{
|
2009-11-05 21:46:15 +01:00
|
|
|
const LibError PATH_EMPTY = -100300;
|
|
|
|
const LibError PATH_COMPONENT_SEPARATOR = -100301;
|
|
|
|
const LibError PATH_NOT_FOUND = -100302;
|
2006-09-22 15:19:40 +02:00
|
|
|
}
|
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* check if name is valid. (see source for criteria)
|
|
|
|
*
|
2006-09-22 15:19:40 +02:00
|
|
|
* @return LibError (ERR::PATH_* or INFO::OK)
|
2006-06-22 20:49:23 +02:00
|
|
|
**/
|
2009-11-03 22:46:35 +01:00
|
|
|
LIB_API LibError path_component_validate(const wchar_t* name);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* is s2 a subpath of s1, or vice versa? (equal counts as subpath)
|
|
|
|
*
|
|
|
|
* @param s1, s2 comparand strings
|
|
|
|
* @return bool
|
|
|
|
**/
|
2009-11-03 22:46:35 +01:00
|
|
|
LIB_API bool path_is_subpath(const wchar_t* s1, const wchar_t* s2);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
2010-07-29 17:55:41 +02:00
|
|
|
* Get the name component of a path.
|
|
|
|
* Skips over all characters up to the last dir separator, if any.
|
2006-06-22 20:49:23 +02:00
|
|
|
*
|
2010-07-29 17:55:41 +02:00
|
|
|
* @param path Input path.
|
|
|
|
* @return pointer to name component within \<path\>.
|
2006-06-22 20:49:23 +02:00
|
|
|
**/
|
2009-11-03 22:46:35 +01:00
|
|
|
LIB_API const wchar_t* path_name_only(const wchar_t* path);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
namespace Path {
|
2011-02-15 11:38:11 +01:00
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline bool IsDirectory(const Path_t& pathname)
|
|
|
|
{
|
2011-03-21 22:06:08 +01:00
|
|
|
if(pathname.empty()) // (ensure length()-1 is safe)
|
|
|
|
return true; // (the VFS root directory is represented as an empty string)
|
2011-03-21 20:54:08 +01:00
|
|
|
|
|
|
|
// note: ideally, path strings would only contain '/' or even SYS_DIR_SEP.
|
|
|
|
// however, windows-specific code (e.g. the sound driver detection)
|
|
|
|
// uses these routines with '\\' strings. converting them all to
|
|
|
|
// '/' and then back before passing to OS functions would be annoying.
|
|
|
|
// also, the self-tests verify correct operation of such strings.
|
|
|
|
// it would be error-prone to only test the platform's separator
|
|
|
|
// strings there. hence, we allow all separators here.
|
2011-03-21 22:06:08 +01:00
|
|
|
return pathname[pathname.length()-1] == '/' || pathname[pathname.length()-1] == '\\';
|
2011-03-21 20:54:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t Path(const Path_t& pathname)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
|
|
|
size_t n = pathname.find_last_of('/');
|
2011-03-21 20:54:08 +01:00
|
|
|
if(n == Path_t::npos)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
|
|
|
n = pathname.find_last_of('\\');
|
2011-03-21 20:54:08 +01:00
|
|
|
if(n == Path_t::npos)
|
2011-02-15 11:38:11 +01:00
|
|
|
return L"";
|
|
|
|
}
|
|
|
|
return pathname.substr(0, n);
|
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t Filename(const Path_t& pathname)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
|
|
|
size_t n = pathname.find_last_of('/');
|
2011-03-21 20:54:08 +01:00
|
|
|
if(n == Path_t::npos)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
|
|
|
n = pathname.find_last_of('\\');
|
2011-03-21 20:54:08 +01:00
|
|
|
if(n == Path_t::npos)
|
2011-02-15 11:38:11 +01:00
|
|
|
return pathname;
|
|
|
|
}
|
|
|
|
return pathname.substr(n+1);
|
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t Basename(const Path_t& pathname)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
2011-03-21 20:54:08 +01:00
|
|
|
const Path_t filename = Filename(pathname);
|
2011-03-21 18:53:13 +01:00
|
|
|
const size_t idxDot = filename.find_last_of('.');
|
2011-03-21 20:54:08 +01:00
|
|
|
if(idxDot == Path_t::npos)
|
2011-02-15 11:38:11 +01:00
|
|
|
return filename;
|
2011-03-21 18:53:13 +01:00
|
|
|
return filename.substr(0, idxDot);
|
2011-02-15 11:38:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline std::wstring Extension(const Path_t& pathname)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
2011-03-21 18:53:13 +01:00
|
|
|
const size_t idxDot = pathname.find_last_of('.');
|
2011-03-21 20:54:08 +01:00
|
|
|
if(idxDot == Path_t::npos)
|
|
|
|
return Path_t();
|
2011-03-21 18:53:13 +01:00
|
|
|
return pathname.substr(idxDot);
|
2011-02-15 11:38:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
static inline std::wstring JoinPathStrings(const std::wstring& path1, const std::wstring& path2)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
2011-03-21 20:54:08 +01:00
|
|
|
std::wstring ret = path1;
|
|
|
|
if(!IsDirectory(path1))
|
2011-02-15 11:38:11 +01:00
|
|
|
ret += '/';
|
|
|
|
ret += path2;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t Join(const Path_t& path1, const Path_t& path2)
|
|
|
|
{
|
|
|
|
return JoinPathStrings(path1, path2);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t Join(const Path_t& path1, const char* literal)
|
|
|
|
{
|
|
|
|
return JoinPathStrings(path1, NativePathFromString(literal));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t Join(const char* literal, const Path_t& path2)
|
2011-03-21 18:53:13 +01:00
|
|
|
{
|
2011-03-21 20:54:08 +01:00
|
|
|
return JoinPathStrings(NativePathFromString(literal), path2);
|
2011-03-21 18:53:13 +01:00
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t AddSlash(const Path_t& path)
|
2011-03-21 18:53:13 +01:00
|
|
|
{
|
2011-03-21 20:54:08 +01:00
|
|
|
return IsDirectory(path)? path : path+L'/';
|
2011-03-21 18:53:13 +01:00
|
|
|
}
|
|
|
|
|
2011-03-21 20:54:08 +01:00
|
|
|
template<class Path_t>
|
|
|
|
static inline Path_t ChangeExtension(const Path_t& pathname, const std::wstring& extension)
|
2011-02-15 11:38:11 +01:00
|
|
|
{
|
2011-03-21 20:54:08 +01:00
|
|
|
return Join(Path(pathname), Basename(pathname)+extension);
|
2011-02-15 11:38:11 +01:00
|
|
|
}
|
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
} // namespace Path
|
|
|
|
|
|
|
|
static inline bool FileExists(const NativePath& pathname)
|
2011-02-25 17:31:42 +01:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
const bool exists = wstat(pathname.c_str(), &s) == 0;
|
|
|
|
return exists;
|
|
|
|
}
|
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
static inline u64 FileSize(const NativePath& pathname)
|
2011-02-25 17:31:42 +01:00
|
|
|
{
|
|
|
|
struct stat s;
|
|
|
|
debug_assert(wstat(pathname.c_str(), &s) == 0);
|
|
|
|
return s.st_size;
|
|
|
|
}
|
|
|
|
|
2011-03-21 18:53:13 +01:00
|
|
|
static inline bool DirectoryExists(const NativePath& path)
|
|
|
|
{
|
|
|
|
WDIR* dir = wopendir(path.c_str());
|
|
|
|
if(dir)
|
|
|
|
{
|
|
|
|
wclosedir(dir);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#endif // #ifndef INCLUDED_PATH_UTIL
|