source: xmlTools/trunk/posUpdate/Util.cs@ 1043

Last change on this file since 1043 was 745, checked in by s10k, 12 years ago

-added double quotes support (windows users can now specify paths with spaces)
-returning last error as output (helps AEI debugging)

File size: 5.8 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.IO;
4using System.Text;
5using System.Text.RegularExpressions;
6using System.Xml;
7
8namespace xmlTools
9{
10 /// <summary>
11 /// Utilities class. Contain function used along all the program and classes.
12 /// </summary>
13 class Util
14 {
15 /// <summary>
16 /// Backup a file, return true if sucessful otherwise returns false
17 /// </summary>
18 /// <param name="file"></param>
19 static public bool backupFile(string file)
20 {
21 try
22 {
23 System.IO.File.Copy(file, file + ".bak", false); //make a backup first //false is to not override already created backups
24 }
25 catch (Exception e)
26 {
27 Program.printAppError(Program.appErrors.BACKUPS_ALREADY_EXISTS, "Couldn't backup file " + file + " :\n" + e.Message);
28 return false;
29 }
30 return true;
31 }
32
33 /// <summary>
34 /// Get all elements with a determined name and optionally a parent name. Result is saved on the paremeter list "result"
35 /// </summary>
36 /// <param name="rootNode"></param>
37 /// <param name="result"></param>
38 /// <param name="posElement"></param>
39 /// <param name="posParentElement"></param>
40 public static void getAllSpecificElements(XmlNode rootNode, ref List<XmlNode> result, String posElement, String posParentElement = "")
41 {
42 foreach (XmlNode element in rootNode.ChildNodes)
43 {
44 if (element.Name == posElement && (posParentElement == "" || posParentElement == element.ParentNode.Name))
45 {
46 result.Add(element);
47 continue;
48 }
49 getAllSpecificElements(element, ref result, posElement, posParentElement); //If not found in this node continue search in subnodes
50 }
51 }
52
53 /// <summary>
54 /// Converts a string to a xmlNode. Throws XmlException if the parsing of xml fails.
55 /// </summary>
56 /// <param name="xmlContent"></param>
57 /// <returns></returns>
58 public static XmlNode stringToXmlNode(string xmlContent)
59 {
60 XmlDocument doc = new XmlDocument();
61 try
62 {
63 doc.LoadXml(xmlContent);
64 }
65 catch (XmlException e)
66 {
67 throw e;
68 }
69 return doc.DocumentElement;
70 }
71
72 /// <summary>
73 /// Gets all xml files in the same directory of the executable
74 /// </summary>
75 /// <returns></returns>
76 static public List<String> getAllXmlFiles()
77 {
78 return getXmlFilesWildcard("*.xml");
79 }
80
81 /// <summary>
82 /// Converts wildcard to regex and uses it to make the match
83 /// </summary>
84 /// <param name="filewildcard"></param>
85 /// <returns></returns>
86 public static List<String> getXmlFilesWildcard(String filewildcard)
87 {
88 List<String> xmlFiles = new List<String>();
89
90 String dir = Path.GetDirectoryName(filewildcard); // Get the specified directory
91 if (dir == "")
92 {
93 dir = Util.getExePath();
94 }
95 String wildcard = Path.GetFileName(filewildcard); // Get files/wildcard
96 String[] files = System.IO.Directory.GetFiles(dir); //Get all files in specified directory
97
98 foreach (String file in files)
99 {
100 Regex wildcardRegex = new Regex(Util.WildcardToRegex(wildcard), RegexOptions.IgnoreCase); //case insensitivity
101 if (wildcardRegex.IsMatch(Path.GetFileName(file)))
102 {
103 xmlFiles.Add(file);
104 }
105 }
106
107 return xmlFiles;
108 }
109
110 /// <summary>
111 /// Converts wildcard to regex (from here: http://www.codeproject.com/Articles/11556/Converting-Wildcards-to-Regexes)
112 /// </summary>
113 /// <param name="pattern"></param>
114 /// <returns></returns>
115 private static string WildcardToRegex(string pattern)
116 {
117 return "^" + Regex.Escape(pattern).
118 Replace("\\*", ".*").
119 Replace("\\?", ".") + "$";
120 }
121
122 /// <summary>
123 /// Check when a string contains a wildcard or not
124 /// </summary>
125 /// <param name="myString"></param>
126 /// <returns></returns>
127 public static bool containsWildcard(String myString)
128 {
129 return (myString.Contains("*") || myString.Contains("?"));
130 }
131
132 public static string getExePath()
133 {
134 return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
135 }
136
137 public static string getExeFileName()
138 {
139 return Environment.GetCommandLineArgs()[0];
140 }
141
142 public static bool IsRunningOnMono()
143 {
144 return Type.GetType("Mono.Runtime") != null;
145 }
146
147 public static bool ContainsIgnoreCase(string source, string sToSearch)
148 {
149 return source.IndexOf(sToSearch, StringComparison.OrdinalIgnoreCase) >= 0;
150 }
151
152 // Thanks DarthDevilous for the regex! http://forums.thedailywtf.com/forums/t/2478.aspx
153 public static string[] stringToArgsArray(string args)
154 {
155 MatchCollection ms = Regex.Matches(args, "([^\" ]*(\"[^\"]*\")[^\" ]*)|[^\" ]+");
156 List<string> listArgs=new List<string>();
157 foreach (Match m in ms)
158 {
159 listArgs.Add(m.Value.Replace("\"","")); //remove quotes or it will cause an error
160 }
161 return listArgs.ToArray();
162 }
163 }
164}
Note: See TracBrowser for help on using the repository browser.