103 lines
3.5 KiB
C#
103 lines
3.5 KiB
C#
|
|
|
|||
|
|
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
|
|||
|
|
namespace DTS.Common.Classes.TMAT
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
/// <summary>
|
|||
|
|
/// this class handles a TMATS template split into two files
|
|||
|
|
/// </summary>
|
|||
|
|
public class TmtSplitFile : TmtBase
|
|||
|
|
{
|
|||
|
|
private readonly List<string> _lines = new List<string>();
|
|||
|
|
private readonly Dictionary<int, List<string>> _allChannels = new Dictionary<int, List<string>>();
|
|||
|
|
private readonly List<string> _channelTemplate = new List<string>();
|
|||
|
|
|
|||
|
|
public override string[] GetAllLines()
|
|||
|
|
{
|
|||
|
|
if (!_allChannels.Any()) { return _lines.ToArray(); }
|
|||
|
|
var lines = new List<string>(_lines.ToArray());
|
|||
|
|
var min = _allChannels.Keys.Min();
|
|||
|
|
var max = _allChannels.Keys.Max();
|
|||
|
|
for( var i = min; i<= max; i++)
|
|||
|
|
{
|
|||
|
|
if (_allChannels.ContainsKey(i))
|
|||
|
|
{
|
|||
|
|
lines.AddRange(_allChannels[i].ToArray());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return lines.ToArray();
|
|||
|
|
}
|
|||
|
|
private void AddChannelIfNeeded(int channelNumber)
|
|||
|
|
{
|
|||
|
|
if (!_allChannels.ContainsKey(channelNumber))
|
|||
|
|
{
|
|||
|
|
_allChannels.Add(channelNumber, _channelTemplate.ToList());
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// updates the files in the file with a given value
|
|||
|
|
/// </summary>
|
|||
|
|
public override void UpdateValue(TMTChannelKeysEx key, string value, int channelNumber)
|
|||
|
|
{
|
|||
|
|
AddChannelIfNeeded(channelNumber);
|
|||
|
|
var lines = _allChannels[channelNumber];
|
|||
|
|
var pattern = TMTKey.GetKey(key);
|
|||
|
|
for (int i = 0; i < lines.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (lines[i].Contains(pattern))
|
|||
|
|
{
|
|||
|
|
lines[i] = lines[i].Replace(pattern, value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// updates the fields in the file with a given value
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
public override void UpdateValue(TMTGlobalKeys key, string value)
|
|||
|
|
{
|
|||
|
|
var pattern = TMTKey.GetKey(key);
|
|||
|
|
for (int i = 0; i < _lines.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (_lines[i].Contains(pattern))
|
|||
|
|
{
|
|||
|
|
_lines[i] = _lines[i].Replace(pattern, value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
/// <summary>
|
|||
|
|
/// updates the files in the file with the given value
|
|||
|
|
/// </summary>
|
|||
|
|
/// <param name="key"></param>
|
|||
|
|
/// <param name="value"></param>
|
|||
|
|
/// <param name="channelNumber"></param>
|
|||
|
|
public override void UpdateValue(TMTChannelKeys key, string value, int channelNumber)
|
|||
|
|
{
|
|||
|
|
var pattern = TMTKey.GetKey(key, channelNumber);
|
|||
|
|
for (int i = 0; i < _lines.Count; i++)
|
|||
|
|
{
|
|||
|
|
if (_lines[i].Contains(pattern))
|
|||
|
|
{
|
|||
|
|
_lines[i] = _lines[i].Replace(pattern, value);
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
public TmtSplitFile(string dasTemplate, string channelTemplate)
|
|||
|
|
{
|
|||
|
|
if (System.IO.File.Exists(dasTemplate))
|
|||
|
|
{
|
|||
|
|
_lines.AddRange(System.IO.File.ReadAllLines(dasTemplate));
|
|||
|
|
}
|
|||
|
|
if (System.IO.File.Exists(channelTemplate))
|
|||
|
|
{
|
|||
|
|
_channelTemplate.AddRange(System.IO.File.ReadAllLines(channelTemplate));
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|