Servus  1.0.0
A small set of network oriented utilities in C++ including a zeroconf implementation.
 All Classes Files Functions Variables Enumerations Enumerator Friends Macros Pages
uri.h
1 /* Copyright (c) 2013-2014, ahmet.bilgili@epfl.ch
2  * 2014, Stefan.Eilemann@epfl.ch
3  *
4  * This file is part of Servus <https://github.com/HBPVIS/Servus>
5  *
6  * This library is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Lesser General Public License version 3.0 as published
8  * by the Free Software Foundation.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13  * details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef SERVUS_URI_H
21 #define SERVUS_URI_H
22 
23 #include <servus/api.h>
24 #include <servus/types.h>
25 
26 #include <map>
27 
28 namespace servus
29 {
30 namespace detail { class URI; }
31 
54 class URI
55 {
56 public:
57  typedef std::map< std::string, std::string > KVMap;
58  typedef KVMap::const_iterator ConstKVIter;
59 
61  SERVUS_API URI();
62 
68  SERVUS_API explicit URI( const std::string& uri );
69 
71  SERVUS_API explicit URI( const char* uri );
72 
74  SERVUS_API URI( const URI& from );
75 
76  SERVUS_API ~URI();
77 
79  SERVUS_API URI& operator = ( const URI& rhs );
80 
82  SERVUS_API bool operator == ( const URI& rhs ) const;
83 
85  SERVUS_API bool operator != ( const URI& rhs ) const;
86 
89  SERVUS_API const std::string& getScheme() const;
90  SERVUS_API const std::string& getUserinfo() const;
91  SERVUS_API uint16_t getPort() const;
92  SERVUS_API const std::string& getHost() const;
93  SERVUS_API const std::string& getPath() const;
94  SERVUS_API const std::string& getQuery() const;
95  SERVUS_API const std::string& getFragment() const;
97 
100  SERVUS_API void setScheme( const std::string& scheme );
101  SERVUS_API void setHost( const std::string& host );
102  SERVUS_API void setPort( uint16_t port );
104 
110  SERVUS_API ConstKVIter queryBegin() const;
111 
115  SERVUS_API ConstKVIter queryEnd() const;
116 
120  SERVUS_API ConstKVIter findQuery( const std::string& key ) const;
121 
123  SERVUS_API void addQuery( const std::string& key,
124  const std::string& value );
126 
127 private:
128  detail::URI* const _impl;
129 };
130 
131 inline std::ostream& operator << ( std::ostream& os, const URI& uri )
132 {
133  if( !uri.getScheme().empty( ))
134  os << uri.getScheme() << "://";
135  if( !uri.getUserinfo().empty( ))
136  os << uri.getUserinfo() << "@";
137  os << uri.getHost();
138  if( uri.getPort( ))
139  os << ':' << uri.getPort();
140  os << uri.getPath();
141  if( !uri.getQuery().empty( ))
142  os << '?' << uri.getQuery();
143  if( !uri.getFragment().empty( ))
144  os << '#' << uri.getFragment();
145  return os;
146 }
147 }
148 
149 namespace std
150 {
151 inline std::string to_string( const servus::URI& uri )
152 {
153  ostringstream os;
154  os << uri;
155  return os.str();
156 }
157 }
158 #endif // SERVUS_URI_H
SERVUS_API URI & operator=(const URI &rhs)
Assign the data from another URI.
The URI class parses the given uri using the generic syntax from RFC3986.
Definition: uri.h:54
SERVUS_API ConstKVIter queryBegin() const
Defines export visibility macros for Servus.
SERVUS_API bool operator==(const URI &rhs) const
Equals operator.
SERVUS_API bool operator!=(const URI &rhs) const
Not equals operator.
SERVUS_API ConstKVIter queryEnd() const
SERVUS_API void addQuery(const std::string &key, const std::string &value)
Add a key-value pair to the query.
SERVUS_API ConstKVIter findQuery(const std::string &key) const
SERVUS_API URI()
Construct an empty URI.