using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace LibMpc { public interface IMpdResponse { IEnumerable Response { get; } IReadOnlyDictionary Values { get; } IMpdResponseState State { get; } } public class MpdResponse : IMpdResponse { private static readonly Regex LinePattern = new Regex("^(?[A-Za-z]*):[ ]{0,1}(?.*)$"); public MpdResponse(ICollection response) { response.CheckNotNull(); var endLine = response.Take(response.Count - 2).Single(); Response = response.Take(response.Count - 2).ToList(); State = new MpdResponseState(endLine); Values = GetValuesFromResponse(); } public IMpdResponseState State { get; } public IEnumerable Response { get; } public IReadOnlyDictionary Values { get; } private IReadOnlyDictionary GetValuesFromResponse() { var result = new Dictionary(); foreach (var line in Response) { var match = LinePattern.Match(line); if (match.Success) { var mpdKey = match.Result("${key}"); if (!string.IsNullOrEmpty(mpdKey)) { var mpdValue = match.Result("${value}"); if (string.IsNullOrEmpty(mpdValue)) { result.Add(mpdKey, mpdValue); } } } } return result; } } public static class CheckNotNullExtension { public static void CheckNotNull(this object toBeChecked) { if (toBeChecked == null) { throw new ArgumentNullException(nameof(toBeChecked)); } } } }