aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Prebuild/src/Core/Utilities/.svn/text-base/Helper.cs.svn-base
diff options
context:
space:
mode:
Diffstat (limited to 'Prebuild/src/Core/Utilities/.svn/text-base/Helper.cs.svn-base')
-rw-r--r--Prebuild/src/Core/Utilities/.svn/text-base/Helper.cs.svn-base575
1 files changed, 575 insertions, 0 deletions
diff --git a/Prebuild/src/Core/Utilities/.svn/text-base/Helper.cs.svn-base b/Prebuild/src/Core/Utilities/.svn/text-base/Helper.cs.svn-base
new file mode 100644
index 0000000..8c3e968
--- /dev/null
+++ b/Prebuild/src/Core/Utilities/.svn/text-base/Helper.cs.svn-base
@@ -0,0 +1,575 @@
1#region BSD License
2/*
3Copyright (c) 2004-2005 Matthew Holmes (matthew@wildfiregames.com), Dan Moorehead (dan05a@gmail.com)
4
5Redistribution and use in source and binary forms, with or without modification, are permitted
6provided 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
16THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
17BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
22IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23*/
24#endregion
25
26using System;
27using System.Collections.Generic;
28using System.Diagnostics;
29using System.IO;
30using System.Runtime.InteropServices;
31using System.Text.RegularExpressions;
32using System.Collections.Specialized;
33using System.Xml;
34using Prebuild.Core.Nodes;
35
36namespace Prebuild.Core.Utilities
37{
38 /// <summary>
39 ///
40 /// </summary>
41 public class Helper
42 {
43 #region Fields
44
45 static bool checkForOSVariables;
46
47 /// <summary>
48 ///
49 /// </summary>
50 public static bool CheckForOSVariables
51 {
52 get
53 {
54 return checkForOSVariables;
55 }
56 set
57 {
58 checkForOSVariables = value;
59 }
60 }
61
62 #endregion
63
64 #region Public Methods
65
66 #region String Parsing
67
68 public delegate string StringLookup(string key);
69
70 /// <summary>
71 /// Gets a collection of StringLocationPair objects that represent the matches
72 /// </summary>
73 /// <param name="target">The target.</param>
74 /// <param name="beforeGroup">The before group.</param>
75 /// <param name="afterGroup">The after group.</param>
76 /// <param name="includeDelimitersInSubstrings">if set to <c>true</c> [include delimiters in substrings].</param>
77 /// <returns></returns>
78 public static StringCollection FindGroups(string target, string beforeGroup, string afterGroup, bool includeDelimitersInSubstrings)
79 {
80 if( beforeGroup == null )
81 {
82 throw new ArgumentNullException("beforeGroup");
83 }
84 if( afterGroup == null )
85 {
86 throw new ArgumentNullException("afterGroup");
87 }
88 StringCollection results = new StringCollection();
89 if(target == null || target.Length == 0)
90 {
91 return results;
92 }
93
94 int beforeMod = 0;
95 int afterMod = 0;
96 if(includeDelimitersInSubstrings)
97 {
98 //be sure to not exlude the delims
99 beforeMod = beforeGroup.Length;
100 afterMod = afterGroup.Length;
101 }
102 int startIndex = 0;
103 while((startIndex = target.IndexOf(beforeGroup,startIndex)) != -1) {
104 int endIndex = target.IndexOf(afterGroup,startIndex);//the index of the char after it
105 if(endIndex == -1)
106 {
107 break;
108 }
109 int length = endIndex - startIndex - beforeGroup.Length;//move to the first char in the string
110 string substring = substring = target.Substring(startIndex + beforeGroup.Length - beforeMod,
111 length - afterMod);
112
113 results.Add(substring);
114 //results.Add(new StringLocationPair(substring,startIndex));
115 startIndex = endIndex + 1;
116 //the Interpolate*() methods will not work if expressions are expandded inside expression due to an optimization
117 //so start after endIndex
118
119 }
120 return results;
121 }
122
123 /// <summary>
124 /// Replaces the groups.
125 /// </summary>
126 /// <param name="target">The target.</param>
127 /// <param name="beforeGroup">The before group.</param>
128 /// <param name="afterGroup">The after group.</param>
129 /// <param name="lookup">The lookup.</param>
130 /// <returns></returns>
131 public static string ReplaceGroups(string target, string beforeGroup, string afterGroup, StringLookup lookup) {
132 if( target == null )
133 {
134 throw new ArgumentNullException("target");
135 }
136 //int targetLength = target.Length;
137 StringCollection strings = FindGroups(target,beforeGroup,afterGroup,false);
138 if( lookup == null )
139 {
140 throw new ArgumentNullException("lookup");
141 }
142 foreach(string substring in strings)
143 {
144 target = target.Replace(beforeGroup + substring + afterGroup, lookup(substring) );
145 }
146 return target;
147 }
148
149 /// <summary>
150 /// Replaces ${var} statements in a string with the corresonding values as detirmined by the lookup delegate
151 /// </summary>
152 /// <param name="target">The target.</param>
153 /// <param name="lookup">The lookup.</param>
154 /// <returns></returns>
155 public static string InterpolateForVariables(string target, StringLookup lookup)
156 {
157 return ReplaceGroups(target, "${" , "}" , lookup);
158 }
159
160 /// <summary>
161 /// Replaces ${var} statements in a string with the corresonding environment variable with name var
162 /// </summary>
163 /// <param name="target"></param>
164 /// <returns></returns>
165 public static string InterpolateForEnvironmentVariables(string target)
166 {
167 return InterpolateForVariables(target, new StringLookup(Environment.GetEnvironmentVariable));
168 }
169
170 #endregion
171
172 /// <summary>
173 /// Translates the value.
174 /// </summary>
175 /// <param name="translateType">Type of the translate.</param>
176 /// <param name="translationItem">The translation item.</param>
177 /// <returns></returns>
178 public static object TranslateValue(Type translateType, string translationItem)
179 {
180 if(translationItem == null)
181 {
182 return null;
183 }
184
185 try
186 {
187 string lowerVal = translationItem.ToLower();
188 if(translateType == typeof(bool))
189 {
190 return (lowerVal == "true" || lowerVal == "1" || lowerVal == "y" || lowerVal == "yes" || lowerVal == "on");
191 }
192 else if(translateType == typeof(int))
193 {
194 return (Int32.Parse(translationItem));
195 }
196 else
197 {
198 return translationItem;
199 }
200 }
201 catch(FormatException)
202 {
203 return null;
204 }
205 }
206
207 /// <summary>
208 /// Deletes if exists.
209 /// </summary>
210 /// <param name="file">The file.</param>
211 /// <returns></returns>
212 public static bool DeleteIfExists(string file)
213 {
214 string resFile = null;
215 try
216 {
217 resFile = ResolvePath(file);
218 }
219 catch(ArgumentException)
220 {
221 return false;
222 }
223
224 if(!File.Exists(resFile))
225 {
226 return false;
227 }
228
229 File.Delete(resFile);
230 return true;
231 }
232
233 static readonly char seperator = Path.DirectorySeparatorChar;
234
235 // This little gem was taken from the NeL source, thanks guys!
236 /// <summary>
237 /// Makes a relative path
238 /// </summary>
239 /// <param name="startPath">Path to start from</param>
240 /// <param name="endPath">Path to end at</param>
241 /// <returns>Path that will get from startPath to endPath</returns>
242 public static string MakePathRelativeTo(string startPath, string endPath)
243 {
244 string tmp = NormalizePath(startPath, seperator);
245 string src = NormalizePath(endPath, seperator);
246 string prefix = "";
247
248 while(true)
249 {
250 if((String.Compare(tmp, 0, src, 0, tmp.Length) == 0))
251 {
252 string ret;
253 int size = tmp.Length;
254 if(size == src.Length)
255 {
256 return "./";
257 }
258 if((src.Length > tmp.Length) && src[tmp.Length - 1] != seperator)
259 {
260 }
261 else
262 {
263 ret = prefix + endPath.Substring(size, endPath.Length - size);
264 ret = ret.Trim();
265 if(ret[0] == seperator)
266 {
267 ret = "." + ret;
268 }
269
270 return NormalizePath(ret);
271 }
272
273 }
274
275 if(tmp.Length < 2)
276 {
277 break;
278 }
279
280 int lastPos = tmp.LastIndexOf(seperator, tmp.Length - 2);
281 int prevPos = tmp.IndexOf(seperator);
282
283 if((lastPos == prevPos) || (lastPos == -1))
284 {
285 break;
286 }
287
288 tmp = tmp.Substring(0, lastPos + 1);
289 prefix += ".." + seperator.ToString();
290 }
291
292 return endPath;
293 }
294
295 /// <summary>
296 /// Resolves the path.
297 /// </summary>
298 /// <param name="path">The path.</param>
299 /// <returns></returns>
300 public static string ResolvePath(string path)
301 {
302 string tmpPath = NormalizePath(path);
303 if(tmpPath.Length < 1)
304 {
305 tmpPath = ".";
306 }
307
308 tmpPath = Path.GetFullPath(tmpPath);
309 if(!File.Exists(tmpPath) && !Directory.Exists(tmpPath))
310 {
311 throw new ArgumentException("Path could not be resolved: " + tmpPath);
312 }
313
314 return tmpPath;
315 }
316
317 /// <summary>
318 /// Normalizes the path.
319 /// </summary>
320 /// <param name="path">The path.</param>
321 /// <param name="separatorCharacter">The separator character.</param>
322 /// <returns></returns>
323 public static string NormalizePath(string path, char separatorCharacter)
324 {
325 if(path == null || path == "" || path.Length < 1)
326 {
327 return "";
328 }
329
330 string tmpPath = path.Replace('\\', '/');
331 tmpPath = tmpPath.Replace('/', separatorCharacter);
332 return tmpPath;
333 }
334
335 /// <summary>
336 /// Normalizes the path.
337 /// </summary>
338 /// <param name="path">The path.</param>
339 /// <returns></returns>
340 public static string NormalizePath(string path)
341 {
342 return NormalizePath(path, Path.DirectorySeparatorChar);
343 }
344
345 /// <summary>
346 /// Ends the path.
347 /// </summary>
348 /// <param name="path">The path.</param>
349 /// <param name="separatorCharacter">The separator character.</param>
350 /// <returns></returns>
351 public static string EndPath(string path, char separatorCharacter)
352 {
353 if(path == null || path == "" || path.Length < 1)
354 {
355 return "";
356 }
357
358 if(!path.EndsWith(separatorCharacter.ToString()))
359 {
360 return (path + separatorCharacter);
361 }
362
363 return path;
364 }
365
366 /// <summary>
367 /// Ends the path.
368 /// </summary>
369 /// <param name="path">The path.</param>
370 /// <returns></returns>
371 public static string EndPath(string path)
372 {
373 return EndPath(path, Path.DirectorySeparatorChar);
374 }
375
376 /// <summary>
377 /// Makes the file path.
378 /// </summary>
379 /// <param name="path">The path.</param>
380 /// <param name="name">The name.</param>
381 /// <param name="ext">The ext.</param>
382 /// <returns></returns>
383 public static string MakeFilePath(string path, string name, string ext)
384 {
385 string ret = EndPath(NormalizePath(path));
386
387 if( name == null )
388 {
389 throw new ArgumentNullException("name");
390 }
391
392 ret += name;
393 if(!name.EndsWith("." + ext))
394 {
395 ret += "." + ext;
396 }
397
398 //foreach(char c in Path.GetInvalidPathChars())
399 //{
400 // ret = ret.Replace(c, '_');
401 //}
402
403 return ret;
404 }
405
406 /// <summary>
407 /// Makes the file path.
408 /// </summary>
409 /// <param name="path">The path.</param>
410 /// <param name="name">The name.</param>
411 /// <returns></returns>
412 public static string MakeFilePath(string path, string name)
413 {
414 string ret = EndPath(NormalizePath(path));
415
416 if( name == null )
417 {
418 throw new ArgumentNullException("name");
419 }
420
421 ret += name;
422
423 //foreach (char c in Path.GetInvalidPathChars())
424 //{
425 // ret = ret.Replace(c, '_');
426 //}
427
428 return ret;
429 }
430
431 /// <summary>
432 ///
433 /// </summary>
434 /// <param name="path"></param>
435 /// <returns></returns>
436 public static string MakeReferencePath(string path)
437 {
438 string ret = EndPath(NormalizePath(path));
439
440 //foreach (char c in Path.GetInvalidPathChars())
441 //{
442 // ret = ret.Replace(c, '_');
443 //}
444
445 return ret;
446 }
447
448 /// <summary>
449 /// Sets the current dir.
450 /// </summary>
451 /// <param name="path">The path.</param>
452 public static void SetCurrentDir(string path)
453 {
454 if( path == null )
455 {
456 throw new ArgumentNullException("path");
457 }
458 if(path.Length < 1)
459 {
460 return;
461 }
462
463 Environment.CurrentDirectory = path;
464 }
465
466 /// <summary>
467 /// Checks the type.
468 /// </summary>
469 /// <param name="typeToCheck">The type to check.</param>
470 /// <param name="attr">The attr.</param>
471 /// <param name="inter">The inter.</param>
472 /// <returns></returns>
473 public static object CheckType(Type typeToCheck, Type attr, Type inter)
474 {
475 if(typeToCheck == null || attr == null)
476 {
477 return null;
478 }
479
480 object[] attrs = typeToCheck.GetCustomAttributes(attr, false);
481 if(attrs == null || attrs.Length < 1)
482 {
483 return null;
484 }
485 if( inter == null )
486 {
487 throw new ArgumentNullException("inter");
488 }
489
490 if(typeToCheck.GetInterface(inter.FullName) == null)
491 {
492 return null;
493 }
494
495 return attrs[0];
496 }
497
498 /// <summary>
499 /// Attributes the value.
500 /// </summary>
501 /// <param name="node">The node.</param>
502 /// <param name="attr">The attr.</param>
503 /// <param name="def">The def.</param>
504 /// <returns></returns>
505 public static string AttributeValue(XmlNode node, string attr, string def)
506 {
507 if( node == null )
508 {
509 throw new ArgumentNullException("node");
510 }
511 if(node.Attributes[attr] == null)
512 {
513 return def;
514 }
515 string val = node.Attributes[attr].Value;
516 if(!CheckForOSVariables)
517 {
518 return val;
519 }
520
521 return InterpolateForEnvironmentVariables(val);
522 }
523
524 /// <summary>
525 /// Parses the boolean.
526 /// </summary>
527 /// <param name="node">The node.</param>
528 /// <param name="attr">The attr.</param>
529 /// <param name="defaultValue">if set to <c>true</c> [default value].</param>
530 /// <returns></returns>
531 public static bool ParseBoolean(XmlNode node, string attr, bool defaultValue)
532 {
533 if( node == null )
534 {
535 throw new ArgumentNullException("node");
536 }
537 if(node.Attributes[attr] == null)
538 {
539 return defaultValue;
540 }
541 return bool.Parse(node.Attributes[attr].Value);
542 }
543
544 /// <summary>
545 /// Enums the attribute value.
546 /// </summary>
547 /// <param name="node">The node.</param>
548 /// <param name="attr">The attr.</param>
549 /// <param name="enumType">Type of the enum.</param>
550 /// <param name="def">The def.</param>
551 /// <returns></returns>
552 public static object EnumAttributeValue(XmlNode node, string attr, Type enumType, object def)
553 {
554 if( def == null )
555 {
556 throw new ArgumentNullException("def");
557 }
558 string val = AttributeValue(node, attr, def.ToString());
559 return Enum.Parse(enumType, val, true);
560 }
561
562 /// <summary>
563 ///
564 /// </summary>
565 /// <param name="assemblyName"></param>
566 /// <param name="projectType"></param>
567 /// <returns></returns>
568 public static string AssemblyFullName(string assemblyName, ProjectType projectType)
569 {
570 return assemblyName + (projectType == ProjectType.Library ? ".dll" : ".exe");
571 }
572
573 #endregion
574 }
575}