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