Ejemplo n.º 1
0
 static void WriteProperties(this TextWriter writer, FileSection section)
 {
     foreach (var f in section.Fields)
     {
         writer.WriteLine($"{f.Name}: {f.Format(section.Object)}");
     }
 }
Ejemplo n.º 2
0
 static void WriteTable(this TextWriter writer, FileSection section)
 {
     writer.WriteColumns(section);
     foreach (var row in (IList)section.Object)
     {
         writer.WriteRow(section, row);
     }
 }
Ejemplo n.º 3
0
 public static void Write <T>(this CdsFile <T> file, TextWriter writer) where T : CdsFile <T>, new()
 {
     using (writer)
         foreach (var section in FileSection.Of(file))
         {
             writer.WriteSection(section);
         }
 }
Ejemplo n.º 4
0
        static bool TryReadHeader(this TextReader reader, FileSection section)
        {
            var line = reader.ReadLine()?.Trim();

            return(line != null &&
                   line.StartsWith("#") &&
                   line.Contains(section.Text));
        }
Ejemplo n.º 5
0
        static bool TryReadColumns(this TextReader reader, FileSection section)
        {
            var line = reader.ReadLine()?.Trim();

            return(line != null &&
                   line.StartsWith("#") &&
                   section.Fields.All(f => line.Contains(f.Name)));
        }
Ejemplo n.º 6
0
 public static void Read <T>(this CdsFile <T> file, TextReader reader) where T : CdsFile <T>, new()
 {
     using (reader)
         foreach (var section in FileSection.Of(file))
         {
             section.Object = reader.Read(section);
         }
 }
Ejemplo n.º 7
0
        static void WriteSection(this TextWriter writer, FileSection section)
        {
            writer.WriteHeader(section);
            if (section.IsTable)
            {
                writer.WriteTable(section);
            }
            else
            {
                writer.WriteProperties(section);
            }

            writer.WriteLine();
        }
Ejemplo n.º 8
0
        static object ReadTable(this TextReader reader, FileSection section)
        {
            var table = section.CreateTable();

            if (!reader.TryReadHeader(section))
            {
                throw new FormatException($"Section {section.Text} not found or out of order.");
            }
            if (!reader.TryReadColumns(section))
            {
                throw new FormatException($"Section {section.Text} is missing some columns.");
            }
            while (reader.TryReadRow(section, out var row))
            {
                table.Add(row);
            }

            return(table);
        }
Ejemplo n.º 9
0
        static object ReadProperties(this TextReader reader, FileSection section)
        {
            var properties = section.CreateObject();

            if (!reader.TryReadHeader(section))
            {
                throw new FormatException($"Section {section.Text} not found or out of order.");
            }

            var fields = section.Fields
                         .ToDictionary(f => f.Name);

            while (reader.TryReadProperty(out var name, out var value))
            {
                fields[name][properties] = Convert.ChangeType(value, fields[name].Type);
            }

            return(properties);
        }
Ejemplo n.º 10
0
        static bool TryReadRow(this TextReader reader, FileSection section, out object row)
        {
            row = section.CreateObject();
            var line = reader.ReadLine()?.Trim();

            if (string.IsNullOrWhiteSpace(line))
            {
                return(false);
            }

            var values = line.Split('|')
                         .Select((v, i) => new { Value = v.Trim(), Index = i })
                         .ToDictionary(x => x.Index, x => x.Value);

            foreach (var x in section.Fields.Select((f, i) => new { Field = f, Index = i }))
            {
                x.Field[row] = Convert.ChangeType(values[x.Index], x.Field.Type);
            }

            return(true);
        }
Ejemplo n.º 11
0
 static void WriteRow(this TextWriter writer, FileSection section, object row) =>
 writer.WriteLine(string.Join(" | ", from f in section.Fields select f.Format(row)));
Ejemplo n.º 12
0
 static void WriteColumns(this TextWriter writer, FileSection section) =>
 writer.WriteLine($"# {string.Join(" | ", from f in section.Fields select f.Name)}");
Ejemplo n.º 13
0
 static void WriteHeader(this TextWriter writer, FileSection section) =>
 writer.WriteLine($"# {section.Text}");
Ejemplo n.º 14
0
 static object Read(this TextReader reader, FileSection section) =>
 section.IsTable
     ? reader.ReadTable(section)
     : reader.ReadProperties(section);