-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.d
188 lines (160 loc) · 6.27 KB
/
app.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import std.algorithm;
import std.exception;
import std.range;
import std.stdio;
import inilike.file;
import inilike.common;
final class DesktopEntry : IniLikeGroup
{
this() {
super("Desktop Entry");
}
protected:
@trusted override void validateKeyValue(string key, string value) const {
enforce(isValidKey(key), "key is invalid");
}
}
final class DesktopFile : IniLikeFile
{
///Flags to manage .ini like file reading
enum ReadOptions
{
noOptions = 0, /// Read all groups, skip comments and empty lines, stop on any error.
preserveComments = 2, /// Preserve comments and empty lines. Use this when you want to keep them across writing.
ignoreGroupDuplicates = 4, /// Ignore group duplicates. The first found will be used.
ignoreInvalidKeys = 8, /// Skip invalid keys during parsing.
ignoreKeyDuplicates = 16, /// Ignore key duplicates. The first found will be used.
ignoreUnknownGroups = 32, /// Don't throw on unknown groups. Still save them.
skipUnknownGroups = 64, /// Don't save unknown groups.
skipExtensionGroups = 128 /// Skip groups started with X-
}
@trusted this(IniLikeReader)(IniLikeReader reader, ReadOptions options = ReadOptions.noOptions)
{
_options = options;
super(reader);
enforce(_desktopEntry !is null, new IniLikeException("No \"Desktop Entry\" group", 0));
}
@safe override void removeGroup(string groupName) nothrow {
if (groupName == "Desktop Entry") {
return;
}
super.removeGroup(groupName);
}
@trusted override void addLeadingComment(string line) nothrow {
if (_options & ReadOptions.preserveComments) {
super.addLeadingComment(line);
}
}
protected:
@trusted override void addCommentForGroup(string comment, IniLikeGroup currentGroup, string groupName)
{
if (currentGroup && (_options & ReadOptions.preserveComments)) {
currentGroup.addComment(comment);
}
}
@trusted override void addKeyValueForGroup(string key, string value, IniLikeGroup currentGroup, string groupName)
{
if (currentGroup) {
if (!isValidKey(key) && (_options & ReadOptions.ignoreInvalidKeys)) {
return;
}
if (currentGroup.contains(key)) {
if (_options & ReadOptions.ignoreKeyDuplicates) {
return;
} else {
throw new Exception("key already exists");
}
}
currentGroup[key] = value;
}
}
@trusted override IniLikeGroup createGroup(string groupName)
{
if (group(groupName) !is null) {
if (_options & ReadOptions.ignoreGroupDuplicates) {
return null;
} else {
throw new Exception("group already exists");
}
}
if (groupName == "Desktop Entry") {
_desktopEntry = new DesktopEntry();
return _desktopEntry;
} else if (groupName.startsWith("X-")) {
if (_options & ReadOptions.skipExtensionGroups) {
return null;
}
return createEmptyGroup(groupName);
} else {
if (_options & ReadOptions.ignoreUnknownGroups) {
if (_options & ReadOptions.skipUnknownGroups) {
return null;
} else {
return createEmptyGroup(groupName);
}
} else {
throw new Exception("Unknown group");
}
}
}
inout(DesktopEntry) desktopEntry() inout {
return _desktopEntry;
}
private:
DesktopEntry _desktopEntry;
ReadOptions _options;
}
void main()
{
string contents =
`# First comment
[Desktop Entry]
Key=Value
# Comment in group`;
auto df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions);
df.removeGroup("Desktop Entry");
assert(df.group("Desktop Entry") !is null);
assert(df.desktopEntry() !is null);
assert(df.leadingComments().empty);
assert(equal(df.desktopEntry().byIniLine(), [IniLikeLine.fromKeyValue("Key", "Value")]));
df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.preserveComments);
assert(equal(df.leadingComments(), ["# First comment"]));
assert(equal(df.desktopEntry().byIniLine(), [IniLikeLine.fromKeyValue("Key", "Value"), IniLikeLine.fromComment("# Comment in group")]));
contents =
`[X-SomeGroup]
Key=Value`;
auto thrown = collectException!IniLikeException(new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions));
assert(thrown !is null);
assert(thrown.lineNumber == 0);
contents =
`[Desktop Entry]
Valid=Key
$=Invalid`;
assertThrown(new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions));
assertNotThrown(new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.ignoreInvalidKeys));
contents =
`[Desktop Entry]
Key=Value1
Key=Value2`;
assertThrown(new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions));
assertNotThrown(df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.ignoreKeyDuplicates));
assert(df.desktopEntry().value("Key") == "Value1");
contents =
`[Desktop Entry]
Name=Name
[Unknown]
Key=Value`;
assertThrown(new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions));
assertNotThrown(df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.ignoreUnknownGroups));
assert(df.group("Unknown") !is null);
df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.ignoreUnknownGroups|DesktopFile.ReadOptions.skipUnknownGroups);
assert(df.group("Unknown") is null);
contents =
`[Desktop Entry]
Name=Name1
[Desktop Entry]
Name=Name2`;
assertThrown(new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.noOptions));
assertNotThrown(df = new DesktopFile(iniLikeStringReader(contents), DesktopFile.ReadOptions.ignoreGroupDuplicates));
assert(df.desktopEntry().value("Name") == "Name1");
}