DirectOutputR1
DirectOutput framework R1 for virtual pinball cabinets.
Go to:
Overview 
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Properties Events Macros Pages
FilePattern.cs
Go to the documentation of this file.
1 using System.Collections.Generic;
2 using System.ComponentModel;
3 using System.IO;
4 using System.Linq;
5 using System.Xml.Serialization;
6 
7 namespace DirectOutput.GlobalConfiguration
8 {
12  public class FilePattern : INotifyPropertyChanged, IXmlSerializable
13  {
14 
15 
16  #region IXmlSerializable Member
17 
18 
19 
20 
21  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  try
123  {
124 
125  DirectoryInfo FilesDirectory = new DirectoryInfo((Path.GetDirectoryName(P).IsNullOrWhiteSpace() ? "." : Path.GetDirectoryName(P)));
126  if (FilesDirectory.Exists)
127  {
128  return FilesDirectory.GetFiles(Path.GetFileName(P)).ToList<FileInfo>();
129  }
130  else
131  {
132  return new List<FileInfo>();
133  }
134  }
135  catch
136  {
137  return new List<FileInfo>();
138  }
139  }
140 
146  public FileInfo GetFirstMatchingFile(Dictionary<string, string> ReplaceValues = null)
147  {
148  List<FileInfo> L = GetMatchingFiles(ReplaceValues);
149  if (L.Count > 0)
150  {
151  return L[0];
152  }
153  else
154  {
155  return null;
156  }
157  }
158 
159 
166  public bool IsValid
167  {
168  get
169  {
170  if (Pattern.IsNullOrWhiteSpace()) return true;
171 
172  try
173  {
174  FileInfo DummyFileInfo = new FileInfo(Pattern.Replace("*", "test").Replace("?", "X"));
175  }
176  catch
177  {
178  return false;
179  }
180 
181  bool BracketOpen = false;
182  bool JustOpend = false;
183  foreach (char C in Pattern)
184  {
185  switch (C)
186  {
187  case '{':
188  if (BracketOpen) return false;
189  BracketOpen = true;
190  JustOpend = true;
191  break;
192  case '}':
193  if (!BracketOpen) return false;
194  if (JustOpend) return false;
195  BracketOpen = false;
196  JustOpend = false;
197  break;
198  case '\\':
199  case '*':
200  case '?':
201  if (BracketOpen) return false;
202  JustOpend = false;
203  break;
204  default:
205  JustOpend = false;
206  break;
207  }
208  }
209  if (BracketOpen) return false;
210  return true;
211  }
212  }
213 
214 
219  public FilePattern(string Pattern)
220  {
221  if (Pattern != null)
222  {
223  this.Pattern = Pattern;
224  }
225 
226  }
227 
234  public override string ToString()
235  {
236  return Pattern;
237  }
238 
239 
243  public FilePattern()
244  {
245 
246  }
247 
248 
249  }
250 }