00001 #include "config-store.h"
00002 #include "attribute-iterator.h"
00003 #include "ns3/string.h"
00004 #include "ns3/log.h"
00005 #include "ns3/attribute-list.h"
00006 #include "ns3/config.h"
00007 #include <string>
00008 #include <fstream>
00009 #include <iostream>
00010 #include <unistd.h>
00011 #include <stdlib.h>
00012
00013 NS_LOG_COMPONENT_DEFINE ("ConfigStore");
00014
00015 namespace ns3 {
00016
00017 NS_OBJECT_ENSURE_REGISTERED (ConfigStore);
00018
00019 TypeId
00020 ConfigStore::GetTypeId (void)
00021 {
00022 static TypeId tid = TypeId ("ns3::ConfigStore")
00023 .SetParent<ObjectBase> ()
00024 .AddAttribute ("LoadFilename",
00025 "The file where the configuration should be loaded from.",
00026 StringValue (""),
00027 MakeStringAccessor (&ConfigStore::m_loadFilename),
00028 MakeStringChecker ())
00029 .AddAttribute ("StoreFilename",
00030 "The file where the configuration should be stored to.",
00031 StringValue (""),
00032 MakeStringAccessor (&ConfigStore::m_storeFilename),
00033 MakeStringChecker ())
00034 ;
00035 return tid;
00036 }
00037 TypeId
00038 ConfigStore::GetInstanceTypeId (void) const
00039 {
00040 return GetTypeId ();
00041 }
00042
00043
00044 ConfigStore::ConfigStore ()
00045 {
00046 ObjectBase::ConstructSelf (AttributeList ());
00047 }
00048
00049 void
00050 ConfigStore::LoadFrom (std::string filename)
00051 {
00052 std::ifstream is;
00053 is.open (filename.c_str (), std::ios::in);
00054 std::string path, value;
00055 while (is.good())
00056 {
00057 is >> path >> value;
00058 NS_LOG_DEBUG (path << " " << value);
00059 Config::Set (path, StringValue (value));
00060 }
00061 }
00062 void
00063 ConfigStore::StoreTo (std::string filename)
00064 {
00065
00066 std::ofstream os;
00067 os.open (filename.c_str (), std::ios::out);
00068 TextFileAttributeIterator iter = TextFileAttributeIterator (os);
00069 iter.Save ();
00070 os.close ();
00071 exit (0);
00072 }
00073
00074 void
00075 ConfigStore::Configure (void)
00076 {
00077 if (m_loadFilename != "")
00078 {
00079 LoadFrom (m_loadFilename);
00080 }
00081 if (m_storeFilename != "")
00082 {
00083 StoreTo (m_storeFilename);
00084 }
00085 }
00086
00087 }