blob: 1014f3dd4f789abc0c68172c462a5314430d52ea [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>
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070023
24#include <iostream>
25
26using namespace std;
27
28namespace ns3 {
29
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070030ATTRIBUTE_HELPER_CPP (CcnxNameComponents);
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070031
32CcnxNameComponents::CcnxNameComponents (const string &s)
33{
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070034 m_prefix.push_back (s);
35}
36
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070037CcnxNameComponents::CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070038{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070039 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
40 {
41 Add (component.get ());
42 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070043}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070044
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070045const std::list<std::string> &
46CcnxNameComponents::GetComponents () const
47{
48 return m_prefix;
49}
50
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070051std::list<boost::reference_wrapper<const std::string> >
52CcnxNameComponents::GetSubComponents (size_t num) const
53{
54 NS_ASSERT_MSG (1<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
55
56 std::list<boost::reference_wrapper<const std::string> > subComponents;
57 std::list<std::string>::const_iterator component = m_prefix.begin();
58 for (size_t i=0; i<num; i++, component++)
59 {
60 subComponents.push_back (boost::ref (*component));
61 }
62
63 return subComponents;
64}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070065
66// const ccn_charbuf*
67// Components::GetName () const
68// {
69// return m_value;
70// }
71
72CcnxNameComponents&
73CcnxNameComponents::operator () (const string &s)
74{
75 // ccn_name_append_str (m_value,s.c_str());
76 m_prefix.push_back (s);
77 return *this;
78}
79
80// Components::operator const unsigned char* ()
81// {
82// return m_value->buf;
83// }
84
85void
86CcnxNameComponents::Print (std::ostream &os) const
87{
88 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
89 {
90 os << "/" << *i;
91 }
92}
93
94std::ostream &
95operator << (std::ostream &os, const CcnxNameComponents &components)
96{
97 components.Print (os);
98 return os;
99}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700100
101std::istream &
102operator >> (std::istream &is, CcnxNameComponents &components)
103{
104 istream_iterator<char> eos; // end of stream
105
106 std::string component = "";
107 for (istream_iterator<char> it (is); it != eos; it++)
108 {
109 if (*it == '/')
110 {
111 if (component != "")
112 components.Add (component);
113 component = "";
114 }
115 else
116 component.push_back (*it);
117 }
118
119 return is;
120}
121
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700122}
123