PageRenderTime 566ms CodeModel.GetById 302ms app.highlight 4ms RepoModel.GetById 258ms app.codeStats 0ms

/Rhino.Etl.Core/Files/FluentFile.cs

http://github.com/ayende/rhino-etl
C# | 118 lines | 58 code | 11 blank | 49 comment | 2 complexity | a3e876acb5c2dabe9f6eb232bea5832a MD5 | raw file
  1namespace Rhino.Etl.Core.Files
  2{
  3    using System;
  4    using System.Collections;
  5    using System.IO;
  6    using System.Text;
  7    using FileHelpers;
  8
  9    /// <summary>
 10    /// Gives a fluent interface syntax on top of the <see cref="FileHelperEngine"/>
 11    /// </summary>
 12    public class FluentFile
 13    {
 14        private readonly FileHelperAsyncEngine engine;
 15
 16        /// <summary>
 17        /// Get a new fluent file instance for <typeparam name="T"></typeparam>
 18        /// </summary>
 19        public static FluentFile For<T>()
 20        {
 21            return new FluentFile(typeof(T));
 22        }
 23
 24        /// <summary>
 25        /// Initializes a new instance of the <see cref="FluentFile"/> class.
 26        /// </summary>
 27        /// <param name="type">The type.</param>
 28        public FluentFile(Type type)
 29        {
 30            engine = new FileHelperAsyncEngine(type);
 31        }
 32
 33        /// <summary>
 34        /// Specify which file to start reading from
 35        /// </summary>
 36        /// <param name="filename">The filename.</param>
 37        public FileEngine From(string filename)
 38        {
 39            filename = NormalizeFilename(filename);
 40            engine.BeginReadFile(filename);
 41            return new FileEngine(engine);
 42        }
 43
 44        /// <summary>
 45        /// Specify which file to start writing to
 46        /// </summary>
 47        /// <param name="filename">The filename.</param>
 48        /// <remarks>
 49        /// This will overwrite the file, use <see cref="AppendTo"/> if you want
 50        /// to append.
 51        /// </remarks>
 52        public FileEngine To(string filename)
 53        {
 54            filename = NormalizeFilename(filename);
 55            engine.BeginWriteFile(filename);
 56            return new FileEngine(engine);
 57        }
 58
 59        /// <summary>
 60        /// Specify which file to start appending to
 61        /// </summary>
 62        /// <param name="filename">The filename.</param>
 63        /// <returns></returns>
 64        public FileEngine AppendTo(string filename)
 65        {
 66            engine.BeginAppendToFile(filename);
 67            return new FileEngine(engine);
 68        }
 69
 70        private static string NormalizeFilename(string filename)
 71        {
 72            if (filename.StartsWith("~") == false)
 73                return filename;
 74            //note that this ignores rooted paths
 75            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, filename);
 76        }
 77
 78        ///// <summary>
 79        ///// Gets or sets the options.
 80        ///// </summary>
 81        ///// <value>The options.</value>
 82        //public RecordOptions Options
 83        //{
 84        //    get { return engine.RecordType.; }
 85        //    set { engine.Options = value; }
 86        //}
 87
 88        /// <summary>
 89        /// Gets or sets the footer text.
 90        /// </summary>
 91        /// <value>The footer text.</value>
 92        public string FooterText
 93        {
 94            get { return engine.FooterText; }
 95            set { engine.FooterText = value; }
 96        }
 97
 98        /// <summary>
 99        /// Gets or sets the header text.
100        /// </summary>
101        /// <value>The header text.</value>
102        public string HeaderText
103        {
104            get { return engine.HeaderText; }
105            set { engine.HeaderText = value; }
106        }
107
108        /// <summary>
109        /// Gets or sets the encoding.
110        /// </summary>
111        /// <value>The encoding.</value>
112        public Encoding Encoding
113        {
114            get { return engine.Encoding; }
115            set { engine.Encoding = value; }
116        }
117    }
118}