blob: 77ac75c9572b9fbf5bb8dfbd3c950da00ec40ba4 [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 Afanasyev3183b5a2011-12-23 20:48:20 -080058std::string
59CcnxNameComponents::GetLastComponent () const
60{
61 if (m_prefix.size () == 0)
62 {
63 return "";
64 }
65
66 return m_prefix.back ();
67}
68
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070069std::list<boost::reference_wrapper<const std::string> >
70CcnxNameComponents::GetSubComponents (size_t num) const
71{
Alexander Afanasyev4a5c2c12011-12-12 18:50:57 -080072 NS_ASSERT_MSG (0<=num && num<=m_prefix.size (), "Invalid number of subcomponents requested");
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -070073
74 std::list<boost::reference_wrapper<const std::string> > subComponents;
75 std::list<std::string>::const_iterator component = m_prefix.begin();
76 for (size_t i=0; i<num; i++, component++)
77 {
78 subComponents.push_back (boost::ref (*component));
79 }
80
81 return subComponents;
82}
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070083
84void
85CcnxNameComponents::Print (std::ostream &os) const
86{
87 for (const_iterator i=m_prefix.begin(); i!=m_prefix.end(); i++)
88 {
89 os << "/" << *i;
90 }
Alexander Afanasyev07827182011-12-13 01:07:32 -080091 if (m_prefix.size ()==0) os << "/";
Ilya Moiseenkod26e6822011-08-23 17:48:38 -070092}
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
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700105
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700106 std::string component = "";
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800107 istream_iterator<char> it (is);
108 for (; it != eos; it++)
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700109 {
110 if (*it == '/')
111 {
112 if (component != "")
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700113 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700114 component = "";
115 }
116 else
117 component.push_back (*it);
118 }
Alexander Afanasyevc5a23e22011-09-07 00:37:36 -0700119 if (component != "")
120 components.Add (component);
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700121
Alexander Afanasyev4975f732011-12-20 17:52:19 -0800122 is.clear ();
123 // NS_LOG_ERROR (components << ", bad: " << is.bad () <<", fail: " << is.fail ());
124
Alexander Afanasyev7fd74f92011-08-25 19:40:17 -0700125 return is;
126}
127
Ilya Moiseenkod26e6822011-08-23 17:48:38 -0700128}
129