Click here to Skip to main content
15,895,606 members
Articles / Web Development / HTML

Transformalizing NorthWind

Rate me:
Please Sign up or sign in to vote.
4.95/5 (29 votes)
24 Jul 2014GPL37 min read 57.8K   341   53  
Combining de-normalization, transformation, replication, and awesome-ness.
#region License
// /*
// See license included in this library folder.
// */
#endregion

using System.IO;
using System.Text;

namespace Transformalize.Libs.FileHelpers.Helpers
{
    internal static class StreamHelper
    {
        internal static TextWriter CreateFileAppender(string fileName, Encoding encode, bool correctEnd)
        {
            return CreateFileAppender(fileName, encode, correctEnd, true);
        }

        internal static TextWriter CreateFileAppender(string fileName, Encoding encode, bool correctEnd, bool disposeStream)
        {
            TextWriter res;

            if (correctEnd)
            {
                FileStream fs = null;

                try
                {
                    fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

                    if (fs.Length >= 2)
                    {
                        fs.Seek(-2, SeekOrigin.End);

                        if (fs.ReadByte() == 13)
                        {
                            if (fs.ReadByte() == 10)
                            {
                                int nowRead;
                                do
                                {
                                    fs.Seek(-2, SeekOrigin.Current);
                                    nowRead = fs.ReadByte();
                                } while (nowRead == 13 || nowRead == 10);
                            }
                        }
                        else
                            fs.ReadByte();

                        fs.WriteByte(13);
                        fs.WriteByte(10);
                    }

                    res = new StreamWriter(fs, encode);
                }
                finally
                {
                    if (disposeStream && fs != null)
                        fs.Close();
                }
            }
            else
            {
                res = new StreamWriter(fileName, true, encode);
            }

            return res;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions