diff --git a/Domain/Something/ExpIniFileGenerator.cs b/Domain/Something/ExpIniFileGenerator.cs new file mode 100644 index 0000000..942e7e3 --- /dev/null +++ b/Domain/Something/ExpIniFileGenerator.cs @@ -0,0 +1,66 @@ +using System.Text; + +namespace Domain.Somthing; + +public class ExpIniFileGenerator(uint rowCount, uint oddColumnCount, uint evenColumnCount) +{ + private const string _lineEnding = "\r\n"; + + public string Generate(IEnumerable frameInfos) + { + var groups = frameInfos + .DistinctBy(q => new {q.Row, q.Column}) + .GroupBy(q => q.Row, (row, columns) => new { Row = row, Columns = columns }); + var sb = new StringBuilder(); + AppendLine(sb, oddColumnCount.ToString()); + AppendLine(sb, evenColumnCount.ToString()); + AppendLine(sb, rowCount.ToString()); + + foreach (var row in groups.OrderBy(q => q.Row)) + { + bool? isPreviousContainAnything = null; + var stateAccumulator = 0u; + foreach (var column in row.Columns) + { + isPreviousContainAnything ??= column.IsContainAnything; + + if (isPreviousContainAnything == column.IsContainAnything) + { + stateAccumulator++; + } + else + { + sb.Append(GetExpValue(isPreviousContainAnything.Value, stateAccumulator)); + stateAccumulator = 0u; + isPreviousContainAnything = column.IsContainAnything; + } + } + + if (stateAccumulator > 0 && isPreviousContainAnything.HasValue) + { + sb.Append(GetExpValue(isPreviousContainAnything.Value, stateAccumulator)); + } + sb.Append(_lineEnding); + } + + return sb.ToString(); + } + + private static void AppendLine(StringBuilder sb, string text) + { + sb.Append(text); + sb.Append(_lineEnding); + } + + private static string GetExpValue(bool isContainAnything, uint accumulator) + { + return string.Concat(accumulator.ToString(), isContainAnything ? 'e' : 'z'); + } + + public class FrameExpInfo + { + public uint Row { get; init; } + public uint Column { get; init; } + public bool IsContainAnything { get; set; } + } +} \ No newline at end of file