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 }
38 {
39  if (m_os != 0)
40  {
41  m_os->close ();
42  }
43  delete m_os;
44  m_os = 0;
45 }
46 void
47 RawTextConfigSave::SetFilename (std::string filename)
48 {
49  m_os = new std::ofstream ();
50  m_os->open (filename.c_str (), std::ios::out);
51 }
52 void
54 {
55  class RawTextDefaultIterator : public AttributeDefaultIterator
56  {
57 public:
58  RawTextDefaultIterator (std::ostream *os) {
59  m_os = os;
60  }
61 private:
62  virtual void StartVisitTypeId (std::string name) {
63  m_typeId = name;
64  }
65  virtual void DoVisitAttribute (std::string name, std::string defaultValue) {
66  *m_os << "default " << m_typeId << "::" << name << " \"" << defaultValue << "\"" << std::endl;
67  }
68  std::string m_typeId;
69  std::ostream *m_os;
70  };
71 
72  RawTextDefaultIterator iterator = RawTextDefaultIterator (m_os);
73  iterator.Iterate ();
74 }
75 void
77 {
79  {
80  StringValue value;
81  (*i)->GetValue (value);
82  *m_os << "global " << (*i)->GetName () << " \"" << value.Get () << "\"" << std::endl;
83  }
84 }
85 void
87 {
88  class RawTextAttributeIterator : public AttributeIterator
89  {
90 public:
91  RawTextAttributeIterator (std::ostream *os)
92  : m_os (os) {}
93 private:
94  virtual void DoVisitAttribute (Ptr<Object> object, std::string name) {
95  StringValue str;
96  object->GetAttribute (name, str);
97  *m_os << "value " << GetCurrentPath () << " \"" << str.Get () << "\"" << std::endl;
98  }
99  std::ostream *m_os;
100  };
101 
102  RawTextAttributeIterator iter = RawTextAttributeIterator (m_os);
103  iter.Iterate ();
104 }
105 
107  : m_is (0)
108 {
109 }
111 {
112  if (m_is != 0)
113  {
114  m_is->close ();
115  delete m_is;
116  m_is = 0;
117  }
118 }
119 void
120 RawTextConfigLoad::SetFilename (std::string filename)
121 {
122  m_is = new std::ifstream ();
123  m_is->open (filename.c_str (), std::ios::in);
124 }
125 std::string
126 RawTextConfigLoad::Strip (std::string value)
127 {
128  std::string::size_type start = value.find ("\"");
129  std::string::size_type end = value.find ("\"", 1);
130  NS_ASSERT (start == 0);
131  NS_ASSERT (end == value.size () - 1);
132  return value.substr (start+1, end-start-1);
133 }
134 
135 void
137 {
138  m_is->clear ();
139  m_is->seekg (0);
140  std::string type, name, value;
141  *m_is >> type >> name >> value;
142  while (m_is->good ())
143  {
144  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
145  value = Strip (value);
146  if (type == "default")
147  {
148  Config::SetDefault (name, StringValue (value));
149  }
150  *m_is >> type >> name >> value;
151  }
152 }
153 void
155 {
156  m_is->clear ();
157  m_is->seekg (0);
158  std::string type, name, value;
159  *m_is >> type >> name >> value;
160  while (m_is->good ())
161  {
162  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
163  value = Strip (value);
164  if (type == "global")
165  {
166  Config::SetGlobal (name, StringValue (value));
167  }
168  *m_is >> type >> name >> value;
169  }
170 }
171 void
173 {
174  m_is->seekg (0);
175  std::string type, path, value;
176  *m_is >> type >> path >> value;
177  while (m_is->good ())
178  {
179  NS_LOG_DEBUG ("type=" << type << ", path=" << path << ", value=" << value);
180  value = Strip (value);
181  if (type == "value")
182  {
183  Config::Set (path, StringValue (value));
184  }
185  *m_is >> type >> path >> value;
186  }
187 }
188 
189 
190 } // namespace ns3
std::string Get(void) const
Definition: string.cc:31
virtual void Attributes(void)
Hold variables of type string.
Definition: string.h:41
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:769
virtual void SetFilename(std::string filename)
def start()
Definition: core.py:1482
#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:201
Vector::const_iterator Iterator
Iterator type for the list of all global values.
Definition: global-value.h:61
virtual void SetFilename(std::string filename)
static Iterator Begin(void)
The Begin iterator.
virtual void Global(void)
std::string Strip(std::string value)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
virtual void Attributes(void)
virtual void Default(void)
void SetGlobal(std::string name, const AttributeValue &value)
Definition: config.cc:814
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:236
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
This class is used internally by ConfigStore and GtkConfigStore.
std::ofstream * m_os
std::ifstream * m_is
virtual void Global(void)
virtual void Default(void)