path.h
Go to the documentation of this file.
1 /*
2 Copyright (c) 2009 zooml.com
3 
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22 #ifndef __TERRALIB_COMMON_INTERNAL_PATH_H
23 #define __TERRALIB_COMMON_INTERNAL_PATH_H
24 #include "../Config.h"
25 #include <string>
26 #include <list>
27 #include <iostream>
28 namespace te
29 {
30  namespace common
31  {
32  namespace uri
33  {
34  /** \brief URI path component.
35  *
36  * Note that there are potential ambiguities
37  * when using ":" and "//" in paths in URIs (see RFC).
38  *
39  * Syntax: Segments are pct-decoded after parsing.
40  * <pre>
41  * ALPHA = %x41-5A | %x61-7A ; A-Z | a-z
42  * DIGIT = %x30-39 ; 0-9
43  * sub-delims = "!" | "$" | "&" | "'" | "(" | ")" | "*"
44  * | "+" | "," | ";" | "="
45  * unreserved = ALPHA | DIGIT | "-" | "." | "_" | "~"
46  * pchar = unreserved | pct-encoded | sub-delims | ":" | "@"
47  * segment = *pchar
48  * path = *( "/" segment )
49  * </pre>
50  * @see http://tools.ietf.org/html/rfc3986#section-3.3 */
52  public:
53  typedef std::list<std::string> segments_type; ///< segments type
54  typedef segments_type::const_iterator const_iterator; ///< segments const iterator type
55  path(); ///< Construct.
56  /// Construct from encoded string. For example "/foo/bar" or
57  /// "/foo/ba%20r". Empty segments are removed, i.e. "a//b"
58  /// becomes "a/b". See \c parse() for less strict construction.
59  /// @exception std::invalid_argument if invalid encoding
60  path(const std::string& v);
61  /// Test if empty and not absolute.
62  bool empty() const {return segments_.empty() && !absolute_;}
63  /// Test if absolute, meaning path begins with "/".
64  bool absolute() const {return absolute_;}
65  /// Set whether absolute or not.
66  void absolute(bool v) {absolute_ = v;}
67  /// Test if a directory, meaning the path ends in a "/".
68  bool is_directory() const {return is_directory_;}
69  /// Set whether a directory or not.
70  void is_directory(bool v) {is_directory_ = v;}
71  /// Get front segment.
72  /// @exception std::out_of_range if empty
73  const std::string& front() const;
74  /// Get back segment.
75  /// @exception std::out_of_range if empty
76  const std::string& back() const;
77  /// Test if the given other URI path is a prefix of this.
78  /// Must match whether absolute as well.
79  bool match_prefix(const path& rhs) const;
80  void pop_front(); ///< Pop front segment and reset absolute.
81  /// Remove the back of the path if it matches the
82  /// argument (including whether directory or not) and
83  /// return whether removed/matched. Note that if this
84  /// starts as a directory it stays a directory. If back is
85  /// absolute and it does not match all of this or this
86  /// is not absolute then there is no match.
87  bool pop_back(const path& back);
88  void clear(); ///< Clear segments and reset absolute and is_directory.
89  size_t size() const {return segments_.size();} /// Get number of segments.
90  bool operator ==(const path& rhs) const; ///< Equal operator.
91  bool operator !=(const path& rhs) const {return !(*this == rhs);} ///< Not equal operator.
92  /// Less operator. Examines the following in order:
93  /// <ol><li>relative are less than absolute</li>
94  /// <li>lexicographical comparison of segments</li>
95  /// <li>non-directories are less than directories</li></ol>
96  bool operator <(const path& rhs) const;
97  /// Append unencoded segment and reset is_directory.
98  path operator +(const std::string& rhs) const {
99  path result = *this;
100  return result += rhs;
101  }
102  path& operator +=(const std::string& rhs); ///< Append unencoded segment and reset is_directory.
103  /// Append path and set is_directory according to rhs.
104  path operator +(const path& rhs) const {
105  path result = *this;
106  return result += rhs;
107  }
108  path& operator +=(const path& rhs); ///< Append path and set is_directory according to rhs.
109  std::string encoding() const; ///< Calculate encoded string.
110  std::string string() const; ///< Return path string. Added by Lauro
111  const_iterator begin() const {return segments_.begin();} ///< Get iterator at beginning.
112  const_iterator end() const {return segments_.end();} ///< Get iterator at end.
113  static const char SEPARATOR_CHAR; ///< separator ('/')
114  private:
115  friend bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, path& v, std::string* errs); // Modified by Lauro
116  bool absolute_;
118  segments_type segments_;
119  };
120  /// Parse URI path, returning whether found or not
121  /// and advancing first and setting path if found.
122  /// Does not skip leading space.
123  ///
124  /// If \p errs is specified the following take place:<ul>
125  /// <li>Errors in decoding segments do not
126  /// cause an immediate false return and and error message
127  /// is reported in the error string \p errs. The segment
128  /// is used without decoding (i.e. assumes an unencoded '%').</li>
129  /// </ul>
130  bool TECOMMONEXPORT parse(std::string::const_iterator& first, std::string::const_iterator last, path& v, std::string* errs = 0);
131  std::string TECOMMONEXPORT convert(const path& v); ///< URI path to string
132  /// String to URI path, returning true if set, which is
133  /// when the string is not empty or all isspace.
134  /// isspace before and/or after value OK.
135  /// @exception std::invalid_argument on conversion error
136  bool TECOMMONEXPORT convert(const std::string& s, path& v);
137  /// Stream out path encoding.
138  inline std::ostream& operator <<(std::ostream& os, const path& v) {return os << v.encoding();}
139  }
140  }
141 }
142 #endif
void is_directory(bool v)
Set whether a directory or not.
Definition: path.h:70
TEDATAACCESSEXPORT te::da::Expression * operator<(const te::da::Expression &e1, const te::da::Expression &e2)
std::string encoding() const
Calculate encoded string.
std::ostream & operator<<(std::ostream &os, const authority &v)
Stream out URI authority.
Definition: authority.h:95
const_iterator end() const
Get iterator at end.
Definition: path.h:112
URI path component.
Definition: path.h:51
bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, authority &v)
Parse URI authority, returning whether found or not and advancing first and setting authority if foun...
segments_type segments_
Definition: path.h:118
bool is_directory() const
Test if a directory, meaning the path ends in a "/".
Definition: path.h:68
TEDATAACCESSEXPORT te::da::Expression * operator==(const te::da::Expression &e1, const te::da::Expression &e2)
TEDATAACCESSEXPORT te::da::Expression * operator!=(const te::da::Expression &e1, const te::da::Expression &e2)
static const char SEPARATOR_CHAR
separator ('/')
Definition: path.h:113
bool empty() const
Test if empty and not absolute.
Definition: path.h:62
segments_type::const_iterator const_iterator
segments const iterator type
Definition: path.h:54
std::list< std::string > segments_type
segments type
Definition: path.h:53
URI C++ Library.
#define TECOMMONEXPORT
You can use this macro in order to export/import classes and functions from this module.
Definition: Config.h:65
const_iterator begin() const
Get iterator at beginning.
Definition: path.h:111
bool absolute() const
Test if absolute, meaning path begins with "/".
Definition: path.h:64
size_t size() const
Definition: path.h:89
std::string TECOMMONEXPORT convert(const path &v)
URI path to string.
void absolute(bool v)
Set whether absolute or not.
Definition: path.h:66