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 private:
71  virtual void StartVisitTypeId (std::string name) {
72  m_typeId = name;
73  }
74  virtual void DoVisitAttribute (std::string name, std::string defaultValue) {
75  NS_LOG_DEBUG ("Saving " << m_typeId << "::" << name);
76  *m_os << "default " << m_typeId << "::" << name << " \"" << defaultValue << "\"" << std::endl;
77  }
78  std::string m_typeId;
79  std::ostream *m_os;
80  };
81 
82  RawTextDefaultIterator iterator = RawTextDefaultIterator (m_os);
83  iterator.Iterate ();
84 }
85 void
87 {
88  NS_LOG_FUNCTION (this);
90  {
91  StringValue value;
92  (*i)->GetValue (value);
93  NS_LOG_LOGIC ("Saving " << (*i)->GetName ());
94  *m_os << "global " << (*i)->GetName () << " \"" << value.Get () << "\"" << std::endl;
95  }
96 }
97 void
99 {
100  NS_LOG_FUNCTION (this);
101  class RawTextAttributeIterator : public AttributeIterator
102  {
103 public:
104  RawTextAttributeIterator (std::ostream *os)
105  : m_os (os) {}
106 private:
107  virtual void DoVisitAttribute (Ptr<Object> object, std::string name) {
108  StringValue str;
109  object->GetAttribute (name, str);
110  NS_LOG_DEBUG ("Saving " << GetCurrentPath ());
111  *m_os << "value " << GetCurrentPath () << " \"" << str.Get () << "\"" << std::endl;
112  }
113  std::ostream *m_os;
114  };
115 
116  RawTextAttributeIterator iter = RawTextAttributeIterator (m_os);
117  iter.Iterate ();
118 }
119 
121  : m_is (0)
122 {
123  NS_LOG_FUNCTION (this);
124 }
126 {
127  NS_LOG_FUNCTION (this);
128  if (m_is != 0)
129  {
130  m_is->close ();
131  delete m_is;
132  m_is = 0;
133  }
134 }
135 void
136 RawTextConfigLoad::SetFilename (std::string filename)
137 {
138  NS_LOG_FUNCTION (this << filename);
139  m_is = new std::ifstream ();
140  m_is->open (filename.c_str (), std::ios::in);
141 }
142 std::string
143 RawTextConfigLoad::Strip (std::string value)
144 {
145  NS_LOG_FUNCTION (this << value);
146  std::string::size_type start = value.find ("\"");
147  std::string::size_type end = value.find ("\"", 1);
148  NS_ABORT_MSG_IF (start != 0, "Ill-formed attribute value: " << value);
149  NS_ABORT_MSG_IF (end != value.size () - 1, "Ill-formed attribute value: " << value);
150  return value.substr (start+1, end-start-1);
151 }
152 
153 void
155 {
156  NS_LOG_FUNCTION (this);
157  m_is->clear ();
158  m_is->seekg (0);
159  std::string type, name, value;
160  for (std::string line; std::getline (*m_is, line);)
161  {
162  if (!ParseLine (line, type, name, value))
163  {
164  continue;
165  }
166 
167  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
168  value = Strip (value);
169  if (type == "default")
170  {
171  Config::SetDefault (name, StringValue (value));
172  }
173  name.clear ();
174  type.clear ();
175  value.clear ();
176  }
177 }
178 void
180 {
181  NS_LOG_FUNCTION (this);
182  m_is->clear ();
183  m_is->seekg (0);
184  std::string type, name, value;
185  for (std::string line; std::getline (*m_is, line);)
186  {
187  if (!ParseLine (line, type, name, value))
188  {
189  continue;
190  }
191 
192  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
193  value = Strip (value);
194  if (type == "global")
195  {
196  Config::SetGlobal (name, StringValue (value));
197  }
198  name.clear ();
199  type.clear ();
200  value.clear ();
201  }
202 }
203 void
205 {
206  NS_LOG_FUNCTION (this);
207  m_is->clear ();
208  m_is->seekg (0);
209  std::string type, name, value;
210  for (std::string line; std::getline (*m_is, line);)
211  {
212  if (!ParseLine (line, type, name, value))
213  {
214  continue;
215  }
216 
217  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
218  value = Strip (value);
219  if (type == "value")
220  {
221  Config::Set (name, StringValue (value));
222  }
223  name.clear ();
224  type.clear ();
225  value.clear ();
226  }
227 }
228 
229 bool
230 RawTextConfigLoad::ParseLine (const std::string &line, std::string &type, std::string &name, std::string &value)
231 {
232  NS_LOG_FUNCTION (this << line << type << name << value);
233 
234  // check for blank line
235  {
236  std::istringstream iss (line);
237  iss >> std::ws; // remove all blanks line
238  if (!iss.good ()) // eofbit set if no non-blanks
239  {
240  return false;
241  }
242  }
243 
244  if (line.front () == '#')
245  {
246  return false; // comment line
247  }
248 
249  // for multiline values, append line to value if type and name not empty
250  if (type.empty () && name.empty ())
251  {
252  std::istringstream iss (line);
253  iss >> type >> name >> std::ws;
254  std::getline (iss, value); // remaining line, includes embedded spaces
255  }
256  else
257  {
258  value.append (line);
259  }
260 
261  // two quotes in value signifies a completed (possibly multi-line)
262  // config-store entry, return True to signal load function to
263  // validate value (see Strip method) and set attribute
264  return std::count (value.begin (), value.end (), '"') == 2;
265 }
266 
267 
268 } // 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:839
virtual void SetFilename(std::string filename)
Set the file name.
def start()
Definition: core.py:1855
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
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
virtual ~RawTextConfigLoad()
destructor
virtual ~RawTextConfigSave()
destructor
static Iterator Begin(void)
The Begin iterator.
RawTextConfigSave()
default constructor
#define NS_LOG_LOGIC(msg)
Use NS_LOG to output a message of level LOG_LOGIC.
Definition: log.h:289
virtual void Global(void)
Load or save the global values.
RawTextConfigLoad()
default constructor
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.
#define NS_ABORT_MSG_IF(cond, msg)
Abnormal program termination if a condition is true, with a message.
Definition: abort.h:108
void SetGlobal(std::string name, const AttributeValue &value)
Definition: config.cc:889
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:273
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
Iterator to iterate on the values of attributes of an ns3::Object.
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.
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.