blob: a59cf8ac81e40dc6201ab354265441ad793e8704 [file] [log] [blame]
Zhenkai Zhuf47109b2013-01-02 19:41:34 -08001#include "ccnx-name.h"
2#include <boost/lexical_cast.hpp>
3#include <ctype.h>
4#include <boost/algorithm/string/join.hpp>
5
6namespace Ccnx{
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -08007CcnxCharbufPtr CcnxCharbuf::Null;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -08008
Zhenkai Zhub0adefe2013-01-04 21:56:38 -08009void
10CcnxCharbuf::init(ccn_charbuf *buf)
11{
12 if (buf != NULL)
13 {
14 m_buf = ccn_charbuf_create();
15 ccn_charbuf_reserve(m_buf, buf->length);
16 memcpy(m_buf->buf, buf->buf, buf->length);
17 m_buf->length = buf->length;
18 }
19}
20
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080021CcnxCharbuf::CcnxCharbuf()
22 : m_buf(NULL)
23{
24 m_buf = ccn_charbuf_create();
25}
26
27CcnxCharbuf::CcnxCharbuf(ccn_charbuf *buf)
28 : m_buf(NULL)
29{
Zhenkai Zhub0adefe2013-01-04 21:56:38 -080030 init(buf);
31}
32
33CcnxCharbuf::CcnxCharbuf(const CcnxCharbuf &other)
34 : m_buf (NULL)
35{
36 init(other.m_buf);
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080037}
38
39CcnxCharbuf::~CcnxCharbuf()
40{
41 ccn_charbuf_destroy(&m_buf);
42}
43
44Name::Name()
45{
46}
47
48Name::Name(const string &name)
49{
50 stringstream ss(name);
51 string compStr;
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080052 bool first = true;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080053 while(getline(ss, compStr, '/'))
54 {
Zhenkai Zhu3b82d432013-01-03 22:48:40 -080055 // discard the first empty comp before the first '/'
56 if (first)
57 {
58 first = false;
59 continue;
60 }
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080061 Bytes comp(compStr.begin(), compStr.end());
62 m_comps.push_back(comp);
63 }
64}
65
66Name::Name(const vector<Bytes> &comps)
67{
68 m_comps = comps;
69}
70
71Name::Name(const Name &other)
72{
73 m_comps = other.m_comps;
74}
75
76Name::Name(const unsigned char *data, const ccn_indexbuf *comps)
77{
78 for (int i = 0; i < comps->n - 1; i++)
79 {
80 const unsigned char *compPtr;
81 size_t size;
82 ccn_name_comp_get(data, comps, i, &compPtr, &size);
83 Bytes comp;
84 readRaw(comp, compPtr, size);
85 m_comps.push_back(comp);
86 }
87}
88
Alexander Afanasyevd09871f2013-01-04 22:36:37 -080089Name::Name (const unsigned char *buf, const size_t length)
90{
91 ccn_indexbuf *idx = ccn_indexbuf_create();
92 const ccn_charbuf namebuf = { length, length, const_cast<unsigned char *> (buf) };
93 ccn_name_split (&namebuf, idx);
94
95 const unsigned char *compPtr = NULL;
96 size_t size = 0;
97 int i = 0;
98 while (ccn_name_comp_get(namebuf.buf, idx, i, &compPtr, &size) == 0)
99 {
100 Bytes comp;
101 readRaw (comp, compPtr, size);
102 m_comps.push_back(comp);
103 i++;
104 }
105 ccn_indexbuf_destroy(&idx);
106}
107
108
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800109Name &
110Name::operator=(const Name &other)
111{
112 m_comps = other.m_comps;
113 return *this;
114}
115bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800116Name::operator==(const string &str) const
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800117{
118 return this->toString() == str;
119}
120
121bool
Zhenkai Zhu3b82d432013-01-03 22:48:40 -0800122Name::operator!=(const string &str) const
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800123{
124 return !(*this == str);
125}
126
127Name
128operator+(const Name &n1, const Name &n2)
129{
130 vector<Bytes> comps = n1.m_comps;
131 copy(n2.m_comps.begin(), n2.m_comps.end(), back_inserter(comps));
132 return Name(comps);
133}
134
135string
136Name::toString() const
137{
138 stringstream ss(stringstream::out);
139 ss << *this;
140 return ss.str();
141}
142
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800143CcnxCharbufPtr
144Name::toCcnxCharbuf() const
145{
146 CcnxCharbufPtr ptr(new CcnxCharbuf());
147 ccn_charbuf *cbuf = ptr->getBuf();
148 ccn_name_init(cbuf);
149 int size = m_comps.size();
150 for (int i = 0; i < size; i++)
151 {
152 ccn_name_append(cbuf, head(m_comps[i]), m_comps[i].size());
153 }
154 return ptr;
155}
156
157Name &
158Name::appendComp(const Bytes &comp)
159{
160 m_comps.push_back(comp);
161 return *this;
162}
163
164Name &
165Name::appendComp(const string &compStr)
166{
167 Bytes comp(compStr.begin(), compStr.end());
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800168 return appendComp(comp);
169}
170
171Name &
172Name::appendComp(const Name &name)
173{
174 for (vector<Bytes>::const_iterator i = name.m_comps.begin (); i != name.m_comps.end (); i++)
175 {
176 appendComp (*i);
177 }
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800178 return *this;
179}
180
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800181Name &
182Name::appendComp (const void *buf, size_t size)
183{
184 Bytes comp (reinterpret_cast<const unsigned char*> (buf), reinterpret_cast<const unsigned char*> (buf) + size);
185 return appendComp(comp);
186}
187
188Name &
189Name::appendComp(uint64_t number)
190{
191 Bytes comp;
192 comp.push_back (0);
193
194 while (number > 0)
195 {
196 comp.push_back (static_cast<unsigned char> (number & 0xFF));
197 number >>= 8;
198 }
199 return appendComp (comp);
200}
201
202uint64_t
203Name::getCompAsInt (int index) const
204{
205 Bytes comp = getComp(index);
206 if (comp.size () < 1 ||
207 comp[0] != 0)
208 {
209 boost::throw_exception(NameException()
210 << error_info_str("Non integer component: " + getCompAsString(index)));
211 }
212 uint64_t ret = 0;
213 for (int i = comp.size () - 1; i >= 1; i--)
214 {
215 ret <<= 8;
216 ret |= comp [i];
217 }
218 return ret;
219}
220
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800221Bytes
222Name::getComp(int index) const
223{
224 if (index >= m_comps.size())
225 {
226 boost::throw_exception(NameException() << error_info_str("Index out of range: " + boost::lexical_cast<string>(index)));
227 }
228 return m_comps[index];
229}
230
231string
232Name::getCompAsString(int index) const
233{
234 Bytes comp = getComp(index);
235 stringstream ss(stringstream::out);
236 int size = comp.size();
237 for (int i = 0; i < size; i++)
238 {
239 unsigned char ch = comp[i];
240 if (isprint(ch))
241 {
242 ss << (char) ch;
243 }
244 else
245 {
Alexander Afanasyevc9eb68f2013-01-07 13:40:00 -0800246 ss << "%" << hex << setfill('0') << setw(2) << (unsigned int)ch;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800247 }
248 }
249
250 return ss.str();
251}
252
253Name
254Name::getPartialName(int start, int n) const
255{
256 int size = m_comps.size();
Alexander Afanasyevdcfa9632013-01-07 16:38:19 -0800257 if (start < 0 || start >= size || (n > 0 && start + n > size))
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800258 {
259 stringstream ss(stringstream::out);
260 ss << "getPartialName() parameter out of range! ";
261 ss << "start = " << start;
262 ss << "n = " << n;
263 ss << "size = " << size;
264 boost::throw_exception(NameException() << error_info_str(ss.str()));
265 }
266
267 vector<Bytes> comps;
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800268 int end;
269 if (n > 0)
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800270 {
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800271 end = start + n;
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800272 }
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800273 else
274 {
275 end = size;
276 }
277
278 for (int i = start; i < end; i++)
279 {
280 comps.push_back(m_comps[i]);
281 }
282
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800283 return Name(comps);
284}
285
286ostream &
287operator <<(ostream &os, const Name &name)
288{
289 int size = name.size();
290 vector<string> strComps;
291 for (int i = 0; i < size; i++)
292 {
293 strComps.push_back(name.getCompAsString(i));
294 }
295 string joined = boost::algorithm::join(strComps, "/");
296 os << "/" << joined;
297 return os;
298}
299
Zhenkai Zhucb2d0dd2013-01-03 14:10:48 -0800300bool
301operator ==(const Name &n1, const Name &n2)
302{
303 stringstream ss1(stringstream::out);
304 stringstream ss2(stringstream::out);
305 ss1 << n1;
306 ss2 << n2;
307 return ss1.str() == ss2.str();
308}
309
310bool
311operator !=(const Name &n1, const Name &n2)
312{
313 return !(n1 == n2);
314}
315
316bool
317operator <(const Name &n1, const Name &n2)
318{
319 stringstream ss1(stringstream::out);
320 stringstream ss2(stringstream::out);
321 ss1 << n1;
322 ss2 << n2;
323 return ss1.str() < ss2.str();
324}
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800325
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800326
327} // Ccnx