-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfoItem.cs
74 lines (69 loc) · 2.6 KB
/
InfoItem.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
namespace agentspy.net
{
class InfoItem
{
public string Name { get; private set; }
private const char ChangeFlag = '`';
public bool JustChanged { get; private set; }
public bool MarkChanges { get; set; }
public bool ChangesOnly { get; set; }
internal string FormatString { get; set; }
internal List<Regex> myPatterns = new List<Regex>() { new Regex(".*") };
public List<string> UndefinedInStatus = new List<string>();
public static string CurrentState;
internal string myValue = "n/a";
public string Value
{
get
{
var v = this.myValue;
this.myValue = this.myValue.TrimEnd(ChangeFlag);
return v;
}
internal set
{
if (0 == string.Compare(myValue.TrimEnd(ChangeFlag), value, StringComparison.InvariantCultureIgnoreCase))
{
JustChanged = false;
return;
}
this.myValue = value + (this.MarkChanges ? ChangeFlag.ToString(CultureInfo.InvariantCulture) : "");
JustChanged = true;
}
}
public override string ToString()
{
return string.Format(FormatString, JustChanged || !ChangesOnly ? Value : "");
}
public InfoItem(string name, string defaultValue = "n/a", string formatString = "{0}", params Regex[] patterns)
{
this.Name = name;
this.myValue = defaultValue;
this.FormatString = formatString;
if (patterns != null) this.myPatterns = patterns.ToList();
this.MarkChanges = true;
this.ChangesOnly = false;
}
/// <summary>
/// matches the given field pattern on a line and returns if something has changed. It returns false as well if the line is not matching.
/// </summary>
/// <param name="line"></param>
public bool Evaluate(string line)
{
var matchingPattern = this.myPatterns.FirstOrDefault(p => p.IsMatch(line));
if (null == matchingPattern) return false;
this.Value = matchingPattern.Match(line).Groups[1].Value;
return JustChanged;
}
public void Reset()
{
this.myValue = "";
this.JustChanged = false;
}
}
}