update ffmpeg.git files for newer versions
[goodguy/cinelerra.git] / cinelerra-5.1 / tools / makeappimagetool / desktopfilewriter.cpp
1 // system includes
2 #include <fstream>
3 #include <sstream>
4
5 // local headers
6 #include "includes/desktopfile_exceptions.h"
7 #include "includes/desktopfilewriter.h"
8 #include "includes/desktopfile_util.h"
9
10 namespace linuxdeploy {
11     namespace desktopfile {
12         class DesktopFileWriter::PrivateData {
13         public:
14             DesktopFile::sections_t data;
15
16         public:
17             void copyData(const std::shared_ptr<PrivateData>& other) {
18                 data = other->data;
19             }
20
21             std::string dumpString() {
22                 std::stringstream ss;
23
24                 for (const auto& section : data) {
25                     ss << "[" << section.first << "]" << std::endl;
26
27                     for (const auto& pair : section.second) {
28                         auto key = pair.first;
29                         trim(key);
30                         auto value = pair.second.value();
31                         trim(value);
32                         ss << key << "=" << value << std::endl;
33                     }
34
35                     // insert an empty line between sections
36                     ss << std::endl;
37                 }
38
39                 return ss.str();
40             }
41         };
42
43         DesktopFileWriter::DesktopFileWriter() : d(std::make_shared<PrivateData>()) {}
44
45         DesktopFileWriter::DesktopFileWriter(DesktopFile::sections_t data) : DesktopFileWriter() {
46             d->data = std::move(data);
47         }
48
49         DesktopFileWriter::DesktopFileWriter(const DesktopFileWriter& other) : DesktopFileWriter() {
50             d->copyData(other.d);
51         }
52
53         DesktopFileWriter& DesktopFileWriter::operator=(const DesktopFileWriter& other) {
54             if (this != &other) {
55                 // set up a new instance of PrivateData, and copy data over from other object
56                 d.reset(new PrivateData);
57                 d->copyData(other.d);
58             }
59
60             return *this;
61         }
62
63         DesktopFileWriter& DesktopFileWriter::operator=(DesktopFileWriter&& other) noexcept {
64             if (this != &other) {
65                 // move other object's data into this one, and remove reference there
66                 d = other.d;
67                 other.d = nullptr;
68             }
69
70             return *this;
71         }
72
73         bool DesktopFileWriter::operator==(const DesktopFileWriter& other) const {
74             return d->data == other.d->data;
75         }
76
77         bool DesktopFileWriter::operator!=(const DesktopFileWriter& other) const {
78             return !operator==(other);
79         }
80
81         DesktopFile::sections_t DesktopFileWriter::data() const {
82             return d->data;
83         }
84
85         void DesktopFileWriter::save(const std::string& path) {
86             std::ofstream ofs(path);
87
88             if (!ofs)
89                 throw IOError("could not open file for writing: " + path);
90
91             save(ofs);
92         }
93
94         void DesktopFileWriter::save(std::ostream& os) {
95             os << d->dumpString();
96         }
97     }
98 }