wasSharp – Diff between revs 42 and 52

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