blob: d7854234a4f581b5db39b9aedfb62945c0341b7d [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 Afanasyev7fd74f92011-08-25 19:40:17 -070046
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070047const std::list<std::string> &
48CcnxNameComponents::GetComponents () const
49{
50 return m_prefix;
51}
52
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070053std::list<boost::reference_wrapper<const std::string> >
54CcnxNameComponents::GetSubComponents (size_t num) const
55{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080056 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070057
58 std::list<boost::reference_wrapper<const std::string> > subComponents;
59 std::list<std::string>::const_iterator component = m_prefix.begin();
60 for (size_t i=0; i<num; i++, component++)
61 {
62 subComponents.push_back (boost::ref (*component));
63 }
64
65 return subComponents;
66}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070067
68void
69CcnxNameComponents::Print (std::ostream &os) const
70{
71 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
72 {
73 os << "/" << *i;
74 }
Alexander Afanasyev07827182011-12-13 01:07:32 -080075 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070076}
77
78std::ostream &
79operator << (std::ostream &os, const CcnxNameComponents &components)
80{
81 components.Print (os);
82 return os;
83}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070084
85std::istream &
86operator >> (std::istream &is, CcnxNameComponents &components)
87{
88 istream_iterator<char> eos; // end of stream
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070089
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070090 std::string component = "";
91 for (istream_iterator<char> it (is); it != eos; it++)
92 {
93 if (*it == '/')
94 {
95 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -070096 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070097 component = "";
98 }
99 else
100 component.push_back (*it);
101 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700102 if (component != "")
103 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700104
105 return is;
106}
107
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700108}
109