A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
raw-text-config.cc
Go to the documentation of this file.
1 #include "raw-text-config.h"
2 #include "attribute-iterator.h"
4 #include "ns3/global-value.h"
5 #include "ns3/string.h"
6 #include "ns3/log.h"
7 #include "ns3/config.h"
8 
9 NS_LOG_COMPONENT_DEFINE ("RawTextConfig");
10 
11 namespace ns3 {
12 
14  : m_os (0)
15 {
16 }
18 {
19  if (m_os != 0)
20  {
21  m_os->close ();
22  }
23  delete m_os;
24  m_os = 0;
25 }
26 void
27 RawTextConfigSave::SetFilename (std::string filename)
28 {
29  m_os = new std::ofstream ();
30  m_os->open (filename.c_str (), std::ios::out);
31 }
32 void
34 {
35  class RawTextDefaultIterator : public AttributeDefaultIterator
36  {
37 public:
38  RawTextDefaultIterator (std::ostream *os) {
39  m_os = os;
40  }
41 private:
42  virtual void StartVisitTypeId (std::string name) {
43  m_typeId = name;
44  }
45  virtual void DoVisitAttribute (std::string name, std::string defaultValue) {
46  *m_os << "default " << m_typeId << "::" << name << " \"" << defaultValue << "\"" << std::endl;
47  }
48  std::string m_typeId;
49  std::ostream *m_os;
50  };
51 
52  RawTextDefaultIterator iterator = RawTextDefaultIterator (m_os);
53  iterator.Iterate ();
54 }
55 void
57 {
59  {
60  StringValue value;
61  (*i)->GetValue (value);
62  *m_os << "global " << (*i)->GetName () << " \"" << value.Get () << "\"" << std::endl;
63  }
64 }
65 void
67 {
68  class RawTextAttributeIterator : public AttributeIterator
69  {
70 public:
71  RawTextAttributeIterator (std::ostream *os)
72  : m_os (os) {}
73 private:
74  virtual void DoVisitAttribute (Ptr<Object> object, std::string name) {
75  StringValue str;
76  object->GetAttribute (name, str);
77  *m_os << "value " << GetCurrentPath () << " \"" << str.Get () << "\"" << std::endl;
78  }
79  std::ostream *m_os;
80  };
81 
82  RawTextAttributeIterator iter = RawTextAttributeIterator (m_os);
83  iter.Iterate ();
84 }
85 
87  : m_is (0)
88 {
89 }
91 {
92  if (m_is != 0)
93  {
94  m_is->close ();
95  delete m_is;
96  m_is = 0;
97  }
98 }
99 void
100 RawTextConfigLoad::SetFilename (std::string filename)
101 {
102  m_is = new std::ifstream ();
103  m_is->open (filename.c_str (), std::ios::in);
104 }
105 std::string
106 RawTextConfigLoad::Strip (std::string value)
107 {
108  std::string::size_type start = value.find ("\"");
109  std::string::size_type end = value.find ("\"", 1);
110  NS_ASSERT (start == 0);
111  NS_ASSERT (end == value.size () - 1);
112  return value.substr (start+1, end-start-1);
113 }
114 
115 void
117 {
118  m_is->clear ();
119  m_is->seekg (0);
120  std::string type, name, value;
121  *m_is >> type >> name >> value;
122  while (m_is->good ())
123  {
124  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
125  value = Strip (value);
126  if (type == "default")
127  {
128  Config::SetDefault (name, StringValue (value));
129  }
130  *m_is >> type >> name >> value;
131  }
132 }
133 void
135 {
136  m_is->clear ();
137  m_is->seekg (0);
138  std::string type, name, value;
139  *m_is >> type >> name >> value;
140  while (m_is->good ())
141  {
142  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
143  value = Strip (value);
144  if (type == "global")
145  {
146  Config::SetGlobal (name, StringValue (value));
147  }
148  *m_is >> type >> name >> value;
149  }
150 }
151 void
153 {
154  m_is->seekg (0);
155  std::string type, path, value;
156  *m_is >> type >> path >> value;
157  while (m_is->good ())
158  {
159  NS_LOG_DEBUG ("type=" << type << ", path=" << path << ", value=" << value);
160  value = Strip (value);
161  if (type == "value")
162  {
163  Config::Set (path, StringValue (value));
164  }
165  *m_is >> type >> path >> value;
166  }
167 }
168 
169 
170 } // namespace ns3
std::string Get(void) const
virtual void Attributes(void)
hold variables of type string
Definition: string.h:19
virtual void SetFilename(std::string filename)
#define NS_ASSERT(condition)
Definition: assert.h:64
Vector::const_iterator Iterator
Definition: global-value.h:51
NS_LOG_COMPONENT_DEFINE("RawTextConfig")
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:662
virtual void SetFilename(std::string filename)
static Iterator Begin(void)
virtual void Global(void)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
std::string Strip(std::string value)
virtual void Attributes(void)
virtual void Default(void)
static Iterator End(void)
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
This class is used internally by ConfigStore and GtkConfigStore.
std::ofstream * m_os
std::ifstream * m_is
virtual void Global(void)
void SetGlobal(std::string name, const AttributeValue &value)
Definition: config.cc:707
virtual void Default(void)