/// <summary> /// Load a collection with the contents of a MicroYaml document /// </summary> /// <param name="reader">A <see cref="TextReader"/> loaded with a MicroYaml document.</param> /// <param name="options">YamlReaderOptions to use when parsing. Null for default.</param> /// <param name="map">The collection into which the contents will be loaded.</param> /// <returns>The number of key-value pairs loaded into the document.</returns> static public int Load(TextReader reader, YamlReaderOptions options, ICollection <KeyValuePair <string, string> > map) { using (var r = new MicroYamlReader(reader, options)) { return(r.CopyTo(map)); } }
// Cut the file at one character shorter until it is zero length. // Each time attempt to parse using all four option permutations. // This will yield numerous syntax errors but should not crash // the parser. static bool TestIterativeEof(string yaml) { try { YamlReaderOptions options = new YamlReaderOptions(); while (yaml.Length > 0) { for (int i = 0; i < 4; ++i) { options.IgnoreTextOutsideDocumentMarkers = (i & 1) != 0; options.MergeDocuments = (i & 2) != 0; using (var reader = new MicroYamlReader(new StringReader(yaml), options)) { while (reader.MoveNextDocument()) { while (reader.MoveNext()) { // Do nothing //Trace("(\"{0}\", \"{1}\")", EscapeString(reader.Current.Key), EscapeString(reader.Current.Value)); } } } } // Sorten by one character yaml = yaml.Substring(0, yaml.Length - 1); } } catch (Exception err) { ReportError(err.ToString()); return(false); } return(true); }
public MicroYamlReader(TextReader reader, YamlReaderOptions options = null) { m_reader = reader; if (options == null) { m_options = new YamlReaderOptions(); } else { m_options = options.Clone(); } m_tokenType = TokenType.Null; m_token = null; ChInit(); }
/// <summary> /// Load a collection with the contents of a MicroYaml document /// </summary> /// <param name="doc">MicroYaml document.</param> /// <param name="options">YamlReaderOptions to use when parsing. Null for default.</param> /// <param name="map">The collection into which the contents will be loaded.</param> /// <returns>The number of key-value pairs loaded into the document.</returns> static public int LoadYaml(String doc, YamlReaderOptions options, ICollection <KeyValuePair <string, string> > map) { if (options == null) { options = new YamlReaderOptions(); } else { options = options.Clone(); options.CloseInput = false; } using (var reader = new StringReader(doc)) { return(Load(reader, options, map)); } }
/// <summary> /// Load a collection with the contents of a MicroYaml document /// </summary> /// <param name="filename">Filename of a MicroYaml document.</param> /// <param name="options">YamlReaderOptions to use when parsing. Null for default.</param> /// <param name="map">The collection into which the contents will be loaded.</param> /// <returns>The number of key-value pairs loaded into the document.</returns> static public int LoadFile(String filename, YamlReaderOptions options, ICollection <KeyValuePair <string, string> > map) { if (options == null) { options = new YamlReaderOptions(); } else { options = options.Clone(); options.CloseInput = false; } using (var reader = new StreamReader(filename, Encoding.UTF8, true)) { return(Load(reader, options, map)); } }
/// <summary> /// Load a collection with the contents of a MicroYaml document /// </summary> /// <param name="stream">A <see cref="Stream"/> loaded with a MicroYaml document.</param> /// <param name="options">YamlReaderOptions to use when parsing. Null for default.</param> /// <param name="map">The collection into which the contents will be loaded.</param> /// <returns>The number of key-value pairs loaded into the document.</returns> static public int Load(Stream stream, YamlReaderOptions options, ICollection <KeyValuePair <string, string> > map) { if (options == null) { options = new YamlReaderOptions(); } else { options = options.Clone(); } using (var reader = new StreamReader(stream, Encoding.UTF8, true, 4096, !options.CloseInput)) { options.CloseInput = false; using (var r = new MicroYamlReader(reader, options)) { return(r.CopyTo(map)); } } }
static bool CompareDocWithExpected(string yaml, IEnumerable <KeyValuePair <string, string> > expected, bool mergeDocs, bool ignoreTextOutside = false) { bool success = true; Yaml.MicroYamlReader reader = null; IEnumerator <KeyValuePair <string, string> > comp = null; try { var options = new YamlReaderOptions(); options.MergeDocuments = mergeDocs; options.IgnoreTextOutsideDocumentMarkers = ignoreTextOutside; reader = new MicroYamlReader(new StringReader(yaml), options); comp = expected.GetEnumerator(); bool eofComp = false; for (;;) { bool docHasValue = reader.MoveNext(); bool compHasValue = false; if (!eofComp) { compHasValue = comp.MoveNext(); if (mergeDocs) { while (compHasValue && comp.Current.Key == null) { compHasValue = comp.MoveNext(); } } eofComp = !compHasValue; } if (!docHasValue) { if (!reader.MoveNextDocument()) { break; } Trace("--- Document Break ---"); if (compHasValue && comp.Current.Key != null) { Trace(" Unexpected document break."); success = false; } continue; } if (eofComp) { ReportError("Expected result at EOF but YAML input remains."); success = false; } Trace("(\"{0}\", \"{1}\")", EscapeString(reader.Current.Key), EscapeString(reader.Current.Value)); if (reader.ImmediateError != null) { ReportError(reader.ImmediateError); success = false; } if (!eofComp) { if (comp.Current.Key == null) { Trace(" Expected document break."); } else { if (!string.Equals(reader.Current.Key, comp.Current.Key, StringComparison.Ordinal)) { Trace(" Keys don't match:\r\n \"{0}\"\r\n \"{1}\"", EscapeString(reader.Current.Key), EscapeString(comp.Current.Key)); success = false; } if (!string.Equals(reader.Current.Value, comp.Current.Value)) { Trace(" Values don't match:\r\n \"{0}\"\r\n \"{1}\"", EscapeString(reader.Current.Value), EscapeString(comp.Current.Value)); success = false; } } } } if (comp.MoveNext()) { ReportError("YAML at EOF but still values in expected result."); } } catch (Exception err) { ReportError(err.ToString()); success = false; } finally { if (comp != null) { comp.Dispose(); comp = null; } if (reader != null) { reader.Dispose(); reader = null; } } return(success); }