diff MetroWpf/MetroWpf.Framework/Extensions/TextReaderExtensions.cs @ 15:060f02cd4591

Initial commit, pre airport work
author stevenh7776 stevenhollidge@hotmail.com
date Mon, 12 Mar 2012 23:05:21 +0800
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Framework/Extensions/TextReaderExtensions.cs	Mon Mar 12 23:05:21 2012 +0800
@@ -0,0 +1,60 @@
+using System;
+using System.Collections.Generic;
+using System.IO;
+
+namespace MetroWpf
+{
+    /// <summary>
+    /// Extension methods for the TextReader class and its sub classes (StreamReader, StringReader)
+    /// </summary>
+    public static class TextReaderExtensions
+    {
+        #region · Extensions ·
+
+        /// <summary>
+        /// The method provides an iterator through all lines of the text reader.
+        /// </summary>
+        /// <param name="reader">The text reader.</param>
+        /// <returns>The iterator</returns>
+        /// <example>
+        /// <code>
+        /// using(var reader = fileInfo.OpenText()) {
+        ///  foreach(var line in reader.IterateLines()) {
+        ///   // ...
+        ///  }
+        /// }
+        /// </code></example>
+        /// <remarks>Contributed by OlivierJ</remarks>
+        public static IEnumerable<string> IterateLines(this TextReader reader)
+        {
+            string line = null;
+            
+            while ((line = reader.ReadLine()) != null)
+            {
+                yield return line;
+            }
+        }
+
+        /// <summary>
+        /// The method executes the passed delegate /lambda expression) for all lines of the text reader.
+        /// </summary>
+        /// <param name="reader">The text reader.</param>
+        /// <param name="action">The action.</param>
+        /// <example>
+        /// 	<code>
+        /// using(var reader = fileInfo.OpenText()) {
+        ///  reader.IterateLines(l => Console.WriteLine(l));
+        /// }
+        /// </code></example>
+        /// <remarks>Contributed by OlivierJ</remarks>
+        public static void IterateLines(this TextReader reader, Action<string> action)
+        {
+            foreach (var line in reader.IterateLines())
+            {
+                action(line);
+            }
+        }
+
+        #endregion
+    }
+}
\ No newline at end of file