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 { |
| 26 | |
| 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 | |
| 98 | return (std::lexicographical_compare (diff.first, end (), diff.second, other.end ())) ? -1 : +1; |
| 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); |
| 145 | return os.str (); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | Component::toUri (std::ostream &os) const |
| 150 | { |
| 151 | Uri::toEscaped (begin (), end (), std::ostream_iterator<char> (os)); |
| 152 | } |
| 153 | |
| 154 | uint64_t |
| 155 | Component::toNumber () const |
| 156 | { |
| 157 | uint64_t ret = 0; |
| 158 | for (const_iterator i = begin (); i != end (); i++) |
| 159 | { |
| 160 | ret <<= 8; |
| 161 | ret |= static_cast<unsigned char> (*i); |
| 162 | } |
| 163 | return ret; |
| 164 | } |
| 165 | |
| 166 | uint64_t |
| 167 | Component::toNumberWithMarker (unsigned char marker) const |
| 168 | { |
| 169 | if (empty () || |
| 170 | static_cast<unsigned char> (*(begin ())) != marker) |
| 171 | { |
| 172 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 173 | << error::msg ("Name component does not have required marker [" + toUri () + "]")); |
| 174 | } |
| 175 | |
| 176 | uint64_t ret = 0; |
| 177 | for (const_iterator i = begin () + 1; i != end (); i++) |
| 178 | { |
| 179 | ret <<= 8; |
| 180 | ret |= static_cast<unsigned char> (*i); |
| 181 | } |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | } // name |
| 186 | |
| 187 | NDN_NAMESPACE_END |