blob: 303e98759e13e9a20d1a504772a905517091ce70 [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
Alexander Afanasyev90d66ce2011-08-25 20:30:17 -070032CcnxNameComponents::CcnxNameComponents (/* root */)
33{
34}
35
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070036CcnxNameComponents::CcnxNameComponents (const string &s)
37{
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070038 m_prefix.push_back (s);
39}
40
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070041CcnxNameComponents::CcnxNameComponents (const std::list<boost::reference_wrapper<const std::string> > &components)
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070042{
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070043 BOOST_FOREACH (const boost::reference_wrapper<const std::string> &component, components)
44 {
45 Add (component.get ());
46 }
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070047}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070048
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070049const std::list<std::string> &
50CcnxNameComponents::GetComponents () const
51{
52 return m_prefix;
53}
54
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070055std::list<boost::reference_wrapper<const std::string> >
56CcnxNameComponents::GetSubComponents (size_t num) const
57{
58 NS_ASSERT_MSG (1<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
59
60 std::list<boost::reference_wrapper<const std::string> > subComponents;
61 std::list<std::string>::const_iterator component = m_prefix.begin();
62 for (size_t i=0; i<num; i++, component++)
63 {
64 subComponents.push_back (boost::ref (*component));
65 }
66
67 return subComponents;
68}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070069
70// const ccn_charbuf*
71// Components::GetName () const
72// {
73// return m_value;
74// }
75
76CcnxNameComponents&
77CcnxNameComponents::operator () (const string &s)
78{
79 // ccn_name_append_str (m_value,s.c_str());
80 m_prefix.push_back (s);
81 return *this;
82}
83
84// Components::operator const unsigned char* ()
85// {
86// return m_value->buf;
87// }
88
89void
90CcnxNameComponents::Print (std::ostream &os) const
91{
92 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
93 {
94 os << "/" << *i;
95 }
96}
97
98std::ostream &
99operator << (std::ostream &os, const CcnxNameComponents &components)
100{
101 components.Print (os);
102 return os;
103}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700104
105std::istream &
106operator >> (std::istream &is, CcnxNameComponents &components)
107{
108 istream_iterator<char> eos; // end of stream
109
110 std::string component = "";
111 for (istream_iterator<char> it (is); it != eos; it++)
112 {
113 if (*it == '/')
114 {
115 if (component != "")
116 components.Add (component);
117 component = "";
118 }
119 else
120 component.push_back (*it);
121 }
122
123 return is;
124}
125
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700126}
127