wasSharp – Diff between revs 29 and 30

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