util, transport: add configuration file support and make default unix socket configurable
add sample library configuration file
refs: #1364
Change-Id: I3cb36d078aa3f0b0a50d9a83a521e95448df0a93
diff --git a/src/util/config-file.cpp b/src/util/config-file.cpp
new file mode 100644
index 0000000..031df25
--- /dev/null
+++ b/src/util/config-file.cpp
@@ -0,0 +1,123 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+
+#include "config-file.hpp"
+
+#include <boost/property_tree/ini_parser.hpp>
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+
+ConfigFile::ConfigFile()
+ : m_path(findConfigFile())
+{
+ if (open())
+ {
+ parse();
+ close();
+ }
+}
+
+ConfigFile::~ConfigFile()
+{
+ if (m_input.is_open())
+ {
+ m_input.close();
+ }
+}
+
+boost::filesystem::path
+ConfigFile::findConfigFile()
+{
+ using namespace boost::filesystem;
+
+ path home(std::getenv("HOME"));
+ if (!home.empty())
+ {
+ home /= ".ndn/client.conf";
+ if (exists(home))
+ {
+ return absolute(home);
+ }
+ }
+
+#ifdef NDN_CPP_SYSCONFDIR
+ path sysconfdir(NDN_CPP_SYSCONFDIR);
+ sysconfdir /= "ndn/client.conf";
+
+ if (exists(sysconfdir))
+ {
+ return absolute(sysconfdir);
+ }
+#endif // NDN_CPP_SYSCONFDIR
+
+ path etc("/etc/ndn/client.conf");
+ if (exists(etc))
+ {
+ return absolute(etc);
+ }
+
+ return path();
+}
+
+
+
+bool
+ConfigFile::open()
+{
+ if (m_path.empty())
+ {
+ return false;
+ }
+
+ m_input.open(m_path.c_str());
+ if (!m_input.good() || !m_input.is_open())
+ {
+ return false;
+ }
+ return true;
+}
+
+void
+ConfigFile::close()
+{
+ if (m_input.is_open())
+ {
+ m_input.close();
+ }
+}
+
+
+const ConfigFile::Parsed&
+ConfigFile::parse()
+{
+ if (m_path.empty())
+ {
+ throw Error("Failed to locate configuration file for parsing");
+ }
+ else if (!m_input.is_open() && !open())
+ {
+ throw Error("Failed to open configuration file for parsing");
+ }
+
+ try
+ {
+ boost::property_tree::read_ini(m_input, m_config);
+ }
+ catch (const boost::property_tree::ini_parser_error& error)
+ {
+ std::stringstream msg;
+ msg << "Failed to parse configuration file";
+ msg << " " << m_path;
+ msg << " " << error.message() << " line " << error.line();
+ throw Error(msg.str());
+ }
+ return m_config;
+}
+
+}
+
diff --git a/src/util/config-file.hpp b/src/util/config-file.hpp
new file mode 100644
index 0000000..cecceba
--- /dev/null
+++ b/src/util/config-file.hpp
@@ -0,0 +1,103 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_MANAGEMENT_CONFIG_FILE_HPP
+#define NDN_MANAGEMENT_CONFIG_FILE_HPP
+
+#include "../common.hpp"
+
+#include <boost/property_tree/ptree.hpp>
+#include <boost/filesystem.hpp>
+
+namespace ndn {
+
+class ConfigFile : noncopyable
+{
+public:
+
+ class Error : public std::runtime_error
+ {
+ public:
+ Error(const std::string& what)
+ : std::runtime_error(what)
+ {
+
+ }
+ };
+
+ typedef boost::property_tree::ptree Parsed;
+
+ /**
+ * Locate, open, and parse a library configuration file.
+ *
+ * @throws ConfigFile::Error on parse error
+ */
+ ConfigFile();
+
+ ~ConfigFile();
+
+ const boost::filesystem::path&
+ getPath() const;
+
+ const Parsed&
+ getParsedConfiguration() const;
+
+private:
+
+ bool
+ open();
+
+ void
+ close();
+
+ /**
+ * Parse a previously discovered and opened configuration file.
+ * For convenience this method will attempt to open the file
+ * if it has previously been located, but open() has not been called.
+ *
+ * @throws ConfigFile::Error on parse error
+ * @throws ConfigFile::Error on failure to open previously un-open configuration file
+ * @throws ConfigFile::Error if no configuration file was previously located
+ */
+ const Parsed&
+ parse();
+
+ /**
+ * Looking for the configuration file in these well-known locations:
+ *
+ * 1. $HOME/.ndn/client.conf
+ * 2. @SYSCONFDIR@/ndn/client.conf
+ * 3. /etc/ndn/client.conf
+ *
+ * @return path to preferred configuration (according to above order) or empty path on failure
+ */
+
+ boost::filesystem::path
+ findConfigFile();
+
+private:
+ boost::filesystem::path m_path; // absolute path to active configuration file (if any)
+ std::ifstream m_input;
+ Parsed m_config;
+};
+
+inline const boost::filesystem::path&
+ConfigFile::getPath() const
+{
+ return m_path;
+}
+
+inline const ConfigFile::Parsed&
+ConfigFile::getParsedConfiguration() const
+{
+ return m_config;
+}
+
+} // namespace ndn
+
+
+#endif // NDN_MANAGEMENT_CONFIG_FILE_HPP
+