Construct empty IniLikeFile, i.e. without any groups or values
Read from file.
Read from range of IniLikeLines.
Alias for saving delegate.
Flags to manage .ini like file reading
Create new group using groupName.
Range of groups in order how they were defined in file.
File path where the object was loaded from.
Get group by name.
Tell whether the string is valid key. For IniLikeFile the valid key is any non-empty string. Reimplement this function in the derived class to throw exception from IniLikeGroup when key is invalid.
Remove group by name.
Use 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.
Save object to the file using .ini-like format.
Save object to string using .ini like format.
1 string contents = 2 `# The first comment 3 [First Entry] 4 # Comment 5 GenericName=File manager 6 GenericName[ru]=Файловый менеджер 7 # Another comment 8 [Another Group] 9 Name=Commander 10 Comment=Manage files 11 # The last comment`; 12 13 auto ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.preserveComments, "contents.ini"); 14 assert(ilf.fileName() == "contents.ini"); 15 assert(ilf.group("First Entry")); 16 assert(ilf.group("Another Group")); 17 assert(ilf.saveToString() == contents); 18 19 string tempFile = buildPath(tempDir(), "inilike-unittest-tempfile"); 20 try { 21 assertNotThrown!IniLikeException(ilf.saveToFile(tempFile)); 22 auto fileContents = cast(string)std.file.read(tempFile); 23 static if( __VERSION__ < 2067 ) { 24 assert(equal(fileContents.splitLines, contents.splitLines), "Contents should be preserved as is"); 25 } else { 26 assert(equal(fileContents.lineSplitter, contents.lineSplitter), "Contents should be preserved as is"); 27 } 28 29 IniLikeFile filf; 30 assertNotThrown!IniLikeException(filf = new IniLikeFile(tempFile, IniLikeFile.ReadOptions.preserveComments)); 31 assert(filf.fileName() == tempFile); 32 remove(tempFile); 33 } catch(Exception e) { 34 //probably some environment issue unrelated to unittest itself, e.g. could not write to file. 35 } 36 37 auto firstEntry = ilf.group("First Entry"); 38 39 assert(firstEntry["GenericName"] == "File manager"); 40 assert(firstEntry.value("GenericName") == "File manager"); 41 firstEntry["GenericName"] = "Manager of files"; 42 assert(firstEntry["GenericName"] == "Manager of files"); 43 firstEntry["Authors"] = "Unknown"; 44 assert(firstEntry["Authors"] == "Unknown"); 45 46 assert(firstEntry.localizedValue("GenericName", "ru") == "Файловый менеджер"); 47 firstEntry.setLocalizedValue("GenericName", "ru", "Менеджер файлов"); 48 assert(firstEntry.localizedValue("GenericName", "ru") == "Менеджер файлов"); 49 firstEntry.setLocalizedValue("Authors", "ru", "Неизвестны"); 50 assert(firstEntry.localizedValue("Authors", "ru") == "Неизвестны"); 51 52 firstEntry.removeEntry("GenericName"); 53 assert(!firstEntry.contains("GenericName")); 54 firstEntry["GenericName"] = "File Manager"; 55 assert(firstEntry["GenericName"] == "File Manager"); 56 57 assert(ilf.group("Another Group")["Name"] == "Commander"); 58 assert(equal(ilf.group("Another Group").byKeyValue(), [ KeyValueTuple("Name", "Commander"), KeyValueTuple("Comment", "Manage files") ])); 59 60 assert(equal(ilf.byGroup().map!(g => g.name), ["First Entry", "Another Group"])); 61 62 ilf.removeGroup("Another Group"); 63 assert(!ilf.group("Another Group")); 64 assert(equal(ilf.byGroup().map!(g => g.name), ["First Entry"])); 65 66 ilf.addGroup("Another Group"); 67 assert(ilf.group("Another Group")); 68 assert(ilf.group("Another Group").byIniLine().empty); 69 assert(ilf.group("Another Group").byKeyValue().empty); 70 71 ilf.addGroup("Other Group"); 72 assert(equal(ilf.byGroup().map!(g => g.name), ["First Entry", "Another Group", "Other Group"])); 73 74 const IniLikeFile cilf = ilf; 75 static assert(is(typeof(cilf.byGroup()))); 76 static assert(is(typeof(cilf.group("First Entry").byKeyValue()))); 77 static assert(is(typeof(cilf.group("First Entry").byIniLine()))); 78 79 contents = 80 `[First] 81 Key=Value 82 [Second] 83 Key=Value`; 84 ilf = new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.firstGroupOnly); 85 assert(ilf.group("First") !is null); 86 assert(ilf.group("Second") is null); 87 assert(ilf.group("First")["Key"] == "Value"); 88 89 contents = 90 `[Group] 91 GenericName=File manager 92 [Group] 93 Name=Commander`; 94 95 IniLikeException shouldThrow = null; 96 try { 97 new IniLikeFile(iniLikeStringReader(contents)); 98 } catch(IniLikeException e) { 99 shouldThrow = e; 100 } 101 assert(shouldThrow !is null, "Duplicate groups should throw"); 102 assert(shouldThrow.lineNumber == 3); 103 assertNotThrown(new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.ignoreGroupDuplicates)); 104 105 contents = 106 `[Group] 107 =File manager`; 108 109 try { 110 new IniLikeFile(iniLikeStringReader(contents)); 111 } catch(IniLikeException e) { 112 shouldThrow = e; 113 } 114 assert(shouldThrow !is null, "Invalid key should throw"); 115 assert(shouldThrow.lineNumber == 2); 116 assertNotThrown(new IniLikeFile(iniLikeStringReader(contents), IniLikeFile.ReadOptions.ignoreInvalidKeys)); 117 118 contents = 119 `[Group] 120 #Comment 121 Valid=Key 122 NotKeyNotGroupNotComment`; 123 124 try { 125 new IniLikeFile(iniLikeStringReader(contents)); 126 } catch(IniLikeException e) { 127 shouldThrow = e; 128 } 129 assert(shouldThrow !is null, "Invalid entry should throw"); 130 assert(shouldThrow.lineNumber == 4);
Ini-like file.