From 3a1d3401f062ecd98e099a1894d1983b1a997383 Mon Sep 17 00:00:00 2001
From: Steven Murray <steven.murray@cern.ch>
Date: Thu, 21 Apr 2016 14:16:04 +0200
Subject: [PATCH] Improved the code readability of the catalogue directory

---
 catalogue/DbLogin.cpp  | 99 ++++++++++++++++++++++--------------------
 catalogue/OcciConn.cpp | 19 +++++---
 catalogue/OcciEnv.cpp  | 18 +++++---
 catalogue/OcciRset.cpp | 21 +++++----
 catalogue/OcciStmt.cpp | 35 +++++++--------
 5 files changed, 107 insertions(+), 85 deletions(-)

diff --git a/catalogue/DbLogin.cpp b/catalogue/DbLogin.cpp
index 35d9eb8632..93bec26b96 100644
--- a/catalogue/DbLogin.cpp
+++ b/catalogue/DbLogin.cpp
@@ -32,64 +32,69 @@
 #include <string.h>
 
 namespace {
-  using namespace cta;
-
-  /**
-   * Reads the entire contents of the specified files and returns a list of the
-   * non-empty lines.
-   *
-   * A line is considered not empty if it contains characters that are not white
-   * space and are not part of a comment.
-   *
-   * @return A list of the non-empty lines.
-   */
-  std::list<std::string> readNonEmptyLines(
-    const std::string &filename) {
-
-    std::ifstream file(filename);
-    if(!file) {
-      exception::Exception ex;
-      ex.getMessage() << "Failed to open " << filename;
-      throw ex;
-    }
 
-    std::list<std::string> lines;
-    std::string line;
+using namespace cta;
 
-    while(std::getline(file, line)) {
-      // Remove the newline character if there is one
-      {
-        const std::string::size_type newlinePos = line.find("\n");
-        if(newlinePos != std::string::npos) {
-          line = line.substr(0, newlinePos);
-        }
-      }
+/**
+ * Reads the entire contents of the specified files and returns a list of the
+ * non-empty lines.
+ *
+ * A line is considered not empty if it contains characters that are not white
+ * space and are not part of a comment.
+ *
+ * @return A list of the non-empty lines.
+ */
+std::list<std::string> readNonEmptyLines(
+  const std::string &filename) {
 
-      // If there is a comment, then remove it from the line
-      {
-        const std::string::size_type startOfComment = line.find("#");
-        if(startOfComment != std::string::npos) {
-          line = line.substr(0, startOfComment);
-        }
-      }
+  std::ifstream file(filename);
+  if(!file) {
+    exception::Exception ex;
+    ex.getMessage() << "Failed to open " << filename;
+    throw ex;
+  }
+
+  std::list<std::string> lines;
+  std::string line;
 
-      // Left and right trim the line of whitespace
-      line = utils::trimString(std::string(line));
+  while(std::getline(file, line)) {
+    // Remove the newline character if there is one
+    {
+      const std::string::size_type newlinePos = line.find("\n");
+      if(newlinePos != std::string::npos) {
+        line = line.substr(0, newlinePos);
+      }
+    }
 
-      // If the line is not empty
-      if(!line.empty()) {
-        lines.push_back(line);
+    // If there is a comment, then remove it from the line
+    {
+      const std::string::size_type startOfComment = line.find("#");
+      if(startOfComment != std::string::npos) {
+        line = line.substr(0, startOfComment);
       }
     }
 
-    return lines;
+    // Left and right trim the line of whitespace
+    line = utils::trimString(std::string(line));
+
+    // If the line is not empty
+    if(!line.empty()) {
+      lines.push_back(line);
+    }
   }
+
+  return lines;
 }
 
+} // anonymous namespace
+
+namespace cta {
+namespace catalogue {
+
 //------------------------------------------------------------------------------
 // constructor
 //------------------------------------------------------------------------------
-cta::catalogue::DbLogin::DbLogin(
+DbLogin::DbLogin(
   const std::string &username,
   const std::string &password,
   const std::string &database):
@@ -101,8 +106,7 @@ cta::catalogue::DbLogin::DbLogin(
 //------------------------------------------------------------------------------
 // readFromFile
 //------------------------------------------------------------------------------
-cta::catalogue::DbLogin cta::catalogue::DbLogin::readFromFile(
-  const std::string &filename) {
+DbLogin DbLogin::readFromFile(const std::string &filename) {
   const std::string fileFormat = "username/password@database";
   const std::list<std::string> lines = readNonEmptyLines(filename);
 
@@ -130,3 +134,6 @@ cta::catalogue::DbLogin cta::catalogue::DbLogin::readFromFile(
 
   return DbLogin(userAndPass[0], userAndPass[1], userPassAndDb[1]);
 }
+
+} // namesapce catalogue
+} // namespace cta
diff --git a/catalogue/OcciConn.cpp b/catalogue/OcciConn.cpp
index 049c803edc..ccf94964fc 100644
--- a/catalogue/OcciConn.cpp
+++ b/catalogue/OcciConn.cpp
@@ -26,11 +26,13 @@
 #include <stdexcept>
 #include <string>
 
+namespace cta {
+namespace catalogue {
+
 //------------------------------------------------------------------------------
 // constructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciConn::OcciConn(OcciEnv &env,
-  oracle::occi::Connection *const conn):
+OcciConn::OcciConn(OcciEnv &env, oracle::occi::Connection *const conn):
   m_env(env),
   m_conn(conn) {
   if(NULL == conn) {
@@ -42,7 +44,7 @@ cta::catalogue::OcciConn::OcciConn(OcciEnv &env,
 //------------------------------------------------------------------------------
 // destructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciConn::~OcciConn() throw() {
+OcciConn::~OcciConn() throw() {
   try {
     close(); // Idempotent close() mthod
   } catch(...) {
@@ -53,7 +55,7 @@ cta::catalogue::OcciConn::~OcciConn() throw() {
 //------------------------------------------------------------------------------
 // close
 //------------------------------------------------------------------------------
-void cta::catalogue::OcciConn::close() {
+void OcciConn::close() {
   std::lock_guard<std::mutex> lock(m_mutex);
 
   if(m_conn != NULL) {
@@ -65,21 +67,21 @@ void cta::catalogue::OcciConn::close() {
 //------------------------------------------------------------------------------
 // get
 //------------------------------------------------------------------------------
-oracle::occi::Connection *cta::catalogue::OcciConn::get() const {
+oracle::occi::Connection *OcciConn::get() const {
   return m_conn;
 }
 
 //------------------------------------------------------------------------------
 // operator->()
 //------------------------------------------------------------------------------
-oracle::occi::Connection *cta::catalogue::OcciConn::operator->() const {
+oracle::occi::Connection *OcciConn::operator->() const {
   return get();
 }
 
 //------------------------------------------------------------------------------
 // createStmt
 //------------------------------------------------------------------------------
-cta::catalogue::OcciStmt *cta::catalogue::OcciConn::createStmt(const char *sql) {
+OcciStmt *OcciConn::createStmt(const char *const sql) {
   try {
     if(NULL == sql) throw std::runtime_error("sql is NULL");
 
@@ -94,3 +96,6 @@ cta::catalogue::OcciStmt *cta::catalogue::OcciConn::createStmt(const char *sql)
     throw std::runtime_error(std::string(__FUNCTION__) + "failed: " + ne.what());
   }
 }
+
+} // namespace catalogue
+} // namespace cta
diff --git a/catalogue/OcciEnv.cpp b/catalogue/OcciEnv.cpp
index d9bbf8c6bf..c4c18b1ab6 100644
--- a/catalogue/OcciEnv.cpp
+++ b/catalogue/OcciEnv.cpp
@@ -25,10 +25,13 @@
 
 #include <stdexcept>
 
+namespace cta {
+namespace catalogue {
+
 //------------------------------------------------------------------------------
 // constructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciEnv::OcciEnv() {
+OcciEnv::OcciEnv() {
   using namespace oracle::occi;
   m_env = Environment::createEnvironment(Environment::THREADED_MUTEXED);
   if(NULL == m_env) {
@@ -40,7 +43,7 @@ cta::catalogue::OcciEnv::OcciEnv() {
 //------------------------------------------------------------------------------
 // destructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciEnv::~OcciEnv() throw() {
+OcciEnv::~OcciEnv() throw() {
   using namespace oracle::occi;
 
   Environment::terminateEnvironment(m_env);
@@ -49,21 +52,21 @@ cta::catalogue::OcciEnv::~OcciEnv() throw() {
 //------------------------------------------------------------------------------
 // get
 //------------------------------------------------------------------------------
-oracle::occi::Environment *cta::catalogue::OcciEnv::get() const {
+oracle::occi::Environment *OcciEnv::get() const {
   return m_env;
 }
 
 //------------------------------------------------------------------------------
 // operator->
 //------------------------------------------------------------------------------
-oracle::occi::Environment *cta::catalogue::OcciEnv::operator->() const {
+oracle::occi::Environment *OcciEnv::operator->() const {
   return get();
 }
 
 //------------------------------------------------------------------------------
-// creatConn
+// createConn
 //------------------------------------------------------------------------------
-cta::catalogue::OcciConn *cta::catalogue::OcciEnv::createConn(
+cta::catalogue::OcciConn *OcciEnv::createConn(
   const char *const username,
   const char *const password,
   const char *const database) {
@@ -82,3 +85,6 @@ cta::catalogue::OcciConn *cta::catalogue::OcciEnv::createConn(
     throw std::runtime_error(std::string(__FUNCTION__) + " failed:" + ne.what());
   }
 }
+
+} // namespace catalogue
+} // namespace cta
diff --git a/catalogue/OcciRset.cpp b/catalogue/OcciRset.cpp
index db6e9f9b5b..83eade26b5 100644
--- a/catalogue/OcciRset.cpp
+++ b/catalogue/OcciRset.cpp
@@ -24,22 +24,22 @@
 
 #include <stdexcept>
 
+namespace cta {
+namespace catalogue {
+
 //------------------------------------------------------------------------------
 // constructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciRset::OcciRset(OcciStmt &stmt,
-  oracle::occi::ResultSet *const rset):
+OcciRset::OcciRset(OcciStmt &stmt, oracle::occi::ResultSet *const rset):
   m_stmt(stmt),
   m_rset(rset) {
-  if(NULL == rset) {
-    throw std::runtime_error(std::string(__FUNCTION__) + " failed: rset is NULL");
-  }
+  if(NULL == rset) throw std::runtime_error(std::string(__FUNCTION__) + " failed: rset is NULL");
 }
 
 //------------------------------------------------------------------------------
 // destructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciRset::~OcciRset() throw() {
+OcciRset::~OcciRset() throw() {
   try {
     close(); // Idempotent close()
   } catch(...) {
@@ -50,7 +50,7 @@ cta::catalogue::OcciRset::~OcciRset() throw() {
 //------------------------------------------------------------------------------
 // close
 //------------------------------------------------------------------------------
-void cta::catalogue::OcciRset::close() {
+void OcciRset::close() {
   std::lock_guard<std::mutex> lock(m_mutex);
 
   if(NULL != m_rset) {
@@ -62,13 +62,16 @@ void cta::catalogue::OcciRset::close() {
 //------------------------------------------------------------------------------
 // get
 //------------------------------------------------------------------------------
-oracle::occi::ResultSet *cta::catalogue::OcciRset::get() const {
+oracle::occi::ResultSet *OcciRset::get() const {
   return m_rset;
 }
 
 //------------------------------------------------------------------------------
 // operator->
 //------------------------------------------------------------------------------
-oracle::occi::ResultSet *cta::catalogue::OcciRset::operator->() const {
+oracle::occi::ResultSet *OcciRset::operator->() const {
   return get();
 }
+
+} // namespace catalogue
+} // namespace cta
diff --git a/catalogue/OcciStmt.cpp b/catalogue/OcciStmt.cpp
index d7683d5ed3..7f2c05f668 100644
--- a/catalogue/OcciStmt.cpp
+++ b/catalogue/OcciStmt.cpp
@@ -25,18 +25,16 @@
 #include <cstring>
 #include <stdexcept>
 
+namespace cta {
+namespace catalogue {
+
 //------------------------------------------------------------------------------
 // constructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciStmt::OcciStmt(const char *const sql, OcciConn &conn,
-  oracle::occi::Statement *const stmt):
+OcciStmt::OcciStmt(const char *const sql, OcciConn &conn, oracle::occi::Statement *const stmt) :
   m_conn(conn),
   m_stmt(stmt) {
-  if(NULL == stmt) {
-    std::runtime_error ex(std::string(__FUNCTION__) + " failed"
-      ": OCCI statment is a NULL pointer");
-    throw ex;
-  }
+  if(NULL == stmt) throw std::runtime_error(std::string(__FUNCTION__) + " failed: stmt is NULL");
 
   // Work with C strings because they haven't changed with respect to _GLIBCXX_USE_CXX11_ABI
   const std::size_t sqlLen = std::strlen(sql);
@@ -48,12 +46,12 @@ cta::catalogue::OcciStmt::OcciStmt(const char *const sql, OcciConn &conn,
 //------------------------------------------------------------------------------
 // destructor
 //------------------------------------------------------------------------------
-cta::catalogue::OcciStmt::~OcciStmt() throw() {
+OcciStmt::~OcciStmt() throw() {
   delete m_sql;
 
   try {
     close(); // Idempotent close() method
-  } catch(...) {
+  } catch (...) {
     // Destructor does not throw
   }
 }
@@ -61,10 +59,10 @@ cta::catalogue::OcciStmt::~OcciStmt() throw() {
 //------------------------------------------------------------------------------
 // close
 //------------------------------------------------------------------------------
-void cta::catalogue::OcciStmt::close() {
+void OcciStmt::close() {
   std::lock_guard<std::mutex> lock(m_mutex);
 
-  if(NULL != m_stmt) {
+  if (NULL != m_stmt) {
     m_conn->terminateStatement(m_stmt);
     m_stmt = NULL;
   }
@@ -73,28 +71,28 @@ void cta::catalogue::OcciStmt::close() {
 //------------------------------------------------------------------------------
 // getSql
 //------------------------------------------------------------------------------
-const char *cta::catalogue::OcciStmt::getSql() const {
+const char *OcciStmt::getSql() const {
   return m_sql;
 }
 
 //------------------------------------------------------------------------------
 // get
 //------------------------------------------------------------------------------
-oracle::occi::Statement *cta::catalogue::OcciStmt::get() const {
+oracle::occi::Statement *OcciStmt::get() const {
   return m_stmt;
 }
 
 //------------------------------------------------------------------------------
 // operator->
 //------------------------------------------------------------------------------
-oracle::occi::Statement *cta::catalogue::OcciStmt::operator->() const {
+oracle::occi::Statement *OcciStmt::operator->() const {
   return get();
 }
 
 //------------------------------------------------------------------------------
 // bind
 //------------------------------------------------------------------------------
-void cta::catalogue::OcciStmt::bind(const char *paramName, const uint64_t paramValue) {
+void OcciStmt::bind(const char *paramName, const uint64_t paramValue) {
   std::runtime_error ex(std::string(__FUNCTION__) + " is not implemented");
   throw ex;
 }
@@ -102,7 +100,7 @@ void cta::catalogue::OcciStmt::bind(const char *paramName, const uint64_t paramV
 //------------------------------------------------------------------------------
 // bind
 //------------------------------------------------------------------------------
-void cta::catalogue::OcciStmt::bind(const char *paramName, const char *paramValue) {
+void OcciStmt::bind(const char *paramName, const char *paramValue) {
   std::runtime_error ex(std::string(__FUNCTION__) + " is not implemented");
   throw ex;
 }
@@ -110,6 +108,9 @@ void cta::catalogue::OcciStmt::bind(const char *paramName, const char *paramValu
 //------------------------------------------------------------------------------
 // execute
 //------------------------------------------------------------------------------
-cta::catalogue::OcciRset *cta::catalogue::OcciStmt::execute() {
+OcciRset *OcciStmt::execute() {
   return NULL;
 }
+
+} // namespace catalogue
+} // namespace cta
-- 
GitLab