summaryrefslogtreecommitdiffstats
path: root/src/common/fs/fs_util.cpp
blob: 442f637281abf417e565df0d7f4d58b56c63c142 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <algorithm>

#include "common/fs/fs_util.h"
#include "common/polyfill_ranges.h"

namespace Common::FS {

std::u8string ToU8String(std::string_view utf8_string) {
    return std::u8string{utf8_string.begin(), utf8_string.end()};
}

std::u8string BufferToU8String(std::span<const u8> buffer) {
    return std::u8string{buffer.begin(), std::ranges::find(buffer, u8{0})};
}

std::u8string_view BufferToU8StringView(std::span<const u8> buffer) {
    return std::u8string_view{reinterpret_cast<const char8_t*>(buffer.data())};
}

std::string ToUTF8String(std::u8string_view u8_string) {
    return std::string{u8_string.begin(), u8_string.end()};
}

std::string BufferToUTF8String(std::span<const u8> buffer) {
    return std::string{buffer.begin(), std::ranges::find(buffer, u8{0})};
}

std::string_view BufferToUTF8StringView(std::span<const u8> buffer) {
    return std::string_view{reinterpret_cast<const char*>(buffer.data())};
}

std::string PathToUTF8String(const std::filesystem::path& path) {
    return ToUTF8String(path.u8string());
}

std::u8string U8FilenameSantizer(const std::u8string_view u8filename) {
    std::u8string u8path_santized{u8filename.begin(), u8filename.end()};
    size_t eSizeSanitized = u8path_santized.size();

    // Special case for ":", for example: 'Pepe: La secuela' --> 'Pepe - La
    // secuela' or 'Pepe : La secuela' --> 'Pepe - La secuela'
    for (size_t i = 0; i < eSizeSanitized; i++) {
        switch (u8path_santized[i]) {
        case u8':':
            if (i == 0 || i == eSizeSanitized - 1) {
                u8path_santized.replace(i, 1, u8"_");
            } else if (u8path_santized[i - 1] == u8' ') {
                u8path_santized.replace(i, 1, u8"-");
            } else {
                u8path_santized.replace(i, 1, u8" -");
                eSizeSanitized++;
            }
            break;
        case u8'\\':
        case u8'/':
        case u8'*':
        case u8'?':
        case u8'\"':
        case u8'<':
        case u8'>':
        case u8'|':
        case u8'\0':
            u8path_santized.replace(i, 1, u8"_");
            break;
        default:
            break;
        }
    }

    // Delete duplicated spaces || Delete duplicated dots (MacOS i think)
    for (size_t i = 0; i < eSizeSanitized - 1; i++) {
        if ((u8path_santized[i] == u8' ' && u8path_santized[i + 1] == u8' ') ||
            (u8path_santized[i] == u8'.' && u8path_santized[i + 1] == u8'.')) {
            u8path_santized.erase(i, 1);
            i--;
        }
    }

    // Delete all spaces and dots at the end (Windows almost)
    while (u8path_santized.back() == u8' ' || u8path_santized.back() == u8'.') {
        u8path_santized.pop_back();
    }

    if (u8path_santized.empty()) {
        return u8"";
    }

    return u8path_santized;
}

std::string UTF8FilenameSantizer(const std::string_view filename) {
    return ToUTF8String(U8FilenameSantizer(ToU8String(filename)));
}

} // namespace Common::FS