wasSharp – Rev 27

Subversion Repositories:
Rev:
///////////////////////////////////////////////////////////////////////////
//  Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3      //
//  Please see: http://www.gnu.org/licenses/gpl.html for legal details,  //
//  rights of fair usage, the disclaimer and warranty conditions.        //
///////////////////////////////////////////////////////////////////////////

using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace wasSharp
{
    public static class IO
    {
        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Combine multiple paths.
        /// </summary>
        /// <param name="paths">an array of paths</param>
        /// <returns>a combined path</returns>
        public static string PathCombine(string separator = @"\", params string[] paths)
        {
            return string.Join(separator, paths);
        }

        /// <summary>
        ///     Strip characters that are incompatible with file names.
        /// </summary>
        /// <param name="fileName">the name of the file</param>
        /// <returns>a clean string</returns>
        private static string CleanFileName(string fileName)
        {
            return Path.GetInvalidFileNameChars()
                .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
        }

        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Compress a stream using Deflate compression.
        /// </summary>
        /// <param name="uncompressedSourceStream">the stream to compress</param>
        /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param>
        /// <param name="leaveOpen">whether to leave the compression stream open</param>
        /// <returns>an awaitable Task</returns>
        public static async Task DeflateCompress(this Stream uncompressedSourceStream,
            Stream compressedDestinationStream, bool leaveOpen = false)
        {
            using (
                var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress,
                    leaveOpen))
            {
                await uncompressedSourceStream.CopyToAsync(compressionStream);
            }
        }

        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Decompress a Deflate-compressed stream.
        /// </summary>
        /// <param name="compressedSourceStream">the compressed stream to decompress</param>
        /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param>
        /// <param name="leaveOpen">whether to leave the stream open after decompression</param>
        /// <returns>an awaitable Task</returns>
        public static async Task DeflateDecompress(this Stream compressedSourceStream,
            Stream uncompressedDestinationStream, bool leaveOpen = false)
        {
            using (
                var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress,
                    leaveOpen))
            {
                await decompressionStream.CopyToAsync(uncompressedDestinationStream);
            }
        }

        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Compress a stream using GZip compression.
        /// </summary>
        /// <param name="uncompressedSourceStream">the stream to compress</param>
        /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param>
        /// <param name="leaveOpen">whether to leave the stream open after compressing</param>
        /// <returns>an awaitable Task</returns>
        public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream,
            bool leaveOpen = false)
        {
            using (
                var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen)
                )
            {
                await uncompressedSourceStream.CopyToAsync(compressionStream);
            }
        }

        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Decompress a GZiped stream.
        /// </summary>
        /// <param name="compressedSourceStream">the stream to decompress</param>
        /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param>
        /// <param name="leaveOpen">whether the decompression stream should be left open</param>
        /// <returns>an awaitable Task</returns>
        public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream,
            bool leaveOpen = false)
        {
            using (
                var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen))
            {
                await decompressionStream.CopyToAsync(uncompressedDestinationStream);
            }
        }

        ///////////////////////////////////////////////////////////////////////////
        //    Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3    //
        ///////////////////////////////////////////////////////////////////////////
        /// <summary>
        ///     Splits a path using a separator and an escape character.
        /// </summary>
        /// <param name="path">the path to split</param>
        /// <param name="separator">the separator character</param>
        /// <param name="escape">the escape character</param>
        /// <returns>path parts</returns>
        public static IEnumerable<string> PathSplit(this string path, char separator, char? escape)
        {
            var s = new Stack<char>();
            var p = new StringBuilder();
            foreach (var c in path)
            {
                if (c == escape)
                {
                    s.Push(c);
                    continue;
                }
                if (c == separator)
                {
                    if (s.Count.Equals(0) || !s.Peek().Equals(escape))
                    {
                        yield return p.ToString();
                        p = new StringBuilder();
                        continue;
                    }
                    s.Pop();
                    while (!s.Count.Equals(0))
                    {
                        p.Append(s.Pop());
                    }
                    p.Append(c);
                    continue;
                }
                p.Append(c);
            }
            while (!s.Count.Equals(0))
            {
                p.Append(s.Pop());
            }
            yield return p.ToString();
        }
    }
}