WIP
DirectOutput framework for virtual pinball cabinets WIP
Go to:
Overview 
FilePattern.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
3 using System.IO;
4 using System.Linq;
5 using System.Xml.Serialization;
6 
7 namespace DirectOutput.General
8 {
12  public class FilePattern : INotifyPropertyChanged, IXmlSerializable
13  {
14 
15 
16  #region IXmlSerializable Member
17  public System.Xml.Schema.XmlSchema GetSchema()
22  {
23  return null;
24  }
25 
30  public void ReadXml(System.Xml.XmlReader reader)
31  {
32  Pattern = reader.ReadString();
33  if (!reader.IsEmptyElement)
34  {
35  reader.ReadEndElement();
36  }
37  else
38  {
39  reader.ReadStartElement();
40  }
41  }
42 
47  public void WriteXml(System.Xml.XmlWriter writer)
48  {
49  writer.WriteString(Pattern);
50  }
51 
52  #endregion
53 
54  #region INotifyPropertyChanged Member
55 
59  public event PropertyChangedEventHandler PropertyChanged;
60 
61  private void NotifyPropertyChanged(string PropertyName)
62  {
63  if (PropertyChanged != null)
64  {
65  PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
66  }
67  }
68 
69  #endregion
70 
71  private string _Pattern="";
72 
79  public string Pattern
80  {
81  get { return _Pattern; }
82  set
83  {
84  if (_Pattern != value)
85  {
86  _Pattern = value;
87  NotifyPropertyChanged("Pattern");
88  }
89  }
90  }
91 
97  public string ReplacePlaceholders(Dictionary<string, string> ReplaceValues = null)
98  {
99  string P = Pattern;
100  if (ReplaceValues != null)
101  {
102  foreach (KeyValuePair<string, string> KV in ReplaceValues)
103  {
104  P = P.Replace("{" + (KV.Key) + "}", KV.Value);
105  }
106  }
107  return P;
108  }
109 
110 
116  public List<FileInfo> GetMatchingFiles(Dictionary<string, string> ReplaceValues = null)
117  {
118  if (Pattern.IsNullOrWhiteSpace()) return new List<FileInfo>();
119 
120  string P = ReplacePlaceholders(ReplaceValues);
121 
122 
123  try
124  {
125 
126  DirectoryInfo FilesDirectory = new DirectoryInfo((Path.GetDirectoryName(P).IsNullOrWhiteSpace() ? "." : Path.GetDirectoryName(P)));
127 
128  if (FilesDirectory.Exists)
129  {
130  return FilesDirectory.GetFiles(Path.GetFileName(P)).ToList<FileInfo>();
131  }
132  else
133  {
134  return new List<FileInfo>();
135  }
136  }
137  catch
138  {
139  return new List<FileInfo>();
140  }
141  }
142 
148  public FileInfo GetFirstMatchingFile(Dictionary<string, string> ReplaceValues = null)
149  {
150  List<FileInfo> L = GetMatchingFiles(ReplaceValues);
151  if (L.Count > 0)
152  {
153  return L[0];
154  }
155  else
156  {
157  return null;
158  }
159  }
160 
161 
168  public bool IsValid
169  {
170  get
171  {
172  if (Pattern.IsNullOrWhiteSpace()) return true;
173 
174  try
175  {
176  FileInfo DummyFileInfo = new FileInfo(Pattern.Replace("*", "test").Replace("?", "X"));
177  }
178  catch
179  {
180  return false;
181  }
182 
183  bool BracketOpen = false;
184  bool JustOpend = false;
185  foreach (char C in Pattern)
186  {
187  switch (C)
188  {
189  case '{':
190  if (BracketOpen) return false;
191  BracketOpen = true;
192  JustOpend = true;
193  break;
194  case '}':
195  if (!BracketOpen) return false;
196  if (JustOpend) return false;
197  BracketOpen = false;
198  JustOpend = false;
199  break;
200  case '\\':
201  case '*':
202  case '?':
203  if (BracketOpen) return false;
204  JustOpend = false;
205  break;
206  default:
207  JustOpend = false;
208  break;
209  }
210  }
211  if (BracketOpen) return false;
212  return true;
213  }
214  }
215 
216 
221  public FilePattern(string Pattern)
222  {
223  if (Pattern != null)
224  {
225  this.Pattern = Pattern;
226  }
227 
228  }
229 
236  public override string ToString()
237  {
238  return Pattern;
239  }
240 
241 
245  public FilePattern()
246  {
247 
248  }
249 
250 
251  }
252 }
FilePattern(string Pattern)
Initializes a new instance of the FilePattern class.
Definition: FilePattern.cs:221
string ReplacePlaceholders(Dictionary< string, string > ReplaceValues=null)
Returns the pattern with replaced placeholders.
Definition: FilePattern.cs:97
void ReadXml(System.Xml.XmlReader reader)
Deserializes the FilePattern in the XmlReader. ReadXml is part of the IXmlSerializable interface...
Definition: FilePattern.cs:30
FileInfo GetFirstMatchingFile(Dictionary< string, string > ReplaceValues=null)
Gets the first file matching the value of the Pattern property.
Definition: FilePattern.cs:148
override string ToString()
Returns a System.String that represents this instance.
Definition: FilePattern.cs:236
List< FileInfo > GetMatchingFiles(Dictionary< string, string > ReplaceValues=null)
Gets the files matching the value of the property Pattern.
Definition: FilePattern.cs:116
void WriteXml(System.Xml.XmlWriter writer)
Serializes the FilePattern to Xml. WriteXml is part of the IXmlSerializable interface.
Definition: FilePattern.cs:47
FilePattern()
Initializes a new instance of the FilePattern class.
Definition: FilePattern.cs:245
PropertyChangedEventHandler PropertyChanged
Is fired if the value of a property changes.
Definition: FilePattern.cs:59
A file pattern class used to lookup files matching a specified pattern.
Definition: FilePattern.cs:12