wasSharp – Blame information for rev 24

Subversion Repositories:
Rev:
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 {
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>
11 office 26 public static string PathCombine(string separator = @"\", params string[] paths)
7 office 27 {
11 office 28 return string.Join(separator, paths);
7 office 29 }
30  
31 /// <summary>
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  
24 office 42 public static async Task DeflateCompress(this Stream uncompressedSourceStream,
43 Stream compressedDestinationStream, bool leaveOpen = false)
44 {
45 using (
46 var compressionStream = new DeflateStream(compressedDestinationStream, CompressionMode.Compress,
47 leaveOpen))
48 {
49 await uncompressedSourceStream.CopyToAsync(compressionStream);
50 }
51 }
52  
53 public static async Task DeflateDecompress(this Stream compressedSourceStream,
54 Stream uncompressedDestinationStream, bool leaveOpen = false)
55 {
56 using (
57 var decompressionStream = new DeflateStream(compressedSourceStream, CompressionMode.Decompress,
58 leaveOpen))
59 {
60 await decompressionStream.CopyToAsync(uncompressedDestinationStream);
61 }
62 }
63  
64 public static async Task GZipCompress(this Stream uncompressedSourceStream, Stream compressedDestinationStream,
65 bool leaveOpen = false)
66 {
67 using (
68 var compressionStream = new GZipStream(compressedDestinationStream, CompressionMode.Compress, leaveOpen)
69 )
70 {
71 await uncompressedSourceStream.CopyToAsync(compressionStream);
72 }
73 }
74  
75 public static async Task GZipDecompress(this Stream compressedSourceStream, Stream uncompressedDestinationStream,
76 bool leaveOpen = false)
77 {
78 using (
79 var decompressionStream = new GZipStream(compressedSourceStream, CompressionMode.Decompress, leaveOpen))
80 {
81 await decompressionStream.CopyToAsync(uncompressedDestinationStream);
82 }
83 }
84  
85 public static string MimeType(string fileName, char extensionSeparator = '.')
86 {
87 switch (fileName.Split('.').Last().ToUpperInvariant())
88 {
89 case "AVI":
90 return "video/x-msvideo";
91 case "CSS":
92 return "text/css";
93 case "DOC":
94 return "application/msword";
95 case "GIF":
96 return "image/gif";
97 case "HTM":
98 case "HTML":
99 return "text/html";
100 case "JPG":
101 case "JPEG":
102 return "image/jpeg";
103 case "JS":
104 return "application/x-javascript";
105 case "MP3":
106 return "audio/mpeg";
107 case "PNG":
108 return "image/png";
109 case "PDF":
110 return "application/pdf";
111 case "PPT":
112 return "application/vnd.ms-powerpoint";
113 case "ZIP":
114 return "application/zip";
115 case "TXT":
116 return "text/plain";
117 case "XML":
118 return "text/xml";
119 case "SQLITE":
120 return "application/x-sqlite3";
121 default:
122 return "application/octet-stream";
123 }
124 }
125  
7 office 126 ///////////////////////////////////////////////////////////////////////////
127 // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 //
128 ///////////////////////////////////////////////////////////////////////////
129 /// <summary>
130 /// Splits a path using a separator and an escape character.
131 /// </summary>
132 /// <param name="path">the path to split</param>
133 /// <param name="separator">the separator character</param>
134 /// <param name="escape">the escape character</param>
135 /// <returns>path parts</returns>
136 public static IEnumerable<string> PathSplit(this string path, char separator, char? escape)
137 {
10 office 138 var s = new Stack<char>();
139 var p = new StringBuilder();
140 foreach (var c in path)
7 office 141 {
142 if (c == escape)
143 {
144 s.Push(c);
145 continue;
146 }
147 if (c == separator)
148 {
149 if (s.Count.Equals(0) || !s.Peek().Equals(escape))
150 {
151 yield return p.ToString();
152 p = new StringBuilder();
153 continue;
154 }
155 s.Pop();
156 while (!s.Count.Equals(0))
157 {
158 p.Append(s.Pop());
159 }
160 p.Append(c);
161 continue;
162 }
163 p.Append(c);
164 }
165 while (!s.Count.Equals(0))
166 {
167 p.Append(s.Pop());
168 }
169 yield return p.ToString();
170 }
171 }
172 }