A Discrete-Event Network Simulator
API
config-store.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 "config-store.h"
22 #include "raw-text-config.h"
23 #include "ns3/abort.h"
24 #include "ns3/string.h"
25 #include "ns3/log.h"
26 #include "ns3/simulator.h"
27 #include "ns3/attribute-construction-list.h"
28 #include "ns3/enum.h"
29 #include "ns3/boolean.h"
30 #include "ns3/config-store-config.h"
31 #ifdef HAVE_LIBXML2
32 #include "xml-config.h"
33 #endif
34 
35 #include <string>
36 #include <fstream>
37 #include <iostream>
38 #include <unistd.h>
39 #include <cstdlib>
40 
41 
42 namespace ns3 {
43 
44 NS_LOG_COMPONENT_DEFINE ("ConfigStore");
45 
46 NS_OBJECT_ENSURE_REGISTERED (ConfigStore);
47 
48 TypeId
50 {
51  static TypeId tid = TypeId ("ns3::ConfigStore")
53  .SetGroupName ("ConfigStore")
54  .AddAttribute ("Mode",
55  "Configuration mode",
59  ConfigStore::LOAD, "Load",
60  ConfigStore::SAVE, "Save"))
61  .AddAttribute ("Filename",
62  "The file where the configuration should be saved to or loaded from.",
63  StringValue (""),
66  .AddAttribute ("FileFormat",
67  "Type of file format",
71  ConfigStore::XML, "Xml"))
72  .AddAttribute ("SaveDeprecated",
73  "Save DEPRECATED attributes",
74  BooleanValue (true),
77  ;
78  return tid;
79 }
80 TypeId
82 {
83  return GetTypeId ();
84 }
85 
86 
88 {
89  NS_LOG_FUNCTION (this);
91 
92 #ifdef HAVE_LIBXML2
94  {
96  {
97  m_file = new XmlConfigSave ();
98  }
99  else if (m_mode == ConfigStore::LOAD)
100  {
101  m_file = new XmlConfigLoad ();
102  }
103  else
104  {
105  m_file = new NoneFileConfig ();
106  }
107  }
108 #else
110  {
112  {
113  NS_ABORT_MSG ("ConfigStore tried to read or write an XML file but XML is not supported.");
114  }
115  else
116  {
117  m_file = new NoneFileConfig ();
118  }
119  }
120 #endif /* HAVE_LIBXML2 */
121 
123  {
124  if (m_mode == ConfigStore::SAVE)
125  {
126  m_file = new RawTextConfigSave ();
127  }
128  else if (m_mode == ConfigStore::LOAD)
129  {
130  m_file = new RawTextConfigLoad ();
131  }
132  else
133  {
134  m_file = new NoneFileConfig ();
135  }
136  }
139 
140  NS_LOG_FUNCTION (this << ": format: " << m_fileFormat
141  << ", mode: " << m_mode
142  << ", file name: " << m_filename);
143 }
144 
146 {
147  NS_LOG_FUNCTION (this);
148  delete m_file;
149  m_file = 0;
150 }
151 
152 void
154 {
155  NS_LOG_FUNCTION (this << mode);
156  m_mode = mode;
157 }
158 void
160 {
161  NS_LOG_FUNCTION (this << format);
162  m_fileFormat = format;
163 }
164 void
165 ConfigStore::SetFilename (std::string filename)
166 {
167  NS_LOG_FUNCTION (this << filename);
168  m_filename = filename;
169 }
170 void
171 ConfigStore::SetSaveDeprecated (bool saveDeprecated)
172 {
173  NS_LOG_FUNCTION (this << saveDeprecated);
174  m_saveDeprecated = saveDeprecated;
175 }
176 
177 void
179 {
180  NS_LOG_FUNCTION (this);
181  m_file->Attributes ();
182 }
183 
184 void
186 {
187  NS_LOG_FUNCTION (this);
188  m_file->Default ();
189  m_file->Global ();
190 }
191 
192 std::ostream &
193 operator << (std::ostream & os, ConfigStore::Mode & mode)
194 {
195  switch (mode)
196  {
197  case ConfigStore::LOAD: os << "LOAD"; break;
198  case ConfigStore::SAVE: os << "SAVE"; break;
199  case ConfigStore::NONE: os << "NONE"; break;
200  default: os << "UNKNOWN";
201  }
202  return os;
203 }
204 
205 std::ostream &
206 operator << (std::ostream & os, ConfigStore::FileFormat & format)
207 {
208  switch (format)
209  {
210  case ConfigStore::XML: os << "XML"; break;
211  case ConfigStore::RAW_TEXT: os << "RAW_TEXT"; break;
212  }
213  return os;
214 }
215 
216 } // namespace ns3
List of Attribute name, value and checker triples used to construct Objects.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
void ConfigureDefaults(void)
Configure the default values.
std::string m_filename
store file name
Definition: config-store.h:125
void SetFilename(std::string filename)
Set the filename.
enum Mode m_mode
store mode
Definition: config-store.h:122
static TypeId GetTypeId(void)
Get the type ID.
Definition: config-store.cc:49
FileConfig * m_file
configuration file
Definition: config-store.h:126
Mode
for ConfigStore operation
Definition: config-store.h:66
void ConfigureAttributes(void)
Configure the attribute values.
enum FileFormat m_fileFormat
store format
Definition: config-store.h:123
void SetSaveDeprecated(bool saveDeprecated)
Set if to save deprecated attributes.
void SetFileFormat(enum FileFormat format)
Set the file format.
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: config-store.cc:81
FileFormat
store format
Definition: config-store.h:76
bool m_saveDeprecated
save deprecated attributes
Definition: config-store.h:124
void SetMode(enum Mode mode)
Set the mode of operation.
Hold variables of type enum.
Definition: enum.h:55
void SetSaveDeprecated(bool saveDeprecated)
Set if to save deprecated attributes.
Definition: file-config.cc:30
virtual void Attributes(void)=0
Load or save the attributes values.
virtual void SetFilename(std::string filename)=0
Set the file name.
virtual void Global(void)=0
Load or save the global values.
virtual void Default(void)=0
Load or save the default values.
A dummy class (does nothing)
Definition: file-config.h:69
Anchor the ns-3 type and attribute system.
Definition: object-base.h:120
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
Definition: object-base.cc:159
A class to enable loading of configuration store from a raw text file.
A class to enable saving of configuration store in a raw text file.
Hold variables of type string.
Definition: string.h:41
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:922
A class to enable loading of configuration store from an XML file.
Definition: xml-config.h:55
A class to enable saving of configuration store in an XML file.
Definition: xml-config.h:37
Ptr< const AttributeChecker > MakeBooleanChecker(void)
Definition: boolean.cc:121
Ptr< const AttributeAccessor > MakeBooleanAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: boolean.h:85
Ptr< const AttributeAccessor > MakeEnumAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: enum.h:205
Ptr< const AttributeAccessor > MakeStringAccessor(T1 a1)
Create an AttributeAccessor for a class data member, or a lone class get functor or set method.
Definition: string.h:42
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:45
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Ptr< const AttributeChecker > MakeEnumChecker(int v, std::string n, Ts... args)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.h:162
std::ostream & operator<<(std::ostream &os, const Angles &a)
Definition: angles.cc:139