wasSharp – Blame information for rev 42

Subversion Repositories:
Rev:
Rev Author Line No. Line
7 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System.Collections.Generic;
8 using System.IO;
24 office 9 using System.IO.Compression;
7 office 10 using System.Linq;
11 using System.Text;
24 office 12 using System.Threading.Tasks;
7 office 13  
14 namespace wasSharp
15 {
16 public static class IO
17 {
18 /// <summary>
19 /// Strip characters that are incompatible with file names.
20 /// </summary>
21 /// <param name="fileName">the name of the file</param>
22 /// <returns>a clean string</returns>
23 private static string CleanFileName(string fileName)
24 {
25 return Path.GetInvalidFileNameChars()
26 .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
27 }
28  
25 office 29 ///////////////////////////////////////////////////////////////////////////
30 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
31 ///////////////////////////////////////////////////////////////////////////
32 /// <summary>
33 /// Compress a stream using Deflate compression.
34 /// </summary>
35 /// <param name="uncompressedSourceStream">the stream to compress</param>
36 /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param>
37 /// <param name="leaveOpen">whether to leave the compression stream open</param>
38 /// <returns>an awaitable Task</returns>
24 office 39 public static async Task DeflateCompress(this Stream uncompressedSourceStream,
40 Stream compressedDestinationStream, bool leaveOpen = false)
41 {
42 using (
43 var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress,
44 leaveOpen))
45 {
46 await uncompressedSourceStream.CopyToAsync(compressionStream);
47 }
48 }
49  
25 office 50 ///////////////////////////////////////////////////////////////////////////
51 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
52 ///////////////////////////////////////////////////////////////////////////
53 /// <summary>
54 /// Decompress a Deflate-compressed stream.
55 /// </summary>
56 /// <param name="compressedSourceStream">the compressed stream to decompress</param>
57 /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param>
58 /// <param name="leaveOpen">whether to leave the stream open after decompression</param>
59 /// <returns>an awaitable Task</returns>
24 office 60 public static async Task DeflateDecompress(this Stream compressedSourceStream,
61 Stream uncompressedDestinationStream, bool leaveOpen = false)
62 {
63 using (
64 var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress,
65 leaveOpen))
66 {
67 await decompressionStream.CopyToAsync(uncompressedDestinationStream);
68 }
69 }
70  
25 office 71 ///////////////////////////////////////////////////////////////////////////
72 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
73 ///////////////////////////////////////////////////////////////////////////
74 /// <summary>
75 /// Compress a stream using GZip compression.
76 /// </summary>
77 /// <param name="uncompressedSourceStream">the stream to compress</param>
78 /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param>
79 /// <param name="leaveOpen">whether to leave the stream open after compressing</param>
80 /// <returns>an awaitable Task</returns>
24 office 81 public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream,
82 bool leaveOpen = false)
83 {
84 using (
85 var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen)
86 )
87 {
88 await uncompressedSourceStream.CopyToAsync(compressionStream);
89 }
90 }
91  
25 office 92 ///////////////////////////////////////////////////////////////////////////
93 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
94 ///////////////////////////////////////////////////////////////////////////
95 /// <summary>
96 /// Decompress a GZiped stream.
97 /// </summary>
98 /// <param name="compressedSourceStream">the stream to decompress</param>
99 /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param>
100 /// <param name="leaveOpen">whether the decompression stream should be left open</param>
101 /// <returns>an awaitable Task</returns>
24 office 102 public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream,
103 bool leaveOpen = false)
104 {
105 using (
106 var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen))
107 {
108 await decompressionStream.CopyToAsync(uncompressedDestinationStream);
109 }
110 }
111  
7 office 112 ///////////////////////////////////////////////////////////////////////////
113 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
114 ///////////////////////////////////////////////////////////////////////////
115 /// <summary>
116 /// Splits a path using a separator and an escape character.
117 /// </summary>
118 /// <param name="path">the path to split</param>
119 /// <param name="separator">the separator character</param>
120 /// <param name="escape">the escape character</param>
42 office 121 /// <param name="includeRootSeparator">true if the initial separator should be included</param>
7 office 122 /// <returns>path parts</returns>
30 office 123 public static IEnumerable<string> PathSplit(this string path, char separator, char? escape = null, bool includeRootSeparator = true)
7 office 124 {
10 office 125 var s = new Stack<char>();
126 var p = new StringBuilder();
127 foreach (var c in path)
7 office 128 {
29 office 129 if (c.Equals(escape))
7 office 130 {
131 s.Push(c);
132 continue;
133 }
29 office 134 if (c.Equals(separator))
7 office 135 {
136 if (s.Count.Equals(0) || !s.Peek().Equals(escape))
137 {
30 office 138 if (p.Length.Equals(0) && includeRootSeparator)
28 office 139 {
140 p.Append(c);
141 continue;
142 }
7 office 143 yield return p.ToString();
144 p = new StringBuilder();
145 continue;
146 }
147 s.Pop();
148 while (!s.Count.Equals(0))
149 {
150 p.Append(s.Pop());
151 }
152 p.Append(c);
153 continue;
154 }
155 p.Append(c);
156 }
157 while (!s.Count.Equals(0))
158 {
159 p.Append(s.Pop());
160 }
161 yield return p.ToString();
162 }
163 }
27 office 164 }