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