1 // Copyright 2010 The Kyua Authors.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of Google Inc. nor the names of its contributors
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include "utils/fs/path.hpp"
31 #include "utils/fs/exceptions.hpp"
32 #include "utils/fs/operations.hpp"
33 #include "utils/sanity.hpp"
35 namespace fs = utils::fs;
41 /// Normalizes an input string to a valid path.
43 /// A normalized path cannot have empty components; i.e. there can be at most
44 /// one consecutive separator (/).
46 /// \param in The string to normalize.
48 /// \return The normalized string, representing a path.
50 /// \throw utils::fs::invalid_path_error If the path is empty.
52 normalize(const std::string& in)
55 throw fs::invalid_path_error(in, "Cannot be empty");
59 std::string::size_type pos = 0;
61 const std::string::size_type next_pos = in.find('/', pos);
63 const std::string component = in.substr(pos, next_pos - pos);
64 if (!component.empty()) {
67 else if (component != ".")
68 out += "/" + component;
71 if (next_pos == std::string::npos)
75 } while (pos != std::string::npos);
77 return out.empty() ? "/" : out;
81 } // anonymous namespace
84 /// Creates a new path object from a textual representation of a path.
86 /// \param text A valid representation of a path in textual form.
88 /// \throw utils::fs::invalid_path_error If the input text does not represent a
90 fs::path::path(const std::string& text) :
91 _repr(normalize(text))
96 /// Gets a view of the path as an array of characters.
98 /// \return A \code const char* \endcode representation for the object.
100 fs::path::c_str(void) const
102 return _repr.c_str();
106 /// Gets a view of the path as a std::string.
108 /// \return A \code std::string& \endcode representation for the object.
110 fs::path::str(void) const
116 /// Gets the branch path (directory name) of the path.
118 /// The branch path of a path with just one component (no separators) is ".".
120 /// \return A new path representing the branch path.
122 fs::path::branch_path(void) const
124 const std::string::size_type end_pos = _repr.rfind('/');
125 if (end_pos == std::string::npos)
126 return fs::path(".");
127 else if (end_pos == 0)
128 return fs::path("/");
130 return fs::path(_repr.substr(0, end_pos));
134 /// Gets the leaf name (base name) of the path.
136 /// \return A new string representing the leaf name.
138 fs::path::leaf_name(void) const
140 const std::string::size_type beg_pos = _repr.rfind('/');
142 if (beg_pos == std::string::npos)
145 return _repr.substr(beg_pos + 1);
149 /// Converts a relative path in the current directory to an absolute path.
151 /// \pre The path is relative.
153 /// \return The absolute representation of the relative path.
155 fs::path::to_absolute(void) const
158 return fs::current_path() / *this;
162 /// \return True if the representation of the path is absolute.
164 fs::path::is_absolute(void) const
166 return _repr[0] == '/';
170 /// Checks whether the path is a parent of another path.
172 /// A path is considered to be a parent of itself.
174 /// \return True if this path is a parent of p.
176 fs::path::is_parent_of(path p) const
182 } while (p != fs::path(".") && p != fs::path("/"));
187 /// Counts the number of components in the path.
189 /// \return The number of components.
191 fs::path::ncomponents(void) const
197 for (std::string::const_iterator iter = _repr.begin();
198 iter != _repr.end(); ++iter) {
207 /// Less-than comparator for paths.
209 /// This is provided to make identifiers useful as map keys.
211 /// \param p The path to compare to.
213 /// \return True if this identifier sorts before the other identifier; false
216 fs::path::operator<(const fs::path& p) const
218 return _repr < p._repr;
222 /// Compares two paths for equality.
224 /// Given that the paths are internally normalized, input paths such as
225 /// ///foo/bar and /foo///bar are exactly the same. However, this does NOT
226 /// check for true equality: i.e. this does not access the file system to check
227 /// if the paths actually point to the same object my means of links.
229 /// \param p The path to compare to.
231 /// \returns A boolean indicating whether the paths are equal.
233 fs::path::operator==(const fs::path& p) const
235 return _repr == p._repr;
239 /// Compares two paths for inequality.
241 /// See the description of operator==() for more details on the comparison
244 /// \param p The path to compare to.
246 /// \returns A boolean indicating whether the paths are different.
248 fs::path::operator!=(const fs::path& p) const
250 return _repr != p._repr;
254 /// Concatenates this path with one or more components.
256 /// \param components The new components to concatenate to the path. These are
257 /// normalized because, in general, they may come from user input. These
258 /// components cannot represent an absolute path.
260 /// \return A new path containing the concatenation of this path and the
261 /// provided components.
263 /// \throw utils::fs::invalid_path_error If components does not represent a
265 /// \throw utils::fs::join_error If the join operation is invalid because the
266 /// two paths are incompatible.
268 fs::path::operator/(const std::string& components) const
270 return (*this) / fs::path(components);
274 /// Concatenates this path with another path.
276 /// \param rest The path to concatenate to this one. Cannot be absolute.
278 /// \return A new path containing the concatenation of this path and the other
281 /// \throw utils::fs::join_error If the join operation is invalid because the
282 /// two paths are incompatible.
284 fs::path::operator/(const fs::path& rest) const
286 if (rest.is_absolute())
287 throw fs::join_error(_repr, rest._repr,
288 "Cannot concatenate a path to an absolute path");
289 return fs::path(_repr + '/' + rest._repr);
293 /// Formats a path for insertion on a stream.
295 /// \param os The output stream.
296 /// \param p The path to inject to the stream.
298 /// \return The output stream os.
300 fs::operator<<(std::ostream& os, const fs::path& p)
302 return (os << p.str());