A Discrete-Event Network Simulator
API
raw-text-config.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 INRIA
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: Mathieu Lacage <mathieu.lacage@cutebugs.net>
19  */
20 
21 #include "raw-text-config.h"
22 #include "attribute-iterator.h"
24 #include "ns3/global-value.h"
25 #include "ns3/string.h"
26 #include "ns3/log.h"
27 #include "ns3/config.h"
28 
29 namespace ns3 {
30 
31 NS_LOG_COMPONENT_DEFINE ("RawTextConfig");
32 
34  : m_os (0)
35 {
36  NS_LOG_FUNCTION (this);
37 }
39 {
40  NS_LOG_FUNCTION (this);
41  if (m_os != 0)
42  {
43  m_os->close ();
44  }
45  delete m_os;
46  m_os = 0;
47 }
48 void
49 RawTextConfigSave::SetFilename (std::string filename)
50 {
51  NS_LOG_FUNCTION (this << filename);
52  m_os = new std::ofstream ();
53  m_os->open (filename.c_str (), std::ios::out);
54 }
55 void
57 {
58  NS_LOG_FUNCTION (this);
59  class RawTextDefaultIterator : public AttributeDefaultIterator
60  {
61 public:
62  RawTextDefaultIterator (std::ostream *os) {
63  m_os = os;
64  }
65 private:
66  virtual void StartVisitTypeId (std::string name) {
67  m_typeId = name;
68  }
69  virtual void DoVisitAttribute (std::string name, std::string defaultValue) {
70  NS_LOG_DEBUG ("Saving " << m_typeId << "::" << name);
71  *m_os << "default " << m_typeId << "::" << name << " \"" << defaultValue << "\"" << std::endl;
72  }
73  std::string m_typeId;
74  std::ostream *m_os;
75  };
76 
77  RawTextDefaultIterator iterator = RawTextDefaultIterator (m_os);
78  iterator.Iterate ();
79 }
80 void
82 {
83  NS_LOG_FUNCTION (this);
85  {
86  StringValue value;
87  (*i)->GetValue (value);
88  NS_LOG_LOGIC ("Saving " << (*i)->GetName ());
89  *m_os << "global " << (*i)->GetName () << " \"" << value.Get () << "\"" << std::endl;
90  }
91 }
92 void
94 {
95  NS_LOG_FUNCTION (this);
96  class RawTextAttributeIterator : public AttributeIterator
97  {
98 public:
99  RawTextAttributeIterator (std::ostream *os)
100  : m_os (os) {}
101 private:
102  virtual void DoVisitAttribute (Ptr<Object> object, std::string name) {
103  StringValue str;
104  object->GetAttribute (name, str);
105  NS_LOG_DEBUG ("Saving " << GetCurrentPath ());
106  *m_os << "value " << GetCurrentPath () << " \"" << str.Get () << "\"" << std::endl;
107  }
108  std::ostream *m_os;
109  };
110 
111  RawTextAttributeIterator iter = RawTextAttributeIterator (m_os);
112  iter.Iterate ();
113 }
114 
116  : m_is (0)
117 {
118  NS_LOG_FUNCTION (this);
119 }
121 {
122  NS_LOG_FUNCTION (this);
123  if (m_is != 0)
124  {
125  m_is->close ();
126  delete m_is;
127  m_is = 0;
128  }
129 }
130 void
131 RawTextConfigLoad::SetFilename (std::string filename)
132 {
133  NS_LOG_FUNCTION (this << filename);
134  m_is = new std::ifstream ();
135  m_is->open (filename.c_str (), std::ios::in);
136 }
137 std::string
138 RawTextConfigLoad::Strip (std::string value)
139 {
140  std::string::size_type start = value.find ("\"");
141  std::string::size_type end = value.find ("\"", 1);
142  NS_ASSERT (start == 0);
143  NS_ASSERT (end == value.size () - 1);
144  return value.substr (start+1, end-start-1);
145 }
146 
147 void
149 {
150  NS_LOG_FUNCTION (this);
151  m_is->clear ();
152  m_is->seekg (0);
153  std::string type, name, value;
154  *m_is >> type >> name >> value;
155  while (m_is->good ())
156  {
157  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
158  value = Strip (value);
159  if (type == "default")
160  {
161  Config::SetDefault (name, StringValue (value));
162  }
163  *m_is >> type >> name >> value;
164  }
165 }
166 void
168 {
169  NS_LOG_FUNCTION (this);
170  m_is->clear ();
171  m_is->seekg (0);
172  std::string type, name, value;
173  *m_is >> type >> name >> value;
174  while (m_is->good ())
175  {
176  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
177  value = Strip (value);
178  if (type == "global")
179  {
180  Config::SetGlobal (name, StringValue (value));
181  }
182  *m_is >> type >> name >> value;
183  }
184 }
185 void
187 {
188  NS_LOG_FUNCTION (this);
189  m_is->clear ();
190  m_is->seekg (0);
191  std::string type, path, value;
192  *m_is >> type >> path >> value;
193  while (m_is->good ())
194  {
195  NS_LOG_DEBUG ("type=" << type << ", path=" << path << ", value=" << value);
196  value = Strip (value);
197  if (type == "value")
198  {
199  Config::Set (path, StringValue (value));
200  }
201  *m_is >> type >> path >> value;
202  }
203 }
204 
205 
206 } // namespace ns3
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
virtual void Attributes(void)
Load or save the attributes values.
Hold variables of type string.
Definition: string.h:41
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:777
virtual void SetFilename(std::string filename)
Set the file name.
def start()
Definition: core.py:1844
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:80
virtual void SetFilename(std::string filename)
Set the file name.
std::string Get(void) const
Definition: string.cc:31
static Iterator Begin(void)
The Begin iterator.
virtual void Global(void)
Load or save the global values.
std::string Strip(std::string value)
Strip out attribute value.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Attributes(void)
Load or save the attributes values.
virtual void Default(void)
Load or save the default values.
NS_LOG_LOGIC("Net device "<< nd<< " is not bridged")
void SetGlobal(std::string name, const AttributeValue &value)
Definition: config.cc:822
static Iterator End(void)
The End iterator.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
Iterator to iterate on the values of attributes of an ns3::Object.
Iterator to iterate on the default values of attributes of an ns3::Object.
std::ofstream * m_os
Config store output stream.
std::ifstream * m_is
Config store input stream.
virtual void Global(void)
Load or save the global values.
virtual void Default(void)
Load or save the default values.