Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * |
| 6 | * BSD license, See the LICENSE file for more information |
| 7 | * |
| 8 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 9 | */ |
| 10 | |
Alexander Afanasyev | 3898e1b | 2013-07-27 12:03:17 -0700 | [diff] [blame] | 11 | #if __clang__ |
| 12 | #pragma clang diagnostic push |
| 13 | #pragma clang diagnostic ignored "-Wreorder" |
Alexander Afanasyev | 4699242 | 2013-08-03 17:21:15 -0700 | [diff] [blame] | 14 | #elif __GNUC__ |
| 15 | #pragma GCC diagnostic ignored "-Wreorder" |
Alexander Afanasyev | 3898e1b | 2013-07-27 12:03:17 -0700 | [diff] [blame] | 16 | #endif |
| 17 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 18 | #include "name-component.h" |
| 19 | |
| 20 | #include "detail/error.h" |
| 21 | #include "detail/uri.h" |
| 22 | |
| 23 | NDN_NAMESPACE_BEGIN |
| 24 | |
| 25 | namespace name { |
Alexander Afanasyev | 38c215d | 2014-07-11 14:20:58 -0700 | [diff] [blame] | 26 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 27 | Component::Component () |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | Component::Component (const std::string &uri) |
| 32 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 33 | copy (uri.begin (), uri.end (), std::back_inserter (*this)); |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | Component::Component (std::string::const_iterator begin, std::string::const_iterator end) |
| 37 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 38 | copy (begin, end, std::back_inserter (*this)); |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | Component::Component (const void *buf, size_t length) |
| 42 | { |
| 43 | copy (static_cast<const char*> (buf), |
| 44 | static_cast<const char*> (buf)+length, |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 45 | std::back_inserter (*this)); |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | Component & |
| 49 | Component::fromUri (const std::string &uri) |
| 50 | { |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 51 | try |
| 52 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 53 | Uri::fromEscaped (uri.begin (), uri.end (), std::back_inserter (*this)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 54 | } |
| 55 | catch (error::Uri &err) |
| 56 | { |
| 57 | // re-throwing different exception |
| 58 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 59 | << error::msg (uri) |
| 60 | << error::pos (error::get_pos (err))); |
| 61 | } |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 62 | |
| 63 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 66 | Component & |
| 67 | Component::fromUri (std::string::const_iterator begin, std::string::const_iterator end) |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 68 | { |
| 69 | try |
| 70 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 71 | Uri::fromEscaped (begin, end, std::back_inserter (*this)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 72 | } |
| 73 | catch (error::Uri &err) |
| 74 | { |
| 75 | // re-throwing different exception |
| 76 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 77 | << error::msg (std::string (begin, end)) |
| 78 | << error::pos (error::get_pos (err))); |
| 79 | } |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 80 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int |
| 84 | Component::compare (const Component &other) const |
| 85 | { |
| 86 | if (size () < other.size ()) |
| 87 | return -1; |
| 88 | |
| 89 | if (size () > other.size ()) |
| 90 | return +1; |
| 91 | |
| 92 | // now we know that sizes are equal |
| 93 | |
| 94 | std::pair<const_iterator, const_iterator> diff = mismatch (begin (), end (), other.begin ()); |
| 95 | if (diff.first == end ()) // components are actually equal |
| 96 | return 0; |
| 97 | |
Alexander Afanasyev | 38c215d | 2014-07-11 14:20:58 -0700 | [diff] [blame] | 98 | return (std::lexicographical_compare (diff.first, end (), diff.second, other.end ())) ? -1 : +1; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 101 | Component & |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 102 | Component::fromNumber (uint64_t number) |
| 103 | { |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 104 | while (number > 0) |
| 105 | { |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 106 | this->push_back (static_cast<unsigned char> (number & 0xFF)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 107 | number >>= 8; |
| 108 | } |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 109 | std::reverse (this->begin (), this->end ()); |
| 110 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 113 | Component & |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 114 | Component::fromNumberWithMarker (uint64_t number, unsigned char marker) |
| 115 | { |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 116 | this->push_back (marker); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 117 | |
| 118 | while (number > 0) |
| 119 | { |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 120 | this->push_back (static_cast<unsigned char> (number & 0xFF)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 121 | number >>= 8; |
| 122 | } |
| 123 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 124 | std::reverse (this->begin () + 1, this->end ()); |
| 125 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | std::string |
| 129 | Component::toBlob () const |
| 130 | { |
| 131 | return std::string (begin (), end ()); |
| 132 | } |
| 133 | |
| 134 | void |
| 135 | Component::toBlob (std::ostream &os) const |
| 136 | { |
| 137 | os.write (buf (), size ()); |
| 138 | } |
| 139 | |
| 140 | std::string |
| 141 | Component::toUri () const |
| 142 | { |
| 143 | std::ostringstream os; |
| 144 | toUri (os); |
Alexander Afanasyev | 38c215d | 2014-07-11 14:20:58 -0700 | [diff] [blame] | 145 | return os.str (); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void |
| 149 | Component::toUri (std::ostream &os) const |
| 150 | { |
Alexander Afanasyev | 38c215d | 2014-07-11 14:20:58 -0700 | [diff] [blame] | 151 | const uint8_t* valuePtr = reinterpret_cast<const uint8_t*>(buf()); |
| 152 | size_t valueSize = size(); |
| 153 | |
| 154 | bool gotNonDot = false; |
| 155 | for (unsigned i = 0; i < valueSize; ++i) { |
| 156 | if (valuePtr[i] != 0x2e) { |
| 157 | gotNonDot = true; |
| 158 | break; |
| 159 | } |
| 160 | } |
| 161 | if (!gotNonDot) { |
| 162 | // Special case for component of zero or more periods. Add 3 periods. |
| 163 | os << "..."; |
| 164 | for (size_t i = 0; i < valueSize; ++i) |
| 165 | os << '.'; |
| 166 | } |
| 167 | else { |
| 168 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 169 | std::ios::fmtflags saveFlags = os.flags(std::ios::hex | std::ios::uppercase); |
| 170 | |
| 171 | for (size_t i = 0; i < valueSize; ++i) { |
| 172 | uint8_t x = valuePtr[i]; |
| 173 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 174 | if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) || |
| 175 | (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d || |
| 176 | x == 0x2e || x == 0x5f) |
| 177 | os << x; |
| 178 | else { |
| 179 | os << '%'; |
| 180 | if (x < 16) |
| 181 | os << '0'; |
| 182 | os << static_cast<unsigned int>(x); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | // Restore. |
| 187 | os.flags(saveFlags); |
| 188 | } |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | uint64_t |
| 192 | Component::toNumber () const |
| 193 | { |
| 194 | uint64_t ret = 0; |
| 195 | for (const_iterator i = begin (); i != end (); i++) |
| 196 | { |
| 197 | ret <<= 8; |
| 198 | ret |= static_cast<unsigned char> (*i); |
| 199 | } |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | uint64_t |
| 204 | Component::toNumberWithMarker (unsigned char marker) const |
| 205 | { |
| 206 | if (empty () || |
| 207 | static_cast<unsigned char> (*(begin ())) != marker) |
| 208 | { |
| 209 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 210 | << error::msg ("Name component does not have required marker [" + toUri () + "]")); |
| 211 | } |
| 212 | |
| 213 | uint64_t ret = 0; |
| 214 | for (const_iterator i = begin () + 1; i != end (); i++) |
| 215 | { |
| 216 | ret <<= 8; |
| 217 | ret |= static_cast<unsigned char> (*i); |
| 218 | } |
| 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | } // name |
| 223 | |
| 224 | NDN_NAMESPACE_END |