JD2022-TU1/main/tools/legacy/UA_AtlasEditor/Tools/History.cs

169 lines
6.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace UA_AtlasEditor
{
[Serializable()]
public class History
{
//=========================================================================================
// Constants
//=========================================================================================
//! number max of files in history
private const int c_maxFiles = 15;
//=========================================================================================
// sub classes
//=========================================================================================
//-----------------------------------------------------------------------------------------
//! one history file, i.e. a file and a date
//
public class HistoryFile
{
public string _name;
public DateTime _date;
};
//=========================================================================================
// members
//=========================================================================================
//! list of history files
private readonly List<HistoryFile> _list = new List<HistoryFile>();
//=========================================================================================
// functions
//=========================================================================================
//-----------------------------------------------------------------------------------------
//! add one file to history
//! \params path string to add
//
public void add( string path, DateTime date = default(DateTime) )
{
string lowerPath = path.ToLower();
lowerPath.Replace('\\', '/');
lowerPath.Replace("//", "/");
HistoryFile result = _list.Find(delegate(HistoryFile find) { return find._name.CompareTo(lowerPath) == 0; });
if (result != null)
{
result._date = DateTime.Now;
return;
}
result = new HistoryFile();
result._name = lowerPath;
result._date = (date == default(DateTime)) ? DateTime.Now : date;
_list.Add(result);
checkFiles();
checkCount();
_list.Sort(delegate(HistoryFile history1, HistoryFile history2) { return history1._name.CompareTo(history2._name); });
}
//-----------------------------------------------------------------------------------------
//! get number of history files
//
public int getCount()
{
return _list.Count();
}
//-----------------------------------------------------------------------------------------
//! get file name of history at given index
//! \params index zero based index of wanted history file
//
public string getPath( int index )
{
if (index < 0 || index >= _list.Count()) return null;
return _list[index]._name;
}
//-----------------------------------------------------------------------------------------
//! get file name of history at given index
//! \params index zero based index of wanted history file
//
public DateTime getDate(int index)
{
if (index < 0 || index >= _list.Count()) return DateTime.Now;
return _list[index]._date;
}
//-----------------------------------------------------------------------------------------
//! convert history file list to an history file array (needed for serialization)
//
public HistoryFile[] toHistoryFileArray()
{
if (getCount() == 0) return null;
HistoryFile[] history = new HistoryFile[getCount()];
for (int i = 0; i < getCount(); i++) history[i] = _list[i];
return history;
}
//-----------------------------------------------------------------------------------------
//! convert history file array to history file list (needed for deserialization)
//! \params historyFileArray array of history file
//
public void fromHistoryFileArray(ref HistoryFile[] historyFileArray )
{
_list.Clear();
for (int i = 0; i < historyFileArray.Count(); i++)
_list.Add( historyFileArray[i] );
checkFiles();
checkCount();
}
//-----------------------------------------------------------------------------------------
//! check that files exists, remove invalid file from list
//
private void checkFiles()
{
for (int i = 0; i < getCount();)
{
if (!File.Exists( _list[i]._name ))
_list.RemoveAt( i );
else i++;
}
}
//-----------------------------------------------------------------------------------------
//! check that number of history file do not exceed c_maxFiles
//! remove older element if needed
//
private void checkCount()
{
while (getCount() > c_maxFiles)
{
DateTime olderFileTime = DateTime.Now;
int olderFileIndex = -1;
for (int i = 0; i < getCount(); i++)
{
if (_list[i]._date >= olderFileTime) continue;
olderFileTime = _list[i]._date;
olderFileIndex = i;
}
if (olderFileIndex == -1) break;
_list.RemoveAt(olderFileIndex);
}
}
//-----------------------------------------------------------------------------------------
//! sort file by date
//
public void sortByDate()
{
_list.Sort( delegate(HistoryFile h1, HistoryFile h2) { return h2._date.CompareTo(h1._date); } );
}
}
}