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" |
Alexander Afanasyev | 452645b | 2013-02-24 15:41:29 -0800 | [diff] [blame] | 26 | #include "ccnx-charbuf.h" |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 27 | |
| 28 | namespace Ccnx { |
| 29 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 30 | struct NameException: |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 31 | virtual boost::exception, virtual std::exception {}; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 32 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 33 | class Name |
| 34 | { |
| 35 | public: |
| 36 | Name(); |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 37 | Name(const std::string &name); |
| 38 | Name(const std::vector<Bytes> &comps); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 39 | Name(const Name &other); |
| 40 | Name(const unsigned char *data, const ccn_indexbuf *comps); |
Alexander Afanasyev | eb575e0 | 2013-01-26 17:14:51 -0800 | [diff] [blame] | 41 | Name (const void *buf, const size_t length); |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 42 | Name (const CcnxCharbuf &buf); |
| 43 | Name (const ccn_charbuf *buf); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 44 | virtual ~Name() {} |
| 45 | |
| 46 | CcnxCharbufPtr |
| 47 | toCcnxCharbuf() const; |
| 48 | |
Alexander Afanasyev | 452645b | 2013-02-24 15:41:29 -0800 | [diff] [blame] | 49 | CcnxCharbuf* |
| 50 | toCcnxCharbufRaw () const; |
| 51 | |
Alexander Afanasyev | d09871f | 2013-01-04 22:36:37 -0800 | [diff] [blame] | 52 | operator CcnxCharbufPtr () const { return toCcnxCharbuf (); } |
| 53 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 54 | Name & |
Alexander Afanasyev | 66f4c49 | 2013-01-20 23:32:50 -0800 | [diff] [blame] | 55 | appendComp(const Name &comp); |
| 56 | |
| 57 | Name & |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 58 | appendComp(const Bytes &comp); |
| 59 | |
| 60 | Name & |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 61 | appendComp(const std::string &compStr); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 62 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 63 | Name & |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 64 | appendComp(const void *buf, size_t size); |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 65 | |
| 66 | /** |
| 67 | * Append int component |
| 68 | * |
| 69 | * Difference between this and appendComp call is that data is appended in network order |
| 70 | * |
| 71 | * Also, this function honors naming convention (%00 prefix is added) |
| 72 | */ |
| 73 | Name & |
| 74 | appendComp(uint64_t number); |
Alexander Afanasyev | 5054789 | 2013-01-19 22:03:45 -0800 | [diff] [blame] | 75 | |
| 76 | template<class T> |
| 77 | Name & |
| 78 | operator ()(const T &comp) { return appendComp (comp); } |
| 79 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 80 | Name & |
| 81 | operator ()(const void *buf, size_t size) { return appendComp (buf, size); } |
| 82 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 83 | int |
| 84 | size() const {return m_comps.size();} |
| 85 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 86 | const Bytes & |
| 87 | getComp (int index) const; |
| 88 | |
| 89 | inline const Bytes & |
| 90 | getCompFromBack (int index) const; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 91 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 92 | // return std::string format of the comp |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 93 | // if all characters are printable, simply returns the string |
| 94 | // if not, print the bytes in hex string format |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 95 | std::string |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 96 | getCompAsString(int index) const; |
| 97 | |
Alexander Afanasyev | c9eb68f | 2013-01-07 13:40:00 -0800 | [diff] [blame] | 98 | uint64_t |
| 99 | getCompAsInt (int index) const; |
| 100 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 101 | inline std::string |
| 102 | getCompFromBackAsString(int index) const; |
| 103 | |
| 104 | inline uint64_t |
| 105 | getCompFromBackAsInt (int index) const; |
| 106 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 107 | Name |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 108 | getPartialName(int start, int n = -1) const; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 109 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 110 | inline Name |
| 111 | getPartialNameFromBack(int start, int n = -1) const; |
| 112 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 113 | std::string |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 114 | toString() const; |
| 115 | |
| 116 | Name & |
| 117 | operator=(const Name &other); |
| 118 | |
| 119 | bool |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 120 | operator==(const std::string &str) const; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 121 | |
| 122 | bool |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 123 | operator!=(const std::string &str) const; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 124 | |
| 125 | friend Name |
| 126 | operator+(const Name &n1, const Name &n2); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 127 | |
Alexander Afanasyev | 0995f32 | 2013-01-22 13:16:46 -0800 | [diff] [blame] | 128 | private: |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 129 | std::vector<Bytes> m_comps; |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 130 | }; |
| 131 | |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 132 | typedef boost::shared_ptr<Name> NamePtr; |
| 133 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 134 | std::ostream& |
| 135 | operator <<(std::ostream &os, const Name &name); |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 136 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 137 | bool |
| 138 | operator ==(const Name &n1, const Name &n2); |
| 139 | |
| 140 | bool |
| 141 | operator !=(const Name &n1, const Name &n2); |
| 142 | |
| 143 | bool |
| 144 | operator <(const Name &n1, const Name &n2); |
| 145 | |
| 146 | |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 147 | std::string |
| 148 | Name::getCompFromBackAsString(int index) const |
| 149 | { |
| 150 | return getCompAsString (m_comps.size () - 1 - index); |
| 151 | } |
| 152 | |
| 153 | uint64_t |
| 154 | Name::getCompFromBackAsInt (int index) const |
| 155 | { |
| 156 | return getCompAsInt (m_comps.size () - 1 - index); |
| 157 | } |
| 158 | |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 159 | Name |
| 160 | Name::getPartialNameFromBack(int start, int n/* = -1*/) const |
| 161 | { |
| 162 | return getPartialName (m_comps.size () - 1 - start, n); |
| 163 | } |
| 164 | |
Alexander Afanasyev | f9978f8 | 2013-01-23 16:30:31 -0800 | [diff] [blame] | 165 | const Bytes & |
| 166 | Name::getCompFromBack (int index) const |
| 167 | { |
| 168 | return getComp (m_comps.size () - 1 - index); |
| 169 | } |
| 170 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 171 | |
| 172 | } // Ccnx |
| 173 | #endif |