wasSharp – Diff between revs 25 and 27
?pathlinks?
Rev 25 | Rev 27 | |||
---|---|---|---|---|
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(string separator = @"\", params string[] paths) |
26 | public static string PathCombine(string separator = @"\", params string[] paths) |
|
27 | { |
27 | { |
|
28 | return string.Join(separator, paths); |
28 | return string.Join(separator, paths); |
|
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 | /// <returns>path parts</returns> |
134 | /// <returns>path parts</returns> |
|
135 | public static IEnumerable<string> PathSplit(this string path, char separator, char? escape) |
135 | public static IEnumerable<string> PathSplit(this string path, char separator, char? escape) |
|
136 | { |
136 | { |
|
137 | var s = new Stack<char>(); |
137 | var s = new Stack<char>(); |
|
138 | var p = new StringBuilder(); |
138 | var p = new StringBuilder(); |
|
139 | foreach (var c in path) |
139 | foreach (var c in path) |
|
140 | { |
140 | { |
|
141 | if (c == escape) |
141 | if (c == escape) |
|
142 | { |
142 | { |
|
143 | s.Push(c); |
143 | s.Push(c); |
|
144 | continue; |
144 | continue; |
|
145 | } |
145 | } |
|
146 | if (c == separator) |
146 | if (c == separator) |
|
147 | { |
147 | { |
|
148 | if (s.Count.Equals(0) || !s.Peek().Equals(escape)) |
148 | if (s.Count.Equals(0) || !s.Peek().Equals(escape)) |
|
149 | { |
149 | { |
|
150 | yield return p.ToString(); |
150 | yield return p.ToString(); |
|
151 | p = new StringBuilder(); |
151 | p = new StringBuilder(); |
|
152 | continue; |
152 | continue; |
|
153 | } |
153 | } |
|
154 | s.Pop(); |
154 | s.Pop(); |
|
155 | while (!s.Count.Equals(0)) |
155 | while (!s.Count.Equals(0)) |
|
156 | { |
156 | { |
|
157 | p.Append(s.Pop()); |
157 | p.Append(s.Pop()); |
|
158 | } |
158 | } |
|
159 | p.Append(c); |
159 | p.Append(c); |
|
160 | continue; |
160 | continue; |
|
161 | } |
161 | } |
|
162 | p.Append(c); |
162 | p.Append(c); |
|
163 | } |
163 | } |
|
164 | while (!s.Count.Equals(0)) |
164 | while (!s.Count.Equals(0)) |
|
165 | { |
165 | { |
|
166 | p.Append(s.Pop()); |
166 | p.Append(s.Pop()); |
|
167 | } |
167 | } |
|
168 | yield return p.ToString(); |
168 | yield return p.ToString(); |
|
169 | } |
169 | } |
|
170 | } |
170 | } |
|
171 | } |
- | ||
172 | |
171 | } |
|
- | 172 | |
||
173 |
|
- | ||
174 | |
- | ||
175 | |
- | ||
176 | |
- |