wasSharp – Diff between revs 7 and 10
?pathlinks?
Rev 7 | Rev 10 | |||
---|---|---|---|---|
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.Linq; |
9 | using System.Linq; |
|
10 | using System.Runtime.InteropServices; |
- | ||
11 | using System.Text; |
10 | using System.Text; |
|
12 | |
11 | |
|
13 | namespace wasSharp |
12 | namespace wasSharp |
|
14 | { |
13 | { |
|
15 | public static class IO |
14 | public static class IO |
|
16 | { |
15 | { |
|
17 | /////////////////////////////////////////////////////////////////////////// |
16 | /////////////////////////////////////////////////////////////////////////// |
|
18 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
17 | // Copyright (C) 2015 Wizardry and Steamworks - License: GNU GPLv3 // |
|
19 | /////////////////////////////////////////////////////////////////////////// |
18 | /////////////////////////////////////////////////////////////////////////// |
|
20 | /// <summary> |
19 | /// <summary> |
|
21 | /// Combine multiple paths. |
20 | /// Combine multiple paths. |
|
22 | /// </summary> |
21 | /// </summary> |
|
23 | /// <param name="paths">an array of paths</param> |
22 | /// <param name="paths">an array of paths</param> |
|
24 | /// <returns>a combined path</returns> |
23 | /// <returns>a combined path</returns> |
|
25 | public static string PathCombine(params string[] paths) |
24 | public static string PathCombine(params string[] paths) |
|
26 | { |
25 | { |
|
27 | return paths.Aggregate((x, y) => Path.Combine(x, y)); |
26 | return paths.Aggregate((x, y) => Path.Combine(x, y)); |
|
28 | } |
27 | } |
|
29 | |
28 | |
|
30 | /// <summary> |
29 | /// <summary> |
|
31 | /// Strip characters that are incompatible with file names. |
30 | /// Strip characters that are incompatible with file names. |
|
32 | /// </summary> |
31 | /// </summary> |
|
33 | /// <param name="fileName">the name of the file</param> |
32 | /// <param name="fileName">the name of the file</param> |
|
34 | /// <returns>a clean string</returns> |
33 | /// <returns>a clean string</returns> |
|
35 | private static string CleanFileName(string fileName) |
34 | private static string CleanFileName(string fileName) |
|
36 | { |
35 | { |
|
37 | return Path.GetInvalidFileNameChars() |
36 | return Path.GetInvalidFileNameChars() |
|
38 | .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); |
37 | .Aggregate(fileName, (current, c) => current.Replace(c.ToString(), string.Empty)); |
|
39 | } |
38 | } |
|
40 | |
39 | |
|
41 | /////////////////////////////////////////////////////////////////////////// |
40 | /////////////////////////////////////////////////////////////////////////// |
|
42 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
41 | // Copyright (C) 2016 Wizardry and Steamworks - License: GNU GPLv3 // |
|
43 | /////////////////////////////////////////////////////////////////////////// |
42 | /////////////////////////////////////////////////////////////////////////// |
|
44 | /// <summary> |
43 | /// <summary> |
|
45 | /// Splits a path using a separator and an escape character. |
44 | /// Splits a path using a separator and an escape character. |
|
46 | /// </summary> |
45 | /// </summary> |
|
47 | /// <param name="path">the path to split</param> |
46 | /// <param name="path">the path to split</param> |
|
48 | /// <param name="separator">the separator character</param> |
47 | /// <param name="separator">the separator character</param> |
|
49 | /// <param name="escape">the escape character</param> |
48 | /// <param name="escape">the escape character</param> |
|
50 | /// <returns>path parts</returns> |
49 | /// <returns>path parts</returns> |
|
51 | public static IEnumerable<string> PathSplit(this string path, char separator, char? escape) |
50 | public static IEnumerable<string> PathSplit(this string path, char separator, char? escape) |
|
52 | { |
51 | { |
|
53 | Stack<char> s = new Stack<char>(); |
52 | var s = new Stack<char>(); |
|
54 | StringBuilder p = new StringBuilder(); |
53 | var p = new StringBuilder(); |
|
55 | foreach (char c in path) |
54 | foreach (var c in path) |
|
56 | { |
55 | { |
|
57 | if (c == escape) |
56 | if (c == escape) |
|
58 | { |
57 | { |
|
59 | s.Push(c); |
58 | s.Push(c); |
|
60 | continue; |
59 | continue; |
|
61 | } |
60 | } |
|
62 | if (c == separator) |
61 | if (c == separator) |
|
63 | { |
62 | { |
|
64 | if (s.Count.Equals(0) || !s.Peek().Equals(escape)) |
63 | if (s.Count.Equals(0) || !s.Peek().Equals(escape)) |
|
65 | { |
64 | { |
|
66 | yield return p.ToString(); |
65 | yield return p.ToString(); |
|
67 | p = new StringBuilder(); |
66 | p = new StringBuilder(); |
|
68 | continue; |
67 | continue; |
|
69 | } |
68 | } |
|
70 | s.Pop(); |
69 | s.Pop(); |
|
71 | while (!s.Count.Equals(0)) |
70 | while (!s.Count.Equals(0)) |
|
72 | { |
71 | { |
|
73 | p.Append(s.Pop()); |
72 | p.Append(s.Pop()); |
|
74 | } |
73 | } |
|
75 | p.Append(c); |
74 | p.Append(c); |
|
76 | continue; |
75 | continue; |
|
77 | } |
76 | } |
|
78 | p.Append(c); |
77 | p.Append(c); |
|
79 | } |
78 | } |
|
80 | while (!s.Count.Equals(0)) |
79 | while (!s.Count.Equals(0)) |
|
81 | { |
80 | { |
|
82 | p.Append(s.Pop()); |
81 | p.Append(s.Pop()); |
|
83 | } |
82 | } |
|
84 | yield return p.ToString(); |
83 | yield return p.ToString(); |
|
85 | } |
84 | } |
|
86 | } |
85 | } |
|
87 | } |
86 | } |
|
88 | |
87 | |
|
89 |
|
88 |
|
|
90 | |
89 | |
|
91 | |
90 | |
|
92 | |
91 | |