using System.Collections.Generic;
using System.Linq;
namespace DTS.Common.Classes.TMAT
{
///
/// this class handles a TMATS template split into two files
///
public class TmtSplitFile : TmtBase
{
private readonly List _lines = new List();
private readonly Dictionary> _allChannels = new Dictionary>();
private readonly List _channelTemplate = new List();
public override string[] GetAllLines()
{
if (!_allChannels.Any()) { return _lines.ToArray(); }
var lines = new List(_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());
}
}
///
/// updates the files in the file with a given value
///
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);
}
}
}
///
/// updates the fields in the file with a given value
///
///
///
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);
}
}
}
///
/// updates the files in the file with the given value
///
///
///
///
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));
}
}
}
}