| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- /*
- * Copyright (c) 2012-2018 Daniele Bartolini and individual contributors.
- * License: https://github.com/dbartolini/crown/blob/master/LICENSE
- */
- #include "core/filesystem/path.h"
- #include "core/platform.h"
- #include "core/strings/dynamic_string.h"
- #include <ctype.h> // isalpha
- #include <string.h> // strrchr
- namespace crown
- {
- #if CROWN_PLATFORM_POSIX
- const char PATH_SEPARATOR = '/';
- #elif CROWN_PLATFORM_WINDOWS
- const char PATH_SEPARATOR = '\\';
- #endif // CROWN_PLATFORM_POSIX
- namespace path
- {
- bool is_absolute(const char* path)
- {
- CE_ENSURE(NULL != path);
- #if CROWN_PLATFORM_POSIX
- return strlen32(path) > 0
- && path[0] == PATH_SEPARATOR
- ;
- #elif CROWN_PLATFORM_WINDOWS
- return strlen32(path) > 2
- && isalpha(path[0])
- && path[1] == ':'
- && path[2] == PATH_SEPARATOR
- ;
- #endif
- }
- bool is_relative(const char* path)
- {
- CE_ENSURE(NULL != path);
- return !is_absolute(path);
- }
- bool is_root(const char* path)
- {
- CE_ENSURE(NULL != path);
- #if CROWN_PLATFORM_POSIX
- return is_absolute(path) && strlen32(path) == 1;
- #elif CROWN_PLATFORM_WINDOWS
- return is_absolute(path) && strlen32(path) == 3;
- #endif
- }
- void join(DynamicString& path, const char* path_a, const char* path_b)
- {
- CE_ENSURE(NULL != path_a);
- CE_ENSURE(NULL != path_b);
- const u32 la = strlen32(path_a);
- const u32 lb = strlen32(path_b);
- path.reserve(la + lb + 1);
- path = path_a;
- if (la != 0 && lb != 0)
- path += PATH_SEPARATOR;
- path += path_b;
- }
- const char* basename(const char* path)
- {
- CE_ENSURE(NULL != path);
- const char* ls = strrchr(path, '/');
- return ls == NULL ? path : ls + 1;
- }
- const char* extension(const char* path)
- {
- CE_ENSURE(NULL != path);
- const char* ld = strrchr(path, '.');
- return ld == NULL ? NULL : ld + 1;
- }
- bool has_trailing_separator(const char* path)
- {
- CE_ENSURE(NULL != path);
- return path[strlen32(path) - 1] == PATH_SEPARATOR;
- }
- inline bool any_separator(char c)
- {
- return c == '/' || c == '\\';
- }
- void reduce(DynamicString& clean, const char* path)
- {
- if (path == NULL)
- return;
- char cc = any_separator(*path) ? PATH_SEPARATOR : *path;
- clean += cc;
- ++path;
- for (; *path; ++path)
- {
- if (cc == PATH_SEPARATOR && any_separator(*path))
- continue;
- cc = any_separator(*path) ? PATH_SEPARATOR : *path;
- clean += cc;
- }
- if (has_trailing_separator(clean.c_str()))
- array::pop_back(clean._data);
- }
- } // namespace path
- } // namespace crown
|