155 lines
5.2 KiB
Plaintext
155 lines
5.2 KiB
Plaintext
|
|
using System;
|
||
|
|
using System.Collections.Generic;
|
||
|
|
using System.Linq;
|
||
|
|
using System.Text;
|
||
|
|
|
||
|
|
namespace IRIGCh10
|
||
|
|
{
|
||
|
|
public class TMATSectionNumbered<T> where T : Enum
|
||
|
|
{
|
||
|
|
public AttributeIdentifiers Identifier { get; set; } = AttributeIdentifiers.GeneralInformation;
|
||
|
|
public int Number { get; set; } = -1;
|
||
|
|
|
||
|
|
private Dictionary<T, string> _values = new Dictionary<T, string>();
|
||
|
|
public void SetValue(T tag, string value) => _values[tag] = value;
|
||
|
|
public string GetValue(T tag) => _values.ContainsKey(tag)?_values[tag] : null;
|
||
|
|
public string Serialize(int number)
|
||
|
|
{
|
||
|
|
var sb = new StringBuilder();
|
||
|
|
var tags = Enum.GetValues(typeof(T)).Cast<T>().ToArray();
|
||
|
|
foreach (var tag in tags)
|
||
|
|
{
|
||
|
|
var s = Serialize(number, tag);
|
||
|
|
if( string.IsNullOrWhiteSpace(s) ){ continue; }
|
||
|
|
|
||
|
|
sb.AppendLine(s);
|
||
|
|
}
|
||
|
|
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
|
||
|
|
private string Serialize(int number, T tag)
|
||
|
|
{
|
||
|
|
var value = GetValue(tag);
|
||
|
|
if( string.IsNullOrWhiteSpace(value) ){ return null; }
|
||
|
|
var attr = DescriptionDecoder.GetDescription(tag);
|
||
|
|
var identifier = DescriptionDecoder.GetDescription(Identifier);
|
||
|
|
if (Number > 0)
|
||
|
|
{
|
||
|
|
return $"{identifier}-{Number}\\{attr}{number}:{value};";
|
||
|
|
}
|
||
|
|
else{ return $"{identifier}\\{attr}{number}:{value};"; }
|
||
|
|
}
|
||
|
|
public void SetValueWithLength(T tag, string value)
|
||
|
|
{
|
||
|
|
var maxLength = MaxLengthDecoder.GetMaxLength(tag);
|
||
|
|
//maxlength is just a suggestion ...
|
||
|
|
//if (maxLength < value.Length)
|
||
|
|
//{
|
||
|
|
// throw new FormatException($"{tag.ToString()} must be {maxLength} characters or less");
|
||
|
|
//}
|
||
|
|
SetValue(tag, value);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public class TMATSectionNumberedArray<T> where T:Enum
|
||
|
|
{
|
||
|
|
private List<TMATSectionNumbered<T>> _items = new List<TMATSectionNumbered<T>>();
|
||
|
|
private AttributeIdentifiers _attributeIdentifier = AttributeIdentifiers.GeneralInformation;
|
||
|
|
private readonly int _number = -1;
|
||
|
|
private readonly int _maxNumber = -1;
|
||
|
|
|
||
|
|
private string _numberedTag;
|
||
|
|
public TMATSectionNumberedArray(string numberedTag)
|
||
|
|
{
|
||
|
|
_numberedTag = numberedTag;
|
||
|
|
}
|
||
|
|
|
||
|
|
public TMATSectionNumberedArray(string numberedTag, int maxNumber)
|
||
|
|
{
|
||
|
|
_numberedTag = numberedTag;
|
||
|
|
_maxNumber = maxNumber;
|
||
|
|
}
|
||
|
|
|
||
|
|
public TMATSectionNumberedArray(AttributeIdentifiers identifier, int number, string numberedTag, int maxNumber)
|
||
|
|
{
|
||
|
|
_number = number;
|
||
|
|
_maxNumber = maxNumber;
|
||
|
|
_numberedTag = numberedTag;
|
||
|
|
_attributeIdentifier = identifier;
|
||
|
|
}
|
||
|
|
//private AttributeIdentifiers _attribute = AttributeIdentifiers.GeneralInformation;
|
||
|
|
|
||
|
|
public virtual void SetValue(int number, T tag, string value)
|
||
|
|
{
|
||
|
|
if (number > _items.Count)
|
||
|
|
{
|
||
|
|
SetCount(number);
|
||
|
|
}
|
||
|
|
|
||
|
|
_items[number - 1].SetValueWithLength(tag, value);
|
||
|
|
}
|
||
|
|
|
||
|
|
public string GetValue(int number, T tag) => number > _items.Count ? null : _items[number - 1].GetValue(tag);
|
||
|
|
|
||
|
|
public string Serialize()
|
||
|
|
{
|
||
|
|
if (!_items.Any())
|
||
|
|
{
|
||
|
|
return string.Empty;
|
||
|
|
}
|
||
|
|
|
||
|
|
var sb = new StringBuilder();
|
||
|
|
var attribute = DescriptionDecoder.GetDescription(_attributeIdentifier);
|
||
|
|
if (_number > 0)
|
||
|
|
{
|
||
|
|
attribute = $"{attribute}-{_number}";
|
||
|
|
}
|
||
|
|
if (!string.IsNullOrWhiteSpace(_numberedTag))
|
||
|
|
{
|
||
|
|
sb.AppendLine($"{attribute}\\{_numberedTag}:{_items.Count()};");
|
||
|
|
}
|
||
|
|
|
||
|
|
var number = 1;
|
||
|
|
foreach (var item in _items)
|
||
|
|
{
|
||
|
|
var s = item.Serialize(number++);
|
||
|
|
if( string.IsNullOrWhiteSpace(s) ){ continue; }
|
||
|
|
sb.Append(s);
|
||
|
|
}
|
||
|
|
return sb.ToString();
|
||
|
|
}
|
||
|
|
|
||
|
|
public int GetCount() => _items.Count;
|
||
|
|
|
||
|
|
public void SetCount(int count)
|
||
|
|
{
|
||
|
|
if (_maxNumber > 0 && count > _maxNumber)
|
||
|
|
{
|
||
|
|
throw new Exception($"count exceeds maximum number for array");
|
||
|
|
}
|
||
|
|
if( _items.Count == count ){ return; }
|
||
|
|
if (_items.Count > count)
|
||
|
|
{
|
||
|
|
var toKeep = _items.Take(count);
|
||
|
|
_items.Clear();
|
||
|
|
_items.AddRange(toKeep);
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
for (var i = _items.Count; i < count; i++)
|
||
|
|
{
|
||
|
|
var newItem = new TMATSectionNumbered<T>();
|
||
|
|
_items.Add(newItem);
|
||
|
|
if (_number > 0)
|
||
|
|
{
|
||
|
|
newItem.Number = _number;;
|
||
|
|
}
|
||
|
|
|
||
|
|
newItem.Identifier = _attributeIdentifier;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|