IniLikeFile

Ini-like file.

Constructors

this
this()

Construct empty IniLikeFile, i.e. without any groups or values

this
this(string fileName, ReadOptions options)

Read from file.

this
this(Range byLine, ReadOptions options, string fileName)

Read from range of IniLikeLines.

Members

Enums

ReadOptions
enum ReadOptions

Flags to manage .ini like file reading

Functions

addFirstComment
void addFirstComment(string line)
Undocumented in source. Be warned that the author may not have intended to support it.
addGroup
IniLikeGroup addGroup(string groupName)

Create new group using groupName.

byGroup
auto byGroup()

Range of groups in order how they were defined in file.

fileName
string fileName()

File path where the object was loaded from.

firstComments
auto firstComments()
Undocumented in source. Be warned that the author may not have intended to support it.
group
inout(IniLikeGroup) group(string groupName)

Get group by name.

removeGroup
void removeGroup(string groupName)

Remove group by name.

save
void save(OutRange sink)

Use Output range or delegate to retrieve strings line by line. Those strings can be written to the file or be showed in text area. Note: returned strings don't have trailing newline character.

saveToFile
void saveToFile(string fileName)

Save object to the file using .ini-like format.

saveToString
string saveToString()

Save object to string using .ini like format.

Static functions

isValidKey
bool isValidKey(string key)

Tell whether the string is valid key. Only the characters A-Za-z0-9- may be used in key names. See

Examples

1     import std.file;
2     import std.path;
3 
4     string contents = 
5 `# The first comment
6 [First Entry]
7 # Comment
8 GenericName=File manager
9 GenericName[ru]=Файловый менеджер
10 # Another comment
11 [Another Group]
12 Name=Commander
13 Comment=Manage files
14 # The last comment`;
15 
16     auto ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.preserveComments, "contents.ini");
17     assert(ilf.fileName() == "contents.ini");
18     assert(ilf.group("First Entry"));
19     assert(ilf.group("Another Group"));
20     assert(ilf.saveToString() == contents);
21     
22     string tempFile = buildPath(tempDir(), "inilike-unittest-tempfile");
23     try {
24         assertNotThrown!IniLikeException(ilf.saveToFile(tempFile));
25         auto fileContents = cast(string)std.file.read(tempFile);
26         static if( __VERSION__ < 2067 ) {
27             assert(equal(fileContents.splitLines, contents.splitLines), "Contents should be preserved as is");
28         } else {
29             assert(equal(fileContents.lineSplitter, contents.lineSplitter), "Contents should be preserved as is");
30         }
31         
32         IniLikeFile filf; 
33         assertNotThrown!IniLikeException(filf = new IniLikeFile(tempFile, IniLikeFile.ReadOptions.preserveComments));
34         assert(filf.fileName() == tempFile);
35         remove(tempFile);
36     } catch(Exception e) {
37         //probably some environment issue unrelated to unittest itself, e.g. could not write to file.
38     }
39     
40     auto firstEntry = ilf.group("First Entry");
41     
42     assert(!firstEntry.contains("NonExistent"));
43     assert(firstEntry.contains("GenericName"));
44     assert(firstEntry.contains("GenericName[ru]"));
45     assert(firstEntry["GenericName"] == "File manager");
46     assert(firstEntry.value("GenericName") == "File manager");
47     firstEntry["GenericName"] = "Manager of files";
48     assert(firstEntry["GenericName"] == "Manager of files");
49     firstEntry["Authors"] = "Unknown";
50     assert(firstEntry["Authors"] == "Unknown");
51     
52     assert(firstEntry.localizedValue("GenericName", "ru") == "Файловый менеджер");
53     firstEntry.setLocalizedValue("GenericName", "ru", "Менеджер файлов");
54     assert(firstEntry.localizedValue("GenericName", "ru") == "Менеджер файлов");
55     firstEntry.setLocalizedValue("Authors", "ru", "Неизвестны");
56     assert(firstEntry.localizedValue("Authors", "ru") == "Неизвестны");
57     
58     firstEntry.removeEntry("GenericName");
59     assert(!firstEntry.contains("GenericName"));
60     firstEntry["GenericName"] = "File Manager";
61     assert(firstEntry["GenericName"] == "File Manager");
62     
63     assert(ilf.group("Another Group")["Name"] == "Commander");
64     assert(equal(ilf.group("Another Group").byKeyValue(), [ KeyValueTuple("Name", "Commander"), KeyValueTuple("Comment", "Manage files") ]));
65     
66     assert(equal(ilf.byGroup().map!(g => g.name), ["First Entry", "Another Group"]));
67     
68     ilf.removeGroup("Another Group");
69     assert(!ilf.group("Another Group"));
70     assert(equal(ilf.byGroup().map!(g => g.name), ["First Entry"]));
71     
72     ilf.addGroup("Another Group");
73     assert(ilf.group("Another Group"));
74     assert(ilf.group("Another Group").byIniLine().empty);
75     assert(ilf.group("Another Group").byKeyValue().empty);
76     
77     ilf.addGroup("Other Group");
78     assert(equal(ilf.byGroup().map!(g => g.name), ["First Entry", "Another Group", "Other Group"]));
79     
80     const IniLikeFile cilf = ilf;
81     static assert(is(typeof(cilf.byGroup())));
82     static assert(is(typeof(cilf.group("First Entry").byKeyValue())));
83     static assert(is(typeof(cilf.group("First Entry").byIniLine())));
84     
85     contents = 
86 `[First]
87 Key=Value
88 [Second]
89 Key=Value`;
90     ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.firstGroupOnly);
91     assert(ilf.group("First") !is null);
92     assert(ilf.group("Second") is null);
93     assert(ilf.group("First")["Key"] == "Value");
94     
95     contents = 
96 `[Group]
97 GenericName=File manager
98 [Group]
99 GenericName=Commander`;
100 
101     IniLikeException shouldThrow = null;
102     try {
103         new IniLikeFile(iniLikeStringReader(contents));
104     } catch(IniLikeException e) {
105         shouldThrow = e;
106     }
107     assert(shouldThrow !is null, "Duplicate groups should throw");
108     assert(shouldThrow.lineNumber == 3);
109     
110     ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.ignoreGroupDuplicates);
111     assert(ilf.group("Group").value("GenericName") == "File manager");
112     
113     contents = 
114 `[Group]
115 Key=Value1
116 Key=Value2`;
117 
118     try {
119         new IniLikeFile(iniLikeStringReader(contents));
120     } catch(IniLikeException e) {
121         shouldThrow = e;
122     }
123     
124     assert(shouldThrow !is null, "Duplicate key should throw");
125     assert(shouldThrow.lineNumber == 3);
126     
127     ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.ignoreKeyDuplicates);
128     assert(ilf.group("Group").value("Key") == "Value1");
129     
130     contents = 
131 `[Group]
132 $#=File manager`;
133 
134     try {
135         new IniLikeFile(iniLikeStringReader(contents));
136     } catch(IniLikeException e) {
137         shouldThrow = e;
138     }
139     assert(shouldThrow !is null, "Invalid key should throw");
140     assert(shouldThrow.lineNumber == 2);
141     assertNotThrown(new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.ignoreInvalidKeys));
142     
143     contents =
144 `[Group]
145 Key=Value
146 =File manager`;
147     try {
148         new IniLikeFile(iniLikeStringReader(contents));
149     } catch(IniLikeException e) {
150         shouldThrow = e;
151     }
152     assert(shouldThrow !is null, "Empty key should throw");
153     assert(shouldThrow.lineNumber == 3);
154     
155     contents = 
156 `[Group]
157 #Comment
158 Valid=Key
159 NotKeyNotGroupNotComment`;
160 
161     try {
162         new IniLikeFile(iniLikeStringReader(contents));
163     } catch(IniLikeException e) {
164         shouldThrow = e;
165     }
166     assert(shouldThrow !is null, "Invalid entry should throw");
167     assert(shouldThrow.lineNumber == 4);

Meta