blob: 438e179527f3dec5996fc463e83027b4406fea65 [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 Afanasyevc5a23e22011-09-07 00:37:36 -070039// CcnxNameComponents::CcnxNameComponents (const string &s)
40// {
41// m_prefix.push_back (s);
42// }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070043
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070044CcnxNameComponents::CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070045{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070046 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
47 {
48 Add (component.get ());
49 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070050}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070051
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070052const std::list<std::string> &
53CcnxNameComponents::GetComponents () const
54{
55 return m_prefix;
56}
57
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070058std::list<boost::reference_wrapper<const std::string> >
59CcnxNameComponents::GetSubComponents (size_t num) const
60{
61 NS_ASSERT_MSG (1<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
62
63 std::list<boost::reference_wrapper<const std::string> > subComponents;
64 std::list<std::string>::const_iterator component = m_prefix.begin();
65 for (size_t i=0; i<num; i++, component++)
66 {
67 subComponents.push_back (boost::ref (*component));
68 }
69
70 return subComponents;
71}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070072
73// const ccn_charbuf*
74// Components::GetName () const
75// {
76// return m_value;
77// }
78
79CcnxNameComponents&
80CcnxNameComponents::operator () (const string &s)
81{
82 // ccn_name_append_str (m_value,s.c_str());
83 m_prefix.push_back (s);
84 return *this;
85}
86
87// Components::operator const unsigned char* ()
88// {
89// return m_value->buf;
90// }
91
92void
93CcnxNameComponents::Print (std::ostream &os) const
94{
95 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
96 {
97 os << "/" << *i;
98 }
99}
100
101std::ostream &
102operator << (std::ostream &os, const CcnxNameComponents &components)
103{
104 components.Print (os);
105 return os;
106}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700107
108std::istream &
109operator >> (std::istream &is, CcnxNameComponents &components)
110{
111 istream_iterator<char> eos; // end of stream
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700112
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700113 std::string component = "";
114 for (istream_iterator<char> it (is); it != eos; it++)
115 {
116 if (*it == '/')
117 {
118 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700119 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700120 component = "";
121 }
122 else
123 component.push_back (*it);
124 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700125 if (component != "")
126 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700127
128 return is;
129}
130
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700131}
132