Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/XdgUtils/DesktopEntry/DesktopEntryKeyPath.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace XdgUtils {
public:
explicit DesktopEntryKeyPath(const std::string& path);

DesktopEntryKeyPath(const std::string& group, const std::string& key, const std::string& locale);

DesktopEntryKeyPath(const DesktopEntryKeyPath& other);

DesktopEntryKeyPath& operator=(const DesktopEntryKeyPath& other);
Expand Down Expand Up @@ -71,6 +73,11 @@ namespace XdgUtils {
*/
void setLocale(const std::string& locale);

/**
* @return key and locale sections
*/
std::string fullKey() const;

/**
* @return string representation of the KeyPath
*/
Expand Down
49 changes: 29 additions & 20 deletions src/DesktopEntry/AST/AST.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,27 @@ namespace XdgUtils {
namespace DesktopEntry {
namespace AST {

std::vector<std::shared_ptr<Node>>& AST::getEntries() {
return entries;
}

const std::vector<std::shared_ptr<Node>>& AST::getEntries() const {
return entries;
}

void AST::setEntries(const std::vector<std::shared_ptr<Node>>& entries) {
AST::entries = entries;
this->entries.clear();
for (const auto& entry: entries)
this->entries.emplace_back(entry->clone());
}

bool AST::operator==(const AST& rhs) const {
auto aItr = entries.begin();
auto bItr = rhs.entries.begin();

while (aItr != entries.end() && bItr != rhs.entries.end()) {
if (auto a = dynamic_cast<Group*>((*aItr).get())) {
// if the first one is an Entry the second one must also be
if (auto b = dynamic_cast<Group*>((*bItr).get())) {
// if both are entries compare them as such
if (*a != *b)
return false;
} else
return false;
}

if (auto a = dynamic_cast<Comment*>((*aItr).get())) {
// if the first one is an Comment the second one must also be
if (auto b = dynamic_cast<Comment*>((*bItr).get())) {
// if both are comments compare them as such
if (*a != *b)
return false;
} else
return false;
}
if (*aItr->get() != *bItr->get())
return false;

++aItr, ++bItr;
}
Expand All @@ -64,6 +53,26 @@ namespace XdgUtils {
output << std::endl;
}
}

AST::AST(const AST& other) {
setEntries(other.entries);
}

AST& AST::operator=(const AST& other) {
setEntries(other.entries);
return *this;
}

AST::AST(AST&& other) noexcept {
entries = std::move(other.entries);
}

AST& AST::operator=(AST&& other) noexcept {
entries = std::move(other.entries);
return *this;
}

AST::AST() = default;
}
}
}
17 changes: 17 additions & 0 deletions src/DesktopEntry/AST/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ namespace XdgUtils {
*/
class AST {
public:
AST();

// Copy constructor
AST(const AST& other);

// Copy assignment
AST& operator=(const AST& other);

// Move constructor
AST(AST&& other) noexcept;

// Move assignment
AST& operator=(AST&& other) noexcept;


std::vector<std::shared_ptr<Node>>& getEntries();

const std::vector<std::shared_ptr<Node>>& getEntries() const;

void setEntries(const std::vector<std::shared_ptr<Node>>& entries);
Expand Down
2 changes: 1 addition & 1 deletion src/DesktopEntry/AST/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ add_library(
Group.cpp
Entry.cpp
Comment.cpp
)
Node.cpp)
4 changes: 4 additions & 0 deletions src/DesktopEntry/AST/Comment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ namespace XdgUtils {
comment.write(os);
return os;
}

Node* Comment::clone() const {
return new Comment(*this);
}
}
}
}
2 changes: 2 additions & 0 deletions src/DesktopEntry/AST/Comment.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ namespace XdgUtils {

void write(std::ostream& output) const override;

Node* clone() const override;

bool operator==(const Comment& rhs) const;

bool operator!=(const Comment& rhs) const;
Expand Down
18 changes: 18 additions & 0 deletions src/DesktopEntry/AST/Entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ namespace XdgUtils {
: keyRaw(keyRaw), keyValue(keyValue), localeRaw(localeRaw),
localeValue(localeValue), valueRaw(valueRaw), valueValue(valueValue) {}

Entry::Entry(const std::string& key, const std::string& locale, const std::string& value) :
keyRaw(key), keyValue(key) {

if (!locale.empty()) {
localeRaw = ('[' + locale + ']');
localeValue = (locale);
}

if (!value.empty()) {
valueValue = value;
valueRaw = '=' + value;
}
}

std::string Entry::getValue() const {
return valueValue;
}
Expand Down Expand Up @@ -51,6 +65,10 @@ namespace XdgUtils {
entry.write(os);
return os;
}

Node* Entry::clone() const {
return new Entry(*this);
}
}
}
}
4 changes: 4 additions & 0 deletions src/DesktopEntry/AST/Entry.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace XdgUtils {
*/
class Entry : public Node {
public:
Entry(const std::string &key, const std::string &locale, const std::string &value);

Entry(const std::string& keyRaw, const std::string& keyValue, const std::string& localeRaw,
const std::string& localeValue, const std::string& valueRaw, const std::string& valueValue);

Expand All @@ -29,6 +31,8 @@ namespace XdgUtils {

void write(std::ostream& output) const override;

Node* clone() const override;

/**
* Compare two Entries according to they fields values. Raw values will be ignored
* @param rhs
Expand Down
62 changes: 42 additions & 20 deletions src/DesktopEntry/AST/Group.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,18 @@ namespace XdgUtils {
}
}

std::vector<std::shared_ptr<Node>>& Group::getEntries() {
return entries;;
}

const std::vector<std::shared_ptr<Node>>& Group::getEntries() const {
return entries;
}

void Group::setEntries(const std::vector<std::shared_ptr<Node>>& entries) {
Group::entries = entries;
this->entries.clear();
for (const auto& entry: entries)
this->entries.emplace_back(entry->clone());
}

bool Group::operator==(const Group& rhs) const {
Expand All @@ -54,25 +60,8 @@ namespace XdgUtils {
auto bItr = rhs.entries.begin();

while (aItr != entries.end() && bItr != rhs.entries.end()) {
if (auto a = dynamic_cast<Entry*>((*aItr).get())) {
// if the first one is an Entry the second one must also be
if (auto b = dynamic_cast<Entry*>((*bItr).get())) {
// if both are entries compare them as such
if (*a != *b)
return false;
} else
return false;
}

if (auto a = dynamic_cast<Comment*>((*aItr).get())) {
// if the first one is an Comment the second one must also be
if (auto b = dynamic_cast<Comment*>((*bItr).get())) {
// if both are comments compare them as such
if (*a != *b)
return false;
} else
return false;
}
if (*aItr->get() != *bItr->get())
return false;

++aItr, ++bItr;
}
Expand All @@ -81,6 +70,7 @@ namespace XdgUtils {
return (aItr == entries.end() && bItr == rhs.entries.end());
}


bool Group::operator!=(const Group& rhs) const {
return !(rhs == *this);
}
Expand All @@ -89,6 +79,38 @@ namespace XdgUtils {
group.write(os);
return os;
}

Node* Group::clone() const {
return new Group(*this);
}

Group::Group(const Group& other) : headerValue(other.headerValue), headerRawValue(other.headerRawValue) {
setEntries(other.entries);
}

Group& Group::operator=(const Group& other) {
headerValue = other.headerValue;
headerRawValue = other.headerRawValue;

setEntries(other.entries);
return *this;
}

Group::Group(Group&& other) noexcept {
headerValue = std::move(other.headerValue);
headerRawValue = std::move(other.headerRawValue);
entries = std::move(other.entries);
}

Group& Group::operator=(Group&& other) noexcept {
headerValue = std::move(other.headerValue);
headerRawValue = std::move(other.headerRawValue);
entries = std::move(other.entries);

return *this;
}

Group::~Group() = default;
}
}
}
18 changes: 18 additions & 0 deletions src/DesktopEntry/AST/Group.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,20 @@ namespace XdgUtils {
*/
Group(const std::string& headerRawValue, const std::string& headerValue);

// Copy constructor
Group(const Group& other);

// Copy assigment
Group& operator=(const Group& other);

// Move constructor
Group(Group&& other) noexcept;

// Move assigment
Group& operator=(Group&& other) noexcept;

~Group() override;

std::string getValue() const override;

/**
Expand All @@ -32,6 +46,10 @@ namespace XdgUtils {

void write(std::ostream& output) const override;

Node* clone() const override;

std::vector<std::shared_ptr<Node>>& getEntries();

const std::vector<std::shared_ptr<Node>>& getEntries() const;

void setEntries(const std::vector<std::shared_ptr<Node>>& entries);
Expand Down
42 changes: 42 additions & 0 deletions src/DesktopEntry/AST/Node.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Local
#include "Node.h"

#include "Comment.h"
#include "Entry.h"
#include "Group.h"

namespace XdgUtils {
namespace DesktopEntry {
namespace AST {

bool Node::operator!=(const Node& rhs) const {
return !operator==(rhs);
}

bool Node::operator==(const Node& rhs) const {
try {
auto a = dynamic_cast<const Comment&>(*this);
auto b = dynamic_cast<const Comment&>(rhs);

return a == b;
} catch (const std::bad_cast&) {}

try {
auto a = dynamic_cast<const Entry&>(*this);
auto b = dynamic_cast<const Entry&>(rhs);

return a == b;
} catch (const std::bad_cast&) {}

try {
auto a = dynamic_cast<const Group&>(*this);
auto b = dynamic_cast<const Group&>(rhs);

return a == b;
} catch (const std::bad_cast&) {}

return false;
}
}
}
}
10 changes: 10 additions & 0 deletions src/DesktopEntry/AST/Node.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,21 @@ namespace XdgUtils {

virtual void write(std::ostream& output) const = 0;

virtual Node* clone() const = 0;

friend std::ostream& operator<<(std::ostream& os, const Node& node) {
node.write(os);
return os;
}


bool operator==(const Node& rhs) const;

bool operator!=(const Node& rhs) const;

virtual ~Node() = default;
};

}
}
}
Loading