PageRenderTime 106ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/Core/Dependencies/Wwise/samples/Tools/FilePackager/FilePackager/Base/Extensions.cs

https://bitbucket.org/barakianc/nvidia-physx-and-apex-in-gge
C# | 333 lines | 190 code | 42 blank | 101 comment | 39 complexity | 8738666f314314cc57750de40572b6ec MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Collections;
  6. using System.Security.Cryptography;
  7. using System.Runtime.InteropServices;
  8. using System.IO;
  9. using System.Diagnostics;
  10. namespace FilePackager.Base
  11. {
  12. public static class Extensions
  13. {
  14. /// <summary>
  15. /// Adds the range of items to a list of a specific type
  16. /// </summary>
  17. /// <typeparam name="T">The type of items in the list.</typeparam>
  18. /// <param name="list">The list to add to.</param>
  19. /// <param name="enumerable">The items to add to the list.</param>
  20. public static void AddRange<T>(this IList<T> list, IEnumerable<T> enumerable)
  21. {
  22. foreach (T obj in enumerable)
  23. list.Add(obj);
  24. }
  25. /// <summary>
  26. /// Remove a range of items from a list of a specific type
  27. /// </summary>
  28. /// <typeparam name="T">The type of items in the list.</typeparam>
  29. /// <param name="list">The list to remove from.</param>
  30. /// <param name="enumerable">The items to remove from the list.</param>
  31. public static void RemoveRange<T>(this IList<T> list, IEnumerable<T> enumerable)
  32. {
  33. foreach (T obj in enumerable.ToList())
  34. list.Remove(obj);
  35. }
  36. /// <summary>
  37. /// Adds the range of items to a list of a specific type
  38. /// </summary>
  39. /// <typeparam name="T">The type of items in the list.</typeparam>
  40. /// <param name="list">The list to add to.</param>
  41. /// <param name="enumerable">The items to add to the list.</param>
  42. public static void AddRange(this IList list, IEnumerable enumerable)
  43. {
  44. foreach (object obj in enumerable)
  45. list.Add(obj);
  46. }
  47. /// <summary>
  48. /// Adds the range of items to a list of a specific type
  49. /// </summary>
  50. /// <typeparam name="T">The type of items in the list.</typeparam>
  51. /// <param name="list">The list to add to.</param>
  52. /// <param name="enumerable">The items to add to the list.</param>
  53. public static void InsertRange<T>(this IList<T> list, int index, IEnumerable<T> enumerable)
  54. {
  55. foreach (T obj in enumerable)
  56. list.Insert(index++, obj);
  57. }
  58. /// <summary>
  59. /// Removes all items in a list by calling Remove on each item.
  60. /// This function does not use Clear().
  61. ///
  62. /// This is useful in cases when INotifyCollectionChanged observers do not implement the
  63. /// Clear action, but only the Add and Remove.
  64. /// </summary>
  65. /// <param name="list">The target list.</param>
  66. public static void RemoveAll(this IList list)
  67. {
  68. while (list.Count > 0)
  69. {
  70. list.RemoveAt(list.Count - 1);
  71. }
  72. }
  73. /// <summary>
  74. /// Returns the Index of the first item that match the predicate in the sequence.
  75. /// </summary>
  76. /// <typeparam name="TSource">The type of the source.</typeparam>
  77. /// <param name="items">The sequence.</param>
  78. /// <param name="predicate">The predicate.</param>
  79. /// <remarks>Throw an <exception cref="ArgumentException"/> if the predicate does not find any matching item</remarks>
  80. /// <returns>index of the first matching item in the sequence</returns>
  81. public static int IndexOf<TSource>(this IEnumerable<TSource> items, Func<TSource, bool> predicate)
  82. {
  83. int index = 0;
  84. foreach (TSource item in items)
  85. {
  86. if (predicate(item))
  87. {
  88. return index;
  89. }
  90. ++index;
  91. }
  92. // Not found
  93. throw new ArgumentException("IndexOf did not found any item");
  94. }
  95. /// <summary>
  96. /// Appends a single item to the end of an enumerable source.
  97. /// </summary>
  98. /// <typeparam name="TSource">The type of the source.</typeparam>
  99. /// <param name="source">The enumerable source.</param>
  100. /// <param name="obj">The object to append.</param>
  101. /// <returns>The modified enumeration.</returns>
  102. public static IEnumerable<TSource> Append<TSource>(this IEnumerable<TSource> source,
  103. TSource obj)
  104. {
  105. return source.Concat(Enumerable.Repeat(obj, 1));
  106. }
  107. /// <summary>
  108. /// Prepends a single item to the front of an enumerable source.
  109. /// </summary>
  110. /// <typeparam name="TSource">The type of the source.</typeparam>
  111. /// <param name="source">The enumerable source.</param>
  112. /// <param name="obj">The object to prepend.</param>
  113. /// <returns>The modified enumeration.</returns>
  114. public static IEnumerable<TSource> Prepend<TSource>(this IEnumerable<TSource> source,
  115. TSource obj)
  116. {
  117. return Enumerable.Repeat(obj, 1).Concat(source);
  118. }
  119. /// <summary>
  120. /// Removes the first item matching the predicate.
  121. /// </summary>
  122. /// <typeparam name="TSource">The type of item in the target list.</typeparam>
  123. /// <param name="list">The target list.</param>
  124. /// <param name="predicate">The predicate.</param>
  125. /// <returns>Returns true if a matching item was found</returns>
  126. public static bool RemoveFirst<TSource>(this IList<TSource> list,
  127. Func<TSource, bool> predicate)
  128. {
  129. bool bRemoved = false;
  130. for (int i = 0; bRemoved == false && i < list.Count; i++)
  131. {
  132. if (predicate(list[i]))
  133. {
  134. list.RemoveAt(i);
  135. bRemoved = true;
  136. }
  137. }
  138. return bRemoved;
  139. }
  140. public static String ToString(this IEnumerable list, String delimiter)
  141. {
  142. return list.Cast<object>().ToString(delimiter, item => item.ToString());
  143. }
  144. public static String ToString<TSource>(this IEnumerable<TSource> list, String delimiter)
  145. {
  146. return list.ToString(delimiter, item => item.ToString());
  147. }
  148. public static String ToString<TSource>(this IEnumerable<TSource> list, String delimiter,
  149. Func<TSource, String> predicate)
  150. {
  151. if (list == null)
  152. return String.Empty;
  153. if (delimiter == null)
  154. throw new ArgumentNullException("delimiter");
  155. StringBuilder sb = new StringBuilder();
  156. IEnumerator<TSource> listEnum = list.GetEnumerator();
  157. if (listEnum.MoveNext())
  158. {
  159. for ( ; ; )
  160. {
  161. if (listEnum.Current == null)
  162. continue;
  163. sb.Append(predicate(listEnum.Current));
  164. if (!listEnum.MoveNext())
  165. break;
  166. sb.Append(delimiter);
  167. }
  168. }
  169. return sb.ToString();
  170. }
  171. /// <summary>
  172. /// Execute the specified action for each item.
  173. /// </summary>
  174. /// <typeparam name="T"></typeparam>
  175. /// <param name="source">The source.</param>
  176. /// <param name="action">The action to execute.</param>
  177. /// <returns></returns>
  178. public static IEnumerable<T> ForEach<T>(
  179. this IEnumerable<T> source,
  180. Action<T> action)
  181. {
  182. foreach (T element in source)
  183. action(element);
  184. return source;
  185. }
  186. /// <summary>
  187. /// Concat 2 lists, you can pass null pointers
  188. /// </summary>
  189. /// <typeparam name="T"></typeparam>
  190. /// <param name="source">The source.</param>
  191. /// <param name="enumerable">The enumerable.</param>
  192. /// <returns></returns>
  193. public static IEnumerable<T> SafeConcat<T>(this IEnumerable<T> source, IEnumerable<T> enumerable)
  194. {
  195. if (source != null && enumerable != null)
  196. return source.Concat(enumerable);
  197. if (source != null)
  198. return source;
  199. if (enumerable != null)
  200. return enumerable;
  201. // Return empty array
  202. return new T[0];
  203. }
  204. /// <summary>
  205. /// Concat 2 lists, you can pass null pointers
  206. /// </summary>
  207. /// <typeparam name="T"></typeparam>
  208. /// <param name="source">The source.</param>
  209. /// <param name="enumerable">The enumerable.</param>
  210. /// <returns></returns>
  211. public static IEnumerable<T> SafeConcat<T>(this IEnumerable source, IEnumerable enumerable)
  212. {
  213. if (source != null && enumerable != null)
  214. return source.Cast<T>().Concat(enumerable.Cast<T>());
  215. if (source != null)
  216. return source.Cast<T>();
  217. if (enumerable != null)
  218. return enumerable.Cast<T>();
  219. // Return empty array
  220. return new T[0];
  221. }
  222. /// <summary>
  223. /// Counts the items of a IEnumerable.
  224. /// </summary>
  225. /// <remarks>Only use when the Generic type is unknown</remarks>
  226. /// <param name="source">The source.</param>
  227. /// <returns>The number of items in the IEnumerable</returns>
  228. public static int CountNonGeneric(this IEnumerable source)
  229. {
  230. if (source == null)
  231. throw new ArgumentNullException("source is null");
  232. // When we know it is a ICollection, use the Count of the collection
  233. ICollection collection = source as ICollection;
  234. if (collection != null)
  235. return collection.Count;
  236. // Count the items
  237. int num = 0;
  238. IEnumerator enumerator = source.GetEnumerator();
  239. while (enumerator.MoveNext())
  240. {
  241. num++;
  242. }
  243. return num;
  244. }
  245. /// <summary>
  246. /// Projects each element of a sequence into a new form.
  247. /// </summary>
  248. /// <typeparam name="TSource">The type of the source. If null, the function returns an empty sequence.</typeparam>
  249. /// <typeparam name="TResult">The type of the result.</typeparam>
  250. /// <param name="source">A sequence of values to invoke a transform function on.</param>
  251. /// <param name="selector">A transform function to apply to each element.</param>
  252. /// <returns>An IEnumerable<> whose elements are the result of invoking the transform function on each element of source.</returns>
  253. public static IEnumerable<TResult> SafeSelect<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, TResult> selector)
  254. {
  255. if (source != null)
  256. return source.Select(selector);
  257. return new TResult[0];
  258. }
  259. public static string PathRelativePathTo(
  260. string from,
  261. bool fromIsDirectory,
  262. string to,
  263. bool toIsDirectory )
  264. {
  265. StringBuilder builder = new StringBuilder(260);
  266. bool ret = PathRelativePathTo(
  267. builder,
  268. from, fromIsDirectory ? FileAttributes.Directory : FileAttributes.Normal,
  269. to, toIsDirectory ? FileAttributes.Directory : FileAttributes.Normal );
  270. return ret ? builder.ToString() : to;
  271. }
  272. public static string PathCanonicalize(string path)
  273. {
  274. StringBuilder builder = new StringBuilder(260);
  275. if (!PathCanonicalize(builder, path))
  276. return path;
  277. return builder.ToString();
  278. }
  279. [DllImport("shlwapi.dll", CharSet = CharSet.Auto)]
  280. private static extern bool PathRelativePathTo(
  281. [Out] StringBuilder pszPath,
  282. [In] string pszFrom,
  283. [In] FileAttributes dwAttrFrom,
  284. [In] string pszTo,
  285. [In] FileAttributes dwAttrTo );
  286. [DllImport("shlwapi", CharSet = CharSet.Auto, SetLastError = true)]
  287. private static extern bool PathCanonicalize(
  288. [Out] StringBuilder lpszDst,
  289. [In] string lpszSrc);
  290. }
  291. }