A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
config-store.cc
1 #include "config-store.h"
2 #include "raw-text-config.h"
3 #include "ns3/abort.h"
4 #include "ns3/string.h"
5 #include "ns3/log.h"
6 #include "ns3/simulator.h"
7 #include "ns3/attribute-construction-list.h"
8 #include "ns3/enum.h"
9 #include "ns3/config-store-config.h"
10 #ifdef HAVE_LIBXML2
11 #include "xml-config.h"
12 #endif
13 
14 #include <string>
15 #include <fstream>
16 #include <iostream>
17 #include <unistd.h>
18 #include <cstdlib>
19 
20 
21 NS_LOG_COMPONENT_DEFINE ("ConfigStore");
22 
23 namespace ns3 {
24 
25 
26 NS_OBJECT_ENSURE_REGISTERED (ConfigStore);
27 
28 TypeId
29 ConfigStore::GetTypeId (void)
30 {
31  static TypeId tid = TypeId ("ns3::ConfigStore")
32  .SetParent<ObjectBase> ()
33  .AddAttribute ("Mode",
34  "Configuration mode",
35  EnumValue (ConfigStore::NONE),
36  MakeEnumAccessor (&ConfigStore::SetMode),
37  MakeEnumChecker (ConfigStore::NONE, "None",
38  ConfigStore::LOAD, "Load",
39  ConfigStore::SAVE, "Save"))
40  .AddAttribute ("Filename",
41  "The file where the configuration should be saved to or loaded from.",
42  StringValue (""),
43  MakeStringAccessor (&ConfigStore::SetFilename),
44  MakeStringChecker ())
45  .AddAttribute ("FileFormat",
46  "Type of file format",
47  EnumValue (ConfigStore::RAW_TEXT),
48  MakeEnumAccessor (&ConfigStore::SetFileFormat),
49  MakeEnumChecker (ConfigStore::RAW_TEXT, "RawText",
50  ConfigStore::XML, "Xml"))
51  ;
52  return tid;
53 }
54 TypeId
56 {
57  return GetTypeId ();
58 }
59 
60 
61 ConfigStore::ConfigStore ()
62 {
64 
65 #ifdef HAVE_LIBXML2
66  if (m_fileFormat == ConfigStore::XML)
67  {
68  if (m_mode == ConfigStore::SAVE)
69  {
70  m_file = new XmlConfigSave ();
71  }
72  else if (m_mode == ConfigStore::LOAD)
73  {
74  m_file = new XmlConfigLoad ();
75  }
76  else
77  {
78  m_file = new NoneFileConfig ();
79  }
80  }
81 #else
82  if (m_fileFormat == ConfigStore::XML)
83  {
84  if (m_mode == ConfigStore::SAVE || m_mode == ConfigStore::LOAD)
85  {
86  NS_ABORT_MSG ("ConfigStore tried to read or write an XML file but XML is not supported.");
87  }
88  else
89  {
90  m_file = new NoneFileConfig ();
91  }
92  }
93 #endif /* HAVE_LIBXML2 */
94 
95  if (m_fileFormat == ConfigStore::RAW_TEXT)
96  {
97  if (m_mode == ConfigStore::SAVE)
98  {
99  m_file = new RawTextConfigSave ();
100  }
101  else if (m_mode == ConfigStore::LOAD)
102  {
103  m_file = new RawTextConfigLoad ();
104  }
105  else
106  {
107  m_file = new NoneFileConfig ();
108  }
109  }
110  m_file->SetFilename (m_filename);
111 }
112 
113 ConfigStore::~ConfigStore ()
114 {
115  delete m_file;
116  m_file = 0;
117 }
118 
119 void
120 ConfigStore::SetMode (enum Mode mode)
121 {
122  m_mode = mode;
123 }
124 void
125 ConfigStore::SetFileFormat (enum FileFormat format)
126 {
127  m_fileFormat = format;
128 }
129 void
130 ConfigStore::SetFilename (std::string filename)
131 {
132  m_filename = filename;
133 }
134 
135 void
136 ConfigStore::ConfigureAttributes (void)
137 {
138  m_file->Attributes ();
139 }
140 
141 void
142 ConfigStore::ConfigureDefaults (void)
143 {
144  m_file->Default ();
145  m_file->Global ();
146 }
147 
148 } // namespace ns3
virtual TypeId GetInstanceTypeId(void) const
Definition: config-store.cc:55
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void ConstructSelf(const AttributeConstructionList &attributes)
Definition: object-base.cc:65
#define NS_ABORT_MSG(msg)
Abnormal program termination.
Definition: abort.h:43