blob: ecc87b188eeccd97dce4ac6e3047719281b21925 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
16using namespace std;
17
18namespace ndn
19{
20namespace name
21{
22
23Component::Component ()
24{
25}
26
27Component::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
42Component::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
57Component::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
64int
65Component::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
82Component
83Component::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
95Component
96Component::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
111std::string
112Component::toBlob () const
113{
114 return std::string (begin (), end ());
115}
116
117void
118Component::toBlob (std::ostream &os) const
119{
120 os.write (buf (), size ());
121}
122
123std::string
124Component::toUri () const
125{
126 ostringstream os;
127 toUri (os);
128 return os.str ();
129}
130
131void
132Component::toUri (std::ostream &os) const
133{
134 Uri::toEscaped (begin (), end (), ostream_iterator<char> (os));
135}
136
137uint64_t
138Component::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
149uint64_t
150Component::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