2019-03-22 16:51:47 +00:00
|
|
|
/*
|
2019-04-08 02:00:49 +00:00
|
|
|
* Copyright (c) 2018-2019 Atmosphère-NX
|
2019-03-22 16:51:47 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms and conditions of the GNU General Public License,
|
|
|
|
* version 2, as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope 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, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#include <switch.h>
|
|
|
|
|
|
|
|
struct FsPath {
|
|
|
|
char str[FS_MAX_PATH];
|
|
|
|
};
|
|
|
|
|
2019-03-28 17:43:33 +00:00
|
|
|
constexpr FsPath EncodeConstantFsPath(const char *p) {
|
|
|
|
FsPath path = {0};
|
|
|
|
for (size_t i = 0; i < sizeof(path) - 1; i++) {
|
|
|
|
path.str[i] = p[i];
|
|
|
|
if (p[i] == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
constexpr size_t GetConstantFsPathLen(FsPath path) {
|
|
|
|
size_t len = 0;
|
|
|
|
for (size_t i = 0; i < sizeof(path) - 1; i++) {
|
|
|
|
if (path.str[i] == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2019-03-22 16:51:47 +00:00
|
|
|
class FsPathUtils {
|
|
|
|
public:
|
2019-03-28 17:43:33 +00:00
|
|
|
static constexpr FsPath RootPath = EncodeConstantFsPath("/");
|
|
|
|
public:
|
2019-03-22 16:51:47 +00:00
|
|
|
static Result VerifyPath(const char *path, size_t max_path_len, size_t max_name_len);
|
|
|
|
static Result ConvertPathForServiceObject(FsPath *out, const char *path);
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2019-03-22 18:28:09 +00:00
|
|
|
static Result IsNormalized(bool *out, const char *path);
|
|
|
|
static Result Normalize(char *out, size_t max_out_size, const char *src, size_t *out_size);
|
2019-03-22 16:51:47 +00:00
|
|
|
|
2019-03-22 18:28:09 +00:00
|
|
|
static bool IsWindowsDriveLetter(const char c) {
|
|
|
|
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
|
|
|
|
}
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2019-03-22 18:52:03 +00:00
|
|
|
static bool IsCurrentDirectory(const char *path) {
|
|
|
|
return path[0] == '.' && (path[1] == 0 || path[1] == '/');
|
|
|
|
}
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2019-03-22 18:52:03 +00:00
|
|
|
static bool IsParentDirectory(const char *path) {
|
|
|
|
return path[0] == '.' && path[1] == '.' && (path[2] == 0 || path[2] == '/');
|
|
|
|
}
|
2019-06-20 20:00:32 +00:00
|
|
|
|
2019-03-22 16:51:47 +00:00
|
|
|
static bool IsWindowsAbsolutePath(const char *path) {
|
|
|
|
/* Nintendo uses this in path comparisons... */
|
2019-03-22 18:28:09 +00:00
|
|
|
return IsWindowsDriveLetter(path[0]) && path[1] == ':';
|
2019-03-22 16:51:47 +00:00
|
|
|
}
|
|
|
|
};
|