scummvm/common/path.h

159 lines
4.1 KiB
C
Raw Normal View History

2021-08-01 23:30:21 +00:00
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#ifndef COMMON_PATH_H
#define COMMON_PATH_H
#include "common/scummsys.h"
#include "common/str.h"
namespace Common {
2021-08-05 03:41:58 +00:00
/**
* @defgroup common_path Path
* @ingroup common
*
* @brief API for working with paths.
*
* @{
*/
2021-08-01 23:30:21 +00:00
const char DIR_SEPARATOR = '\x1f'; // unit separator
2021-08-05 03:41:58 +00:00
/**
* Simple path class. Allows simple conversion to/from path strings with
* arbitrary directory separators, providing a common representation.
*
* Internally, this is just a simple wrapper around a String, using
* '\x1f' (unit separator) as a directory separator. As this is not
* a printable character, it should not appear in file names, unlike
* '/', '\', or ':', which are allowed on certain platforms.
*/
2021-08-01 23:30:21 +00:00
class Path {
private:
String _str;
public:
2021-08-05 03:41:58 +00:00
/** Construct a new empty path. */
2021-08-01 23:30:21 +00:00
Path() {}
2021-08-05 03:41:58 +00:00
/** Construct a copy of the given path. */
2021-08-01 23:30:21 +00:00
Path(const Path &path);
2021-08-05 03:41:58 +00:00
/**
* Construct a new path from the given NULL-terminated C string.
*
* @param str A NULL-terminated C string representing a path,
* e.g. "foo/bar/baz"
* @param separator The directory separator used in the path string.
* Defaults to '/'.
*/
2021-08-01 23:30:21 +00:00
Path(const char *str, char separator = '/');
2021-08-05 03:41:58 +00:00
/**
* Construct a new path from the given String.
*
* @param str A String representing a path, e.g. "foo/bar/baz"
* @param separator The directory separator used in the path string.
* Defaults to '/'.
*/
2021-08-01 23:30:21 +00:00
Path(const String &str, char separator = '/');
2021-08-05 03:41:58 +00:00
/** Construct a path consisting of the given character. */
2021-08-01 23:30:21 +00:00
Path(char c, char separator = '/');
private:
void setString(const char *str, char separator = '/');
public:
2021-08-05 03:41:58 +00:00
/**
* Returns the unmodified, internal representation of the path,
* using '\x1f' as a directory separator.
*/
2021-08-01 23:30:21 +00:00
const String &rawString() const { return _str; }
2021-08-05 03:41:58 +00:00
/**
* Converts a path to a string using the given directory separator.
*
* @param separator The character used to separate directory names.
* Defaults to '/'.
*/
2021-08-01 23:30:21 +00:00
String toString(char separator = '/') const;
2021-08-05 03:41:58 +00:00
/** Check whether this path is identical to path @p x. */
2021-08-01 23:30:21 +00:00
bool operator==(const Path &x) const;
2021-08-05 03:41:58 +00:00
/** Check whether this path is different than path @p x. */
2021-08-01 23:30:21 +00:00
bool operator!=(const Path &x) const;
2021-08-05 03:41:58 +00:00
/** Return if this path is empty */
2021-08-01 23:30:21 +00:00
bool empty() const;
2021-08-05 03:41:58 +00:00
/** Assign a given path to this path. */
2021-08-01 23:30:21 +00:00
Path &operator=(const Path &str);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path &operator=(const char *str);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path &operator=(const String &str);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path &operator=(char c);
2021-08-05 03:41:58 +00:00
/**
* Append the given path to this path. Does not automatically
* add a directory separator.
*/
2021-08-01 23:30:21 +00:00
Path &operator+=(const Path &str);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path &operator+=(const char *str);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path &operator+=(const String &str);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path &operator+=(char c);
};
2021-08-05 03:41:58 +00:00
/**
* Concatenate paths @p x and @p y. Does not automatically
* add a directory separator between them.
*/
2021-08-01 23:30:21 +00:00
Path operator+(const Path &x, const Path &y);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path operator+(const Path &x, const String &y);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path operator+(const Path &x, const char *y);
2021-08-05 03:41:58 +00:00
/** @overload */
2021-08-01 23:30:21 +00:00
Path operator+(const Path &x, char y);
2021-08-05 03:41:58 +00:00
/** @} */
2021-08-01 23:30:21 +00:00
} // End of namespace Common
#endif