X-Git-Url: https://cinelerra-gg.org/git/?a=blobdiff_plain;ds=sidebyside;f=cinelerra-5.1%2Ftools%2Fmakeappimagetool%2Fdesktopfilewriter.cpp;fp=cinelerra-5.1%2Ftools%2Fmakeappimagetool%2Fdesktopfilewriter.cpp;h=4e842ff805d0201ee61583beba1f5a85d5375043;hb=194ea84742f4d9973b1aad567fe833ca13a8c4f9;hp=0000000000000000000000000000000000000000;hpb=d8393b13b37b8654f0039ec1dba9a71c02af9411;p=goodguy%2Fcinelerra.git diff --git a/cinelerra-5.1/tools/makeappimagetool/desktopfilewriter.cpp b/cinelerra-5.1/tools/makeappimagetool/desktopfilewriter.cpp new file mode 100644 index 00000000..4e842ff8 --- /dev/null +++ b/cinelerra-5.1/tools/makeappimagetool/desktopfilewriter.cpp @@ -0,0 +1,98 @@ +// system includes +#include +#include + +// local headers +#include "includes/desktopfile_exceptions.h" +#include "includes/desktopfilewriter.h" +#include "includes/desktopfile_util.h" + +namespace linuxdeploy { + namespace desktopfile { + class DesktopFileWriter::PrivateData { + public: + DesktopFile::sections_t data; + + public: + void copyData(const std::shared_ptr& other) { + data = other->data; + } + + std::string dumpString() { + std::stringstream ss; + + for (const auto& section : data) { + ss << "[" << section.first << "]" << std::endl; + + for (const auto& pair : section.second) { + auto key = pair.first; + trim(key); + auto value = pair.second.value(); + trim(value); + ss << key << "=" << value << std::endl; + } + + // insert an empty line between sections + ss << std::endl; + } + + return ss.str(); + } + }; + + DesktopFileWriter::DesktopFileWriter() : d(std::make_shared()) {} + + DesktopFileWriter::DesktopFileWriter(DesktopFile::sections_t data) : DesktopFileWriter() { + d->data = std::move(data); + } + + DesktopFileWriter::DesktopFileWriter(const DesktopFileWriter& other) : DesktopFileWriter() { + d->copyData(other.d); + } + + DesktopFileWriter& DesktopFileWriter::operator=(const DesktopFileWriter& other) { + if (this != &other) { + // set up a new instance of PrivateData, and copy data over from other object + d.reset(new PrivateData); + d->copyData(other.d); + } + + return *this; + } + + DesktopFileWriter& DesktopFileWriter::operator=(DesktopFileWriter&& other) noexcept { + if (this != &other) { + // move other object's data into this one, and remove reference there + d = other.d; + other.d = nullptr; + } + + return *this; + } + + bool DesktopFileWriter::operator==(const DesktopFileWriter& other) const { + return d->data == other.d->data; + } + + bool DesktopFileWriter::operator!=(const DesktopFileWriter& other) const { + return !operator==(other); + } + + DesktopFile::sections_t DesktopFileWriter::data() const { + return d->data; + } + + void DesktopFileWriter::save(const std::string& path) { + std::ofstream ofs(path); + + if (!ofs) + throw IOError("could not open file for writing: " + path); + + save(ofs); + } + + void DesktopFileWriter::save(std::ostream& os) { + os << d->dumpString(); + } + } +}