lpcm with tsmuxer cleanup by Andrew
[goodguy/cinelerra.git] / cinelerra-5.1 / tools / makeappimagetool / desktopfileentry.cpp
1 // system headers
2 #include <sstream>
3
4 // local headers
5 #include "includes/desktopfileentry.h"
6 #include "includes/desktopfile_util.h"
7
8 namespace linuxdeploy {
9     namespace desktopfile {
10         class DesktopFileEntry::PrivateData {
11         public:
12             std::string key;
13             std::string value;
14
15         public:
16             void copyData(const std::shared_ptr<PrivateData>& other) {
17                 key = other->key;
18                 value = other->value;
19             }
20
21             void assertValueNotEmpty() {
22                 if (value.empty())
23                     throw std::invalid_argument("value is empty");
24             }
25         };
26
27         DesktopFileEntry::DesktopFileEntry() : d(new PrivateData) {}
28
29         DesktopFileEntry::DesktopFileEntry(std::string key, std::string value) : DesktopFileEntry() {
30             d->key = std::move(key);
31             d->value = std::move(value);
32         }
33
34         DesktopFileEntry::DesktopFileEntry(const DesktopFileEntry& other) : DesktopFileEntry() {
35             d->copyData(other.d);
36         }
37
38         DesktopFileEntry& DesktopFileEntry::operator=(const DesktopFileEntry& other) {
39             if (this != &other) {
40                 d.reset(new PrivateData);
41                 d->copyData(other.d);
42             }
43
44             return *this;
45         }
46
47         DesktopFileEntry& DesktopFileEntry::operator=(DesktopFileEntry&& other) noexcept {
48             if (this != &other) {
49                 d = other.d;
50                 other.d = nullptr;
51             }
52
53             return *this;
54         }
55
56         bool DesktopFileEntry::operator==(const DesktopFileEntry& other) const {
57             return d->key == other.d->key && d->value == other.d->value;
58         }
59
60         bool DesktopFileEntry::operator!=(const DesktopFileEntry& other) const {
61             return !operator==(other);
62         }
63
64         bool DesktopFileEntry::isEmpty() const {
65             return d->key.empty();
66         }
67
68         const std::string& DesktopFileEntry::key() const {
69             return d->key;
70         }
71
72         const std::string& DesktopFileEntry::value() const {
73             return d->value;
74         }
75
76         int32_t DesktopFileEntry::asInt() const {
77             d->assertValueNotEmpty();
78
79             return lexicalCast<int32_t>(value());
80         }
81
82         int64_t DesktopFileEntry::asLong() const {
83             d->assertValueNotEmpty();
84
85             return lexicalCast<int64_t>(value());
86         }
87
88         double DesktopFileEntry::asDouble() const {
89             d->assertValueNotEmpty();
90
91             return lexicalCast<double>(value());
92         }
93
94         std::vector<std::string> DesktopFileEntry::parseStringList() const {
95             const auto& value = this->value();
96
97             if (value.empty())
98                 return {};
99
100 //            if (value.back() != ';')
101 //                ldLog() << LD_DEBUG << "desktop file string list does not end with semicolon:" << value
102 //                        << std::endl;
103
104             std::vector<std::string> list;
105
106             std::stringstream ss(value);
107
108             std::string currentVal;
109             while (std::getline(ss, currentVal, ';')) {
110                 // the last value will be empty, as in desktop files, lists shall end with a semicolon
111                 // therefore we skip all empty values (assuming that empty values in lists in desktop files don't make sense anyway)
112                 if (!currentVal.empty())
113                     list.emplace_back(currentVal);
114             }
115
116             return list;
117         }
118     }
119 }