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/config-store-config.h"
30 #ifdef HAVE_LIBXML2
31 #include "xml-config.h"
32 #endif
33 
34 #include <string>
35 #include <fstream>
36 #include <iostream>
37 #include <unistd.h>
38 #include <cstdlib>
39 
40 
41 namespace ns3 {
42 
43 NS_LOG_COMPONENT_DEFINE ("ConfigStore");
44 
45 NS_OBJECT_ENSURE_REGISTERED (ConfigStore);
46 
47 TypeId
49 {
50  static TypeId tid = TypeId ("ns3::ConfigStore")
52  .SetGroupName ("ConfigStore")
53  .AddAttribute ("Mode",
54  "Configuration mode",
58  ConfigStore::LOAD, "Load",
59  ConfigStore::SAVE, "Save"))
60  .AddAttribute ("Filename",
61  "The file where the configuration should be saved to or loaded from.",
62  StringValue (""),
65  .AddAttribute ("FileFormat",
66  "Type of file format",
70  ConfigStore::XML, "Xml"))
71  ;
72  return tid;
73 }
74 TypeId
76 {
77  return GetTypeId ();
78 }
79 
80 
82 {
83  NS_LOG_FUNCTION (this);
85 
86 #ifdef HAVE_LIBXML2
88  {
90  {
91  m_file = new XmlConfigSave ();
92  }
93  else if (m_mode == ConfigStore::LOAD)
94  {
95  m_file = new XmlConfigLoad ();
96  }
97  else
98  {
99  m_file = new NoneFileConfig ();
100  }
101  }
102 #else
104  {
106  {
107  NS_ABORT_MSG ("ConfigStore tried to read or write an XML file but XML is not supported.");
108  }
109  else
110  {
111  m_file = new NoneFileConfig ();
112  }
113  }
114 #endif /* HAVE_LIBXML2 */
115 
117  {
118  if (m_mode == ConfigStore::SAVE)
119  {
120  m_file = new RawTextConfigSave ();
121  }
122  else if (m_mode == ConfigStore::LOAD)
123  {
124  m_file = new RawTextConfigLoad ();
125  }
126  else
127  {
128  m_file = new NoneFileConfig ();
129  }
130  }
132  NS_LOG_FUNCTION (this << ": format: " << m_fileFormat
133  << ", mode: " << m_mode
134  << ", file name: " << m_filename);
135 }
136 
138 {
139  NS_LOG_FUNCTION (this);
140  delete m_file;
141  m_file = 0;
142 }
143 
144 void
146 {
147  NS_LOG_FUNCTION (this << mode);
148  m_mode = mode;
149 }
150 void
152 {
153  NS_LOG_FUNCTION (this << format);
154  m_fileFormat = format;
155 }
156 void
157 ConfigStore::SetFilename (std::string filename)
158 {
159  NS_LOG_FUNCTION (this << filename);
160  m_filename = filename;
161 }
162 
163 void
165 {
166  NS_LOG_FUNCTION (this);
167  m_file->Attributes ();
168 }
169 
170 void
172 {
173  NS_LOG_FUNCTION (this);
174  m_file->Default ();
175  m_file->Global ();
176 }
177 
178 std::ostream &
179 operator << (std::ostream & os, ConfigStore::Mode & mode)
180 {
181  switch (mode)
182  {
183  case ConfigStore::LOAD: os << "LOAD"; break;
184  case ConfigStore::SAVE: os << "SAVE"; break;
185  case ConfigStore::NONE: os << "NONE"; break;
186  default: os << "UNKNOWN";
187  }
188  return os;
189 }
190 
191 std::ostream &
192 operator << (std::ostream & os, ConfigStore::FileFormat & format)
193 {
194  switch (format)
195  {
196  case ConfigStore::XML: os << "XML"; break;
197  case ConfigStore::RAW_TEXT: os << "RAW_TEXT"; break;
198  }
199  return os;
200 }
201 
202 } // namespace ns3
FileConfig * m_file
Definition: config-store.h:83
Ptr< const AttributeChecker > MakeStringChecker(void)
Definition: string.cc:30
virtual void Attributes(void)=0
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:50
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:44
Hold variables of type string.
Definition: string.h:41
virtual TypeId GetInstanceTypeId(void) const
Get the most derived TypeId for this Object.
Definition: config-store.cc:75
void SetFilename(std::string filename)
static TypeId GetTypeId(void)
Definition: config-store.cc:48
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:209
virtual void Default(void)=0
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
Anchor the ns-3 type and attribute system.
Definition: object-base.h:68
Hold variables of type enum.
Definition: enum.h:54
List of Attribute name, value and checker triples used to construct Objects.
enum Mode m_mode
Definition: config-store.h:80
void ConfigureDefaults(void)
std::ostream & operator<<(std::ostream &os, const Angles &a)
print a struct Angles to output
Definition: angles.cc:42
void SetFileFormat(enum FileFormat format)
void SetMode(enum Mode mode)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void ConstructSelf(const AttributeConstructionList &attributes)
Complete construction of ObjectBase; invoked by derived classes.
Definition: object-base.cc:80
enum FileFormat m_fileFormat
Definition: config-store.h:81
virtual void SetFilename(std::string filename)=0
virtual void Global(void)=0
Ptr< const AttributeChecker > MakeEnumChecker(int v1, std::string n1, int v2, std::string n2, int v3, std::string n3, int v4, std::string n4, int v5, std::string n5, int v6, std::string n6, int v7, std::string n7, int v8, std::string n8, int v9, std::string n9, int v10, std::string n10, int v11, std::string n11, int v12, std::string n12, int v13, std::string n13, int v14, std::string n14, int v15, std::string n15, int v16, std::string n16, int v17, std::string n17, int v18, std::string n18, int v19, std::string n19, int v20, std::string n20, int v21, std::string n21, int v22, std::string n22)
Make an EnumChecker pre-configured with a set of allowed values by name.
Definition: enum.cc:184
void ConfigureAttributes(void)
std::string m_filename
Definition: config-store.h:82
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
a unique identifier for an interface.
Definition: type-id.h:58
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:904