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 | * |
| 18 | * Author: Ilya Moiseenko <iliamo@cs.ucla.edu> |
| 19 | */ |
| 20 | |
| 21 | #include "ccnx-name-components.h" |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 22 | #include <boost/foreach.hpp> |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 23 | #include "ns3/log.h" |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 24 | |
| 25 | #include <iostream> |
| 26 | |
| 27 | using namespace std; |
| 28 | |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 29 | NS_LOG_COMPONENT_DEFINE ("CcnxNameComponents"); |
| 30 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 31 | namespace ns3 { |
| 32 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 33 | ATTRIBUTE_HELPER_CPP (CcnxNameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 34 | |
Alexander Afanasyev | 90d66ce | 2011-08-25 20:30:17 -0700 | [diff] [blame] | 35 | CcnxNameComponents::CcnxNameComponents (/* root */) |
| 36 | { |
| 37 | } |
| 38 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 39 | CcnxNameComponents::CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 40 | { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 41 | BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components) |
| 42 | { |
| 43 | Add (component.get ()); |
| 44 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 45 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 46 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 47 | const std::list<std::string> & |
| 48 | CcnxNameComponents::GetComponents () const |
| 49 | { |
| 50 | return m_prefix; |
| 51 | } |
| 52 | |
Alexander Afanasyev | 3183b5a | 2011-12-23 20:48:20 -0800 | [diff] [blame] | 53 | std::string |
| 54 | CcnxNameComponents::GetLastComponent () const |
| 55 | { |
| 56 | if (m_prefix.size () == 0) |
| 57 | { |
| 58 | return ""; |
| 59 | } |
| 60 | |
| 61 | return m_prefix.back (); |
| 62 | } |
| 63 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 64 | std::list<boost::reference_wrapper<const std::string> > |
| 65 | CcnxNameComponents::GetSubComponents (size_t num) const |
| 66 | { |
Alexander Afanasyev | 4a5c2c1 | 2011-12-12 18:50:57 -0800 | [diff] [blame] | 67 | 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] | 68 | |
| 69 | std::list<boost::reference_wrapper<const std::string> > subComponents; |
| 70 | std::list<std::string>::const_iterator component = m_prefix.begin(); |
| 71 | for (size_t i=0; i<num; i++, component++) |
| 72 | { |
| 73 | subComponents.push_back (boost::ref (*component)); |
| 74 | } |
| 75 | |
| 76 | return subComponents; |
| 77 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 78 | |
| 79 | void |
| 80 | CcnxNameComponents::Print (std::ostream &os) const |
| 81 | { |
| 82 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 83 | { |
| 84 | os << "/" << *i; |
| 85 | } |
Alexander Afanasyev | 0782718 | 2011-12-13 01:07:32 -0800 | [diff] [blame] | 86 | if (m_prefix.size ()==0) os << "/"; |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | std::ostream & |
| 90 | operator << (std::ostream &os, const CcnxNameComponents &components) |
| 91 | { |
| 92 | components.Print (os); |
| 93 | return os; |
| 94 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 95 | |
| 96 | std::istream & |
| 97 | operator >> (std::istream &is, CcnxNameComponents &components) |
| 98 | { |
| 99 | istream_iterator<char> eos; // end of stream |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 100 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 101 | std::string component = ""; |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 102 | istream_iterator<char> it (is); |
| 103 | for (; it != eos; it++) |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 104 | { |
| 105 | if (*it == '/') |
| 106 | { |
| 107 | if (component != "") |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 108 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 109 | component = ""; |
| 110 | } |
| 111 | else |
| 112 | component.push_back (*it); |
| 113 | } |
Alexander Afanasyev | c5a23e2 | 2011-09-07 00:37:36 -0700 | [diff] [blame] | 114 | if (component != "") |
| 115 | components.Add (component); |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 116 | |
Alexander Afanasyev | 4975f73 | 2011-12-20 17:52:19 -0800 | [diff] [blame] | 117 | is.clear (); |
| 118 | // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ()); |
| 119 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 120 | return is; |
| 121 | } |
| 122 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 123 | } |
| 124 | |