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> |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 23 | |
| 24 | #include <iostream> |
| 25 | |
| 26 | using namespace std; |
| 27 | |
| 28 | namespace ns3 { |
| 29 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 30 | ATTRIBUTE_HELPER_CPP (CcnxNameComponents); |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 31 | |
Alexander Afanasyev | 90d66ce | 2011-08-25 20:30:17 -0700 | [diff] [blame] | 32 | CcnxNameComponents::CcnxNameComponents (/* root */) |
| 33 | { |
| 34 | } |
| 35 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 36 | CcnxNameComponents::CcnxNameComponents (const string &s) |
| 37 | { |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 38 | m_prefix.push_back (s); |
| 39 | } |
| 40 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 41 | CcnxNameComponents::CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components) |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 42 | { |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 43 | BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components) |
| 44 | { |
| 45 | Add (component.get ()); |
| 46 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 47 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 48 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 49 | const std::list<std::string> & |
| 50 | CcnxNameComponents::GetComponents () const |
| 51 | { |
| 52 | return m_prefix; |
| 53 | } |
| 54 | |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 55 | std::list<boost::reference_wrapper<const std::string> > |
| 56 | CcnxNameComponents::GetSubComponents (size_t num) const |
| 57 | { |
| 58 | NS_ASSERT_MSG (1<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested"); |
| 59 | |
| 60 | std::list<boost::reference_wrapper<const std::string> > subComponents; |
| 61 | std::list<std::string>::const_iterator component = m_prefix.begin(); |
| 62 | for (size_t i=0; i<num; i++, component++) |
| 63 | { |
| 64 | subComponents.push_back (boost::ref (*component)); |
| 65 | } |
| 66 | |
| 67 | return subComponents; |
| 68 | } |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 69 | |
| 70 | // const ccn_charbuf* |
| 71 | // Components::GetName () const |
| 72 | // { |
| 73 | // return m_value; |
| 74 | // } |
| 75 | |
| 76 | CcnxNameComponents& |
| 77 | CcnxNameComponents::operator () (const string &s) |
| 78 | { |
| 79 | // ccn_name_append_str (m_value,s.c_str()); |
| 80 | m_prefix.push_back (s); |
| 81 | return *this; |
| 82 | } |
| 83 | |
| 84 | // Components::operator const unsigned char* () |
| 85 | // { |
| 86 | // return m_value->buf; |
| 87 | // } |
| 88 | |
| 89 | void |
| 90 | CcnxNameComponents::Print (std::ostream &os) const |
| 91 | { |
| 92 | for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++) |
| 93 | { |
| 94 | os << "/" << *i; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | std::ostream & |
| 99 | operator << (std::ostream &os, const CcnxNameComponents &components) |
| 100 | { |
| 101 | components.Print (os); |
| 102 | return os; |
| 103 | } |
Alexander Afanasyev | 7fd74f9 | 2011-08-25 19:40:17 -0700 | [diff] [blame] | 104 | |
| 105 | std::istream & |
| 106 | operator >> (std::istream &is, CcnxNameComponents &components) |
| 107 | { |
| 108 | istream_iterator<char> eos; // end of stream |
| 109 | |
| 110 | std::string component = ""; |
| 111 | for (istream_iterator<char> it (is); it != eos; it++) |
| 112 | { |
| 113 | if (*it == '/') |
| 114 | { |
| 115 | if (component != "") |
| 116 | components.Add (component); |
| 117 | component = ""; |
| 118 | } |
| 119 | else |
| 120 | component.push_back (*it); |
| 121 | } |
| 122 | |
| 123 | return is; |
| 124 | } |
| 125 | |
Ilya Moiseenko | d26e682 | 2011-08-23 17:48:38 -0700 | [diff] [blame] | 126 | } |
| 127 | |