From 3107ec0127b7e1b1c0f488ddb8ff8ef63bb849c6 Mon Sep 17 00:00:00 2001 From: Michael Scire Date: Wed, 3 Feb 2021 14:28:01 -0800 Subject: [PATCH] pf2: skeleton str api --- .../libvapours/include/vapours/prfile2.hpp | 1 + .../vapours/prfile2/prfile2_common.hpp | 1 + .../include/vapours/prfile2/prfile2_str.hpp | 65 +++++++++++ .../vapours/prfile2/prfile2_wide_string.hpp | 32 +++++ .../libvapours/source/prfile2/prfile2_str.cpp | 110 ++++++++++++++++++ .../source/prfile2/prfile2_wide_string.cpp | 109 +++++++++++++++++ 6 files changed, 318 insertions(+) create mode 100644 libraries/libvapours/include/vapours/prfile2/prfile2_str.hpp create mode 100644 libraries/libvapours/include/vapours/prfile2/prfile2_wide_string.hpp create mode 100644 libraries/libvapours/source/prfile2/prfile2_str.cpp create mode 100644 libraries/libvapours/source/prfile2/prfile2_wide_string.cpp diff --git a/libraries/libvapours/include/vapours/prfile2.hpp b/libraries/libvapours/include/vapours/prfile2.hpp index 80844e8ab..3b8a90642 100644 --- a/libraries/libvapours/include/vapours/prfile2.hpp +++ b/libraries/libvapours/include/vapours/prfile2.hpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include diff --git a/libraries/libvapours/include/vapours/prfile2/prfile2_common.hpp b/libraries/libvapours/include/vapours/prfile2/prfile2_common.hpp index 4e339aa25..80994a7eb 100644 --- a/libraries/libvapours/include/vapours/prfile2/prfile2_common.hpp +++ b/libraries/libvapours/include/vapours/prfile2/prfile2_common.hpp @@ -17,6 +17,7 @@ #include #include #include +#include namespace ams::prfile2 { diff --git a/libraries/libvapours/include/vapours/prfile2/prfile2_str.hpp b/libraries/libvapours/include/vapours/prfile2/prfile2_str.hpp new file mode 100644 index 000000000..30e112300 --- /dev/null +++ b/libraries/libvapours/include/vapours/prfile2/prfile2_str.hpp @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include + +namespace ams::prfile2::str { + + enum CodeMode { + CodeMode_Invalid = 0, + CodeMode_Local = 1, + CodeMode_Unicode = 2, + }; + + enum TargetString { + TargetString_Head = 1, + TargetString_Tail = 2, + }; + + struct String { + const char *head; + const char *tail; + CodeMode code_mode; + }; + + pf::Error Initialize(String *str, const char *s, CodeMode code_mode); + + void SetCodeMode(String *str, CodeMode code_mode); + CodeMode GetCodeMode(const String *str); + + char *GetPos(String *str, TargetString target); + void MovePos(String *str, s16 num_char); + + u16 GetLength(String *str); + u16 GetNumChar(String *str, TargetString target); + + int Compare(const String *str, const char *rhs); + int Compare(const String *str, const WideChar *rhs); + + /* TODO: StrNCmp */ + /* TODO: ToUpperNStr */ + + constexpr bool IsNull(const String *str) { + return str->head == nullptr; + } + +} + +namespace ams::prfile2 { + + using String = str::String; + +} diff --git a/libraries/libvapours/include/vapours/prfile2/prfile2_wide_string.hpp b/libraries/libvapours/include/vapours/prfile2/prfile2_wide_string.hpp new file mode 100644 index 000000000..ea1a75bac --- /dev/null +++ b/libraries/libvapours/include/vapours/prfile2/prfile2_wide_string.hpp @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#pragma once +#include + +namespace ams::prfile2 { + + using WideChar = u16; + + size_t w_strlen(const WideChar *s); + size_t w_strnlen(const WideChar *s, size_t length); + + WideChar *w_strcpy(WideChar *dst, const WideChar *src); + WideChar *w_strncpy(WideChar *dst, const WideChar *src, size_t length); + + int w_strcmp(const WideChar *lhs, const WideChar *rhs); + int w_strncmp(const WideChar *lhs, const WideChar *rhs, size_t length); + +} diff --git a/libraries/libvapours/source/prfile2/prfile2_str.cpp b/libraries/libvapours/source/prfile2/prfile2_str.cpp new file mode 100644 index 000000000..1f30abcfa --- /dev/null +++ b/libraries/libvapours/source/prfile2/prfile2_str.cpp @@ -0,0 +1,110 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#if defined(ATMOSPHERE_IS_STRATOSPHERE) +#include +#elif defined(ATMOSPHERE_IS_MESOSPHERE) +#include +#elif defined(ATMOSPHERE_IS_EXOSPHERE) +#include +#else +#include +#endif + +namespace ams::prfile2::str { + + namespace { + + /* TODO: Where does this come from? */ + /* It's maximum path length * 2, but where should the definition live? */ + constexpr inline size_t StringLengthMax = 520; + + } + + pf::Error Initialize(String *str, const char *s, CodeMode code_mode) { + /* Check parameters. */ + if (str == nullptr || s == nullptr) { + return pf::Error_InvalidParameter; + } + + /* Initialize the string. */ + switch (code_mode) { + case CodeMode_Local: + { + str->head = s; + str->tail = s + sizeof(char) * strnlen(s, StringLengthMax); + } + break; + case CodeMode_Unicode: + { + str->head = s; + str->tail = s + sizeof(WideChar) * w_strnlen(reinterpret_cast(s), StringLengthMax); + } + break; + default: + return pf::Error_InvalidParameter; + } + + /* Set the code mode. */ + str->code_mode = code_mode; + + return pf::Error_Ok; + } + + void SetCodeMode(String *str, CodeMode code_mode) { + str->code_mode = code_mode; + } + + CodeMode GetCodeMode(const String *str) { + return str->code_mode; + } + + char *GetPos(String *str, TargetString target) { + if (target == TargetString_Head) { + return const_cast(str->head); + } else { + return const_cast(str->tail); + } + } + + void MovePos(String *str, s16 num_char) { + AMS_UNUSED(str, num_char); + AMS_ABORT("TODO: oem charset"); + } + + u16 GetLength(String *str) { + if (str->code_mode == CodeMode_Unicode) { + return (str->tail - str->head) / sizeof(WideChar); + } else { + return (str->tail - str->head) / sizeof(char); + } + } + + u16 GetNumChar(String *str, TargetString target) { + AMS_UNUSED(str, target); + AMS_ABORT("TODO: oem charset"); + } + + int Compare(const String *str, const char *rhs) { + AMS_UNUSED(str, rhs); + AMS_ABORT("TODO: oem charset"); + } + + int Compare(const String *str, const WideChar *rhs) { + AMS_UNUSED(str, rhs); + AMS_ABORT("TODO: oem charset"); + } + +} diff --git a/libraries/libvapours/source/prfile2/prfile2_wide_string.cpp b/libraries/libvapours/source/prfile2/prfile2_wide_string.cpp new file mode 100644 index 000000000..36d681c79 --- /dev/null +++ b/libraries/libvapours/source/prfile2/prfile2_wide_string.cpp @@ -0,0 +1,109 @@ +/* + * Copyright (c) 2018-2020 Atmosphère-NX + * + * 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 . + */ +#if defined(ATMOSPHERE_IS_STRATOSPHERE) +#include +#elif defined(ATMOSPHERE_IS_MESOSPHERE) +#include +#elif defined(ATMOSPHERE_IS_EXOSPHERE) +#include +#else +#include +#endif + +namespace ams::prfile2 { + + size_t w_strlen(const WideChar *s) { + const WideChar *cur; + for (cur = s; *cur != 0; ++cur) { + /* ... */ + } + return cur - s; + } + + size_t w_strnlen(const WideChar *s, size_t length) { + const WideChar *cur; + for (cur = s; *cur != 0 && length != 0; ++cur, --length) { + /* ... */ + } + return cur - s; + } + + WideChar *w_strcpy(WideChar *dst, const WideChar *src) { + WideChar * const ret = dst; + while (true) { + const auto c = *(src++); + *(dst++) = c; + + if (c == 0) { + break; + } + } + return ret; + } + + WideChar *w_strncpy(WideChar *dst, const WideChar *src, size_t length) { + WideChar * const ret = dst; + while (length > 1) { + const auto c = *(src++); + *(dst++) = c; + + if (c == 0) { + return ret; + } + } + + if (length == 1) { + *(dst++) = 0; + } + + return ret; + } + + int w_strcmp(const WideChar *lhs, const WideChar *rhs) { + WideChar l, r; + while (true) { + l = *(lhs++); + r = *(rhs++); + if (l == 0 || r == 0 || l != r) { + break; + } + } + + return l - r; + } + + int w_strncmp(const WideChar *lhs, const WideChar *rhs, size_t length) { + if (length == 0) { + return 0; + } + + WideChar l, r; + while (true) { + l = *(lhs++); + r = *(rhs++); + if (l == 0 || r == 0 || l != r) { + break; + } + + if ((--length) == 0) { + return 0; + } + } + + return l - r; + } + +}