All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
fragment.cpp
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 #include "fragment.h"
23 #include "urisyn.h"
24 #include <stdexcept>
25 namespace te
26 {
27  namespace common
28  {
29  namespace uri
30  {
32  fragment::fragment(const std::string& v) {
33  if (!v.empty()) {
34  std::string::const_iterator first = v.begin();
35  if (!parse(first, v.end(), *this) || first != v.end())
36  throw std::invalid_argument("invalid URI fragment: \"" + v + "\"");
37  }
38  }
39  std::string fragment::encoding() const {
41  }
42  bool parse(std::string::const_iterator& first, std::string::const_iterator last, fragment& v, std::string* errs) {
43  std::string::const_iterator f = first;
44  fragment tmp;
45  std::string e;
47  if (tmp.empty())
48  return false;
49  if (!urisyn::decode(tmp.string_)) { // Invalid encoding.
50  if (!errs)
51  return false;
52  e = ", invalid encoding (using as is)";
53  }
54  if (errs && !e.empty()) {
55  if (!errs->empty())
56  *errs += "; ";
57  *errs += "URI fragment: \"" + tmp.string_ + "\"" + e;
58  }
59  v = tmp;
60  first = f;
61  return true;
62  }
63  }
64  }
65 }
bool empty() const
Test if null/empty.
Definition: fragment.h:49
friend bool TECOMMONEXPORT parse(std::string::const_iterator &first, std::string::const_iterator last, fragment &v, std::string *errs)
Parse URI fragment, returning whether found or not and advancing first and setting fragment if found...
Definition: fragment.cpp:42
bool 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...
Definition: authority.cpp:156
URI fragment component.
Definition: fragment.h:41
bool parse(const traits &ts, std::string::const_iterator &first, std::string::const_iterator last, std::string &comp, char *endc)
Parse the URI componet, returning whether successful and setting the string and end char and advancin...
Definition: urisyn.cpp:136
std::string encode(const traits &ts, const std::string &comp)
Encode the URI (sub) component.
Definition: urisyn.cpp:157
std::string encoding() const
Calculate encoded string.
Definition: fragment.cpp:39
bool decode(std::string &s)
Decode the pct-encoded (hex) sequences, if any, return success.
Definition: urisyn.cpp:171
const traits FRAGMENT_TRAITS
fragment traits
Definition: urisyn.cpp:115