6 #include "includes/desktopfile_exceptions.h"
7 #include "includes/desktopfile.h"
8 #include "includes/desktopfilereader.h"
9 #include "includes/desktopfilewriter.h"
11 namespace linuxdeploy {
12 namespace desktopfile {
13 class DesktopFile::PrivateData {
19 PrivateData() = default;
21 void copyData(const std::shared_ptr<PrivateData>& other) {
27 bool isEmpty() const {
32 DesktopFile::DesktopFile() : d(std::make_shared<PrivateData>()) {}
34 DesktopFile::DesktopFile(const std::string& path) : DesktopFile() {
35 // if the file doesn't exist, an exception shall be thrown
36 // otherwise, a user cannot know for sure whether a file was actually read (would need to check this
37 // manually beforehand
39 std::ifstream ifs(path);
41 throw IOError("Could not find file " + path);
45 // will throw exceptions in case of issues
49 DesktopFile::DesktopFile(std::istream& is) : DesktopFile() {
50 // will throw exceptions in case of issues
55 DesktopFile::DesktopFile(const DesktopFile& other) : DesktopFile() {
59 // copy assignment constructor
60 DesktopFile& DesktopFile::operator=(const DesktopFile& other) {
68 // move assignment operator
69 DesktopFile& DesktopFile::operator=(DesktopFile&& other) noexcept {
77 void DesktopFile::read(const std::string& path) {
80 // clear data before reading a new file
83 DesktopFileReader reader(path);
84 d->data = std::move(reader.data());
87 void DesktopFile::read(std::istream& is) {
88 // clear data before reading a new file
91 DesktopFileReader reader(is);
92 d->data = reader.data();
95 std::string DesktopFile::path() const {
99 void DesktopFile::setPath(const std::string& path) {
103 bool DesktopFile::isEmpty() const {
107 void DesktopFile::clear() {
111 bool DesktopFile::save() const {
112 return save(d->path);
115 bool DesktopFile::save(const std::string& path) const {
116 DesktopFileWriter writer(d->data);
122 bool DesktopFile::save(std::ostream& os) const {
123 DesktopFileWriter writer(d->data);
129 bool DesktopFile::entryExists(const std::string& section, const std::string& key) const {
130 auto it = d->data.find(section);
131 if (it == d->data.end())
134 return (it->second.find(key) != it->second.end());
137 bool DesktopFile::setEntry(const std::string& section, const DesktopFileEntry& entry) {
138 // check if value exists -- used for return value
139 auto rv = entryExists(section, entry.key());
141 d->data[section][entry.key()] = entry;
146 bool DesktopFile::setEntry(const std::string& section, DesktopFileEntry&& entry) {
147 // check if value exists -- used for return value
148 auto rv = entryExists(section, entry.key());
150 d->data[section][entry.key()] = entry;
155 bool DesktopFile::getEntry(const std::string& section, const std::string& key, DesktopFileEntry& entry) const {
156 if (!entryExists(section, key))
159 entry = d->data[section][key];
161 // make sure keys are equal
162 assert(key == entry.key());
167 bool DesktopFile::validate() const {
168 // FIXME: call desktop-file-validate
172 bool operator==(const DesktopFile& first, const DesktopFile& second) {
173 return first.d->path == second.d->path && first.d->data == second.d->data;
176 bool operator !=(const DesktopFile& first, const DesktopFile& second) {
177 return !operator==(first, second);