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" |
| 14 | #endif |
| 15 | |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 16 | #include "name-component.h" |
| 17 | |
| 18 | #include "detail/error.h" |
| 19 | #include "detail/uri.h" |
| 20 | |
| 21 | NDN_NAMESPACE_BEGIN |
| 22 | |
| 23 | namespace name { |
| 24 | |
| 25 | Component::Component () |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | Component::Component (const std::string &uri) |
| 30 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 31 | copy (uri.begin (), uri.end (), std::back_inserter (*this)); |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | Component::Component (std::string::const_iterator begin, std::string::const_iterator end) |
| 35 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 36 | copy (begin, end, std::back_inserter (*this)); |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | Component::Component (const void *buf, size_t length) |
| 40 | { |
| 41 | copy (static_cast<const char*> (buf), |
| 42 | static_cast<const char*> (buf)+length, |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 43 | std::back_inserter (*this)); |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | Component & |
| 47 | Component::fromUri (const std::string &uri) |
| 48 | { |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 49 | try |
| 50 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 51 | Uri::fromEscaped (uri.begin (), uri.end (), std::back_inserter (*this)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 52 | } |
| 53 | catch (error::Uri &err) |
| 54 | { |
| 55 | // re-throwing different exception |
| 56 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 57 | << error::msg (uri) |
| 58 | << error::pos (error::get_pos (err))); |
| 59 | } |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 60 | |
| 61 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 64 | Component & |
| 65 | Component::fromUri (std::string::const_iterator begin, std::string::const_iterator end) |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 66 | { |
| 67 | try |
| 68 | { |
Alexander Afanasyev | f23a7b6 | 2013-07-17 13:22:25 -0700 | [diff] [blame] | 69 | Uri::fromEscaped (begin, end, std::back_inserter (*this)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 70 | } |
| 71 | catch (error::Uri &err) |
| 72 | { |
| 73 | // re-throwing different exception |
| 74 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 75 | << error::msg (std::string (begin, end)) |
| 76 | << error::pos (error::get_pos (err))); |
| 77 | } |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 78 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | int |
| 82 | Component::compare (const Component &other) const |
| 83 | { |
| 84 | if (size () < other.size ()) |
| 85 | return -1; |
| 86 | |
| 87 | if (size () > other.size ()) |
| 88 | return +1; |
| 89 | |
| 90 | // now we know that sizes are equal |
| 91 | |
| 92 | std::pair<const_iterator, const_iterator> diff = mismatch (begin (), end (), other.begin ()); |
| 93 | if (diff.first == end ()) // components are actually equal |
| 94 | return 0; |
| 95 | |
| 96 | return (std::lexicographical_compare (diff.first, end (), diff.second, other.end ())) ? -1 : +1; |
| 97 | } |
| 98 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 99 | Component & |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 100 | Component::fromNumber (uint64_t number) |
| 101 | { |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 102 | while (number > 0) |
| 103 | { |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 104 | this->push_back (static_cast<unsigned char> (number & 0xFF)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 105 | number >>= 8; |
| 106 | } |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 107 | std::reverse (this->begin (), this->end ()); |
| 108 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 111 | Component & |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 112 | Component::fromNumberWithMarker (uint64_t number, unsigned char marker) |
| 113 | { |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 114 | this->push_back (marker); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 115 | |
| 116 | while (number > 0) |
| 117 | { |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 118 | this->push_back (static_cast<unsigned char> (number & 0xFF)); |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 119 | number >>= 8; |
| 120 | } |
| 121 | |
Alexander Afanasyev | 157c9e6 | 2013-07-16 20:58:04 -0700 | [diff] [blame] | 122 | std::reverse (this->begin () + 1, this->end ()); |
| 123 | return *this; |
Alexander Afanasyev | 9213601 | 2013-07-16 20:36:30 -0700 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | std::string |
| 127 | Component::toBlob () const |
| 128 | { |
| 129 | return std::string (begin (), end ()); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | Component::toBlob (std::ostream &os) const |
| 134 | { |
| 135 | os.write (buf (), size ()); |
| 136 | } |
| 137 | |
| 138 | std::string |
| 139 | Component::toUri () const |
| 140 | { |
| 141 | std::ostringstream os; |
| 142 | toUri (os); |
| 143 | return os.str (); |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | Component::toUri (std::ostream &os) const |
| 148 | { |
| 149 | Uri::toEscaped (begin (), end (), std::ostream_iterator<char> (os)); |
| 150 | } |
| 151 | |
| 152 | uint64_t |
| 153 | Component::toNumber () const |
| 154 | { |
| 155 | uint64_t ret = 0; |
| 156 | for (const_iterator i = begin (); i != end (); i++) |
| 157 | { |
| 158 | ret <<= 8; |
| 159 | ret |= static_cast<unsigned char> (*i); |
| 160 | } |
| 161 | return ret; |
| 162 | } |
| 163 | |
| 164 | uint64_t |
| 165 | Component::toNumberWithMarker (unsigned char marker) const |
| 166 | { |
| 167 | if (empty () || |
| 168 | static_cast<unsigned char> (*(begin ())) != marker) |
| 169 | { |
| 170 | BOOST_THROW_EXCEPTION (error::name::Component () |
| 171 | << error::msg ("Name component does not have required marker [" + toUri () + "]")); |
| 172 | } |
| 173 | |
| 174 | uint64_t ret = 0; |
| 175 | for (const_iterator i = begin () + 1; i != end (); i++) |
| 176 | { |
| 177 | ret <<= 8; |
| 178 | ret |= static_cast<unsigned char> (*i); |
| 179 | } |
| 180 | return ret; |
| 181 | } |
| 182 | |
| 183 | } // name |
| 184 | |
| 185 | NDN_NAMESPACE_END |