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 #include <istream>
30 #include <sstream>
31 #include <algorithm>
32 #include <functional>
33 
34 namespace ns3 {
35 
36 NS_LOG_COMPONENT_DEFINE ("RawTextConfig");
37 
39  : m_os (0)
40 {
41  NS_LOG_FUNCTION (this);
42 }
44 {
45  NS_LOG_FUNCTION (this);
46  if (m_os != 0)
47  {
48  m_os->close ();
49  }
50  delete m_os;
51  m_os = 0;
52 }
53 void
54 RawTextConfigSave::SetFilename (std::string filename)
55 {
56  NS_LOG_FUNCTION (this << filename);
57  m_os = new std::ofstream ();
58  m_os->open (filename.c_str (), std::ios::out);
59 }
60 void
62 {
63  NS_LOG_FUNCTION (this);
64  class RawTextDefaultIterator : public AttributeDefaultIterator
65  {
66 public:
67  RawTextDefaultIterator (std::ostream *os) {
68  m_os = os;
69  }
70  void SetSaveDeprecated (bool saveDeprecated) {
71  m_saveDeprecated = saveDeprecated;
72  }
73 
74 private:
75  virtual void StartVisitTypeId (std::string name) {
76  m_typeId = name;
77  }
78  virtual void DoVisitAttribute (std::string name, std::string defaultValue) {
79  NS_LOG_DEBUG ("Saving " << m_typeId << "::" << name);
80  TypeId tid = TypeId::LookupByName (m_typeId);
81  ns3::TypeId::SupportLevel supportLevel = TypeId::SupportLevel::SUPPORTED;
82  for (std::size_t i = 0; i < tid.GetAttributeN (); i++)
83  {
84  struct TypeId::AttributeInformation tmp = tid.GetAttribute (i);
85  if (tmp.name == name)
86  {
87  supportLevel = tmp.supportLevel;
88  break;
89  }
90  }
91  if (supportLevel == TypeId::SupportLevel::OBSOLETE)
92  {
93  NS_LOG_WARN ("Global attribute "
94  << m_typeId << "::" << name
95  << " was not saved because it is OBSOLETE");
96  }
97  else if ((supportLevel == TypeId::SupportLevel::DEPRECATED) && (m_saveDeprecated == false))
98  {
99  NS_LOG_WARN ("Global attribute " << m_typeId << "::" << name
100  << " was not saved because it is DEPRECATED");
101  }
102  else
103  {
104  *m_os << "default " << m_typeId << "::" << name << " \"" << defaultValue << "\""
105  << std::endl;
106  }
107  }
108  std::string m_typeId;
109  std::ostream *m_os;
110  bool m_saveDeprecated;
111  };
112 
113  RawTextDefaultIterator iterator = RawTextDefaultIterator (m_os);
114  iterator.SetSaveDeprecated (m_saveDeprecated);
115  iterator.Iterate ();
116 }
117 void
119 {
120  NS_LOG_FUNCTION (this);
122  {
123  StringValue value;
124  (*i)->GetValue (value);
125  NS_LOG_LOGIC ("Saving " << (*i)->GetName ());
126  *m_os << "global " << (*i)->GetName () << " \"" << value.Get () << "\"" << std::endl;
127  }
128 }
129 void
131 {
132  NS_LOG_FUNCTION (this);
133  class RawTextAttributeIterator : public AttributeIterator
134  {
135 public:
136  RawTextAttributeIterator (std::ostream *os)
137  : m_os (os) {}
138  void
139  SetSaveDeprecated (bool saveDeprecated) {
140  m_saveDeprecated = saveDeprecated;
141  }
142 
143 private:
144  virtual void DoVisitAttribute (Ptr<Object> object, std::string name) {
145  StringValue str;
146 
147  ns3::TypeId::SupportLevel supportLevel = TypeId::SupportLevel::SUPPORTED;
148  TypeId tid = object->GetInstanceTypeId ();
149 
150  for (std::size_t i = 0; i < tid.GetAttributeN (); i++)
151  {
152  struct TypeId::AttributeInformation tmp = tid.GetAttribute (i);
153  if (tmp.name == name)
154  {
155  supportLevel = tmp.supportLevel;
156  break;
157  }
158  }
159  if (supportLevel == TypeId::SupportLevel::OBSOLETE)
160  {
161  NS_LOG_WARN ("Attribute " << GetCurrentPath ()
162  << " was not saved because it is OBSOLETE");
163  }
164  else if ((supportLevel == TypeId::SupportLevel::DEPRECATED) && (m_saveDeprecated == false))
165  {
166  NS_LOG_WARN ("Attribute " << GetCurrentPath ()
167  << " was not saved because it is DEPRECATED");
168  }
169  else
170  {
171  object->GetAttribute (name, str);
172  NS_LOG_DEBUG ("Saving " << GetCurrentPath ());
173  *m_os << "value " << GetCurrentPath () << " \"" << str.Get () << "\"" << std::endl;
174  }
175  }
176  std::ostream *m_os;
177  bool m_saveDeprecated;
178  };
179 
180  RawTextAttributeIterator iter = RawTextAttributeIterator (m_os);
181  iter.SetSaveDeprecated (m_saveDeprecated);
182  iter.Iterate ();
183 }
184 
186  : m_is (0)
187 {
188  NS_LOG_FUNCTION (this);
189 }
191 {
192  NS_LOG_FUNCTION (this);
193  if (m_is != 0)
194  {
195  m_is->close ();
196  delete m_is;
197  m_is = 0;
198  }
199 }
200 void
201 RawTextConfigLoad::SetFilename (std::string filename)
202 {
203  NS_LOG_FUNCTION (this << filename);
204  m_is = new std::ifstream ();
205  m_is->open (filename.c_str (), std::ios::in);
206 }
207 std::string
208 RawTextConfigLoad::Strip (std::string value)
209 {
210  NS_LOG_FUNCTION (this << value);
211  std::string::size_type start = value.find ("\"");
212  std::string::size_type end = value.find ("\"", 1);
213  NS_ABORT_MSG_IF (start != 0, "Ill-formed attribute value: " << value);
214  NS_ABORT_MSG_IF (end != value.size () - 1, "Ill-formed attribute value: " << value);
215  return value.substr (start+1, end-start-1);
216 }
217 
218 void
220 {
221  NS_LOG_FUNCTION (this);
222  m_is->clear ();
223  m_is->seekg (0);
224  std::string type, name, value;
225  for (std::string line; std::getline (*m_is, line);)
226  {
227  if (!ParseLine (line, type, name, value))
228  {
229  continue;
230  }
231 
232  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
233  value = Strip (value);
234  if (type == "default")
235  {
236  Config::SetDefault (name, StringValue (value));
237  }
238  name.clear ();
239  type.clear ();
240  value.clear ();
241  }
242 }
243 void
245 {
246  NS_LOG_FUNCTION (this);
247  m_is->clear ();
248  m_is->seekg (0);
249  std::string type, name, value;
250  for (std::string line; std::getline (*m_is, line);)
251  {
252  if (!ParseLine (line, type, name, value))
253  {
254  continue;
255  }
256 
257  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
258  value = Strip (value);
259  if (type == "global")
260  {
261  Config::SetGlobal (name, StringValue (value));
262  }
263  name.clear ();
264  type.clear ();
265  value.clear ();
266  }
267 }
268 void
270 {
271  NS_LOG_FUNCTION (this);
272  m_is->clear ();
273  m_is->seekg (0);
274  std::string type, name, value;
275  for (std::string line; std::getline (*m_is, line);)
276  {
277  if (!ParseLine (line, type, name, value))
278  {
279  continue;
280  }
281 
282  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
283  value = Strip (value);
284  if (type == "value")
285  {
286  Config::Set (name, StringValue (value));
287  }
288  name.clear ();
289  type.clear ();
290  value.clear ();
291  }
292 }
293 
294 bool
295 RawTextConfigLoad::ParseLine (const std::string &line, std::string &type, std::string &name, std::string &value)
296 {
297  NS_LOG_FUNCTION (this << line << type << name << value);
298 
299  // check for blank line
300  {
301  std::istringstream iss (line);
302  iss >> std::ws; // remove all blanks line
303  if (!iss.good ()) // eofbit set if no non-blanks
304  {
305  return false;
306  }
307  }
308 
309  if (line.front () == '#')
310  {
311  return false; // comment line
312  }
313 
314  // for multiline values, append line to value if type and name not empty
315  if (type.empty () && name.empty ())
316  {
317  std::istringstream iss (line);
318  iss >> type >> name >> std::ws;
319  std::getline (iss, value); // remaining line, includes embedded spaces
320  }
321  else
322  {
323  value.append (line);
324  }
325 
326  // two quotes in value signifies a completed (possibly multi-line)
327  // config-store entry, return True to signal load function to
328  // validate value (see Strip method) and set attribute
329  return std::count (value.begin (), value.end (), '"') == 2;
330 }
331 
332 
333 } // namespace ns3
ns3::TypeId
a unique identifier for an interface.
Definition: type-id.h:59
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::RawTextConfigLoad::RawTextConfigLoad
RawTextConfigLoad()
default constructor
Definition: raw-text-config.cc:185
ns3::Config::Set
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
ns3::RawTextConfigLoad::Global
virtual void Global(void)
Load or save the global values.
Definition: raw-text-config.cc:244
ns3::AttributeDefaultIterator
Iterator to iterate on the default values of attributes of an ns3::Object.
Definition: attribute-default-iterator.h:34
ns3::RawTextConfigSave::Global
virtual void Global(void)
Load or save the global values.
Definition: raw-text-config.cc:118
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::RawTextConfigSave::Attributes
virtual void Attributes(void)
Load or save the attributes values.
Definition: raw-text-config.cc:130
ns3::RawTextConfigSave::m_os
std::ofstream * m_os
Config store output stream.
Definition: raw-text-config.h:47
NS_LOG_WARN
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:265
ns3::TypeId::SupportLevel
SupportLevel
The level of support or deprecation for attributes or trace sources.
Definition: type-id.h:71
ns3::TypeId::AttributeInformation::name
std::string name
Attribute name.
Definition: type-id.h:80
raw-text-config.h
ns3::TypeId::GetAttributeN
std::size_t GetAttributeN(void) const
Get the number of attributes.
Definition: type-id.cc:1077
ns3::Ptr< Object >
visualizer.core.start
def start()
Definition: core.py:1855
attribute-iterator.h
ns3::TypeId::AttributeInformation
Attribute implementation.
Definition: type-id.h:78
ns3::RawTextConfigSave::SetFilename
virtual void SetFilename(std::string filename)
Set the file name.
Definition: raw-text-config.cc:54
ns3::RawTextConfigLoad::SetFilename
virtual void SetFilename(std::string filename)
Set the file name.
Definition: raw-text-config.cc:201
ns3::GlobalValue::Begin
static Iterator Begin(void)
The Begin iterator.
Definition: global-value.cc:187
ns3::RawTextConfigLoad::Attributes
virtual void Attributes(void)
Load or save the attributes values.
Definition: raw-text-config.cc:269
ns3::AttributeIterator
Iterator to iterate on the values of attributes of an ns3::Object.
Definition: attribute-iterator.h:36
ns3::GlobalValue::End
static Iterator End(void)
The End iterator.
Definition: global-value.cc:194
ns3::RawTextConfigSave::~RawTextConfigSave
virtual ~RawTextConfigSave()
destructor
Definition: raw-text-config.cc:43
ns3::StringValue::Get
std::string Get(void) const
Definition: string.cc:31
NS_ABORT_MSG_IF
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
ns3::RawTextConfigLoad::m_is
std::ifstream * m_is
Config store input stream.
Definition: raw-text-config.h:87
NS_LOG_LOGIC
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
ns3::StringValue
Hold variables of type string.
Definition: string.h:41
attribute-default-iterator.h
NS_LOG_DEBUG
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
ns3::RawTextConfigLoad::~RawTextConfigLoad
virtual ~RawTextConfigLoad()
destructor
Definition: raw-text-config.cc:190
ns3::RawTextConfigLoad::Default
virtual void Default(void)
Load or save the default values.
Definition: raw-text-config.cc:219
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::Config::SetGlobal
void SetGlobal(std::string name, const AttributeValue &value)
Definition: config.cc:891
ns3::RawTextConfigSave::RawTextConfigSave
RawTextConfigSave()
default constructor
Definition: raw-text-config.cc:38
ns3::FileConfig::SetSaveDeprecated
void SetSaveDeprecated(bool saveDeprecated)
Set if to save deprecated attributes.
Definition: file-config.cc:30
ns3::Config::SetDefault
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
ns3::RawTextConfigSave::Default
virtual void Default(void)
Load or save the default values.
Definition: raw-text-config.cc:61
ns3::GlobalValue::Iterator
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:80
ns3::RawTextConfigLoad::Strip
std::string Strip(std::string value)
Strip out attribute value.
Definition: raw-text-config.cc:208
ns3::TypeId::LookupByName
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:830
ns3::RawTextConfigLoad::ParseLine
virtual bool ParseLine(const std::string &line, std::string &type, std::string &name, std::string &value)
Parse (potentially multi-) line configs into type, name, and values.
Definition: raw-text-config.cc:295
ns3::TypeId::GetAttribute
struct TypeId::AttributeInformation GetAttribute(std::size_t i) const
Get Attribute information by index.
Definition: type-id.cc:1084
ns3::TypeId::AttributeInformation::supportLevel
TypeId::SupportLevel supportLevel
Support level/deprecation.
Definition: type-id.h:94
ns3::FileConfig::m_saveDeprecated
bool m_saveDeprecated
save deprecated attributes
Definition: file-config.h:61