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