source: rrlib_uri/tURI.h @ 7:12724774ac13

17.03
Last change on this file since 7:12724774ac13 was 7:12724774ac13, checked in by Max Reichardt <mreichardt@…>, 3 years ago

Adds tURI constructor for const char* arguments

File size: 5.5 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/uri/tURI.h
23 *
24 * \author  Max Reichardt
25 *
26 * \date    2017-01-29
27 *
28 * \brief   Contains tURI
29 *
30 * \b tURI
31 *
32 * This class wraps a RFC 3986 URI.
33 *
34 */
35//----------------------------------------------------------------------
36#ifndef __rrlib__uri__tURI_h__
37#define __rrlib__uri__tURI_h__
38
39//----------------------------------------------------------------------
40// External includes (system with <>, local with "")
41//----------------------------------------------------------------------
42
43//----------------------------------------------------------------------
44// Internal includes with ""
45//----------------------------------------------------------------------
46#include "rrlib/uri/tURIElements.h"
47
48//----------------------------------------------------------------------
49// Namespace declaration
50//----------------------------------------------------------------------
51namespace rrlib
52{
53namespace uri
54{
55
56//----------------------------------------------------------------------
57// Forward declarations / typedefs / enums
58//----------------------------------------------------------------------
59
60//----------------------------------------------------------------------
61// Class declaration
62//----------------------------------------------------------------------
63//! URI
64/*!
65 * This class wraps a RFC 3986 URI.
66 */
67class tURI
68{
69
70//----------------------------------------------------------------------
71// Public methods and typedefs
72//----------------------------------------------------------------------
73public:
74
75  static const char* cUNENCODED_RESERVED_CHARACTERS_PATH;  // !$&'()*+,;=:@
76
77  /*! URI string */
78  tURI(const std::string uri = std::string()) :
79    uri(uri)
80  {}
81
82  tURI(const char* uri) :
83    uri(uri)
84  {}
85
86
87  /*! Creates local URI from path */
88  tURI(const tPath& path, const char* unencoded_reserved_characters = cUNENCODED_RESERVED_CHARACTERS_PATH);
89
90  /*!
91   * Converts percent-encoded string to decoded string
92   *
93   * \param decode_buffer Buffer for decoded string. As no character could be percent-encoded, should have a size == encoded.Length()
94   * \param encoded Percent-encoded string
95   * \return Pointer to character after the last character written in decode_buffer (notably string in decode_buffer is not null-terminated)
96   * \throws std::invalid_argument if string cannot be decoded
97   */
98  static char* Decode(char* decode_buffer, const tStringRange& encoded);
99
100  /*!
101   * Converts decoded string to percent-encoded string
102   *
103   * \param encode_buffer Buffer for percent-encoded string. As all characters could be percent-encoded, should have a size >= 3 * decoded.Length()
104   * \param decoded String to encode
105   * \param unencoded_reserved_characters Reserved characters not to encode (see constants above)
106   * \return Pointer to character after the last character written in encode_buffer (notably string in encode_buffer is not null-terminated)
107   */
108  static char* Encode(char* encode_buffer, const tStringRange& decoded, const char* unencoded_reserved_characters);
109
110  /*!
111   * Parses URI
112   *
113   * \param result Object to store results in. If many URI are parsed it makes sense to reuse the object - as this avoid reallocation of memory if its fields are sufficiently large.
114   * \throw Throws std::invalid_argument if URI could not be parsed
115   */
116  void Parse(tURIElements& result) const;
117
118  /*!
119   * \return URI string
120   */
121  const std::string& ToString() const
122  {
123    return uri;
124  }
125
126  friend inline serialization::tInputStream& operator >> (serialization::tInputStream& stream, tURI& uri)
127  {
128    stream.ReadString(uri.uri);
129    return stream;
130  }
131  friend inline serialization::tStringInputStream& operator >> (serialization::tStringInputStream& stream, tURI& uri)
132  {
133    stream >> uri.uri;
134    return stream;
135  }
136//----------------------------------------------------------------------
137// Private fields and methods
138//----------------------------------------------------------------------
139private:
140
141  /*! URI string */
142  std::string uri;
143
144};
145
146inline bool operator==(const tURI& lhs, const tURI& rhs)
147{
148  return lhs.ToString() == rhs.ToString();
149}
150
151inline bool operator<(const tURI& lhs, const tURI& rhs)
152{
153  return lhs.ToString() < rhs.ToString();
154}
155
156inline serialization::tOutputStream& operator << (serialization::tOutputStream& stream, const tURI& uri)
157{
158  stream << uri.ToString();
159  return stream;
160}
161
162inline serialization::tStringOutputStream& operator << (serialization::tStringOutputStream& stream, const tURI& uri)
163{
164  stream << uri.ToString();
165  return stream;
166}
167
168//----------------------------------------------------------------------
169// End of namespace declaration
170//----------------------------------------------------------------------
171}
172}
173
174
175#endif
Note: See TracBrowser for help on using the repository browser.