aboutsummaryrefslogtreecommitdiff
path: root/include/Path.hpp
blob: bb08cc68d909836ae2414ad5ad5c984cdfad28de (plain)
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
#pragma once

#include <string>

namespace QuickMedia {
    class Path {
    public:
        Path() = default;

        Path(const char *path) : data(path) {}
        Path(const std::string &path) : data(path) {}

        Path& join(const Path &other) {
            data += "/";
            data += other.data;
            return *this;
        }

        Path& append(const std::string &str) {
            data += str;
            return *this;
        }

        // Includes extension
        const char* filename() const {
            size_t index = data.rfind('/');
            if(index == std::string::npos)
                return data.c_str();
            return data.c_str() + index + 1;
        }

        std::string filename_no_ext() const {
            const char *name = filename();
            const char *extension = ext();
            if(extension[0] == '\0')
                return name;
            else
                return data.substr(name - data.data(), extension - name);
        }

        // Returns empty string if no extension
        const char* ext() const {
            size_t slash_index = data.rfind('/');
            size_t index = data.rfind('.');
            if(index != std::string::npos && (slash_index == std::string::npos || index > slash_index))
                return data.c_str() + index;
            return "";
        }

        Path parent() {
            size_t slash_index = data.rfind('/');
            if(slash_index != std::string::npos && slash_index > 0)
                return Path(data.substr(0, slash_index));
            if(!data.empty() && data[0] == '/')
                return Path("/");
            else
                return Path("");
        }

        std::string data;
    };
}