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