2 #include <unordered_map>
5 #include "desktopfileentry.h"
9 namespace linuxdeploy {
10 namespace desktopfile {
12 * Parse and read desktop files.
16 // describes a single section
17 typedef std::unordered_map<std::string, DesktopFileEntry> section_t;
19 // describes all sections in the desktop file
20 typedef std::unordered_map<std::string, section_t> sections_t;
23 // private data class pattern
25 std::shared_ptr<PrivateData> d;
27 // (in)equality operators are implemented outside this class
28 friend bool operator==(const DesktopFile& first, const DesktopFile& second);
29 friend bool operator!=(const DesktopFile& first, const DesktopFile& second);
32 // default constructor
35 // construct from existing desktop file
36 // if the file exists, it will be read using DesktopFileReader
37 // if reading fails, exceptions will be thrown (see DesktopFileReader for more information)
38 explicit DesktopFile(const std::string& path);
40 // construct by reading an existing stream
41 // file must exist, otherwise std::runtime_error is thrown
42 explicit DesktopFile(std::istream& is);
45 DesktopFile(const DesktopFile& other);
47 // copy assignment constructor
48 DesktopFile& operator=(const DesktopFile& other);
50 // move assignment operator
51 DesktopFile& operator=(DesktopFile&& other) noexcept;
54 // returns true if a file has been loaded, false otherwise
58 // sets path associated with this file
59 // throws exceptions in case of issues, see DesktopFileReader for more information
60 void read(const std::string& path);
62 // read desktop file from existing stream
63 // throws exceptions in case of issues, see DesktopFileReader for more information
64 void read(std::istream& is);
66 // get path associated with this file
67 std::string path() const;
69 // sets the path associated with this desktop file
70 // used to e.g., save the desktop file
71 void setPath(const std::string& path);
73 // clear contents of desktop file
79 // save desktop file to path
80 // does not change path associated with desktop file
81 // throws exceptions in case of errors, see DesktopFileWriter::save(...) for more information
82 bool save(const std::string& path) const;
84 // save desktop file to ostream
85 // does not change path associated with desktop file
86 // throws exceptions in case of errors, see DesktopFileWriter::save(...) for more information
87 bool save(std::ostream& os) const;
89 // check if entry exists in given section and key
90 bool entryExists(const std::string& section, const std::string& key) const;
92 // get key from desktop file
93 // an std::string passed as value parameter will be populated with the contents
94 // returns true (and populates value) if the key exists, false otherwise
95 bool getEntry(const std::string& section, const std::string& key, DesktopFileEntry& value) const;
97 // add key to section in desktop file
98 // the section will be created if it doesn't exist already
99 // returns true if an existing key was overwritten, false otherwise
100 bool setEntry(const std::string& section, const DesktopFileEntry& entry);
102 // add key to section in desktop file
103 // the section will be created if it doesn't exist already
104 // returns true if an existing key was overwritten, false otherwise
105 bool setEntry(const std::string& section, DesktopFileEntry&& entry);
107 // validate desktop file
108 bool validate() const;
111 // DesktopFile equality operator
112 bool operator==(const DesktopFile& first, const DesktopFile& second);
114 // DesktopFile inequality operator
115 bool operator!=(const DesktopFile& first, const DesktopFile& second);