blob: c852d33e4e6063c1e12fc67d82112f53cd97fc8d [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 *
18 * Author: Ilya Moiseenko <iliamo@cs.ucla.edu>
19 */
20
21#include "ccnx-name-components.h"
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070022#include <boost/foreach.hpp>
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070023#include "ns3/log.h"
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070024
25#include <iostream>
26
27using namespace std;
28
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070029NS_LOG_COMPONENT_DEFINE ("CcnxNameComponents");
30
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070031namespace ns3 {
32
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070033ATTRIBUTE_HELPER_CPP (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070034
Alexander Afanasyev90d66ce2011-08-25 20:30:17 -070035CcnxNameComponents::CcnxNameComponents (/* root */)
36{
37}
38
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070039CcnxNameComponents::CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070040{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070041 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
42 {
43 Add (component.get ());
44 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070045}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070046
47CcnxNameComponents::CcnxNameComponents (const std::string &prefix)
48{
49 istringstream is (prefix);
50 is >> *this;
51}
52
Alexander Afanasyev0560eec2012-07-16 15:44:31 -070053CcnxNameComponents::CcnxNameComponents (const char *prefix)
54{
55 NS_ASSERT (prefix != 0);
56
57 istringstream is (prefix);
58 is >> *this;
59}
Alexander Afanasyevf1e013f2012-07-11 17:59:40 -070060
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070061const std::list<std::string> &
62CcnxNameComponents::GetComponents () const
63{
64 return m_prefix;
65}
66
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080067std::string
68CcnxNameComponents::GetLastComponent () const
69{
70 if (m_prefix.size () == 0)
71 {
72 return "";
73 }
74
75 return m_prefix.back ();
76}
77
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070078std::list<boost::reference_wrapper<const std::string> >
79CcnxNameComponents::GetSubComponents (size_t num) const
80{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080081 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070082
83 std::list<boost::reference_wrapper<const std::string> > subComponents;
84 std::list<std::string>::const_iterator component = m_prefix.begin();
85 for (size_t i=0; i<num; i++, component++)
86 {
87 subComponents.push_back (boost::ref (*component));
88 }
89
90 return subComponents;
91}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070092
Alexander Afanasyev59dedfd2012-07-26 17:55:29 -070093CcnxNameComponents
94CcnxNameComponents::cut (size_t minusComponents) const
95{
96 CcnxNameComponents retval;
97 std::list<std::string>::const_iterator component = m_prefix.begin ();
98 for (uint32_t i = 0; i < m_prefix.size () - minusComponents; i++, component++)
99 {
100 retval.Add (*component);
101 }
102
103 return retval;
104}
105
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700106void
107CcnxNameComponents::Print (std::ostream &os) const
108{
109 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
110 {
111 os << "/" << *i;
112 }
Alexander Afanasyev07827182011-12-13 01:07:32 -0800113 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700114}
115
116std::ostream &
117operator << (std::ostream &os, const CcnxNameComponents &components)
118{
119 components.Print (os);
120 return os;
121}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700122
123std::istream &
124operator >> (std::istream &is, CcnxNameComponents &components)
125{
126 istream_iterator<char> eos; // end of stream
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700127
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700128 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800129 istream_iterator<char> it (is);
130 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700131 {
132 if (*it == '/')
133 {
134 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700135 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700136 component = "";
137 }
138 else
139 component.push_back (*it);
140 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700141 if (component != "")
142 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700143
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800144 is.clear ();
145 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
146
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700147 return is;
148}
149
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700150}
151