1
0
forked from 0ad/0ad
0ad/source/lib/path_util.h
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

136 lines
3.7 KiB
C++

/**
* =========================================================================
* File : path_util.h
* Project : 0 A.D.
* Description : helper functions for path strings.
* =========================================================================
*/
// license: GPL; see lib/license.txt
// notes:
// - this module is independent of lib/file so that it can be used from
// 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
// native and portable and VFS paths.
// when reading, both '/' and SYS_DIR_SEP are accepted; '/' is written.
#ifndef INCLUDED_PATH_UTIL
#define INCLUDED_PATH_UTIL
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;
}
/**
* check if path is valid. (see source for criteria)
*
* @return LibError (ERR::PATH_* or INFO::OK)
**/
extern LibError path_validate(const char* path);
/**
* return appropriate code if path is invalid, otherwise continue.
**/
#define CHECK_PATH(path) RETURN_ERR(path_validate(path))
/**
* check if name is valid. (see source for criteria)
*
* @return LibError (ERR::PATH_* or INFO::OK)
**/
extern LibError path_component_validate(const char* name);
/**
* is the given character a path separator character?
*
* @param c character to test
* @return bool
**/
extern bool path_is_dir_sep(char c);
/**
* is the given path(name) a directory?
*
* @return bool
**/
extern bool path_IsDirectory(const char* path);
/**
* is s2 a subpath of s1, or vice versa? (equal counts as subpath)
*
* @param s1, s2 comparand strings
* @return bool
**/
extern bool path_is_subpath(const char* s1, const char* s2);
/**
* 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
**/
extern void path_copy(char* dst, const char* src);
/**
* flags controlling path_append behavior
**/
enum PathAppendFlags
{
/**
* make sure <dst> ends up with a trailing slash. this is useful for
* VFS directory paths, which have that requirement.
**/
PATH_APPEND_SLASH = 1
};
/**
* 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.
* @return LibError
**/
extern LibError path_append(char* dst, const char* path1, const char* path2, int flags = 0);
/**
* 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>.
**/
extern const char* path_name_only(const char* path);
/**
* strip away the name component in a path.
*
* @param path input and output; chopped by inserting '\0'.
**/
extern void path_strip_fn(char* path);
/**
* 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".
**/
extern const char* path_extension(const char* fn);
#endif // #ifndef INCLUDED_PATH_UTIL