wasSharp – Diff between revs 52 and 54

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 52 Rev 54
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 /////////////////////////////////////////////////////////////////////////// 18 ///////////////////////////////////////////////////////////////////////////
19 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // 19 // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 //
20 /////////////////////////////////////////////////////////////////////////// 20 ///////////////////////////////////////////////////////////////////////////
21 /// <summary> 21 /// <summary>
22 /// Combine multiple paths. 22 /// Combine multiple paths.
23 /// </summary> 23 /// </summary>
24 /// <param name="paths">an array of paths</param> 24 /// <param name="paths">an array of paths</param>
25 /// <returns>a combined path</returns> 25 /// <returns>a combined path</returns>
26 public static string PathCombine(params string[] paths) 26 public static string PathCombine(params string[] paths)
27 { 27 {
28 return paths.Aggregate((x, y) => Path.Combine(x, y)); 28 return paths.Aggregate((x, y) => Path.Combine(x, y));
29 } 29 }
30   30  
31 /// <summary> 31 /// <summary>
32 /// Strip characters that are incompatible with file names. 32 /// Strip characters that are incompatible with file names.
33 /// </summary> 33 /// </summary>
34 /// <param name="fileName">the name of the file</param> 34 /// <param name="fileName">the name of the file</param>
35 /// <returns>a clean string</returns> 35 /// <returns>a clean string</returns>
36 private static string CleanFileName(string fileName) 36 private static string CleanFileName(string fileName)
37 { 37 {
38 return Path.GetInvalidFileNameChars() 38 return Path.GetInvalidFileNameChars()
39 .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); 39 .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty));
40 } 40 }
41   41  
42 /////////////////////////////////////////////////////////////////////////// 42 ///////////////////////////////////////////////////////////////////////////
43 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // 43 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
44 /////////////////////////////////////////////////////////////////////////// 44 ///////////////////////////////////////////////////////////////////////////
45 /// <summary> 45 /// <summary>
46 /// Compress a stream using Deflate compression. 46 /// Compress a stream using Deflate compression.
47 /// </summary> 47 /// </summary>
48 /// <param name="uncompressedSourceStream">the stream to compress</param> 48 /// <param name="uncompressedSourceStream">the stream to compress</param>
49 /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param> 49 /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param>
50 /// <param name="leaveOpen">whether to leave the compression stream open</param> 50 /// <param name="leaveOpen">whether to leave the compression stream open</param>
51 /// <returns>an awaitable Task</returns> 51 /// <returns>an awaitable Task</returns>
52 public static async Task DeflateCompress(this Stream uncompressedSourceStream, 52 public static async Task DeflateCompress(this Stream uncompressedSourceStream,
53 Stream compressedDestinationStream, bool leaveOpen = false) 53 Stream compressedDestinationStream, bool leaveOpen = false)
54 { 54 {
55 using ( 55 using (
56 var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress, 56 var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress,
57 leaveOpen)) 57 leaveOpen))
58 { 58 {
59 await uncompressedSourceStream.CopyToAsync(compressionStream); 59 await uncompressedSourceStream.CopyToAsync(compressionStream);
60 } 60 }
61 } 61 }
62   62  
63 /////////////////////////////////////////////////////////////////////////// 63 ///////////////////////////////////////////////////////////////////////////
64 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // 64 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
65 /////////////////////////////////////////////////////////////////////////// 65 ///////////////////////////////////////////////////////////////////////////
66 /// <summary> 66 /// <summary>
67 /// Decompress a Deflate-compressed stream. 67 /// Decompress a Deflate-compressed stream.
68 /// </summary> 68 /// </summary>
69 /// <param name="compressedSourceStream">the compressed stream to decompress</param> 69 /// <param name="compressedSourceStream">the compressed stream to decompress</param>
70 /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param> 70 /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param>
71 /// <param name="leaveOpen">whether to leave the stream open after decompression</param> 71 /// <param name="leaveOpen">whether to leave the stream open after decompression</param>
72 /// <returns>an awaitable Task</returns> 72 /// <returns>an awaitable Task</returns>
73 public static async Task DeflateDecompress(this Stream compressedSourceStream, 73 public static async Task DeflateDecompress(this Stream compressedSourceStream,
74 Stream uncompressedDestinationStream, bool leaveOpen = false) 74 Stream uncompressedDestinationStream, bool leaveOpen = false)
75 { 75 {
76 using ( 76 using (
77 var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress, 77 var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress,
78 leaveOpen)) 78 leaveOpen))
79 { 79 {
80 await decompressionStream.CopyToAsync(uncompressedDestinationStream); 80 await decompressionStream.CopyToAsync(uncompressedDestinationStream);
81 } 81 }
82 } 82 }
83   83  
84 /////////////////////////////////////////////////////////////////////////// 84 ///////////////////////////////////////////////////////////////////////////
85 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // 85 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
86 /////////////////////////////////////////////////////////////////////////// 86 ///////////////////////////////////////////////////////////////////////////
87 /// <summary> 87 /// <summary>
88 /// Compress a stream using GZip compression. 88 /// Compress a stream using GZip compression.
89 /// </summary> 89 /// </summary>
90 /// <param name="uncompressedSourceStream">the stream to compress</param> 90 /// <param name="uncompressedSourceStream">the stream to compress</param>
91 /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param> 91 /// <param name="compressedDestinationStream">the stream to write the compressed stream to</param>
92 /// <param name="leaveOpen">whether to leave the stream open after compressing</param> 92 /// <param name="leaveOpen">whether to leave the stream open after compressing</param>
93 /// <returns>an awaitable Task</returns> 93 /// <returns>an awaitable Task</returns>
94 public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream, 94 public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream,
95 bool leaveOpen = false) 95 bool leaveOpen = false)
96 { 96 {
97 using ( 97 using (
98 var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen) 98 var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen)
99 ) 99 )
100 { 100 {
101 await uncompressedSourceStream.CopyToAsync(compressionStream); 101 await uncompressedSourceStream.CopyToAsync(compressionStream);
102 } 102 }
103 } 103 }
104   104  
105 /////////////////////////////////////////////////////////////////////////// 105 ///////////////////////////////////////////////////////////////////////////
106 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 // 106 // Copyright (C) 2017 Wizardry and Steamworks - License: GNU GPLv3 //
107 /////////////////////////////////////////////////////////////////////////// 107 ///////////////////////////////////////////////////////////////////////////
108 /// <summary> 108 /// <summary>
109 /// Decompress a GZiped stream. 109 /// Decompress a GZiped stream.
110 /// </summary> 110 /// </summary>
111 /// <param name="compressedSourceStream">the stream to decompress</param> 111 /// <param name="compressedSourceStream">the stream to decompress</param>
112 /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param> 112 /// <param name="uncompressedDestinationStream">the stream to write the decompressed stream to</param>
113 /// <param name="leaveOpen">whether the decompression stream should be left open</param> 113 /// <param name="leaveOpen">whether the decompression stream should be left open</param>
114 /// <returns>an awaitable Task</returns> 114 /// <returns>an awaitable Task</returns>
115 public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream, 115 public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream,
116 bool leaveOpen = false) 116 bool leaveOpen = false)
117 { 117 {
118 using ( 118 using (
119 var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen)) 119 var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen))
120 { 120 {
121 await decompressionStream.CopyToAsync(uncompressedDestinationStream); 121 await decompressionStream.CopyToAsync(uncompressedDestinationStream);
122 } 122 }
123 } 123 }
124   124  
125 /////////////////////////////////////////////////////////////////////////// 125 ///////////////////////////////////////////////////////////////////////////
126 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // 126 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
127 /////////////////////////////////////////////////////////////////////////// 127 ///////////////////////////////////////////////////////////////////////////
128 /// <summary> 128 /// <summary>
129 /// Splits a path using a separator and an escape character. 129 /// Splits a path using a separator and an escape character.
130 /// </summary> 130 /// </summary>
131 /// <param name="path">the path to split</param> 131 /// <param name="path">the path to split</param>
132 /// <param name="separator">the separator character</param> 132 /// <param name="separator">the separator character</param>
133 /// <param name="escape">the escape character</param> 133 /// <param name="escape">the escape character</param>
134 /// <param name="includeRootSeparator">true if the initial separator should be included</param> 134 /// <param name="includeRootSeparator">true if the initial separator should be included</param>
135 /// <returns>path parts</returns> 135 /// <returns>path parts</returns>
136 public static IEnumerable<string> PathSplit(this string path, char separator, char? escape = null, bool includeRootSeparator = true) 136 public static IEnumerable<string> PathSplit(this string path, char separator, char? escape = null, bool includeRootSeparator = true)
137 { 137 {
138 var s = new Stack<char>(); 138 var s = new Stack<char>();
139 var p = new StringBuilder(); 139 var p = new StringBuilder();
140 foreach (var c in path) 140 foreach (var c in path)
141 { 141 {
142 if (c.Equals(escape)) 142 if (c.Equals(escape))
143 { 143 {
144 s.Push(c); 144 s.Push(c);
145 continue; 145 continue;
146 } 146 }
147 if (c.Equals(separator)) 147 if (c.Equals(separator))
148 { 148 {
149 if (s.Count.Equals(0) || !s.Peek().Equals(escape)) 149 if (s.Count().Equals(0) || !s.Peek().Equals(escape))
150 { 150 {
151 if (p.Length.Equals(0) && includeRootSeparator) 151 if (p.Length.Equals(0) && includeRootSeparator)
152 { 152 {
153 p.Append(c); 153 p.Append(c);
154 continue; 154 continue;
155 } 155 }
156 yield return p.ToString(); 156 yield return p.ToString();
157 p = new StringBuilder(); 157 p = new StringBuilder();
158 continue; 158 continue;
159 } 159 }
160 s.Pop(); 160 s.Pop();
161 while (!s.Count.Equals(0)) 161 while (!s.Count().Equals(0))
162 { 162 {
163 p.Append(s.Pop()); 163 p.Append(s.Pop());
164 } 164 }
165 p.Append(c); 165 p.Append(c);
166 continue; 166 continue;
167 } 167 }
168 p.Append(c); 168 p.Append(c);
169 } 169 }
170 while (!s.Count.Equals(0)) 170 while (!s.Count().Equals(0))
171 { 171 {
172 p.Append(s.Pop()); 172 p.Append(s.Pop());
173 } 173 }
174 yield return p.ToString(); 174 yield return p.ToString();
175 } 175 }
176 } 176 }
177 } 177 }
178   178