Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2011 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 | * |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 18 | * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 19 | * Ilya Moiseenko <iliamo@cs.ucla.edu> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 20 | */ |
| 21 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 22 | #include "ndn-name-components.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 23 | #include <boost/foreach.hpp> |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 24 | #include "ns3/log.h" |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 25 | |
| 26 | #include <iostream> |
| 27 | |
| 28 | using namespace std; |
| 29 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 30 | NS_LOG_COMPONENT_DEFINE ("NdnNameComponents"); |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 31 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 32 | namespace ns3 { |
| 33 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 34 | ATTRIBUTE_HELPER_CPP (NdnNameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 35 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 36 | NdnNameComponents::NdnNameComponents (/* root */) |
Alexander Afanasyev | 90d66ce | 2011-08-25 20:30:17 -0700 | [diff] [blame] | 37 | { |
| 38 | } |
| 39 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 40 | NdnNameComponents::NdnNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 41 | { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 42 | BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components) |
| 43 | { |
| 44 | Add (component.get ()); |
| 45 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 46 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 47 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 48 | NdnNameComponents::NdnNameComponents (const std::string &prefix) |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 49 | { |
| 50 | istringstream is (prefix); |
| 51 | is >> *this; |
| 52 | } |
| 53 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 54 | NdnNameComponents::NdnNameComponents (const char *prefix) |
Alexander Afanasyev | 0560eec | 2012-07-16 15:44:31 -0700 | [diff] [blame] | 55 | { |
| 56 | NS_ASSERT (prefix != 0); |
| 57 | |
| 58 | istringstream is (prefix); |
| 59 | is >> *this; |
| 60 | } |
Alexander Afanasyev | f1e013f | 2012-07-11 17:59:40 -0700 | [diff] [blame] | 61 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 62 | const std::list<std::string> & |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 63 | NdnNameComponents::GetComponents () const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 64 | { |
| 65 | return m_prefix; |
| 66 | } |
| 67 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 68 | std::string |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 69 | NdnNameComponents::GetLastComponent () const |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 70 | { |
| 71 | if (m_prefix.size () == 0) |
| 72 | { |
| 73 | return ""; |
| 74 | } |
| 75 | |
| 76 | return m_prefix.back (); |
| 77 | } |
| 78 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 79 | std::list<boost::reference_wrapper<const std::string> > |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 80 | NdnNameComponents::GetSubComponents (size_t num) const |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 81 | { |
Alexander Afanasyev | 4a5c2c1 | 2011-12-12 18:50:57 -0800 | [diff] [blame] | 82 | NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested"); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 83 | |
| 84 | std::list<boost::reference_wrapper<const std::string> > subComponents; |
| 85 | std::list<std::string>::const_iterator component = m_prefix.begin(); |
| 86 | for (size_t i=0; i<num; i++, component++) |
| 87 | { |
| 88 | subComponents.push_back (boost::ref (*component)); |
| 89 | } |
| 90 | |
| 91 | return subComponents; |
| 92 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 93 | |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 94 | NdnNameComponents |
| 95 | NdnNameComponents::cut (size_t minusComponents) const |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 96 | { |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 97 | NdnNameComponents retval; |
Alexander Afanasyev | 59dedfd | 2012-07-26 17:55:29 -0700 | [diff] [blame] | 98 | std::list<std::string>::const_iterator component = m_prefix.begin (); |
| 99 | for (uint32_t i = 0; i < m_prefix.size () - minusComponents; i++, component++) |
| 100 | { |
| 101 | retval.Add (*component); |
| 102 | } |
| 103 | |
| 104 | return retval; |
| 105 | } |
| 106 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 107 | void |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 108 | NdnNameComponents::Print (std::ostream &os) const |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 109 | { |
| 110 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 111 | { |
| 112 | os << "/" << *i; |
| 113 | } |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 114 | if (m_prefix.size ()==0) os << "/"; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | std::ostream & |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 118 | operator << (std::ostream &os, const NdnNameComponents &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 119 | { |
| 120 | components.Print (os); |
| 121 | return os; |
| 122 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 123 | |
| 124 | std::istream & |
Alexander Afanasyev | 4aac557 | 2012-08-09 10:49:55 -0700 | [diff] [blame] | 125 | operator >> (std::istream &is, NdnNameComponents &components) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 126 | { |
| 127 | istream_iterator<char> eos; // end of stream |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 128 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 129 | std::string component = ""; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 130 | istream_iterator<char> it (is); |
| 131 | for (; it != eos; it++) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 132 | { |
| 133 | if (*it == '/') |
| 134 | { |
| 135 | if (component != "") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 136 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 137 | component = ""; |
| 138 | } |
| 139 | else |
| 140 | component.push_back (*it); |
| 141 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 142 | if (component != "") |
| 143 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 144 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 145 | is.clear (); |
| 146 | // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ()); |
| 147 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 148 | return is; |
| 149 | } |
| 150 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 151 | } |
| 152 | |