2009-04-18 19:00:33 +02:00
|
|
|
/* Copyright (C) 2009 Wildfire Games.
|
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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.
|
|
|
|
*
|
|
|
|
* 0 A.D. 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 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2006-09-22 15:19:40 +02:00
|
|
|
namespace ERR
|
|
|
|
{
|
|
|
|
const LibError PATH_LENGTH = -100300;
|
|
|
|
const LibError PATH_EMPTY = -100301;
|
|
|
|
const LibError PATH_NOT_RELATIVE = -100302;
|
|
|
|
const LibError PATH_NON_PORTABLE = -100303;
|
|
|
|
const LibError PATH_NON_CANONICAL = -100304;
|
|
|
|
const LibError PATH_COMPONENT_SEPARATOR = -100305;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* check if path 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
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern LibError path_validate(const char* path);
|
2006-06-22 20:49:23 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* return appropriate code if path is invalid, otherwise continue.
|
|
|
|
**/
|
2006-05-17 16:48:18 +02:00
|
|
|
#define CHECK_PATH(path) RETURN_ERR(path_validate(path))
|
|
|
|
|
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
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern LibError path_component_validate(const char* name);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2007-04-22 18:43:54 +02:00
|
|
|
/**
|
|
|
|
* is the given character a path separator character?
|
|
|
|
*
|
|
|
|
* @param c character to test
|
|
|
|
* @return bool
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern bool path_is_dir_sep(char c);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2007-12-01 19:23:28 +01:00
|
|
|
/**
|
|
|
|
* is the given path(name) a directory?
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern bool path_IsDirectory(const char* path);
|
2007-12-01 19:23:28 +01: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
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern bool path_is_subpath(const char* s1, const char* s2);
|
2008-07-21 22:43:37 +02:00
|
|
|
extern bool path_is_subpathw(const wchar_t* s1, const wchar_t* s2);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* copy path strings (provided for convenience).
|
|
|
|
*
|
|
|
|
* @param dst destination; must be at least as large as source buffer,
|
|
|
|
* and should hold PATH_MAX chars.
|
|
|
|
* @param src source; should not exceed PATH_MAX chars
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern void path_copy(char* dst, const char* src);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* flags controlling path_append behavior
|
|
|
|
**/
|
2006-05-17 16:48:18 +02:00
|
|
|
enum PathAppendFlags
|
|
|
|
{
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* make sure <dst> ends up with a trailing slash. this is useful for
|
|
|
|
* VFS directory paths, which have that requirement.
|
|
|
|
**/
|
2006-05-17 16:48:18 +02:00
|
|
|
PATH_APPEND_SLASH = 1
|
|
|
|
};
|
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* append one path onto another, adding directory separator if necessary.
|
|
|
|
*
|
|
|
|
* @param dst destination into which combined path is written;
|
|
|
|
* must hold at least PATH_MAX chars.
|
|
|
|
* @param path1, path2 strings: empty, filenames, or full paths.
|
|
|
|
* total resulting string must not exceed PATH_MAX chars.
|
|
|
|
* @param flags see PathAppendFlags.
|
|
|
|
**/
|
2008-09-18 13:27:55 +02:00
|
|
|
extern void path_append(char* dst, const char* path1, const char* path2, size_t flags = 0);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* get the name component of a path.
|
|
|
|
*
|
|
|
|
* skips over all characters up to the last dir separator, if any.
|
|
|
|
* @param path input path.
|
|
|
|
* @return pointer to name component within <path>.
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern const char* path_name_only(const char* path);
|
2006-05-17 16:48:18 +02:00
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* strip away the name component in a path.
|
|
|
|
*
|
|
|
|
* @param path input and output; chopped by inserting '\0'.
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern void path_strip_fn(char* path);
|
2007-11-20 19:44:36 +01:00
|
|
|
|
2006-06-22 20:49:23 +02:00
|
|
|
/**
|
|
|
|
* get filename's extension.
|
|
|
|
*
|
|
|
|
* @return pointer to extension within <fn>, or "" if there is none.
|
|
|
|
* NOTE: does not include the period; e.g. "a.bmp" yields "bmp".
|
|
|
|
**/
|
2008-01-07 21:03:19 +01:00
|
|
|
extern const char* path_extension(const char* fn);
|
2006-04-24 01:14:18 +02:00
|
|
|
|
2007-05-07 18:33:24 +02:00
|
|
|
#endif // #ifndef INCLUDED_PATH_UTIL
|