6 #include "includes/desktopfile_exceptions.h"
7 #include "includes/desktopfilewriter.h"
8 #include "includes/desktopfile_util.h"
10 namespace linuxdeploy {
11 namespace desktopfile {
12 class DesktopFileWriter::PrivateData {
14 DesktopFile::sections_t data;
17 void copyData(const std::shared_ptr<PrivateData>& other) {
21 std::string dumpString() {
24 for (const auto& section : data) {
25 ss << "[" << section.first << "]" << std::endl;
27 for (const auto& pair : section.second) {
28 auto key = pair.first;
30 auto value = pair.second.value();
32 ss << key << "=" << value << std::endl;
35 // insert an empty line between sections
43 DesktopFileWriter::DesktopFileWriter() : d(std::make_shared<PrivateData>()) {}
45 DesktopFileWriter::DesktopFileWriter(DesktopFile::sections_t data) : DesktopFileWriter() {
46 d->data = std::move(data);
49 DesktopFileWriter::DesktopFileWriter(const DesktopFileWriter& other) : DesktopFileWriter() {
53 DesktopFileWriter& DesktopFileWriter::operator=(const DesktopFileWriter& other) {
55 // set up a new instance of PrivateData, and copy data over from other object
56 d.reset(new PrivateData);
63 DesktopFileWriter& DesktopFileWriter::operator=(DesktopFileWriter&& other) noexcept {
65 // move other object's data into this one, and remove reference there
73 bool DesktopFileWriter::operator==(const DesktopFileWriter& other) const {
74 return d->data == other.d->data;
77 bool DesktopFileWriter::operator!=(const DesktopFileWriter& other) const {
78 return !operator==(other);
81 DesktopFile::sections_t DesktopFileWriter::data() const {
85 void DesktopFileWriter::save(const std::string& path) {
86 std::ofstream ofs(path);
89 throw IOError("could not open file for writing: " + path);
94 void DesktopFileWriter::save(std::ostream& os) {
95 os << d->dumpString();