/// <summary> /// Returns a padding string for specified level per set options /// </summary> public static string Indent(int level, LaconfigWritingOptions opt) { if (opt.IndentWidth > 0) { return(string.Empty.PadLeft(level * opt.IndentWidth)); } return(string.Empty); }
/// <summary> /// Appends LaconicConfiguration data into the instance of StringBuilder /// </summary> public static void Write(IConfigSectionNode data, StringBuilder sb, LaconfigWritingOptions options = null) { if (options == null) { options = LaconfigWritingOptions.PrettyPrint; } writeSection(sb, data, 0, options); }
/// <summary> /// Writes LaconicConfiguration data to the string /// </summary> public static string Write(IConfigSectionNode data, LaconfigWritingOptions options = null) { if (options==null) options = LaconfigWritingOptions.PrettyPrint; var sb = new StringBuilder(); writeSection(sb, data, 0, options); return sb.ToString(); }
/// <summary> /// Writes LaconicConfiguration data to the string /// </summary> public static string Write(IConfigSectionNode data, LaconfigWritingOptions options = null) { if (options == null) { options = LaconfigWritingOptions.PrettyPrint; } var sb = new StringBuilder(); writeSection(sb, data, 0, options); return(sb.ToString()); }
/// <summary> /// Appends LaconicConfiguration data into the instance of StringBuilder /// </summary> public static void Write(Configuration data, StringBuilder sb, LaconfigWritingOptions options = null) { Write(data.Root, sb, options); }
/// <summary> /// Writes LaconicConfiguration data to the string /// </summary> public static string Write(Configuration data, LaconfigWritingOptions options = null) { return(Write(data.Root, options)); }
public void Options_DontWriteRootSectionDeclaration() { var conf = "nfx=90{a=1 b=2 c{d=5}}".AsLaconicConfig(handling: ConvertErrorHandling.Throw); var opt = new LaconfigWritingOptions{ DontWriteRootSectionDeclaration = true }; var saved = conf.Configuration.ToLaconicString(opt); Console.WriteLine(saved); Assert.AreEqual("a=1 b=2 c{d=5}", saved); opt = new LaconfigWritingOptions{ /* DontWriteRootSectionDeclaration = false - default */ }; saved = conf.Configuration.ToLaconicString(opt).Trim(); Console.WriteLine(saved); Assert.AreEqual("nfx=90{a=1 b=2 c{d=5}}", saved); }
/// <summary> /// Saves configuration into a file /// </summary> public void SaveAs(string filename, LaconfigWritingOptions options = null) { using(var fs = new FileStream(filename, FileMode.Create)) LaconfigWriter.Write(this, fs, options); base.SaveAs(filename); }
/// <summary> /// Writes LaconicConfiguration data to the stream /// </summary> public static void Write(Configuration data, Stream stream, LaconfigWritingOptions options = null, Encoding encoding = null) { Write(data.Root, stream, options, encoding); }
/// <summary> /// Writes LaconicConfiguration data to the stream /// </summary> public static void Write(IConfigSectionNode data, Stream stream, LaconfigWritingOptions options = null, Encoding encoding = null) { using(var writer = new StreamWriter(stream, encoding ?? UTF8Encoding.UTF8)) writer.Write( Write(data, options) ); }
/// <summary> /// Returns a padding string for specified level per set options /// </summary> public static string Indent(int level, LaconfigWritingOptions opt) { if (opt.IndentWidth>0) return string.Empty.PadLeft(level * opt.IndentWidth); return string.Empty; }
private static void writeSection(StringBuilder sb, IConfigSectionNode section, int level, LaconfigWritingOptions opt) { var writeSectionDecl = section.Parent.Exists || !opt.DontWriteRootSectionDeclaration; if (writeSectionDecl) { if (opt.SectionLineBreak && section.Parent.Exists) { sb.AppendLine(); sb.Append(Indent(level, opt)); } // else sb.Append(' '); writeString(sb, section.Name); var value = section.VerbatimValue; if (value.IsNotNullOrWhiteSpace()) { sb.Append("="); writeString(sb, value); } if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('{'); } bool wasAttr = false; foreach(var anode in section.Attributes) { if (opt.AttributeLineBreak) { sb.AppendLine(); sb.Append(Indent(level+1, opt)); } else if (wasAttr) sb.Append(' '); wasAttr = true; writeString(sb, anode.Name); sb.Append("="); writeString(sb, anode.VerbatimValue); } bool wasChild = false; foreach(var csect in section.Children) { if (!wasChild && wasAttr && !opt.SectionLineBreak) sb.Append(' '); wasChild = true; writeSection(sb, csect, level+1, opt); } if (writeSectionDecl) { if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('}'); } }
/// <summary> /// Appends LaconicConfiguration data into the instance of StringBuilder /// </summary> public static void Write(IConfigSectionNode data, StringBuilder sb, LaconfigWritingOptions options = null) { if (options==null) options = LaconfigWritingOptions.PrettyPrint; writeSection(sb, data, 0, options); }
private static void writeSection(StringBuilder sb, IConfigSectionNode section, int level, LaconfigWritingOptions opt) { if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } else { sb.Append(' '); } writeString(sb, section.Name); var value = section.VerbatimValue; if (value.IsNotNullOrWhiteSpace()) { sb.Append("="); writeString(sb, value); } if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('{'); foreach (var anode in section.Attributes) { if (opt.AttributeLineBreak) { sb.AppendLine(); sb.Append(Indent(level + 1, opt)); } else { sb.Append(' '); } writeString(sb, anode.Name); sb.Append("="); writeString(sb, anode.VerbatimValue); } sb.Append(' '); foreach (var csect in section.Children) { writeSection(sb, csect, level + 1, opt); } if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('}'); }
private static void writeSection(StringBuilder sb, IConfigSectionNode section, int level, LaconfigWritingOptions opt) { var writeSectionDecl = section.Parent.Exists || !opt.DontWriteRootSectionDeclaration; if (writeSectionDecl) { if (opt.SectionLineBreak && section.Parent.Exists) { sb.AppendLine(); sb.Append(Indent(level, opt)); } // else sb.Append(' '); writeString(sb, section.Name); var value = section.VerbatimValue; if (value.IsNotNullOrWhiteSpace()) { sb.Append("="); writeString(sb, value); } if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('{'); } bool wasAttr = false; foreach (var anode in section.Attributes) { if (opt.AttributeLineBreak) { sb.AppendLine(); sb.Append(Indent(level + 1, opt)); } else if (wasAttr) { sb.Append(' '); } wasAttr = true; writeString(sb, anode.Name); sb.Append("="); writeString(sb, anode.VerbatimValue); } bool wasChild = false; foreach (var csect in section.Children) { if (!wasChild && wasAttr && !opt.SectionLineBreak) { sb.Append(' '); } wasChild = true; writeSection(sb, csect, level + 1, opt); } if (writeSectionDecl) { if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('}'); } }
/// <summary> /// Writes LaconicConfiguration data to the string /// </summary> public static string Write(Configuration data, LaconfigWritingOptions options = null) { return Write(data.Root, options); }
/// <summary> /// Writes LaconicConfiguration data to the stream /// </summary> public static void Write(IConfigSectionNode data, Stream stream, LaconfigWritingOptions options = null, Encoding encoding = null) { using (var writer = new StreamWriter(stream, encoding ?? UTF8Encoding.UTF8)) writer.Write(Write(data, options)); }
/// <summary> /// Saves laconic configuration into string in Laconfig format and returns it /// </summary> public string SaveToString(LaconfigWritingOptions options = null) { return LaconfigWriter.Write(this, options); }
private static void writeSection(StringBuilder sb, IConfigSectionNode section, int level, LaconfigWritingOptions opt) { if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } else sb.Append(' '); writeString(sb, section.Name); var value = section.VerbatimValue; if (value.IsNotNullOrWhiteSpace()) { sb.Append("="); writeString(sb, value); } if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('{'); foreach(var anode in section.Attributes) { if (opt.AttributeLineBreak) { sb.AppendLine(); sb.Append(Indent(level+1, opt)); } else sb.Append(' '); writeString(sb, anode.Name); sb.Append("="); writeString(sb, anode.VerbatimValue); } sb.Append(' '); foreach(var csect in section.Children) { writeSection(sb, csect, level+1, opt); } if (opt.SectionLineBreak) { sb.AppendLine(); sb.Append(Indent(level, opt)); } sb.Append('}'); }