Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 1 | #include "ccnx-name.h" |
| 2 | #include <boost/lexical_cast.hpp> |
| 3 | #include <ctype.h> |
| 4 | #include <boost/algorithm/string/join.hpp> |
| 5 | |
| 6 | namespace Ccnx{ |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 7 | CcnxCharbufPtr CcnxCharbuf::Null; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 8 | |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 9 | void |
| 10 | CcnxCharbuf::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 Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 21 | CcnxCharbuf::CcnxCharbuf() |
| 22 | : m_buf(NULL) |
| 23 | { |
| 24 | m_buf = ccn_charbuf_create(); |
| 25 | } |
| 26 | |
| 27 | CcnxCharbuf::CcnxCharbuf(ccn_charbuf *buf) |
| 28 | : m_buf(NULL) |
| 29 | { |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 30 | init(buf); |
| 31 | } |
| 32 | |
| 33 | CcnxCharbuf::CcnxCharbuf(const CcnxCharbuf &other) |
| 34 | : m_buf (NULL) |
| 35 | { |
| 36 | init(other.m_buf); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | CcnxCharbuf::~CcnxCharbuf() |
| 40 | { |
| 41 | ccn_charbuf_destroy(&m_buf); |
| 42 | } |
| 43 | |
| 44 | Name::Name() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | Name::Name(const string &name) |
| 49 | { |
| 50 | stringstream ss(name); |
| 51 | string compStr; |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 52 | bool first = true; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 53 | while(getline(ss, compStr, '/')) |
| 54 | { |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 55 | // discard the first empty comp before the first '/' |
| 56 | if (first) |
| 57 | { |
| 58 | first = false; |
| 59 | continue; |
| 60 | } |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 61 | Bytes comp(compStr.begin(), compStr.end()); |
| 62 | m_comps.push_back(comp); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | Name::Name(const vector<Bytes> &comps) |
| 67 | { |
| 68 | m_comps = comps; |
| 69 | } |
| 70 | |
| 71 | Name::Name(const Name &other) |
| 72 | { |
| 73 | m_comps = other.m_comps; |
| 74 | } |
| 75 | |
| 76 | Name::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 Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 89 | Name::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 Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 109 | Name & |
| 110 | Name::operator=(const Name &other) |
| 111 | { |
| 112 | m_comps = other.m_comps; |
| 113 | return *this; |
| 114 | } |
| 115 | bool |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 116 | Name::operator==(const string &str) const |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 117 | { |
| 118 | return this->toString() == str; |
| 119 | } |
| 120 | |
| 121 | bool |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 122 | Name::operator!=(const string &str) const |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 123 | { |
| 124 | return !(*this == str); |
| 125 | } |
| 126 | |
| 127 | Name |
| 128 | operator+(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 | |
| 135 | string |
| 136 | Name::toString() const |
| 137 | { |
| 138 | stringstream ss(stringstream::out); |
| 139 | ss << *this; |
| 140 | return ss.str(); |
| 141 | } |
| 142 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 143 | CcnxCharbufPtr |
| 144 | Name::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 | |
| 157 | Name & |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 158 | Name::appendComp(const Name &comp) |
| 159 | { |
| 160 | m_comps.insert (m_comps.end (), |
| 161 | comp.m_comps.begin (), comp.m_comps.end ()); |
| 162 | return *this; |
| 163 | } |
| 164 | |
| 165 | Name & |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 166 | Name::appendComp(const Bytes &comp) |
| 167 | { |
| 168 | m_comps.push_back(comp); |
| 169 | return *this; |
| 170 | } |
| 171 | |
| 172 | Name & |
| 173 | Name::appendComp(const string &compStr) |
| 174 | { |
| 175 | Bytes comp(compStr.begin(), compStr.end()); |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 176 | return appendComp(comp); |
| 177 | } |
| 178 | |
| 179 | Name & |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 180 | Name::appendComp (const void *buf, size_t size) |
| 181 | { |
| 182 | Bytes comp (reinterpret_cast<const unsigned char*> (buf), reinterpret_cast<const unsigned char*> (buf) + size); |
| 183 | return appendComp(comp); |
| 184 | } |
| 185 | |
| 186 | Name & |
| 187 | Name::appendComp(uint64_t number) |
| 188 | { |
| 189 | Bytes comp; |
| 190 | comp.push_back (0); |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 191 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 192 | while (number > 0) |
| 193 | { |
| 194 | comp.push_back (static_cast<unsigned char> (number & 0xFF)); |
| 195 | number >>= 8; |
| 196 | } |
| 197 | return appendComp (comp); |
| 198 | } |
| 199 | |
| 200 | uint64_t |
| 201 | Name::getCompAsInt (int index) const |
| 202 | { |
| 203 | Bytes comp = getComp(index); |
| 204 | if (comp.size () < 1 || |
| 205 | comp[0] != 0) |
| 206 | { |
| 207 | boost::throw_exception(NameException() |
| 208 | << error_info_str("Non integer component: " + getCompAsString(index))); |
| 209 | } |
| 210 | uint64_t ret = 0; |
| 211 | for (int i = comp.size () - 1; i >= 1; i--) |
| 212 | { |
| 213 | ret <<= 8; |
| 214 | ret |= comp [i]; |
| 215 | } |
| 216 | return ret; |
| 217 | } |
| 218 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 219 | Bytes |
| 220 | Name::getComp(int index) const |
| 221 | { |
| 222 | if (index >= m_comps.size()) |
| 223 | { |
| 224 | boost::throw_exception(NameException() << error_info_str("Index out of range: " + boost::lexical_cast<string>(index))); |
| 225 | } |
| 226 | return m_comps[index]; |
| 227 | } |
| 228 | |
| 229 | string |
| 230 | Name::getCompAsString(int index) const |
| 231 | { |
| 232 | Bytes comp = getComp(index); |
| 233 | stringstream ss(stringstream::out); |
| 234 | int size = comp.size(); |
| 235 | for (int i = 0; i < size; i++) |
| 236 | { |
| 237 | unsigned char ch = comp[i]; |
| 238 | if (isprint(ch)) |
| 239 | { |
| 240 | ss << (char) ch; |
| 241 | } |
| 242 | else |
| 243 | { |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 244 | ss << "%" << hex << setfill('0') << setw(2) << (unsigned int)ch; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | return ss.str(); |
| 249 | } |
| 250 | |
| 251 | Name |
| 252 | Name::getPartialName(int start, int n) const |
| 253 | { |
| 254 | int size = m_comps.size(); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 255 | if (start < 0 || start >= size || (n > 0 && start + n > size)) |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 256 | { |
| 257 | stringstream ss(stringstream::out); |
| 258 | ss << "getPartialName() parameter out of range! "; |
| 259 | ss << "start = " << start; |
| 260 | ss << "n = " << n; |
| 261 | ss << "size = " << size; |
| 262 | boost::throw_exception(NameException() << error_info_str(ss.str())); |
| 263 | } |
| 264 | |
| 265 | vector<Bytes> comps; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 266 | int end; |
| 267 | if (n > 0) |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 268 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 269 | end = start + n; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 270 | } |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 271 | else |
| 272 | { |
| 273 | end = size; |
| 274 | } |
| 275 | |
| 276 | for (int i = start; i < end; i++) |
| 277 | { |
| 278 | comps.push_back(m_comps[i]); |
| 279 | } |
| 280 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 281 | return Name(comps); |
| 282 | } |
| 283 | |
| 284 | ostream & |
| 285 | operator <<(ostream &os, const Name &name) |
| 286 | { |
| 287 | int size = name.size(); |
| 288 | vector<string> strComps; |
| 289 | for (int i = 0; i < size; i++) |
| 290 | { |
| 291 | strComps.push_back(name.getCompAsString(i)); |
| 292 | } |
| 293 | string joined = boost::algorithm::join(strComps, "/"); |
| 294 | os << "/" << joined; |
| 295 | return os; |
| 296 | } |
| 297 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 298 | bool |
| 299 | operator ==(const Name &n1, const Name &n2) |
| 300 | { |
| 301 | stringstream ss1(stringstream::out); |
| 302 | stringstream ss2(stringstream::out); |
| 303 | ss1 << n1; |
| 304 | ss2 << n2; |
| 305 | return ss1.str() == ss2.str(); |
| 306 | } |
| 307 | |
| 308 | bool |
| 309 | operator !=(const Name &n1, const Name &n2) |
| 310 | { |
| 311 | return !(n1 == n2); |
| 312 | } |
| 313 | |
| 314 | bool |
| 315 | operator <(const Name &n1, const Name &n2) |
| 316 | { |
| 317 | stringstream ss1(stringstream::out); |
| 318 | stringstream ss2(stringstream::out); |
| 319 | ss1 << n1; |
| 320 | ss2 << n2; |
| 321 | return ss1.str() < ss2.str(); |
| 322 | } |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 323 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 324 | |
| 325 | } // Ccnx |