2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
2003-01-22 00:45:36 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
2003-01-22 00:45:36 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2003-01-22 00:45:36 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2003-01-22 00:45:36 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
2001-10-04 23:12:57 +08:00
|
|
|
|
2001-09-20 05:08:56 +08:00
|
|
|
#ifndef OSGDB_FILENAMEUTILS
|
|
|
|
#define OSGDB_FILENAMEUTILS 1
|
|
|
|
|
|
|
|
#include <osgDB/Export>
|
|
|
|
|
|
|
|
#include <string>
|
2011-05-12 21:27:14 +08:00
|
|
|
#include <vector>
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
namespace osgDB {
|
|
|
|
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */
|
2002-12-05 20:43:08 +08:00
|
|
|
extern OSGDB_EXPORT std::string getFilePath(const std::string& filename);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets the extension without dot (Ex: /a/b/c.Ext => Ext). */
|
2002-12-05 20:43:08 +08:00
|
|
|
extern OSGDB_EXPORT std::string getFileExtension(const std::string& filename);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets the extension including dot (Ex: /a/b/c.Ext => .Ext). */
|
2008-09-22 22:46:54 +08:00
|
|
|
extern OSGDB_EXPORT std::string getFileExtensionIncludingDot(const std::string& filename);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets the lowercase extension without dot (Ex: /a/b/c.Ext => ext). */
|
2002-12-05 20:43:08 +08:00
|
|
|
extern OSGDB_EXPORT std::string getLowerCaseFileExtension(const std::string& filename);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets file name with extension (Ex: /a/b/c.Ext => c.Ext). */
|
2002-12-05 20:43:08 +08:00
|
|
|
extern OSGDB_EXPORT std::string getSimpleFileName(const std::string& fileName);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets file path without last extension (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file.ext1). */
|
2004-05-07 23:18:59 +08:00
|
|
|
extern OSGDB_EXPORT std::string getNameLessExtension(const std::string& fileName);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Gets file path without \b all extensions (Ex: /a/b/c.Ext => /a/b/c ; file.ext1.ext2 => file). */
|
|
|
|
extern OSGDB_EXPORT std::string getNameLessAllExtensions(const std::string& fileName);
|
|
|
|
/** Gets file name without last extension (Ex: /a/b/c.Ext => c ; file.ext1.ext2 => file.ext1). */
|
2002-12-05 20:43:08 +08:00
|
|
|
extern OSGDB_EXPORT std::string getStrippedName(const std::string& fileName);
|
2011-01-21 19:46:15 +08:00
|
|
|
/** If 'to' is in a subdirectory of 'from' then this function returns the subpath, otherwise it just returns the file name.
|
|
|
|
* The function does \b not automagically resolve paths as the system does, so be careful to give canonical paths.
|
2018-04-21 00:18:22 +08:00
|
|
|
* However, the function interprets slashes ('/') and backslashes ('\') as they were equal.
|
2011-01-21 19:46:15 +08:00
|
|
|
*/
|
|
|
|
extern OSGDB_EXPORT std::string getPathRelative(const std::string& from, const std::string& to);
|
2011-04-21 03:20:28 +08:00
|
|
|
/** Gets root part of a path ("/" or "C:"), or an empty string if none found. */
|
|
|
|
extern OSGDB_EXPORT std::string getPathRoot(const std::string& path);
|
|
|
|
/** Tests if path is absolute, as !getPathRoot(path).empty(). */
|
|
|
|
extern OSGDB_EXPORT bool isAbsolutePath(const std::string& path);
|
2002-12-05 20:43:08 +08:00
|
|
|
|
2005-02-11 17:58:30 +08:00
|
|
|
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Converts forward slashes (/) to back slashes (\). */
|
2005-02-11 17:58:30 +08:00
|
|
|
extern OSGDB_EXPORT std::string convertFileNameToWindowsStyle(const std::string& fileName);
|
2010-03-10 19:40:17 +08:00
|
|
|
/** Converts back slashes (\) to forward slashes (/). */
|
2005-02-11 17:58:30 +08:00
|
|
|
extern OSGDB_EXPORT std::string convertFileNameToUnixStyle(const std::string& fileName);
|
2008-07-13 23:24:45 +08:00
|
|
|
extern OSGDB_EXPORT std::string convertToLowerCase(const std::string& fileName);
|
2005-02-11 17:58:30 +08:00
|
|
|
|
2010-11-01 18:52:20 +08:00
|
|
|
const char UNIX_PATH_SEPARATOR = '/';
|
|
|
|
const char WINDOWS_PATH_SEPARATOR = '\\';
|
|
|
|
|
|
|
|
/** Get the path separator for the current platform. */
|
|
|
|
extern OSGDB_EXPORT char getNativePathSeparator();
|
|
|
|
/** Check if the path contains only the current platform's path separators. */
|
2005-02-11 17:58:30 +08:00
|
|
|
extern OSGDB_EXPORT bool isFileNameNativeStyle(const std::string& fileName);
|
2010-11-01 18:52:20 +08:00
|
|
|
/** Convert the path to contain only the current platform's path separators. */
|
2005-02-11 17:58:30 +08:00
|
|
|
extern OSGDB_EXPORT std::string convertFileNameToNativeStyle(const std::string& fileName);
|
|
|
|
|
2002-12-05 20:43:08 +08:00
|
|
|
extern OSGDB_EXPORT bool equalCaseInsensitive(const std::string& lhs,const std::string& rhs);
|
|
|
|
extern OSGDB_EXPORT bool equalCaseInsensitive(const std::string& lhs,const char* rhs);
|
2001-09-20 05:08:56 +08:00
|
|
|
|
2004-10-06 21:11:04 +08:00
|
|
|
extern OSGDB_EXPORT bool containsServerAddress(const std::string& filename);
|
2009-03-10 20:21:13 +08:00
|
|
|
extern OSGDB_EXPORT std::string getServerProtocol(const std::string& filename);
|
2004-10-06 21:11:04 +08:00
|
|
|
extern OSGDB_EXPORT std::string getServerAddress(const std::string& filename);
|
|
|
|
extern OSGDB_EXPORT std::string getServerFileName(const std::string& filename);
|
|
|
|
|
2007-01-17 23:59:29 +08:00
|
|
|
/** Concatenates two paths */
|
|
|
|
extern OSGDB_EXPORT std::string concatPaths(const std::string& left, const std::string& right);
|
|
|
|
|
|
|
|
/** Removes .. and . dirs in a path */
|
|
|
|
extern OSGDB_EXPORT std::string getRealPath(const std::string& path);
|
|
|
|
|
2011-05-12 21:27:14 +08:00
|
|
|
/** Splits a path into elements between separators (including Windows' root, if any). */
|
|
|
|
extern OSGDB_EXPORT void getPathElements(const std::string& path, std::vector<std::string> & out_elements);
|
2007-01-17 23:59:29 +08:00
|
|
|
|
2012-09-13 00:02:02 +08:00
|
|
|
/** Functor for helping sort filename in alphabetical and numerical order when using in conjunction with std::sort.*/
|
|
|
|
struct FileNameComparator
|
|
|
|
{
|
|
|
|
inline bool operator() (const std::string& lhs, const std::string& rhs) const
|
|
|
|
{
|
|
|
|
std::string::size_type size_lhs = lhs.size();
|
|
|
|
std::string::size_type size_rhs = rhs.size();
|
|
|
|
std::string::size_type pos_lhs = 0;
|
|
|
|
std::string::size_type pos_rhs = 0;
|
|
|
|
while(pos_lhs<size_lhs && pos_rhs<size_rhs)
|
|
|
|
{
|
|
|
|
char c_lhs = lhs[pos_rhs];
|
|
|
|
char c_rhs = rhs[pos_rhs];
|
|
|
|
bool numeric_lhs = lhs[pos_lhs]>='0' && lhs[pos_lhs]<='9';
|
|
|
|
bool numeric_rhs = rhs[pos_rhs]>='0' && rhs[pos_rhs]<='9';
|
|
|
|
if (numeric_lhs && numeric_rhs)
|
|
|
|
{
|
|
|
|
std::string::size_type start_lhs = pos_lhs;
|
|
|
|
++pos_lhs;
|
|
|
|
while(pos_lhs<size_lhs && (lhs[pos_lhs]>='0' && lhs[pos_lhs]<='9')) ++pos_lhs;
|
|
|
|
|
|
|
|
std::string::size_type start_rhs = pos_rhs;
|
|
|
|
++pos_rhs;
|
|
|
|
while(pos_rhs<size_rhs && (rhs[pos_rhs]>='0' && rhs[pos_rhs]<='9')) ++pos_rhs;
|
|
|
|
|
|
|
|
if (pos_lhs<pos_rhs) return true;
|
|
|
|
else if (pos_rhs<pos_lhs) return false;
|
|
|
|
|
|
|
|
while(start_lhs<pos_lhs && start_rhs<pos_rhs)
|
|
|
|
{
|
|
|
|
if (lhs[start_lhs]<rhs[start_rhs]) return true;
|
|
|
|
if (lhs[start_lhs]>rhs[start_rhs]) return false;
|
|
|
|
++start_lhs;
|
|
|
|
++start_rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (c_lhs<c_rhs) return true;
|
|
|
|
else if (c_rhs<c_lhs) return false;
|
|
|
|
|
|
|
|
++pos_lhs;
|
|
|
|
++pos_rhs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pos_lhs<pos_rhs;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-06-25 23:08:15 +08:00
|
|
|
|
2016-06-25 23:32:58 +08:00
|
|
|
extern OSGDB_EXPORT void stringcopy(char* dest, const char* src, size_t length);
|
2016-06-25 23:08:15 +08:00
|
|
|
|
2016-06-25 23:13:56 +08:00
|
|
|
#define stringcopyfixedsize(DEST, SRC) stringcopy(DEST, SRC, sizeof(DEST));
|
2016-06-25 23:08:15 +08:00
|
|
|
|
2002-02-03 20:33:41 +08:00
|
|
|
}
|
2001-09-20 05:08:56 +08:00
|
|
|
|
|
|
|
#endif
|