Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Jeff Thompson <jefft0@remap.ucla.edu> |
| 22 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 23 | * @author Zhenkai Zhu <http://irl.cs.ucla.edu/~zhenkai/> |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #ifndef NDN_NAME_COMPONENT_HPP |
| 27 | #define NDN_NAME_COMPONENT_HPP |
| 28 | |
| 29 | #include "common.hpp" |
| 30 | #include "encoding/block.hpp" |
Alexander Afanasyev | 258ec2b | 2014-05-14 16:15:37 -0700 | [diff] [blame] | 31 | #include "encoding/block-helpers.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 32 | #include "encoding/encoding-buffer.hpp" |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 33 | #include "util/string-helper.hpp" |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 34 | |
| 35 | namespace ndn { |
| 36 | namespace name { |
| 37 | |
| 38 | /** |
Alexander Afanasyev | 3aeeaeb | 2014-04-22 23:34:23 -0700 | [diff] [blame] | 39 | * @brief Component holds a read-only name component value. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 40 | */ |
| 41 | class Component : public Block |
| 42 | { |
| 43 | public: |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 44 | /** |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 45 | * @brief Error that can be thrown from name::Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 46 | */ |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 47 | class Error : public Block::Error |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 48 | { |
| 49 | public: |
| 50 | explicit |
| 51 | Error(const std::string& what) |
Alexander Afanasyev | 2a7f720 | 2014-04-23 14:25:29 -0700 | [diff] [blame] | 52 | : Block::Error(what) |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | { |
| 54 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 57 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 58 | * Create a new name::Component with an empty value |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 59 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 60 | Component(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 61 | |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 62 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 63 | * @brief Create name::Component from a wire block |
| 64 | * |
| 65 | * @param wire Tlv::NameComponent Block from which to create name::Component |
| 66 | * @throws Error if wire.type() is not Tlv::NameComponent |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 67 | * |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 68 | * Any block can be implicitly converted to name::Component |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 69 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 70 | Component(const Block& wire); |
| 71 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 72 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 73 | * @brief Create a new name::Component from the buffer pointer (buffer pointer will be copied) |
| 74 | * |
| 75 | * @param buffer A pointer to an immutable buffer |
| 76 | * |
| 77 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 78 | * Note that this method **will not** allocate new memory for and copy the payload until |
| 79 | * toWire() method is called. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 80 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 81 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 82 | Component(const ConstBufferPtr& buffer); |
| 83 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 84 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 85 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 86 | * @param buffer A reference to the buffer |
| 87 | * |
| 88 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 89 | * Note that this method **will** allocate new memory for and copy the payload. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 90 | */ |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 91 | explicit |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 92 | Component(const Buffer& buffer); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 93 | |
| 94 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 95 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 96 | * @param buffer A pointer to the first byte of the buffer |
| 97 | * @param bufferSize Size of the buffer |
| 98 | * |
| 99 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 100 | * Note that this method **will** allocate new memory for and copy the payload. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 101 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 102 | Component(const uint8_t* buffer, size_t bufferSize); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 103 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 104 | /** |
| 105 | * @brief Create a new name::Component from the buffer (data from buffer will be copied) |
| 106 | * @param begin Iterator pointing to the beginning of the buffer |
| 107 | * @param end Iterator pointing to the ending of the buffer |
| 108 | * |
| 109 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 110 | * Note that this method **will** allocate new memory for and copy the payload. |
| 111 | */ |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 112 | template<class InputIterator> |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 113 | Component(InputIterator begin, InputIterator end); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 114 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 115 | /** |
| 116 | * @brief Create a new name::Component from the C string (data from string will be copied) |
| 117 | * |
| 118 | * @param str Zero-ended string. Note that this string will be interpreted as is (i.e., |
| 119 | * it will not be interpreted as URI) |
| 120 | * |
| 121 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 122 | * Note that this method **will** allocate new memory for and copy the payload. |
| 123 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 124 | explicit |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 125 | Component(const char* str); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 126 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 127 | /** |
| 128 | * @brief Create a new name::Component from the STL string (data from string will be copied) |
| 129 | * |
| 130 | * @param str Const reference to STL string. Note that this string will be interpreted |
| 131 | * as is (i.e., it will not be interpreted as URI) |
| 132 | * |
| 133 | * This constructor will create a new Tlv::NameComponent Block with `buffer` as a payload. |
| 134 | * Note that this method **will** allocate new memory for and copy the payload. |
| 135 | */ |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 136 | explicit |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 137 | Component(const std::string& str); |
| 138 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 139 | /** |
| 140 | * @brief Fast encoding or block size estimation |
| 141 | */ |
| 142 | template<bool T> |
| 143 | size_t |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 144 | wireEncode(EncodingImpl<T>& block) const; |
| 145 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 146 | /** |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 147 | * @brief Encode to a wire format |
| 148 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 149 | const Block& |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 150 | wireEncode() const; |
| 151 | |
| 152 | /** |
| 153 | * @brief Decode from the wire format |
| 154 | */ |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 155 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 156 | wireDecode(const Block& wire); |
| 157 | |
| 158 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 159 | * @brief Create name::Component by decoding the escapedString between beginOffset and |
| 160 | * endOffset according to the NDN URI Scheme. |
| 161 | * |
| 162 | * If the escaped string is "", "." or ".." then return an empty name::Component. Note |
| 163 | * that an empty name::Component should not be added to Name and if attempted, an |
| 164 | * exception will be thrown. |
| 165 | * |
| 166 | * @param escapedString String containing NDN URI-encoded name |
| 167 | * component. [escapedString+beginOffset, beginOffset+endOffset) |
| 168 | * must be a valid memory buffer. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 169 | * @param beginOffset The offset in escapedString of the beginning of the portion to decode. |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 170 | * @param endOffset The offset in escapedString of the end of the portion to decode. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 171 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 172 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 173 | fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 174 | |
| 175 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 176 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 177 | * |
| 178 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 179 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 180 | static Component |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 181 | fromEscapedString(const char* escapedString) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 182 | { |
| 183 | return fromEscapedString(escapedString, 0, ::strlen(escapedString)); |
| 184 | } |
| 185 | |
| 186 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 187 | * @brief Create name::Component by decoding the escapedString according to the NDN URI Scheme |
| 188 | * |
| 189 | * This overload is a convenience wrapper for fromEscapedString(char*,size_t,size) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 190 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 191 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 192 | fromEscapedString(const std::string& escapedString) |
| 193 | { |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 194 | return fromEscapedString(escapedString.c_str(), 0, escapedString.size()); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 198 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 199 | * |
| 200 | * @deprecated Use toUri(std::ostream&) instead |
| 201 | * |
| 202 | * This also adds "..." to a value with zero or more "." |
| 203 | * |
| 204 | * @param os The output stream to where write the URI escaped version *this |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 205 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 206 | DEPRECATED( |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 207 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 208 | toEscapedString(std::ostream& os) const) |
| 209 | { |
| 210 | return toUri(os); |
| 211 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 212 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 213 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 214 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 215 | * |
| 216 | * @deprecated Use toUri() instead |
| 217 | * |
| 218 | * This also adds "..." to a value with zero or more "." |
| 219 | * |
| 220 | * @return The escaped string |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 221 | */ |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 222 | DEPRECATED( |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 223 | std::string |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 224 | toEscapedString() const) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 225 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 226 | return toUri(); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 227 | } |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 228 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 229 | /** |
| 230 | * @brief Write *this to the output stream, escaping characters according to the NDN URI Scheme |
| 231 | * |
| 232 | * This also adds "..." to a value with zero or more "." |
| 233 | * |
| 234 | * @param os The output stream to where write the URI escaped version *this |
| 235 | */ |
| 236 | void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 237 | toUri(std::ostream& os) const; |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 238 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 239 | /** |
| 240 | * @brief Convert *this by escaping characters according to the NDN URI Scheme |
| 241 | * |
| 242 | * This also adds "..." to a value with zero or more "." |
| 243 | * |
| 244 | * @return The escaped string |
| 245 | */ |
| 246 | std::string |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 247 | toUri() const |
| 248 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 249 | std::ostringstream os; |
| 250 | toUri(os); |
| 251 | return os.str(); |
Alexander Afanasyev | 52eb20d | 2014-02-06 18:25:54 -0800 | [diff] [blame] | 252 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 253 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 254 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 255 | * @brief Interpret this name component as nonNegativeInteger |
| 256 | * |
| 257 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 258 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 259 | * @return The integer number. |
| 260 | */ |
| 261 | uint64_t |
| 262 | toNumber() const; |
| 263 | |
| 264 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 265 | * @brief An alias for toNumber() |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 266 | */ |
| 267 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 268 | toVersion() const; |
| 269 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 270 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 271 | * @brief An alias for toNumber() |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 272 | */ |
| 273 | uint64_t |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 274 | toSegment() const; |
| 275 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 276 | /** |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 277 | * @brief Create a component encoded as nonNegativeInteger |
| 278 | * |
| 279 | * @see http://named-data.net/doc/ndn-tlv/tlv.html#non-negative-integer-encoding |
| 280 | * |
| 281 | * @param number The non-negative number |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 282 | * @return The component value. |
| 283 | */ |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 284 | static Component |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 285 | fromNumber(uint64_t number); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 286 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 287 | bool |
| 288 | empty() const |
| 289 | { |
| 290 | return !hasValue(); |
| 291 | } |
| 292 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 293 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 294 | * @brief Check if this is the same component as other |
| 295 | * |
| 296 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 297 | * @return true if the components are equal, otherwise false. |
| 298 | */ |
| 299 | bool |
| 300 | equals(const Component& other) const |
| 301 | { |
| 302 | if (value_size() != other.value_size()) |
| 303 | return false; |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 304 | if (value_size() == 0 /* == other.value_size()*/) |
| 305 | return true; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 306 | |
Alexander Afanasyev | 60c8681 | 2014-02-20 15:19:33 -0800 | [diff] [blame] | 307 | // somehow, behavior is wrong on OSX 10.9 when component is empty |
| 308 | // (probably some bug in STL...) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 309 | return std::equal(value_begin(), value_end(), other.value_begin()); |
| 310 | } |
| 311 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 312 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 313 | * @brief Compare this to the other Component using NDN canonical ordering |
| 314 | * |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 315 | * @param other The other Component to compare with. |
| 316 | * @return 0 If they compare equal, -1 if *this comes before other in the canonical ordering, or |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 317 | * 1 if *this comes after other in the canonical ordering. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 318 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 319 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 320 | */ |
| 321 | int |
| 322 | compare(const Component& other) const; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 323 | |
| 324 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 325 | * @brief Check if this is the same component as other |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 326 | * |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 327 | * @param other The other Component to compare with. |
| 328 | * @return true if the components are equal, otherwise false. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 329 | */ |
| 330 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 331 | operator==(const Component& other) const |
| 332 | { |
| 333 | return equals(other); |
| 334 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 335 | |
| 336 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 337 | * @brief Check if this is not the same component as other |
| 338 | * @param other The other Component to compare with |
| 339 | * @return true if the components are not equal, otherwise false |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 340 | */ |
| 341 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 342 | operator!=(const Component& other) const |
| 343 | { |
| 344 | return !equals(other); |
| 345 | } |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 346 | |
| 347 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 348 | * @brief Check if the *this is less than or equal to the other in NDN canonical ordering |
| 349 | * @param other The other Component to compare with |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 350 | * |
| 351 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 352 | */ |
| 353 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 354 | operator<=(const Component& other) const |
| 355 | { |
| 356 | return compare(other) <= 0; |
| 357 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 358 | |
| 359 | /** |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 360 | * @brief Check if the *this is less than the other in NDN canonical ordering |
| 361 | * @param other The other Component to compare with |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 362 | * |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 363 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 364 | */ |
| 365 | bool |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 366 | operator<(const Component& other) const |
| 367 | { |
| 368 | return compare(other) < 0; |
| 369 | } |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 370 | |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 371 | /** |
| 372 | * @brief Check if the *this is greater or equal than the other in NDN canonical ordering |
| 373 | * @param other The other Component to compare with |
| 374 | * |
| 375 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 376 | */ |
| 377 | bool |
| 378 | operator>=(const Component& other) const |
| 379 | { |
| 380 | return compare(other) >= 0; |
| 381 | } |
| 382 | |
| 383 | /** |
| 384 | * @brief Check if the *this is greater than the other in NDN canonical ordering |
| 385 | * @param other The other Component to compare with |
| 386 | * |
| 387 | * @see http://named-data.net/doc/ndn-tlv/name.html#canonical-order |
| 388 | */ |
| 389 | bool |
| 390 | operator>(const Component& other) const |
| 391 | { |
| 392 | return compare(other) > 0; |
| 393 | } |
| 394 | |
| 395 | // !!! NOTE TO IMPLEMENTOR !!! |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 396 | // |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 397 | // This class MUST NOT contain any data fields. |
| 398 | // Block can be reinterpret_cast'ed as Component type. |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 399 | }; |
| 400 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 401 | inline std::ostream& |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 402 | operator<<(std::ostream& os, const Component& component) |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 403 | { |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 404 | component.toUri(os); |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 405 | return os; |
| 406 | } |
| 407 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 408 | inline |
| 409 | Component::Component() |
| 410 | : Block(Tlv::NameComponent) |
| 411 | { |
| 412 | } |
| 413 | |
| 414 | inline |
| 415 | Component::Component(const Block& wire) |
| 416 | : Block(wire) |
| 417 | { |
| 418 | if (type() != Tlv::NameComponent) |
| 419 | throw Error("Constructing name component from non name component TLV wire block"); |
| 420 | } |
| 421 | |
| 422 | inline |
| 423 | Component::Component(const ConstBufferPtr& buffer) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 424 | : Block(Tlv::NameComponent, buffer) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 425 | { |
| 426 | } |
| 427 | |
| 428 | inline |
| 429 | Component::Component(const Buffer& value) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 430 | : Block(dataBlock(Tlv::NameComponent, value.buf(), value.size())) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 431 | { |
| 432 | } |
| 433 | |
| 434 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 435 | Component::Component(const uint8_t* value, size_t valueLen) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 436 | : Block(dataBlock(Tlv::NameComponent, value, valueLen)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 437 | { |
| 438 | } |
| 439 | |
| 440 | template<class InputIterator> |
| 441 | inline |
| 442 | Component::Component(InputIterator begin, InputIterator end) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 443 | : Block(dataBlock(Tlv::NameComponent, begin, end)) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 444 | { |
| 445 | } |
| 446 | |
| 447 | inline |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 448 | Component::Component(const char* str) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 449 | : Block(dataBlock(Tlv::NameComponent, str, ::strlen(str))) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 450 | { |
| 451 | } |
| 452 | |
| 453 | inline |
| 454 | Component::Component(const std::string& str) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 455 | : Block(dataBlock(Tlv::NameComponent, str.c_str(), str.size())) |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 456 | { |
| 457 | } |
| 458 | |
| 459 | |
| 460 | inline Component |
| 461 | Component::fromEscapedString(const char* escapedString, size_t beginOffset, size_t endOffset) |
| 462 | { |
| 463 | std::string trimmedString(escapedString + beginOffset, escapedString + endOffset); |
| 464 | trim(trimmedString); |
| 465 | std::string value = unescape(trimmedString); |
| 466 | |
| 467 | if (value.find_first_not_of(".") == std::string::npos) { |
| 468 | // Special case for component of only periods. |
| 469 | if (value.size() <= 2) |
| 470 | // Zero, one or two periods is illegal. Ignore this component. |
| 471 | return Component(); |
| 472 | else |
| 473 | // Remove 3 periods. |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 474 | return Component(reinterpret_cast<const uint8_t*>(&value[3]), value.size() - 3); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 475 | } |
| 476 | else |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 477 | return Component(reinterpret_cast<const uint8_t*>(&value[0]), value.size()); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| 481 | inline void |
Alexander Afanasyev | 9c57818 | 2014-05-14 17:28:28 -0700 | [diff] [blame] | 482 | Component::toUri(std::ostream& result) const |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 483 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 484 | const uint8_t* valuePtr = value(); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 485 | size_t valueSize = value_size(); |
| 486 | |
| 487 | bool gotNonDot = false; |
| 488 | for (unsigned i = 0; i < valueSize; ++i) { |
| 489 | if (valuePtr[i] != 0x2e) { |
| 490 | gotNonDot = true; |
| 491 | break; |
| 492 | } |
| 493 | } |
| 494 | if (!gotNonDot) { |
| 495 | // Special case for component of zero or more periods. Add 3 periods. |
| 496 | result << "..."; |
| 497 | for (size_t i = 0; i < valueSize; ++i) |
| 498 | result << '.'; |
| 499 | } |
| 500 | else { |
| 501 | // In case we need to escape, set to upper case hex and save the previous flags. |
| 502 | std::ios::fmtflags saveFlags = result.flags(std::ios::hex | std::ios::uppercase); |
| 503 | |
| 504 | for (size_t i = 0; i < valueSize; ++i) { |
| 505 | uint8_t x = valuePtr[i]; |
| 506 | // Check for 0-9, A-Z, a-z, (+), (-), (.), (_) |
| 507 | if ((x >= 0x30 && x <= 0x39) || (x >= 0x41 && x <= 0x5a) || |
| 508 | (x >= 0x61 && x <= 0x7a) || x == 0x2b || x == 0x2d || |
| 509 | x == 0x2e || x == 0x5f) |
| 510 | result << x; |
| 511 | else { |
| 512 | result << '%'; |
| 513 | if (x < 16) |
| 514 | result << '0'; |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 515 | result << static_cast<unsigned int>(x); |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 516 | } |
| 517 | } |
| 518 | |
| 519 | // Restore. |
| 520 | result.flags(saveFlags); |
| 521 | } |
| 522 | } |
| 523 | |
Alexander Afanasyev | 4b98e8c | 2014-03-22 19:10:19 -0700 | [diff] [blame] | 524 | |
| 525 | inline Component |
| 526 | Component::fromNumber(uint64_t number) |
| 527 | { |
| 528 | /// \todo Change to Tlv::NumberComponent |
| 529 | return nonNegativeIntegerBlock(Tlv::NameComponent, number); |
| 530 | } |
| 531 | |
| 532 | |
| 533 | inline uint64_t |
| 534 | Component::toNumber() const |
| 535 | { |
| 536 | /// \todo Check if Component is of Tlv::NumberComponent type |
| 537 | return readNonNegativeInteger(static_cast<const Block&>(*this)); |
| 538 | } |
| 539 | |
| 540 | |
| 541 | inline uint64_t |
| 542 | Component::toVersion() const |
| 543 | { |
| 544 | return toNumber(); |
| 545 | } |
| 546 | |
| 547 | inline uint64_t |
| 548 | Component::toSegment() const |
| 549 | { |
| 550 | return toNumber(); |
| 551 | } |
| 552 | |
Alexander Afanasyev | 1dd95c5 | 2014-03-22 19:11:36 -0700 | [diff] [blame] | 553 | inline int |
| 554 | Component::compare(const Component& other) const |
| 555 | { |
| 556 | // Imitate ndn_Exclude_compareComponents. |
| 557 | if (value_size() < other.value_size()) |
| 558 | return -1; |
| 559 | if (value_size() > other.value_size()) |
| 560 | return 1; |
| 561 | |
| 562 | if (value_size() == 0) |
| 563 | return 0; |
| 564 | |
| 565 | // The components are equal length. Just do a byte compare. |
| 566 | return std::memcmp(value(), other.value(), value_size()); |
| 567 | } |
| 568 | |
| 569 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 570 | template<bool T> |
| 571 | inline size_t |
| 572 | Component::wireEncode(EncodingImpl<T>& block) const |
| 573 | { |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 574 | size_t totalLength = 0; |
Alexander Afanasyev | 380420b | 2014-02-09 20:52:29 -0800 | [diff] [blame] | 575 | if (value_size() > 0) |
Alexander Afanasyev | 770827c | 2014-05-13 17:42:55 -0700 | [diff] [blame] | 576 | totalLength += block.prependByteArray(value(), value_size()); |
| 577 | totalLength += block.prependVarNumber(value_size()); |
| 578 | totalLength += block.prependVarNumber(Tlv::NameComponent); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 579 | return totalLength; |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 580 | } |
| 581 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 582 | inline const Block& |
| 583 | Component::wireEncode() const |
| 584 | { |
| 585 | if (this->hasWire()) |
| 586 | return *this; |
| 587 | |
| 588 | EncodingEstimator estimator; |
| 589 | size_t estimatedSize = wireEncode(estimator); |
| 590 | |
| 591 | EncodingBuffer buffer(estimatedSize, 0); |
| 592 | wireEncode(buffer); |
| 593 | |
| 594 | const_cast<Component&>(*this) = buffer.block(); |
| 595 | return *this; |
| 596 | } |
| 597 | |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 598 | inline void |
| 599 | Component::wireDecode(const Block& wire) |
| 600 | { |
| 601 | if (wire.type() != Tlv::NameComponent) |
| 602 | throw Error("wireDecode name component from non name component TLV wire block"); |
| 603 | |
| 604 | *this = wire; |
| 605 | } |
| 606 | |
| 607 | |
Alexander Afanasyev | 95e8c2f | 2014-02-06 17:29:30 -0800 | [diff] [blame] | 608 | } // namespace name |
| 609 | } // namespace ndn |
| 610 | |
| 611 | #endif // NDN_NAME_COMPONENT_HPP |