Skip to content

Commit fd03351

Browse files
author
Alexis Lopez Zubieta
committed
Add Array Subscript Operator to DesktopEntry
1 parent ff78165 commit fd03351

File tree

4 files changed

+69
-15
lines changed

4 files changed

+69
-15
lines changed

include/XdgUtils/DesktopEntry/DesktopEntry.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
// local
1010
#include <XdgUtils/DesktopEntry/Exceptions.h>
11+
#include <XdgUtils/DesktopEntry/DesktopEntryKeyPath.h>
12+
#include <XdgUtils/DesktopEntry/DesktopEntryKeyValue.h>
1113

1214
namespace XdgUtils {
1315
namespace DesktopEntry {
@@ -77,6 +79,20 @@ namespace XdgUtils {
7779
friend std::ostream& operator<<(std::ostream& os, const DesktopEntry& entry);
7880

7981

82+
/**
83+
* Access the key pointed by <keyPath> if it doesn't exist a new one will be created.
84+
* @param keyPath
85+
* @return
86+
*/
87+
DesktopEntryKeyValue operator[](const DesktopEntryKeyPath& keyPath);
88+
89+
/**
90+
* Access the key pointed by <keyPath> if it doesn't exist a new one will be created.
91+
* @param keyPath
92+
* @return
93+
*/
94+
DesktopEntryKeyValue operator[](const std::string& keyPath);
95+
8096
bool operator==(const DesktopEntry& rhs) const;
8197

8298
bool operator!=(const DesktopEntry& rhs) const;

src/DesktopEntry/AST/Group.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace XdgUtils {
2222

2323
auto pos = headerRawValue.find(headerValue);
2424

25-
headerRawValue.replace(pos, std::string::npos, newValue);
25+
headerRawValue = '[' + newValue + ']';
2626
headerValue = newValue;
2727
}
2828

src/DesktopEntry/DesktopEntry.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "Reader/Tokenizer.h"
1212
#include "Reader/Reader.h"
1313
#include "Reader/Errors.h"
14+
#include "DesktopEntryKeyValuePriv.h"
1415

1516
namespace XdgUtils {
1617
namespace DesktopEntry {
@@ -65,6 +66,11 @@ namespace XdgUtils {
6566

6667
void createEntry(const DesktopEntryKeyPath& keyPath, const std::string& value) {
6768
auto group = std::dynamic_pointer_cast<AST::Group>(paths[keyPath.group()]);
69+
// create group if it doesn't exists
70+
if (!group) {
71+
createGroup(keyPath.group());
72+
group = std::dynamic_pointer_cast<AST::Group>(paths[keyPath.group()]);
73+
}
6874

6975
// append entry to group
7076
auto entry = std::make_shared<AST::Entry>(keyPath.key(), keyPath.locale(), value);
@@ -75,20 +81,6 @@ namespace XdgUtils {
7581
paths[keyPath.string()] = entry;
7682
}
7783

78-
std::string createEntryPath(const std::string& groupName, const AST::Entry& entry) {
79-
std::stringstream path;
80-
81-
if (!groupName.empty())
82-
path << groupName << '/';
83-
84-
path << entry.getKey();
85-
86-
if (!entry.getLocale().empty())
87-
path << '[' << entry.getLocale() << ']';
88-
89-
return path.str();
90-
}
91-
9284
void removeGroup(const std::string& groupName) {
9385
auto g = paths[groupName];
9486

@@ -128,6 +120,18 @@ namespace XdgUtils {
128120
paths.erase(path);
129121
}
130122
}
123+
124+
std::shared_ptr<AST::Node> getOrCreateEntry(const DesktopEntryKeyPath& keyPath) {
125+
auto itr = paths.find(keyPath.string());
126+
if (itr == paths.end()) {
127+
if (keyPath.key().empty())
128+
createGroup(keyPath.group());
129+
else
130+
createEntry(keyPath, "");
131+
}
132+
133+
return paths[keyPath.string()];
134+
}
131135
};
132136

133137
DesktopEntry::DesktopEntry() : priv(new Priv) {}
@@ -215,6 +219,16 @@ namespace XdgUtils {
215219
bool DesktopEntry::operator!=(const DesktopEntry& rhs) const {
216220
return !(rhs == *this);
217221
}
222+
223+
DesktopEntryKeyValue DesktopEntry::operator[](const DesktopEntryKeyPath& keyPath) {
224+
auto entry = priv->getOrCreateEntry(keyPath);
225+
226+
return DesktopEntryKeyValue(new DesktopEntryKeyValue::Priv(keyPath, entry));
227+
}
228+
229+
DesktopEntryKeyValue DesktopEntry::operator[](const std::string& keyPath) {
230+
return operator[](DesktopEntryKeyPath(keyPath));
231+
}
218232
}
219233

220234
}

tests/DesktopEntry/TestDesktopEntry.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,27 @@ TEST_F(TestDesktopEntry, removeEntry) {
199199

200200
ASSERT_EQ(out.str(), "[G1]\nName=1\n[g2]\nName=2\n");
201201
}
202+
203+
TEST_F(TestDesktopEntry, arraySubscriptOperatorAccessGroup) {
204+
DesktopEntry::DesktopEntry entry;
205+
206+
entry["Group"] = "Group";
207+
208+
std::stringstream out;
209+
out << entry;
210+
211+
ASSERT_EQ(out.str(), "[Group]\n");
212+
}
213+
214+
TEST_F(TestDesktopEntry, arraySubscriptOperatorAccessEntry) {
215+
DesktopEntry::DesktopEntry entry;
216+
entry["Group/Key"] = "My App";
217+
218+
ASSERT_EQ(static_cast<std::string>(entry["Group/Key"]), "My App");
219+
220+
std::stringstream out;
221+
out << entry;
222+
223+
ASSERT_EQ(out.str(), "[Group]\n"
224+
"Key=My App");
225+
}

0 commit comments

Comments
 (0)