1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity average)
4 module util.Path;
5 
6 import common;
7 
8 import tango.io.FilePath;
9 import tango.sys.Environment;
10 import std.path;
11 
12 enum dirSep = dirSeparator[0]; /// Dir separator character.
13 alias Environment = tango.sys.Environment.Environment;
14 
15 /// This class is like FilePath, but adds additional
16 /// operators to make things easier.
17 class Path : FilePath
18 {
19   /// Constructs from a string.
20   this(cstring s)
21   {
22     super(s.dup);
23   }
24 
25   /// Constructs an empty Path.
26   this()
27   {
28     super();
29   }
30 
31   /// Returns a new Path object.
32   static Path opCall(cstring s)
33   {
34     return new Path(s);
35   }
36 
37   /// ditto
38   static Path opCall()
39   {
40     return new Path();
41   }
42 
43   Path append(cstring s)
44   {
45     super.append(s);
46     return this;
47   }
48 
49   Path cat(cstring s)
50   {
51     super.cat(s);
52     return this;
53   }
54 
55   alias set = super.set;
56 
57   Path set(cstring s)
58   {
59     super.set(s);
60     return this;
61   }
62 
63   Path set(Path p)
64   {
65     super.set(p);
66     return this;
67   }
68 
69   Path dup()
70   {
71     return Path(this[]);
72   }
73 
74   /// The path without its extension. Returns a copy.
75   Path noext()
76   {
77     auto ext_len = this.suffix().length;
78     return Path(this[][0..$-ext_len]);
79   }
80 
81   /// Returns a normalized path, removing parts like "./" and "../".
82   Path normalize()
83   {
84     //import tango.io.Path : normalize;
85     //return Path(normalize(this[]));
86     return Path(buildNormalizedPath(this[]));
87   }
88 
89   /// Returns an absolute path relative to the current working directory.
90   Path absolute()
91   {
92     return Path(Environment.toAbsolute(this[].dup));
93   }
94 
95   cstring name()
96   {
97     return super.name();
98   }
99 
100   cstring folder()
101   {
102     return super.folder();
103   }
104 
105   /// Append s. p /= s
106   Path opDivAssign(cstring s)
107   {
108     return this.append(s);
109   }
110 
111   /// ditto
112   Path opDivAssign(Path p)
113   {
114     return this.append(p[]);
115   }
116 
117   /// Concatenate s. path ~= s
118   Path opCatAssign(cstring s)
119   {
120     return this.cat(s);
121   }
122 
123   /// ditto
124   Path opCatAssign(Path p)
125   {
126     return this.cat(p[]);
127   }
128 
129   /// Append s. Returns a copy.
130   Path opDiv(cstring s)
131   {
132     return this.dup().append(s);
133   }
134   /// ditto
135   Path opDiv(Path p)
136   {
137     return this.dup().append(p[]);
138   }
139 
140   /// Concatenate s. Returns a copy.
141   Path opCat(cstring s)
142   {
143     return this.dup().cat(s);
144   }
145   /// ditto
146   Path opCat(Path p)
147   {
148     return this.dup().cat(p[]);
149   }
150 
151   /// Returns the path as a string.
152   inout(char)[] opSlice() inout
153   {
154     return cString()[0..$-1];
155   }
156 }