Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2012-2013 University of California, Los Angeles |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation; |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 17 | * |
| 18 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 19 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 22 | #ifndef CCNX_NAME_H |
| 23 | #define CCNX_NAME_H |
| 24 | #include <boost/shared_ptr.hpp> |
| 25 | #include "ccnx-common.h" |
| 26 | |
| 27 | namespace Ccnx { |
| 28 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 29 | class CcnxCharbuf; |
| 30 | typedef boost::shared_ptr<CcnxCharbuf> CcnxCharbufPtr; |
| 31 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 32 | // This class is mostly used in CcnxWrapper; users should not be directly using this class |
| 33 | // The main purpose of this class to is avoid manually create and destroy charbuf everytime |
| 34 | class CcnxCharbuf |
| 35 | { |
| 36 | public: |
| 37 | CcnxCharbuf(); |
| 38 | CcnxCharbuf(ccn_charbuf *buf); |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 39 | CcnxCharbuf(const CcnxCharbuf &other); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 40 | CcnxCharbuf(const void *buf, size_t length); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 41 | ~CcnxCharbuf(); |
| 42 | |
| 43 | // expose internal data structure, use with caution!! |
| 44 | ccn_charbuf * |
| 45 | getBuf() { return m_buf; } |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 46 | |
| 47 | const ccn_charbuf * |
| 48 | getBuf() const { return m_buf; } |
| 49 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 50 | const unsigned char * |
| 51 | buf () const |
| 52 | { return m_buf->buf; } |
| 53 | |
| 54 | size_t |
| 55 | length () const |
| 56 | { return m_buf->length; } |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 57 | |
Zhenkai Zhu | b0adefe | 2013-01-04 21:56:38 -0800 | [diff] [blame] | 58 | private: |
| 59 | void init(ccn_charbuf *buf); |
| 60 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 61 | protected: |
| 62 | ccn_charbuf *m_buf; |
| 63 | }; |
| 64 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 65 | |
| 66 | struct NameException: |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 67 | virtual boost::exception, virtual std::exception {}; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 68 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 69 | class Name |
| 70 | { |
| 71 | public: |
| 72 | Name(); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 73 | Name(const std::string &name); |
| 74 | Name(const std::vector<Bytes> &comps); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 75 | Name(const Name &other); |
| 76 | Name(const unsigned char *data, const ccn_indexbuf *comps); |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 77 | Name (const unsigned char *buf, const size_t length); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 78 | Name (const CcnxCharbuf &buf); |
| 79 | Name (const ccn_charbuf *buf); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 80 | virtual ~Name() {} |
| 81 | |
| 82 | CcnxCharbufPtr |
| 83 | toCcnxCharbuf() const; |
| 84 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 85 | operator CcnxCharbufPtr () const { return toCcnxCharbuf (); } |
| 86 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 87 | Name & |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 88 | appendComp(const Name &comp); |
| 89 | |
| 90 | Name & |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 91 | appendComp(const Bytes &comp); |
| 92 | |
| 93 | Name & |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 94 | appendComp(const std::string &compStr); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 95 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 96 | Name & |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 97 | appendComp(const void *buf, size_t size); |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 98 | |
| 99 | /** |
| 100 | * Append int component |
| 101 | * |
| 102 | * Difference between this and appendComp call is that data is appended in network order |
| 103 | * |
| 104 | * Also, this function honors naming convention (%00 prefix is added) |
| 105 | */ |
| 106 | Name & |
| 107 | appendComp(uint64_t number); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 108 | |
| 109 | template<class T> |
| 110 | Name & |
| 111 | operator ()(const T &comp) { return appendComp (comp); } |
| 112 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 113 | Name & |
| 114 | operator ()(const void *buf, size_t size) { return appendComp (buf, size); } |
| 115 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 116 | int |
| 117 | size() const {return m_comps.size();} |
| 118 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 119 | const Bytes & |
| 120 | getComp (int index) const; |
| 121 | |
| 122 | inline const Bytes & |
| 123 | getCompFromBack (int index) const; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 124 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 125 | // return std::string format of the comp |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 126 | // if all characters are printable, simply returns the string |
| 127 | // if not, print the bytes in hex string format |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 128 | std::string |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 129 | getCompAsString(int index) const; |
| 130 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 131 | uint64_t |
| 132 | getCompAsInt (int index) const; |
| 133 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 134 | inline std::string |
| 135 | getCompFromBackAsString(int index) const; |
| 136 | |
| 137 | inline uint64_t |
| 138 | getCompFromBackAsInt (int index) const; |
| 139 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 140 | Name |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 141 | getPartialName(int start, int n = -1) const; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 142 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 143 | inline Name |
| 144 | getPartialNameFromBack(int start, int n = -1) const; |
| 145 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 146 | std::string |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 147 | toString() const; |
| 148 | |
| 149 | Name & |
| 150 | operator=(const Name &other); |
| 151 | |
| 152 | bool |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 153 | operator==(const std::string &str) const; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 154 | |
| 155 | bool |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 156 | operator!=(const std::string &str) const; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 157 | |
| 158 | friend Name |
| 159 | operator+(const Name &n1, const Name &n2); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 160 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 161 | private: |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 162 | std::vector<Bytes> m_comps; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 163 | }; |
| 164 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 165 | std::ostream& |
| 166 | operator <<(std::ostream &os, const Name &name); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 167 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 168 | bool |
| 169 | operator ==(const Name &n1, const Name &n2); |
| 170 | |
| 171 | bool |
| 172 | operator !=(const Name &n1, const Name &n2); |
| 173 | |
| 174 | bool |
| 175 | operator <(const Name &n1, const Name &n2); |
| 176 | |
| 177 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 178 | std::string |
| 179 | Name::getCompFromBackAsString(int index) const |
| 180 | { |
| 181 | return getCompAsString (m_comps.size () - 1 - index); |
| 182 | } |
| 183 | |
| 184 | uint64_t |
| 185 | Name::getCompFromBackAsInt (int index) const |
| 186 | { |
| 187 | return getCompAsInt (m_comps.size () - 1 - index); |
| 188 | } |
| 189 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 190 | Name |
| 191 | Name::getPartialNameFromBack(int start, int n/* = -1*/) const |
| 192 | { |
| 193 | return getPartialName (m_comps.size () - 1 - start, n); |
| 194 | } |
| 195 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 196 | const Bytes & |
| 197 | Name::getCompFromBack (int index) const |
| 198 | { |
| 199 | return getComp (m_comps.size () - 1 - index); |
| 200 | } |
| 201 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 202 | |
| 203 | } // Ccnx |
| 204 | #endif |