Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | a98000c | 2013-12-16 14:40:09 -0800 | [diff] [blame] | 5 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Jeff Thompson | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 6 | * See COPYING for copyright and distribution information. |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 7 | */ |
| 8 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 9 | #include "name.hpp" |
| 10 | |
Jeff Thompson | b8f1b13 | 2013-08-13 11:07:43 -0700 | [diff] [blame] | 11 | #include <algorithm> |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 12 | #include <cstring> |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 13 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 14 | #include "util/time.hpp" |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 15 | #include "util/string-helper.hpp" |
| 16 | |
Jeff Thompson | 9c41dfe | 2013-06-27 12:10:25 -0700 | [diff] [blame] | 17 | namespace ndn { |
| 18 | |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 19 | void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 20 | Name::set(const char *uri_cstr) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 21 | { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 22 | clear(); |
Jeff Thompson | 67515bd | 2013-08-15 17:43:22 -0700 | [diff] [blame] | 23 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 24 | std::string uri = uri_cstr; |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 25 | trim(uri); |
| 26 | if (uri.size() == 0) |
| 27 | return; |
| 28 | |
| 29 | size_t iColon = uri.find(':'); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 30 | if (iColon != std::string::npos) { |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 31 | // Make sure the colon came before a '/'. |
| 32 | size_t iFirstSlash = uri.find('/'); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 33 | if (iFirstSlash == std::string::npos || iColon < iFirstSlash) { |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 34 | // Omit the leading protocol such as ndn: |
| 35 | uri.erase(0, iColon + 1); |
| 36 | trim(uri); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Trim the leading slash and possibly the authority. |
| 41 | if (uri[0] == '/') { |
| 42 | if (uri.size() >= 2 && uri[1] == '/') { |
| 43 | // Strip the authority following "//". |
| 44 | size_t iAfterAuthority = uri.find('/', 2); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 45 | if (iAfterAuthority == std::string::npos) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 46 | // Unusual case: there was only an authority. |
| 47 | return; |
| 48 | else { |
| 49 | uri.erase(0, iAfterAuthority + 1); |
| 50 | trim(uri); |
| 51 | } |
| 52 | } |
| 53 | else { |
| 54 | uri.erase(0, 1); |
| 55 | trim(uri); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | size_t iComponentStart = 0; |
| 60 | |
| 61 | // Unescape the components. |
| 62 | while (iComponentStart < uri.size()) { |
| 63 | size_t iComponentEnd = uri.find("/", iComponentStart); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 64 | if (iComponentEnd == std::string::npos) |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 65 | iComponentEnd = uri.size(); |
| 66 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 67 | Component component = Component::fromEscapedString(&uri[0], iComponentStart, iComponentEnd); |
Jeff Thompson | 46411c9 | 2013-09-13 19:31:25 -0700 | [diff] [blame] | 68 | // Ignore illegal components. This also gets rid of a trailing '/'. |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 69 | if (!component.empty()) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 70 | append(Component(component)); |
Jeff Thompson | 443398d | 2013-07-02 19:45:46 -0700 | [diff] [blame] | 71 | |
| 72 | iComponentStart = iComponentEnd + 1; |
| 73 | } |
| 74 | } |
| 75 | |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 76 | Name& |
| 77 | Name::append(const Name& name) |
| 78 | { |
| 79 | if (&name == this) |
| 80 | // Copying from this name, so need to make a copy first. |
| 81 | return append(Name(name)); |
| 82 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 83 | for (size_t i = 0; i < name.size(); ++i) |
| 84 | append(name.at(i)); |
Jeff Thompson | 26b0d79 | 2013-09-23 16:19:01 -0700 | [diff] [blame] | 85 | |
| 86 | return *this; |
| 87 | } |
| 88 | |
Alexander Afanasyev | 594cdb2 | 2014-01-03 15:11:33 -0800 | [diff] [blame] | 89 | Name& |
| 90 | Name::appendVersion() |
| 91 | { |
| 92 | appendVersion(ndn_getNowMilliseconds()); |
| 93 | return *this; |
| 94 | } |
| 95 | |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 96 | Name |
| 97 | Name::getSubName(size_t iStartComponent, size_t nComponents) const |
| 98 | { |
| 99 | Name result; |
| 100 | |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 101 | size_t iEnd = iStartComponent + nComponents; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 102 | for (size_t i = iStartComponent; i < iEnd && i < size(); ++i) |
| 103 | result.append(at(i)); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 104 | |
| 105 | return result; |
| 106 | } |
| 107 | |
| 108 | Name |
| 109 | Name::getSubName(size_t iStartComponent) const |
| 110 | { |
| 111 | Name result; |
| 112 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 113 | for (size_t i = iStartComponent; i < size(); ++i) |
| 114 | result.append(at(i)); |
Jeff Thompson | d0159d7 | 2013-09-23 13:34:15 -0700 | [diff] [blame] | 115 | |
| 116 | return result; |
| 117 | } |
| 118 | |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 119 | bool |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 120 | Name::equals(const Name& name) const |
| 121 | { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 122 | if (size() != name.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 123 | return false; |
| 124 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 125 | for (size_t i = 0; i < size(); ++i) { |
| 126 | if (at(i) != name.at(i)) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 127 | return false; |
| 128 | } |
| 129 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 130 | return true; |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | bool |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 134 | Name::isPrefixOf(const Name& name) const |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 135 | { |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 136 | // This name is longer than the name we are checking it against. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 137 | if (size() > name.size()) |
Jeff Thompson | 3c2ab01 | 2013-10-02 14:18:16 -0700 | [diff] [blame] | 138 | return false; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 139 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 140 | // Check if at least one of given components doesn't match. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 141 | for (size_t i = 0; i < size(); ++i) { |
| 142 | if (at(i) != name.at(i)) |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 143 | return false; |
| 144 | } |
| 145 | |
Jeff Thompson | e589c3f | 2013-10-12 17:30:50 -0700 | [diff] [blame] | 146 | return true; |
Jeff Thompson | cc35cd4 | 2013-08-20 12:23:14 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Jeff Thompson | ec7789a | 2013-08-21 11:08:36 -0700 | [diff] [blame] | 149 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 150 | int |
| 151 | Name::compare(const Name& other) const |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 152 | { |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 153 | for (size_t i = 0; i < size() && i < other.size(); ++i) { |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 154 | int comparison = at(i).compare(other.at(i)); |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 155 | if (comparison == 0) |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 156 | // The components at this index are equal, so check the next components. |
| 157 | continue; |
| 158 | |
| 159 | // Otherwise, the result is based on the components at this index. |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 160 | return comparison; |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Jeff Thompson | afc45a9 | 2014-01-15 12:42:45 -0800 | [diff] [blame] | 163 | // The components up to min(this.size(), other.size()) are equal, so the shorter name is less. |
| 164 | if (size() < other.size()) |
| 165 | return -1; |
| 166 | else if (size() > other.size()) |
| 167 | return 1; |
| 168 | else |
| 169 | return 0; |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Alexander Afanasyev | af283d8 | 2014-01-03 13:23:34 -0800 | [diff] [blame] | 172 | std::ostream& |
| 173 | operator << (std::ostream& os, const Name& name) |
| 174 | { |
| 175 | if (name.empty()) |
| 176 | { |
| 177 | os << "/"; |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | for (Name::const_iterator i = name.begin(); i != name.end(); i++) { |
| 182 | os << "/"; |
| 183 | i->toEscapedString(os); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return os; |
| 188 | } |
Jeff Thompson | 82568ad | 2013-12-17 15:17:40 -0800 | [diff] [blame] | 189 | |
Wentao Shang | 7794921 | 2014-02-01 23:42:24 -0800 | [diff] [blame] | 190 | } // namespace ndn |