blob: a79b6bb6af60b076645bf47ad80376a61e1a142f [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
53
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070054const std::list<std::string> &
55CcnxNameComponents::GetComponents () const
56{
57 return m_prefix;
58}
59
Alexander Afanasyev3183b5a2011-12-23 20:48:20 -080060std::string
61CcnxNameComponents::GetLastComponent () const
62{
63 if (m_prefix.size () == 0)
64 {
65 return "";
66 }
67
68 return m_prefix.back ();
69}
70
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070071std::list<boost::reference_wrapper<const std::string> >
72CcnxNameComponents::GetSubComponents (size_t num) const
73{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080074 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070075
76 std::list<boost::reference_wrapper<const std::string> > subComponents;
77 std::list<std::string>::const_iterator component = m_prefix.begin();
78 for (size_t i=0; i<num; i++, component++)
79 {
80 subComponents.push_back (boost::ref (*component));
81 }
82
83 return subComponents;
84}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070085
86void
87CcnxNameComponents::Print (std::ostream &os) const
88{
89 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
90 {
91 os << "/" << *i;
92 }
Alexander Afanasyev07827182011-12-13 01:07:32 -080093 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070094}
95
96std::ostream &
97operator << (std::ostream &os, const CcnxNameComponents &components)
98{
99 components.Print (os);
100 return os;
101}
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700102
103std::istream &
104operator >> (std::istream &is, CcnxNameComponents &components)
105{
106 istream_iterator<char> eos; // end of stream
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700107
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700108 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800109 istream_iterator<char> it (is);
110 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700111 {
112 if (*it == '/')
113 {
114 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700115 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700116 component = "";
117 }
118 else
119 component.push_back (*it);
120 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700121 if (component != "")
122 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700123
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800124 is.clear ();
125 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
126
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700127 return is;
128}
129
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700130}
131