blob: 96d40ecaaca6ab381c5d781b0579af621d39bb44 [file] [log] [blame]
Ilya Moiseenkod26e6822011-08-23 17:48:38 -07001/* -*- 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 Afanasyev4aac5572012-08-09 10:49:55 -070018 * Author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
19 * Ilya Moiseenko <iliamo@cs.ucla.edu>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070020 */
21
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070022#include "ndn-name-components.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070023#include <boost/foreach.hpp>
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070024#include "ns3/log.h"
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070025
26#include <iostream>
27
28using namespace std;
29
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070030NS_LOG_COMPONENT_DEFINE ("NdnNameComponents");
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070031
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070032namespace ns3 {
33
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070034ATTRIBUTE_HELPER_CPP (NdnNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070035
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070036NdnNameComponents::NdnNameComponents (/* root */)
Alexander Afanasyev90d66ce2011-08-25 20:30:17 -070037{
38}
39
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070040NdnNameComponents::NdnNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070041{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070042 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
43 {
44 Add (component.get ());
45 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070046}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070047
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070048NdnNameComponents::NdnNameComponents (const std::string &prefix)
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070049{
50 istringstream is (prefix);
51 is >> *this;
52}
53
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070054NdnNameComponents::NdnNameComponents (const char *prefix)
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070055{
56 NS_ASSERT (prefix != 0);
57
58 istringstream is (prefix);
59 is >> *this;
60}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070061
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070062const std::list<std::string> &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070063NdnNameComponents::GetComponents () const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070064{
65 return m_prefix;
66}
67
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080068std::string
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070069NdnNameComponents::GetLastComponent () const
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080070{
71 if (m_prefix.size () == 0)
72 {
73 return "";
74 }
75
76 return m_prefix.back ();
77}
78
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070079std::list<boost::reference_wrapper<const std::string> >
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070080NdnNameComponents::GetSubComponents (size_t num) const
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070081{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080082 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070083
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 Moiseenkod26e6822011-08-23 17:48:38 -070093
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070094NdnNameComponents
95NdnNameComponents::cut (size_t minusComponents) const
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -070096{
Alexander Afanasyev4aac5572012-08-09 10:49:55 -070097 NdnNameComponents retval;
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -070098 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 Moiseenkod26e6822011-08-23 17:48:38 -0700107void
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700108NdnNameComponents::Print (std::ostream &os) const
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700109{
110 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
111 {
112 os << "/" << *i;
113 }
Alexander Afanasyev07827182011-12-13 01:07:32 -0800114 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700115}
116
117std::ostream &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700118operator << (std::ostream &os, const NdnNameComponents &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700119{
120 components.Print (os);
121 return os;
122}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700123
124std::istream &
Alexander Afanasyev4aac5572012-08-09 10:49:55 -0700125operator >> (std::istream &is, NdnNameComponents &components)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700126{
127 istream_iterator<char> eos; // end of stream
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700128
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700129 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800130 istream_iterator<char> it (is);
131 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700132 {
133 if (*it == '/')
134 {
135 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700136 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700137 component = "";
138 }
139 else
140 component.push_back (*it);
141 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700142 if (component != "")
143 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700144
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800145 is.clear ();
146 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
147
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700148 return is;
149}
150
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700151}
152