public static TomlDocument DocumentFrom(Type type, object t) { var val = ValueFrom(type, t); return(val switch { TomlDocument doc => doc, TomlTable table => new TomlDocument(table), _ => throw new TomlPrimitiveToDocumentException(type) });
private IDictionary <string, string> ParseStream(Stream input) { var options = default(TomlDocumentOptions); using (StreamReader streamReader = new StreamReader(input)) { using (TomlDocument tomlDocument = TomlDocument.Parse(streamReader.ReadToEnd(), options)) { } } return(null); }
private static TomlDocument OneTable(string path) { try { Console.WriteLine("---------- {0} ----------", path); var toml = new TomlDocument(); toml.LoadPath(path); Console.WriteLine(toml); return(toml); } catch (TomlAnalisysException e) { Console.WriteLine(e.ToString()); return(null); } }
public override void SaveConfig() { if (UIManager.Initializing) { return; } var tomlDocument = TomlDocument.CreateEmpty(); foreach (var entry in ConfigManager.InternalConfigs) { tomlDocument.Put(entry.Key, entry.Value.BoxedValue as string, false); } File.WriteAllText(CONFIG_PATH, tomlDocument.SerializedValue); }
public override void SaveConfig() { var document = TomlDocument.CreateEmpty(); foreach (var config in ConfigManager.ConfigElements) { document.Put(config.Key, config.Value.BoxedValue.ToString()); } if (!Directory.Exists(ExplorerCore.Loader.ExplorerFolder)) { Directory.CreateDirectory(ExplorerCore.Loader.ExplorerFolder); } File.WriteAllText(CONFIG_PATH, document.SerializedValue); }
public bool TryLoadConfig() { try { if (!File.Exists(CONFIG_PATH)) { return(false); } TomlDocument document = TomlParser.ParseFile(CONFIG_PATH); foreach (var key in document.Keys) { var panelKey = (UIManager.Panels)Enum.Parse(typeof(UIManager.Panels), key); ConfigManager.GetPanelSaveData(panelKey).Value = document.GetString(key); } return(true); } catch (Exception ex) { ExplorerCore.LogWarning("Error loading internal data: " + ex.ToString()); return(false); } }
private void ReadTableArrayStatement(StringReader reader, TomlDocument document) { //Consume the (second) opening bracket if (!reader.ExpectAndConsume('[')) { throw new ArgumentException("Internal Tomlet Bug: ReadTableArrayStatement called and first char is not a ["); } //Array var arrayName = reader.ReadWhile(c => !c.IsEndOfArrayChar() && !c.IsNewline()); if (!reader.ExpectAndConsume(']') || !reader.ExpectAndConsume(']')) { throw new UnterminatedTomlTableArrayException(_lineNumber); } TomlTable parentTable; if (_lastTableArrayName != null && arrayName.StartsWith(_lastTableArrayName + ".")) { //nested array of tables directly relative to parent - we can cheat //Save parent table parentTable = _currentTable !; //Work out relative key var relativeKey = arrayName.Replace(_lastTableArrayName + ".", ""); //Make new array and table var newArray = new TomlArray(); _currentTable = new TomlTable(); newArray.ArrayValues.Add(_currentTable); //Insert into parent table parentTable !.ParserPutValue(relativeKey, newArray, _lineNumber); //Save variables _lastTableArrayName = arrayName; return; } if (TomlKeyUtils.IsSimpleKey(arrayName)) { //Not present - create and populate with one table. _currentTable = new TomlTable(); TomlArray tableArray; //Simple key so looking up via document.ContainsKey is fine. if (!document.ContainsKey(arrayName)) { //make a new one if it doesn't exist tableArray = new TomlArray { IsTableArray = true } } ; else if (document.Entries.TryGetValue(arrayName, out var hopefullyArray) && hopefullyArray is TomlArray arr) { //already exists, use it tableArray = arr; } else { throw new TomlTableArrayAlreadyExistsAsNonArrayException(_lineNumber, arrayName); } if (!tableArray.IsTableArray) { throw new TomlNonTableArrayUsedAsTableArrayException(_lineNumber, arrayName); } tableArray.ArrayValues.Add(_currentTable); if (!document.ContainsKey(arrayName)) { //Insert into the document document.ParserPutValue(arrayName, tableArray, _lineNumber); } //Save variables _lastTableArrayName = arrayName; return; } //Need to add to a complex-keyed table array, so may be behind one or more table arrays. parentTable = document; var components = TomlKeyUtils.GetKeyComponents(arrayName).ToList(); //Don't check last component for (var index = 0; index < components.Count - 1; index++) { var pathComponent = components[index]; if (!parentTable.ContainsKey(pathComponent)) { throw new MissingIntermediateInTomlTableArraySpecException(_lineNumber, pathComponent); } var value = parentTable.GetValue(pathComponent); if (value is TomlArray intermediateArray) { if (intermediateArray.Last() is TomlTable table) { parentTable = table; } else { throw new TomlTableArrayIntermediateNonTableException(_lineNumber, arrayName); } } else if (value is TomlTable table) { parentTable = table; } else { throw new TomlKeyRedefinitionException(_lineNumber, pathComponent); } } var lastComponent = components.Last(); if (parentTable.ContainsKey(lastComponent)) { if (!(parentTable.GetValue(lastComponent) is TomlArray array)) { throw new TomlTableArrayAlreadyExistsAsNonArrayException(_lineNumber, lastComponent); } if (!array.IsTableArray) { throw new TomlNonTableArrayUsedAsTableArrayException(_lineNumber, arrayName); } _currentTable = new TomlTable(); array.ArrayValues.Add(_currentTable); _lastTableArrayName = arrayName; } else { var array = new TomlArray { IsTableArray = true }; _currentTable = new TomlTable(); array.ArrayValues.Add(_currentTable); parentTable.PutValue(lastComponent, array); _lastTableArrayName = arrayName; } }
private void ReadTableStatement(StringReader reader, TomlDocument document) { //Table name var currentTableKey = reader.ReadWhile(c => !c.IsEndOfArrayChar() && !c.IsNewline()); var parent = (TomlTable)document; var relativeKey = currentTableKey; if (_lastTableArrayName != null && currentTableKey.StartsWith(_lastTableArrayName + ".")) { parent = (TomlTable)document.GetArray(_lastTableArrayName).Last(); relativeKey = relativeKey.Replace(_lastTableArrayName + ".", ""); } try { if (parent.ContainsKey(relativeKey)) { try { // this is an intentional variable, resharper. // ReSharper disable once UnusedVariable var table = (TomlTable)parent.GetValue(relativeKey); //The cast succeeded - we are redefining an existing table throw new TomlTableRedefinitionException(_lineNumber, currentTableKey); } catch (InvalidCastException) { //The cast failed, we are re-defining a non-table. throw new TomlKeyRedefinitionException(_lineNumber, currentTableKey); } } } catch (TomlContainsDottedKeyNonTableException e) { //Re-throw with correct line number and exception type. //To be clear - here we're re-defining a NON-TABLE key as a table, so this is a key redefinition exception //while the one above is a TableRedefinition exception because it's re-defining a key which is already a table. throw new TomlKeyRedefinitionException(_lineNumber, e._key, e); } if (!reader.TryPeek(out _)) { throw new TomlEOFException(_lineNumber); } if (!reader.ExpectAndConsume(']')) { throw new UnterminatedTomlTableNameException(_lineNumber); } reader.SkipWhitespace(); reader.SkipAnyComment(); reader.SkipPotentialCR(); if (!reader.TryPeek(out var shouldBeNewline)) { throw new TomlEOFException(_lineNumber); } if (!shouldBeNewline.IsNewline()) { throw new TomlMissingNewlineException(_lineNumber, (char)shouldBeNewline); } _currentTable = new TomlTable(); _lastTableArrayName = null; parent.ParserPutValue(relativeKey, _currentTable, _lineNumber); }
public TomlDocument Parse(string input) { try { var document = new TomlDocument(); using var reader = new StringReader(input); while (reader.TryPeek(out _)) { //We have more to read. _lineNumber += reader.SkipAnyCommentNewlineWhitespaceEtc(); if (!reader.TryPeek(out var nextChar)) { break; } if (nextChar == '[') { reader.Read(); //Consume the [ //Table or table-array? if (!reader.TryPeek(out var potentialSecondBracket)) { throw new TomlEOFException(_lineNumber); } if (potentialSecondBracket != '[') { ReadTableStatement(reader, document); } else { ReadTableArrayStatement(reader, document); } continue; //Restart loop. } //Read a key-value pair ReadKeyValuePair(reader, out var key, out var value); if (_currentTable != null) { //Insert into current table _currentTable.ParserPutValue(key, value, _lineNumber); } else { //Insert into the document document.ParserPutValue(key, value, _lineNumber); } //Read up until the end of the line, ignoring any comments or whitespace reader.SkipWhitespace(); reader.SkipAnyComment(); //Ensure we have a newline reader.SkipPotentialCR(); if (!reader.ExpectAndConsume('\n') && reader.TryPeek(out var shouldHaveBeenLF)) { //Not EOF and found a non-newline char throw new TomlMissingNewlineException(_lineNumber, (char)shouldHaveBeenLF); } _lineNumber++; //We've consumed a newline, move to the next line number. } return(document); } catch (Exception e) when(!(e is TomlException)) { throw new TomlInternalException(_lineNumber, e); } }
static void Main(string[] args) { var tomlData = @" title = ""TOML sample"" ld1 = 1979-05-27 lt1 = 07:32:00 lt2 = 00:32:00.999999 [database] server = ""192.168.1.1"" ports = [8001, 8002, 8003] connection_max = 5000 enabled = true [servers] # Indentation (tabs and/or spaces) is allowed but not required [servers.alpha] ip = ""10.0.0.1"" dc = ""eqdc10"" [servers.beta] ip = ""10.0.0.2"" dc = ""eqdc10"" [clients] data = [ [""gamma"", ""delta""], [1, 2] ] # Line breaks are OK when inside arrays hosts = [ ""alpha"", ""omega"" ] 1234 = ""value"" """" = ""blank"" # 可能ですがお奨めしません "; dynamic toml = new TomlDocument(); toml.Load(tomlData); Console.WriteLine(toml); AreEqual((string)toml.title, "TOML sample"); AreEqual((string)toml.database.server, "192.168.1.1"); AreEqual((int)toml.database.ports[0], 8001); AreEqual((int)toml.database.ports[1], 8002); AreEqual((int)toml.database.ports[2], 8003); AreEqual((int)toml.database.connection_max, 5000); AreEqual((bool)toml.database.enabled, true); AreEqual((string)toml.servers.alpha.ip, "10.0.0.1"); AreEqual((string)toml.servers.alpha.dc, "eqdc10"); AreEqual((string)toml.servers.beta.ip, "10.0.0.2"); AreEqual((string)toml.servers.beta.dc, "eqdc10"); AreEqual((string)toml.clients.data[0][0], "gamma"); AreEqual((string)toml.clients.data[0][1], "delta"); AreEqual((int)toml.clients.data[1][0], 1); AreEqual((int)toml.clients.data[1][1], 2); AreEqual((string)toml.clients.hosts[0], "alpha"); AreEqual((string)toml.clients.hosts[1], "omega"); OneTable("tests\\example.toml"); OneTable("tests\\table-array-nest.toml"); OneTable("tests\\table-array-many.toml"); OneTable("tests\\table-array-one.toml"); OneTable("tests\\table-array-implicit.toml"); OneTable("tests\\table-with-pound.toml"); OneTable("tests\\table-whitespace.toml"); OneTable("tests\\table-sub-empty.toml"); OneTable("tests\\table-empty.toml"); OneTable("tests\\implicit-groups.toml"); OneTable("tests\\implicit-and-explicit-before.toml"); OneTable("tests\\implicit-and-explicit-after.toml"); OneTable("tests\\key-special-chars.toml"); OneTable("tests\\key-space.toml"); OneTable("tests\\key-equals-nospace.toml"); OneTable("tests\\empty.toml"); OneTable("tests\\dottrd-keys.toml"); OneTable("tests\\comments-everywhere.toml"); OneTable("tests\\arrays-nested.toml"); OneTable("tests\\arrays-hetergeneous.toml"); OneTable("tests\\arrays.toml"); OneTable("tests\\array-nospaces.toml"); OneTable("tests\\array-empty.toml"); OneTable("tests\\float_no_number.toml"); OneTable("tests\\inline.toml"); OneTable("tests\\unicode-escape.toml"); OneTable("tests\\unicode-literal.toml"); OneTable("tests\\bool.toml"); OneTable("tests\\float.toml"); OneTable("tests\\integer.toml"); OneTable("tests\\long-float.toml"); OneTable("tests\\long-integer.toml"); OneTable("tests\\number-formated-decimal.toml"); OneTable("tests\\datetime.toml"); OneTable("tests\\raw-string.toml"); OneTable("tests\\string-empty.toml"); OneTable("tests\\string-escapes.toml"); OneTable("tests\\string-simple.toml"); OneTable("tests\\string-with-pound.toml"); OneTable("tests\\raw-multiline-string.toml"); OneTable("tests\\multiline-string.toml"); OneTable("invalid\\array-mixed-types-arrays-and-ints.toml"); OneTable("invalid\\array-mixed-types-ints-and-floats.toml"); OneTable("invalid\\array-mixed-types-strings-and-ints.toml"); OneTable("invalid\\datetime-malformed-no-leads.toml"); OneTable("invalid\\datetime-malformed-no-secs.toml"); OneTable("invalid\\datetime-malformed-no-t.toml"); OneTable("invalid\\datetime-malformed-with-milli.toml"); OneTable("invalid\\duplicate-keys.toml"); OneTable("invalid\\duplicate-key-table.toml"); OneTable("invalid\\duplicate-tables.toml"); OneTable("invalid\\empty-implicit-table.toml"); OneTable("invalid\\empty-table.toml"); OneTable("invalid\\float-no-leading-zero.toml"); OneTable("invalid\\float-no-trailing-digits.toml"); OneTable("invalid\\key-empty.toml"); OneTable("invalid\\key-hash.toml"); OneTable("invalid\\key-newline.toml"); OneTable("invalid\\key-open-bracket.toml"); OneTable("invalid\\key-single-open-bracket.toml"); OneTable("invalid\\key-space.toml"); OneTable("invalid\\key-start-bracket.toml"); OneTable("invalid\\key-two-equals.toml"); OneTable("invalid\\multiline-string-err.toml"); OneTable("invalid\\number_2_1_err.toml"); OneTable("invalid\\number_2_err.toml"); OneTable("invalid\\number_8_1_err.toml"); OneTable("invalid\\number_8_err.toml"); OneTable("invalid\\number_16_1_err.toml"); OneTable("invalid\\number_16_err.toml"); OneTable("invalid\\string-bad-byte-escape.toml"); OneTable("invalid\\string-bad-escape.toml"); OneTable("invalid\\string-byte-escapes.toml"); OneTable("invalid\\string-no-close.toml"); OneTable("invalid\\table-array-implicit.toml"); OneTable("invalid\\table-array-malformed-bracket.toml"); OneTable("invalid\\table-array-malformed-empty.toml"); OneTable("invalid\\table-empty.toml"); OneTable("invalid\\table-nested-brackets-close.toml"); OneTable("invalid\\table-nested-brackets-open.toml"); OneTable("invalid\\table-whitespace.toml"); OneTable("invalid\\table-with-pound.toml"); OneTable("invalid\\text-after-array-entries.toml"); OneTable("invalid\\text-after-integer.toml"); OneTable("invalid\\text-after-string.toml"); OneTable("invalid\\text-after-table.toml"); OneTable("invalid\\text-before-array-separator.toml"); OneTable("invalid\\text-in-array.toml"); test_var0_5(); }
public void Init() { fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "test.toml"); doc = TomlParser.FromFile(fileName); }