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