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->seekg (0);
119  std::string type, name, value;
120  *m_is >> type >> name >> value;
121  while (m_is->good ())
122  {
123  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
124  value = Strip (value);
125  if (type == "default")
126  {
127  Config::SetDefault (name, StringValue (value));
128  }
129  *m_is >> type >> name >> value;
130  }
131 }
132 void
134 {
135  m_is->seekg (0);
136  std::string type, name, value;
137  *m_is >> type >> name >> value;
138  while (m_is->good ())
139  {
140  NS_LOG_DEBUG ("type=" << type << ", name=" << name << ", value=" << value);
141  value = Strip (value);
142  if (type == "global")
143  {
144  Config::SetGlobal (name, StringValue (value));
145  }
146  *m_is >> type >> name >> value;
147  }
148 }
149 void
151 {
152  m_is->seekg (0);
153  std::string type, path, value;
154  *m_is >> type >> path >> value;
155  while (m_is->good ())
156  {
157  NS_LOG_DEBUG ("type=" << type << ", path=" << path << ", value=" << value);
158  value = Strip (value);
159  if (type == "value")
160  {
161  Config::Set (path, StringValue (value));
162  }
163  *m_is >> type >> path >> value;
164  }
165 }
166 
167 
168 } // namespace ns3