diff options
author | MW | 2007-05-26 13:40:19 +0000 |
---|---|---|
committer | MW | 2007-05-26 13:40:19 +0000 |
commit | 3436961bb5c01d659d09be134368f4f69460cef9 (patch) | |
tree | 3753ba4d7818df2a6bce0bbe863ff033cdfd568a /Prebuild/src/Core/Utilities | |
download | opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.zip opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.tar.gz opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.tar.bz2 opensim-SC-3436961bb5c01d659d09be134368f4f69460cef9.tar.xz |
Start of rewrite 5279!
Diffstat (limited to 'Prebuild/src/Core/Utilities')
-rw-r--r-- | Prebuild/src/Core/Utilities/CommandLineCollection.cs | 162 | ||||
-rw-r--r-- | Prebuild/src/Core/Utilities/CurrentDirectory.cs | 89 | ||||
-rw-r--r-- | Prebuild/src/Core/Utilities/Helper.cs | 661 | ||||
-rw-r--r-- | Prebuild/src/Core/Utilities/Log.cs | 279 |
4 files changed, 1191 insertions, 0 deletions
diff --git a/Prebuild/src/Core/Utilities/CommandLineCollection.cs b/Prebuild/src/Core/Utilities/CommandLineCollection.cs new file mode 100644 index 0000000..496731f --- /dev/null +++ b/Prebuild/src/Core/Utilities/CommandLineCollection.cs | |||
@@ -0,0 +1,162 @@ | |||
1 | #region BSD License | ||
2 | /* | ||
3 | Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without modification, are permitted | ||
6 | provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
9 | and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
11 | and the following disclaimer in the documentation and/or other materials provided with the | ||
12 | distribution. | ||
13 | * The name of the author may not be used to endorse or promote products derived from this software | ||
14 | without specific prior written permission. | ||
15 | |||
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
21 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
22 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
23 | */ | ||
24 | #endregion | ||
25 | |||
26 | #region CVS Information | ||
27 | /* | ||
28 | * $Source$ | ||
29 | * $Author: robloach $ | ||
30 | * $Date: 2006-09-26 00:30:53 +0200 (ti, 26 sep 2006) $ | ||
31 | * $Revision: 165 $ | ||
32 | */ | ||
33 | #endregion | ||
34 | |||
35 | using System; | ||
36 | using System.Collections; | ||
37 | using System.Collections.Specialized; | ||
38 | using System.Diagnostics; | ||
39 | |||
40 | namespace Prebuild.Core.Utilities | ||
41 | { | ||
42 | /// <summary> | ||
43 | /// The CommandLine class parses and interprets the command-line arguments passed to | ||
44 | /// prebuild. | ||
45 | /// </summary> | ||
46 | public class CommandLineCollection | ||
47 | { | ||
48 | #region Fields | ||
49 | |||
50 | // The raw OS arguments | ||
51 | private string[] m_RawArgs; | ||
52 | |||
53 | // Command-line argument storage | ||
54 | private Hashtable m_Arguments; | ||
55 | |||
56 | #endregion | ||
57 | |||
58 | #region Constructors | ||
59 | |||
60 | /// <summary> | ||
61 | /// Create a new CommandLine instance and set some internal variables. | ||
62 | /// </summary> | ||
63 | public CommandLineCollection(string[] args) | ||
64 | { | ||
65 | m_RawArgs = args; | ||
66 | m_Arguments = new Hashtable(); | ||
67 | |||
68 | Parse(); | ||
69 | } | ||
70 | |||
71 | #endregion | ||
72 | |||
73 | #region Private Methods | ||
74 | |||
75 | private void Parse() | ||
76 | { | ||
77 | if(m_RawArgs.Length < 1) | ||
78 | return; | ||
79 | |||
80 | int idx = 0; | ||
81 | string arg = null, lastArg = null; | ||
82 | |||
83 | while(idx <m_RawArgs.Length) | ||
84 | { | ||
85 | arg = m_RawArgs[idx]; | ||
86 | |||
87 | if(arg.Length > 2 && arg[0] == '/') | ||
88 | { | ||
89 | arg = arg.Substring(1); | ||
90 | lastArg = arg; | ||
91 | m_Arguments[arg] = ""; | ||
92 | } | ||
93 | else | ||
94 | { | ||
95 | if(lastArg != null) | ||
96 | { | ||
97 | m_Arguments[lastArg] = arg; | ||
98 | lastArg = null; | ||
99 | } | ||
100 | } | ||
101 | |||
102 | idx++; | ||
103 | } | ||
104 | } | ||
105 | |||
106 | #endregion | ||
107 | |||
108 | #region Public Methods | ||
109 | |||
110 | /// <summary> | ||
111 | /// Wases the passed. | ||
112 | /// </summary> | ||
113 | /// <param name="arg">The arg.</param> | ||
114 | /// <returns></returns> | ||
115 | public bool WasPassed(string arg) | ||
116 | { | ||
117 | return (m_Arguments.ContainsKey(arg)); | ||
118 | } | ||
119 | |||
120 | #endregion | ||
121 | |||
122 | #region Properties | ||
123 | |||
124 | /// <summary> | ||
125 | /// Gets the parameter associated with the command line option | ||
126 | /// </summary> | ||
127 | /// <remarks>Returns null if option was not specified, | ||
128 | /// null string if no parameter was specified, and the value if a parameter was specified</remarks> | ||
129 | public string this[string index] | ||
130 | { | ||
131 | get | ||
132 | { | ||
133 | if(m_Arguments.ContainsKey(index)) | ||
134 | { | ||
135 | return (string)(m_Arguments[index]); | ||
136 | } | ||
137 | else | ||
138 | { | ||
139 | return null; | ||
140 | } | ||
141 | } | ||
142 | } | ||
143 | |||
144 | #endregion | ||
145 | |||
146 | #region IEnumerable Members | ||
147 | |||
148 | /// <summary> | ||
149 | /// Returns an enumerator that can iterate through a collection. | ||
150 | /// </summary> | ||
151 | /// <returns> | ||
152 | /// An <see cref="T:System.Collections.IDictionaryEnumerator"/> | ||
153 | /// that can be used to iterate through the collection. | ||
154 | /// </returns> | ||
155 | public IDictionaryEnumerator GetEnumerator() | ||
156 | { | ||
157 | return m_Arguments.GetEnumerator(); | ||
158 | } | ||
159 | |||
160 | #endregion | ||
161 | } | ||
162 | } | ||
diff --git a/Prebuild/src/Core/Utilities/CurrentDirectory.cs b/Prebuild/src/Core/Utilities/CurrentDirectory.cs new file mode 100644 index 0000000..a76d844 --- /dev/null +++ b/Prebuild/src/Core/Utilities/CurrentDirectory.cs | |||
@@ -0,0 +1,89 @@ | |||
1 | #region BSD License | ||
2 | /* | ||
3 | Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without modification, are permitted | ||
6 | provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
9 | and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
11 | and the following disclaimer in the documentation and/or other materials provided with the | ||
12 | distribution. | ||
13 | * The name of the author may not be used to endorse or promote products derived from this software | ||
14 | without specific prior written permission. | ||
15 | |||
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
21 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
22 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
23 | */ | ||
24 | #endregion | ||
25 | |||
26 | #region CVS Information | ||
27 | /* | ||
28 | * $Source$ | ||
29 | * $Author: jendave $ | ||
30 | * $Date: 2006-01-28 01:49:58 +0100 (lö, 28 jan 2006) $ | ||
31 | * $Revision: 71 $ | ||
32 | */ | ||
33 | #endregion | ||
34 | |||
35 | using System; | ||
36 | using System.Collections; | ||
37 | |||
38 | namespace Prebuild.Core.Utilities | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// | ||
42 | /// </summary> | ||
43 | public class CurrentDirectory | ||
44 | { | ||
45 | #region Fields | ||
46 | |||
47 | private Stack m_Stack; | ||
48 | |||
49 | #endregion | ||
50 | |||
51 | #region Constructors | ||
52 | |||
53 | /// <summary> | ||
54 | /// Initializes a new instance of the <see cref="CurrentDirectory"/> class. | ||
55 | /// </summary> | ||
56 | public CurrentDirectory() | ||
57 | { | ||
58 | m_Stack = new Stack(); | ||
59 | } | ||
60 | |||
61 | #endregion | ||
62 | |||
63 | #region Public Methods | ||
64 | |||
65 | /// <summary> | ||
66 | /// Pushes this instance. | ||
67 | /// </summary> | ||
68 | public void Push() | ||
69 | { | ||
70 | m_Stack.Push(Environment.CurrentDirectory); | ||
71 | } | ||
72 | |||
73 | /// <summary> | ||
74 | /// Pops this instance. | ||
75 | /// </summary> | ||
76 | public void Pop() | ||
77 | { | ||
78 | if(m_Stack.Count < 1) | ||
79 | { | ||
80 | return; | ||
81 | } | ||
82 | |||
83 | string cwd = (string)m_Stack.Pop(); | ||
84 | Helper.SetCurrentDir(cwd); | ||
85 | } | ||
86 | |||
87 | #endregion | ||
88 | } | ||
89 | } | ||
diff --git a/Prebuild/src/Core/Utilities/Helper.cs b/Prebuild/src/Core/Utilities/Helper.cs new file mode 100644 index 0000000..33c9618 --- /dev/null +++ b/Prebuild/src/Core/Utilities/Helper.cs | |||
@@ -0,0 +1,661 @@ | |||
1 | #region BSD License | ||
2 | /* | ||
3 | Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without modification, are permitted | ||
6 | provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
9 | and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
11 | and the following disclaimer in the documentation and/or other materials provided with the | ||
12 | distribution. | ||
13 | * The name of the author may not be used to endorse or promote products derived from this software | ||
14 | without specific prior written permission. | ||
15 | |||
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
21 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
22 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
23 | */ | ||
24 | #endregion | ||
25 | |||
26 | #region CVS Information | ||
27 | /* | ||
28 | * $Source$ | ||
29 | * $Author: jendave $ | ||
30 | * $Date: 2007-02-13 21:58:03 +0100 (ti, 13 feb 2007) $ | ||
31 | * $Revision: 205 $ | ||
32 | */ | ||
33 | #endregion | ||
34 | |||
35 | using System; | ||
36 | using System.Collections; | ||
37 | using System.Diagnostics; | ||
38 | using System.IO; | ||
39 | using System.Runtime.InteropServices; | ||
40 | using System.Text.RegularExpressions; | ||
41 | using System.Collections.Specialized; | ||
42 | using System.Xml; | ||
43 | using Prebuild.Core.Nodes; | ||
44 | |||
45 | namespace Prebuild.Core.Utilities | ||
46 | { | ||
47 | /// <summary> | ||
48 | /// | ||
49 | /// </summary> | ||
50 | public class Helper | ||
51 | { | ||
52 | #region Fields | ||
53 | |||
54 | private static Stack dirStack; | ||
55 | private static Regex varRegex; | ||
56 | static bool checkForOSVariables; | ||
57 | |||
58 | /// <summary> | ||
59 | /// | ||
60 | /// </summary> | ||
61 | public static bool CheckForOSVariables | ||
62 | { | ||
63 | get | ||
64 | { | ||
65 | return checkForOSVariables; | ||
66 | } | ||
67 | set | ||
68 | { | ||
69 | checkForOSVariables = value; | ||
70 | } | ||
71 | } | ||
72 | |||
73 | #endregion | ||
74 | |||
75 | #region Constructors | ||
76 | |||
77 | /// <summary> | ||
78 | /// Initializes the <see cref="Helper"/> class. | ||
79 | /// </summary> | ||
80 | static Helper() | ||
81 | { | ||
82 | dirStack = new Stack(); | ||
83 | //m_VarRegex = new Regex(@"\${(?<var>[\w|_]+)}"); | ||
84 | } | ||
85 | |||
86 | #endregion | ||
87 | |||
88 | #region Properties | ||
89 | |||
90 | /// <summary> | ||
91 | /// | ||
92 | /// </summary> | ||
93 | public static Stack DirStack | ||
94 | { | ||
95 | get | ||
96 | { | ||
97 | return dirStack; | ||
98 | } | ||
99 | } | ||
100 | |||
101 | /// <summary> | ||
102 | /// | ||
103 | /// </summary> | ||
104 | public static Regex VarRegex | ||
105 | { | ||
106 | get | ||
107 | { | ||
108 | return varRegex; | ||
109 | } | ||
110 | set | ||
111 | { | ||
112 | varRegex = value; | ||
113 | } | ||
114 | } | ||
115 | |||
116 | #endregion | ||
117 | |||
118 | #region Public Methods | ||
119 | |||
120 | #region String Parsing | ||
121 | #region Inner Classes and Delegates | ||
122 | /// <summary> | ||
123 | /// | ||
124 | /// </summary> | ||
125 | public delegate string StringLookup(string key); | ||
126 | |||
127 | #endregion | ||
128 | |||
129 | /// <summary> | ||
130 | /// Gets a collection of StringLocationPair objects that represent the matches | ||
131 | /// </summary> | ||
132 | /// <param name="target">The target.</param> | ||
133 | /// <param name="beforeGroup">The before group.</param> | ||
134 | /// <param name="afterGroup">The after group.</param> | ||
135 | /// <param name="includeDelimitersInSubstrings">if set to <c>true</c> [include delimiters in substrings].</param> | ||
136 | /// <returns></returns> | ||
137 | public static StringCollection FindGroups(string target, string beforeGroup, string afterGroup, bool includeDelimitersInSubstrings) | ||
138 | { | ||
139 | if( beforeGroup == null ) | ||
140 | { | ||
141 | throw new ArgumentNullException("beforeGroup"); | ||
142 | } | ||
143 | if( afterGroup == null ) | ||
144 | { | ||
145 | throw new ArgumentNullException("afterGroup"); | ||
146 | } | ||
147 | StringCollection results = new StringCollection(); | ||
148 | if(target == null || target.Length == 0) | ||
149 | { | ||
150 | return results; | ||
151 | } | ||
152 | |||
153 | int beforeMod = 0; | ||
154 | int afterMod = 0; | ||
155 | if(includeDelimitersInSubstrings) | ||
156 | { | ||
157 | //be sure to not exlude the delims | ||
158 | beforeMod = beforeGroup.Length; | ||
159 | afterMod = afterGroup.Length; | ||
160 | } | ||
161 | int startIndex = 0; | ||
162 | while((startIndex = target.IndexOf(beforeGroup,startIndex)) != -1) { | ||
163 | int endIndex = target.IndexOf(afterGroup,startIndex);//the index of the char after it | ||
164 | if(endIndex == -1) | ||
165 | { | ||
166 | break; | ||
167 | } | ||
168 | int length = endIndex - startIndex - beforeGroup.Length;//move to the first char in the string | ||
169 | string substring = substring = target.Substring(startIndex + beforeGroup.Length - beforeMod, | ||
170 | length - afterMod); | ||
171 | |||
172 | results.Add(substring); | ||
173 | //results.Add(new StringLocationPair(substring,startIndex)); | ||
174 | startIndex = endIndex + 1; | ||
175 | //the Interpolate*() methods will not work if expressions are expandded inside expression due to an optimization | ||
176 | //so start after endIndex | ||
177 | |||
178 | } | ||
179 | return results; | ||
180 | } | ||
181 | |||
182 | /// <summary> | ||
183 | /// Replaces the groups. | ||
184 | /// </summary> | ||
185 | /// <param name="target">The target.</param> | ||
186 | /// <param name="beforeGroup">The before group.</param> | ||
187 | /// <param name="afterGroup">The after group.</param> | ||
188 | /// <param name="lookup">The lookup.</param> | ||
189 | /// <returns></returns> | ||
190 | public static string ReplaceGroups(string target, string beforeGroup, string afterGroup, StringLookup lookup) { | ||
191 | if( target == null ) | ||
192 | { | ||
193 | throw new ArgumentNullException("target"); | ||
194 | } | ||
195 | //int targetLength = target.Length; | ||
196 | StringCollection strings = FindGroups(target,beforeGroup,afterGroup,false); | ||
197 | if( lookup == null ) | ||
198 | { | ||
199 | throw new ArgumentNullException("lookup"); | ||
200 | } | ||
201 | foreach(string substring in strings) | ||
202 | { | ||
203 | target = target.Replace(beforeGroup + substring + afterGroup, lookup(substring) ); | ||
204 | } | ||
205 | return target; | ||
206 | } | ||
207 | |||
208 | /// <summary> | ||
209 | /// Replaces ${var} statements in a string with the corresonding values as detirmined by the lookup delegate | ||
210 | /// </summary> | ||
211 | /// <param name="target">The target.</param> | ||
212 | /// <param name="lookup">The lookup.</param> | ||
213 | /// <returns></returns> | ||
214 | public static string InterpolateForVariables(string target, StringLookup lookup) | ||
215 | { | ||
216 | return ReplaceGroups(target, "${" , "}" , lookup); | ||
217 | } | ||
218 | |||
219 | /// <summary> | ||
220 | /// Replaces ${var} statements in a string with the corresonding environment variable with name var | ||
221 | /// </summary> | ||
222 | /// <param name="target"></param> | ||
223 | /// <returns></returns> | ||
224 | public static string InterpolateForEnvironmentVariables(string target) | ||
225 | { | ||
226 | return InterpolateForVariables(target, new StringLookup(Environment.GetEnvironmentVariable)); | ||
227 | } | ||
228 | |||
229 | #endregion | ||
230 | |||
231 | /// <summary> | ||
232 | /// Translates the value. | ||
233 | /// </summary> | ||
234 | /// <param name="translateType">Type of the translate.</param> | ||
235 | /// <param name="translationItem">The translation item.</param> | ||
236 | /// <returns></returns> | ||
237 | public static object TranslateValue(Type translateType, string translationItem) | ||
238 | { | ||
239 | if(translationItem == null) | ||
240 | { | ||
241 | return null; | ||
242 | } | ||
243 | |||
244 | try | ||
245 | { | ||
246 | string lowerVal = translationItem.ToLower(); | ||
247 | if(translateType == typeof(bool)) | ||
248 | { | ||
249 | return (lowerVal == "true" || lowerVal == "1" || lowerVal == "y" || lowerVal == "yes" || lowerVal == "on"); | ||
250 | } | ||
251 | else if(translateType == typeof(int)) | ||
252 | { | ||
253 | return (Int32.Parse(translationItem)); | ||
254 | } | ||
255 | else | ||
256 | { | ||
257 | return translationItem; | ||
258 | } | ||
259 | } | ||
260 | catch(FormatException) | ||
261 | { | ||
262 | return null; | ||
263 | } | ||
264 | } | ||
265 | |||
266 | /// <summary> | ||
267 | /// Deletes if exists. | ||
268 | /// </summary> | ||
269 | /// <param name="file">The file.</param> | ||
270 | /// <returns></returns> | ||
271 | public static bool DeleteIfExists(string file) | ||
272 | { | ||
273 | string resFile = null; | ||
274 | try | ||
275 | { | ||
276 | resFile = ResolvePath(file); | ||
277 | } | ||
278 | catch(ArgumentException) | ||
279 | { | ||
280 | return false; | ||
281 | } | ||
282 | |||
283 | if(!File.Exists(resFile)) | ||
284 | { | ||
285 | return false; | ||
286 | } | ||
287 | |||
288 | File.Delete(resFile); | ||
289 | return true; | ||
290 | } | ||
291 | |||
292 | // This little gem was taken from the NeL source, thanks guys! | ||
293 | /// <summary> | ||
294 | /// Makes a relative path | ||
295 | /// </summary> | ||
296 | /// <param name="startPath">Path to start from</param> | ||
297 | /// <param name="endPath">Path to end at</param> | ||
298 | /// <returns>Path that will get from startPath to endPath</returns> | ||
299 | public static string MakePathRelativeTo(string startPath, string endPath) | ||
300 | { | ||
301 | string tmp = NormalizePath(startPath, '/'); | ||
302 | string src = NormalizePath(endPath, '/'); | ||
303 | string prefix = ""; | ||
304 | |||
305 | while(true) | ||
306 | { | ||
307 | if((String.Compare(tmp, 0, src, 0, tmp.Length) == 0)) | ||
308 | { | ||
309 | string ret; | ||
310 | int size = tmp.Length; | ||
311 | if(size == src.Length) | ||
312 | { | ||
313 | return "./"; | ||
314 | } | ||
315 | if ((src.Length > tmp.Length) && src[tmp.Length-1] != '/' && src[tmp.Length-1] != '\\') | ||
316 | { | ||
317 | } | ||
318 | else | ||
319 | { | ||
320 | ret = prefix + endPath.Substring(size, endPath.Length - size); | ||
321 | ret = ret.Trim(); | ||
322 | if(ret[0] == '/' || ret[0] == '\\') | ||
323 | { | ||
324 | ret = "." + ret; | ||
325 | } | ||
326 | |||
327 | return NormalizePath(ret); | ||
328 | } | ||
329 | |||
330 | } | ||
331 | |||
332 | if(tmp.Length < 2) | ||
333 | { | ||
334 | break; | ||
335 | } | ||
336 | |||
337 | int lastPos = tmp.LastIndexOf('/', tmp.Length - 2); | ||
338 | int prevPos = tmp.IndexOf('/'); | ||
339 | |||
340 | if((lastPos == prevPos) || (lastPos == -1)) | ||
341 | { | ||
342 | break; | ||
343 | } | ||
344 | |||
345 | tmp = tmp.Substring(0, lastPos + 1); | ||
346 | prefix += "../"; | ||
347 | } | ||
348 | |||
349 | return endPath; | ||
350 | } | ||
351 | |||
352 | /// <summary> | ||
353 | /// Resolves the path. | ||
354 | /// </summary> | ||
355 | /// <param name="path">The path.</param> | ||
356 | /// <returns></returns> | ||
357 | public static string ResolvePath(string path) | ||
358 | { | ||
359 | string tmpPath = NormalizePath(path); | ||
360 | if(tmpPath.Length < 1) | ||
361 | { | ||
362 | tmpPath = "."; | ||
363 | } | ||
364 | |||
365 | tmpPath = Path.GetFullPath(tmpPath); | ||
366 | if(!File.Exists(tmpPath) && !Directory.Exists(tmpPath)) | ||
367 | { | ||
368 | throw new ArgumentException("Path could not be resolved: " + tmpPath); | ||
369 | } | ||
370 | |||
371 | return tmpPath; | ||
372 | } | ||
373 | |||
374 | /// <summary> | ||
375 | /// Normalizes the path. | ||
376 | /// </summary> | ||
377 | /// <param name="path">The path.</param> | ||
378 | /// <param name="separatorCharacter">The separator character.</param> | ||
379 | /// <returns></returns> | ||
380 | public static string NormalizePath(string path, char separatorCharacter) | ||
381 | { | ||
382 | if(path == null || path == "" || path.Length < 1) | ||
383 | { | ||
384 | return ""; | ||
385 | } | ||
386 | |||
387 | string tmpPath = path.Replace('\\', '/'); | ||
388 | tmpPath = tmpPath.Replace('/', separatorCharacter); | ||
389 | return tmpPath; | ||
390 | } | ||
391 | |||
392 | /// <summary> | ||
393 | /// Normalizes the path. | ||
394 | /// </summary> | ||
395 | /// <param name="path">The path.</param> | ||
396 | /// <returns></returns> | ||
397 | public static string NormalizePath(string path) | ||
398 | { | ||
399 | return NormalizePath(path, Path.DirectorySeparatorChar); | ||
400 | } | ||
401 | |||
402 | /// <summary> | ||
403 | /// Ends the path. | ||
404 | /// </summary> | ||
405 | /// <param name="path">The path.</param> | ||
406 | /// <param name="separatorCharacter">The separator character.</param> | ||
407 | /// <returns></returns> | ||
408 | public static string EndPath(string path, char separatorCharacter) | ||
409 | { | ||
410 | if(path == null || path == "" || path.Length < 1) | ||
411 | { | ||
412 | return ""; | ||
413 | } | ||
414 | |||
415 | if(!path.EndsWith(separatorCharacter.ToString())) | ||
416 | { | ||
417 | return (path + separatorCharacter); | ||
418 | } | ||
419 | |||
420 | return path; | ||
421 | } | ||
422 | |||
423 | /// <summary> | ||
424 | /// Ends the path. | ||
425 | /// </summary> | ||
426 | /// <param name="path">The path.</param> | ||
427 | /// <returns></returns> | ||
428 | public static string EndPath(string path) | ||
429 | { | ||
430 | return EndPath(path, Path.DirectorySeparatorChar); | ||
431 | } | ||
432 | |||
433 | /// <summary> | ||
434 | /// Makes the file path. | ||
435 | /// </summary> | ||
436 | /// <param name="path">The path.</param> | ||
437 | /// <param name="name">The name.</param> | ||
438 | /// <param name="ext">The ext.</param> | ||
439 | /// <returns></returns> | ||
440 | public static string MakeFilePath(string path, string name, string ext) | ||
441 | { | ||
442 | string ret = EndPath(NormalizePath(path)); | ||
443 | |||
444 | if( name == null ) | ||
445 | { | ||
446 | throw new ArgumentNullException("name"); | ||
447 | } | ||
448 | |||
449 | ret += name; | ||
450 | if(!name.EndsWith("." + ext)) | ||
451 | { | ||
452 | ret += "." + ext; | ||
453 | } | ||
454 | |||
455 | //foreach(char c in Path.GetInvalidPathChars()) | ||
456 | //{ | ||
457 | // ret = ret.Replace(c, '_'); | ||
458 | //} | ||
459 | |||
460 | return ret; | ||
461 | } | ||
462 | |||
463 | /// <summary> | ||
464 | /// Makes the file path. | ||
465 | /// </summary> | ||
466 | /// <param name="path">The path.</param> | ||
467 | /// <param name="name">The name.</param> | ||
468 | /// <returns></returns> | ||
469 | public static string MakeFilePath(string path, string name) | ||
470 | { | ||
471 | string ret = EndPath(NormalizePath(path)); | ||
472 | |||
473 | if( name == null ) | ||
474 | { | ||
475 | throw new ArgumentNullException("name"); | ||
476 | } | ||
477 | |||
478 | ret += name; | ||
479 | |||
480 | //foreach (char c in Path.GetInvalidPathChars()) | ||
481 | //{ | ||
482 | // ret = ret.Replace(c, '_'); | ||
483 | //} | ||
484 | |||
485 | return ret; | ||
486 | } | ||
487 | |||
488 | /// <summary> | ||
489 | /// | ||
490 | /// </summary> | ||
491 | /// <param name="path"></param> | ||
492 | /// <returns></returns> | ||
493 | public static string MakeReferencePath(string path) | ||
494 | { | ||
495 | string ret = EndPath(NormalizePath(path)); | ||
496 | |||
497 | //foreach (char c in Path.GetInvalidPathChars()) | ||
498 | //{ | ||
499 | // ret = ret.Replace(c, '_'); | ||
500 | //} | ||
501 | |||
502 | return ret; | ||
503 | } | ||
504 | |||
505 | /// <summary> | ||
506 | /// Sets the current dir. | ||
507 | /// </summary> | ||
508 | /// <param name="path">The path.</param> | ||
509 | public static void SetCurrentDir(string path) | ||
510 | { | ||
511 | if( path == null ) | ||
512 | { | ||
513 | throw new ArgumentNullException("path"); | ||
514 | } | ||
515 | if(path.Length < 1) | ||
516 | { | ||
517 | return; | ||
518 | } | ||
519 | |||
520 | Environment.CurrentDirectory = path; | ||
521 | } | ||
522 | |||
523 | /// <summary> | ||
524 | /// Checks the type. | ||
525 | /// </summary> | ||
526 | /// <param name="typeToCheck">The type to check.</param> | ||
527 | /// <param name="attr">The attr.</param> | ||
528 | /// <param name="inter">The inter.</param> | ||
529 | /// <returns></returns> | ||
530 | public static object CheckType(Type typeToCheck, Type attr, Type inter) | ||
531 | { | ||
532 | if(typeToCheck == null || attr == null) | ||
533 | { | ||
534 | return null; | ||
535 | } | ||
536 | |||
537 | object[] attrs = typeToCheck.GetCustomAttributes(attr, false); | ||
538 | if(attrs == null || attrs.Length < 1) | ||
539 | { | ||
540 | return null; | ||
541 | } | ||
542 | if( inter == null ) | ||
543 | { | ||
544 | throw new ArgumentNullException("inter"); | ||
545 | } | ||
546 | |||
547 | if(typeToCheck.GetInterface(inter.FullName) == null) | ||
548 | { | ||
549 | return null; | ||
550 | } | ||
551 | |||
552 | return attrs[0]; | ||
553 | } | ||
554 | |||
555 | /* A bit of overhead for simple group parsing, there are problems with Regex in Mono | ||
556 | public static string ParseValue(string val) | ||
557 | { | ||
558 | if(val == null || val.Length < 1 || !CheckForOSVariables) | ||
559 | return val; | ||
560 | |||
561 | string tmp = val; | ||
562 | Match m = m_VarRegex.Match(val); | ||
563 | while(m.Success) | ||
564 | { | ||
565 | if(m.Groups["var"] == null) | ||
566 | continue; | ||
567 | |||
568 | Capture c = m.Groups["var"].Captures[0]; | ||
569 | if(c == null) | ||
570 | continue; | ||
571 | |||
572 | string var = c.Value; | ||
573 | string envVal = Environment.GetEnvironmentVariable(var); | ||
574 | if(envVal == null) | ||
575 | envVal = ""; | ||
576 | |||
577 | tmp = tmp.Replace("${" + var + "}", envVal); | ||
578 | m = m.NextMatch(); | ||
579 | } | ||
580 | |||
581 | return tmp; | ||
582 | }*/ | ||
583 | |||
584 | /// <summary> | ||
585 | /// Attributes the value. | ||
586 | /// </summary> | ||
587 | /// <param name="node">The node.</param> | ||
588 | /// <param name="attr">The attr.</param> | ||
589 | /// <param name="def">The def.</param> | ||
590 | /// <returns></returns> | ||
591 | public static string AttributeValue(XmlNode node, string attr, string def) | ||
592 | { | ||
593 | if( node == null ) | ||
594 | { | ||
595 | throw new ArgumentNullException("node"); | ||
596 | } | ||
597 | if(node.Attributes[attr] == null) | ||
598 | { | ||
599 | return def; | ||
600 | } | ||
601 | string val = node.Attributes[attr].Value; | ||
602 | if(!CheckForOSVariables) | ||
603 | { | ||
604 | return val; | ||
605 | } | ||
606 | |||
607 | return InterpolateForEnvironmentVariables(val); | ||
608 | } | ||
609 | |||
610 | /// <summary> | ||
611 | /// Parses the boolean. | ||
612 | /// </summary> | ||
613 | /// <param name="node">The node.</param> | ||
614 | /// <param name="attr">The attr.</param> | ||
615 | /// <param name="defaultValue">if set to <c>true</c> [default value].</param> | ||
616 | /// <returns></returns> | ||
617 | public static bool ParseBoolean(XmlNode node, string attr, bool defaultValue) | ||
618 | { | ||
619 | if( node == null ) | ||
620 | { | ||
621 | throw new ArgumentNullException("node"); | ||
622 | } | ||
623 | if(node.Attributes[attr] == null) | ||
624 | { | ||
625 | return defaultValue; | ||
626 | } | ||
627 | return bool.Parse(node.Attributes[attr].Value); | ||
628 | } | ||
629 | |||
630 | /// <summary> | ||
631 | /// Enums the attribute value. | ||
632 | /// </summary> | ||
633 | /// <param name="node">The node.</param> | ||
634 | /// <param name="attr">The attr.</param> | ||
635 | /// <param name="enumType">Type of the enum.</param> | ||
636 | /// <param name="def">The def.</param> | ||
637 | /// <returns></returns> | ||
638 | public static object EnumAttributeValue(XmlNode node, string attr, Type enumType, object def) | ||
639 | { | ||
640 | if( def == null ) | ||
641 | { | ||
642 | throw new ArgumentNullException("def"); | ||
643 | } | ||
644 | string val = AttributeValue(node, attr, def.ToString()); | ||
645 | return Enum.Parse(enumType, val, true); | ||
646 | } | ||
647 | |||
648 | /// <summary> | ||
649 | /// | ||
650 | /// </summary> | ||
651 | /// <param name="assemblyName"></param> | ||
652 | /// <param name="projectType"></param> | ||
653 | /// <returns></returns> | ||
654 | public static string AssemblyFullName(string assemblyName, ProjectType projectType) | ||
655 | { | ||
656 | return assemblyName + (projectType == ProjectType.Library ? ".dll" : ".exe"); | ||
657 | } | ||
658 | |||
659 | #endregion | ||
660 | } | ||
661 | } | ||
diff --git a/Prebuild/src/Core/Utilities/Log.cs b/Prebuild/src/Core/Utilities/Log.cs new file mode 100644 index 0000000..da2cc96 --- /dev/null +++ b/Prebuild/src/Core/Utilities/Log.cs | |||
@@ -0,0 +1,279 @@ | |||
1 | #region BSD License | ||
2 | /* | ||
3 | Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com) | ||
4 | |||
5 | Redistribution and use in source and binary forms, with or without modification, are permitted | ||
6 | provided that the following conditions are met: | ||
7 | |||
8 | * Redistributions of source code must retain the above copyright notice, this list of conditions | ||
9 | and the following disclaimer. | ||
10 | * Redistributions in binary form must reproduce the above copyright notice, this list of conditions | ||
11 | and the following disclaimer in the documentation and/or other materials provided with the | ||
12 | distribution. | ||
13 | * The name of the author may not be used to endorse or promote products derived from this software | ||
14 | without specific prior written permission. | ||
15 | |||
16 | THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, | ||
17 | BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
18 | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | ||
19 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | ||
20 | OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | ||
21 | OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
22 | IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
23 | */ | ||
24 | #endregion | ||
25 | |||
26 | #region CVS Information | ||
27 | /* | ||
28 | * $Source$ | ||
29 | * $Author: jendave $ | ||
30 | * $Date: 2006-01-28 01:49:58 +0100 (lö, 28 jan 2006) $ | ||
31 | * $Revision: 71 $ | ||
32 | */ | ||
33 | #endregion | ||
34 | |||
35 | using System; | ||
36 | using System.IO; | ||
37 | |||
38 | namespace Prebuild.Core.Utilities | ||
39 | { | ||
40 | /// <summary> | ||
41 | /// | ||
42 | /// </summary> | ||
43 | public enum LogType | ||
44 | { | ||
45 | /// <summary> | ||
46 | /// | ||
47 | /// </summary> | ||
48 | None, | ||
49 | /// <summary> | ||
50 | /// | ||
51 | /// </summary> | ||
52 | Info, | ||
53 | /// <summary> | ||
54 | /// | ||
55 | /// </summary> | ||
56 | Warning, | ||
57 | /// <summary> | ||
58 | /// | ||
59 | /// </summary> | ||
60 | Error | ||
61 | } | ||
62 | |||
63 | /// <summary> | ||
64 | /// | ||
65 | /// </summary> | ||
66 | [Flags] | ||
67 | public enum LogTargets | ||
68 | { | ||
69 | /// <summary> | ||
70 | /// | ||
71 | /// </summary> | ||
72 | None = 0, | ||
73 | /// <summary> | ||
74 | /// | ||
75 | /// </summary> | ||
76 | Null = 1, | ||
77 | /// <summary> | ||
78 | /// | ||
79 | /// </summary> | ||
80 | File = 2, | ||
81 | /// <summary> | ||
82 | /// | ||
83 | /// </summary> | ||
84 | Console = 4 | ||
85 | } | ||
86 | |||
87 | /// <summary> | ||
88 | /// Summary description for Log. | ||
89 | /// </summary> | ||
90 | public class Log : IDisposable | ||
91 | { | ||
92 | #region Fields | ||
93 | |||
94 | private StreamWriter m_Writer; | ||
95 | private LogTargets m_Target = LogTargets.Null; | ||
96 | bool disposed; | ||
97 | |||
98 | #endregion | ||
99 | |||
100 | #region Constructors | ||
101 | |||
102 | /// <summary> | ||
103 | /// Initializes a new instance of the <see cref="Log"/> class. | ||
104 | /// </summary> | ||
105 | /// <param name="target">The target.</param> | ||
106 | /// <param name="fileName">Name of the file.</param> | ||
107 | public Log(LogTargets target, string fileName) | ||
108 | { | ||
109 | m_Target = target; | ||
110 | |||
111 | if((m_Target & LogTargets.File) != 0) | ||
112 | { | ||
113 | m_Writer = new StreamWriter(fileName, false); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | #endregion | ||
118 | |||
119 | #region Public Methods | ||
120 | |||
121 | /// <summary> | ||
122 | /// Writes this instance. | ||
123 | /// </summary> | ||
124 | public void Write() | ||
125 | { | ||
126 | Write(string.Empty); | ||
127 | } | ||
128 | |||
129 | /// <summary> | ||
130 | /// Writes the specified MSG. | ||
131 | /// </summary> | ||
132 | /// <param name="msg">The MSG.</param> | ||
133 | public void Write(string msg) | ||
134 | { | ||
135 | if((m_Target & LogTargets.Null) != 0) | ||
136 | { | ||
137 | return; | ||
138 | } | ||
139 | |||
140 | if((m_Target & LogTargets.Console) != 0) | ||
141 | { | ||
142 | Console.WriteLine(msg); | ||
143 | } | ||
144 | if((m_Target & LogTargets.File) != 0 && m_Writer != null) | ||
145 | { | ||
146 | m_Writer.WriteLine(msg); | ||
147 | } | ||
148 | } | ||
149 | |||
150 | /// <summary> | ||
151 | /// Writes the specified format. | ||
152 | /// </summary> | ||
153 | /// <param name="format">The format.</param> | ||
154 | /// <param name="args">The args.</param> | ||
155 | public void Write(string format, params object[] args) | ||
156 | { | ||
157 | Write(string.Format(format,args)); | ||
158 | } | ||
159 | |||
160 | /// <summary> | ||
161 | /// Writes the specified type. | ||
162 | /// </summary> | ||
163 | /// <param name="type">The type.</param> | ||
164 | /// <param name="format">The format.</param> | ||
165 | /// <param name="args">The args.</param> | ||
166 | public void Write(LogType type, string format, params object[] args) | ||
167 | { | ||
168 | if((m_Target & LogTargets.Null) != 0) | ||
169 | { | ||
170 | return; | ||
171 | } | ||
172 | |||
173 | string str = ""; | ||
174 | switch(type) | ||
175 | { | ||
176 | case LogType.Info: | ||
177 | str = "[I] "; | ||
178 | break; | ||
179 | case LogType.Warning: | ||
180 | str = "[!] "; | ||
181 | break; | ||
182 | case LogType.Error: | ||
183 | str = "[X] "; | ||
184 | break; | ||
185 | } | ||
186 | |||
187 | Write(str + format,args); | ||
188 | } | ||
189 | |||
190 | /// <summary> | ||
191 | /// Writes the exception. | ||
192 | /// </summary> | ||
193 | /// <param name="type">The type.</param> | ||
194 | /// <param name="ex">The ex.</param> | ||
195 | public void WriteException(LogType type, Exception ex) | ||
196 | { | ||
197 | if(ex != null) | ||
198 | { | ||
199 | Write(type, ex.Message); | ||
200 | //#if DEBUG | ||
201 | m_Writer.WriteLine("Exception @{0} stack trace [[", ex.TargetSite.Name); | ||
202 | m_Writer.WriteLine(ex.StackTrace); | ||
203 | m_Writer.WriteLine("]]"); | ||
204 | //#endif | ||
205 | } | ||
206 | } | ||
207 | |||
208 | /// <summary> | ||
209 | /// Flushes this instance. | ||
210 | /// </summary> | ||
211 | public void Flush() | ||
212 | { | ||
213 | if(m_Writer != null) | ||
214 | { | ||
215 | m_Writer.Flush(); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | #endregion | ||
220 | |||
221 | #region IDisposable Members | ||
222 | |||
223 | /// <summary> | ||
224 | /// Performs application-defined tasks associated with freeing, releasing, or | ||
225 | /// resetting unmanaged resources. | ||
226 | /// </summary> | ||
227 | public void Dispose() | ||
228 | { | ||
229 | Dispose(true); | ||
230 | GC.SuppressFinalize(this); | ||
231 | } | ||
232 | |||
233 | /// <summary> | ||
234 | /// Dispose objects | ||
235 | /// </summary> | ||
236 | /// <param name="disposing"> | ||
237 | /// If true, it will dispose close the handle | ||
238 | /// </param> | ||
239 | /// <remarks> | ||
240 | /// Will dispose managed and unmanaged resources. | ||
241 | /// </remarks> | ||
242 | protected virtual void Dispose(bool disposing) | ||
243 | { | ||
244 | if (!this.disposed) | ||
245 | { | ||
246 | if (disposing) | ||
247 | { | ||
248 | if (m_Writer != null) | ||
249 | { | ||
250 | m_Writer.Close(); | ||
251 | m_Writer = null; | ||
252 | } | ||
253 | } | ||
254 | } | ||
255 | this.disposed = true; | ||
256 | } | ||
257 | |||
258 | /// <summary> | ||
259 | /// | ||
260 | /// </summary> | ||
261 | ~Log() | ||
262 | { | ||
263 | this.Dispose(false); | ||
264 | } | ||
265 | |||
266 | /// <summary> | ||
267 | /// Closes and destroys this object | ||
268 | /// </summary> | ||
269 | /// <remarks> | ||
270 | /// Same as Dispose(true) | ||
271 | /// </remarks> | ||
272 | public void Close() | ||
273 | { | ||
274 | Dispose(); | ||
275 | } | ||
276 | |||
277 | #endregion | ||
278 | } | ||
279 | } | ||