A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
raw-text-config.cc
1 #include "raw-text-config.h"
2 #include "attribute-iterator.h"
3 #include "attribute-default-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 
13 RawTextConfigSave::RawTextConfigSave ()
14  : m_os (0)
15 {
16 }
17 RawTextConfigSave::~RawTextConfigSave ()
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
33 RawTextConfigSave::Default (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
56 RawTextConfigSave::Global (void)
57 {
58  for (GlobalValue::Iterator i = GlobalValue::Begin (); i != GlobalValue::End (); ++i)
59  {
60  StringValue value;
61  (*i)->GetValue (value);
62  *m_os << "global " << (*i)->GetName () << " \"" << value.Get () << "\"" << std::endl;
63  }
64 }
65 void
66 RawTextConfigSave::Attributes (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 
86 RawTextConfigLoad::RawTextConfigLoad ()
87  : m_is (0)
88 {
89 }
90 RawTextConfigLoad::~RawTextConfigLoad ()
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
116 RawTextConfigLoad::Default (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
134 RawTextConfigLoad::Global (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
152 RawTextConfigLoad::Attributes (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
#define NS_ASSERT(condition)
Definition: assert.h:64
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:662
static Iterator Begin(void)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
static Iterator End(void)
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
void SetGlobal(std::string name, const AttributeValue &value)
Definition: config.cc:707