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