Esempio n. 1
0
        /// <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);
        }
Esempio n. 2
0
        /// <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);
        }
Esempio n. 3
0
        /// <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());
        }
Esempio n. 4
0
 /// <summary>
 /// Writes LaconicConfiguration data to the string
 /// </summary>
 public static string Write(Configuration data, LaconfigWritingOptions options = null)
 {
     return(Write(data.Root, options));
 }
Esempio n. 5
0
 /// <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));
 }
Esempio n. 6
0
 /// <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);
 }
Esempio n. 7
0
        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("=");

                var v = anode.VerbatimValue;

                if (v == null)
                {
                    sb.Append("null");
                }
                else
                {
                    writeString(sb, v);
                }
            }

            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('}');
            }
        }
Esempio n. 8
0
 /// <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);
 }
Esempio n. 9
0
 /// <summary>
 /// Writes LaconicConfiguration data to the string
 /// </summary>
 public static string Write(Configuration data, LaconfigWritingOptions options = null)
 => Write(data.Root, options);