Commit ff1db4d3 authored by Steven Murray's avatar Steven Murray
Browse files

Changed Utils class into a namespace

parent 1faed310
......@@ -142,7 +142,7 @@ namespace cta { namespace common {
return defaultValue;
}
if (!Utils::isValidUInt(strValue.c_str())) {
if (!utils::isValidUInt(strValue.c_str())) {
InvalidConfigEntry ex(category.c_str(),
key.c_str(), strValue.c_str());
ex.getMessage() << "Failed to get configuration entry " << category <<
......@@ -184,7 +184,7 @@ namespace cta { namespace common {
const std::string &key, log::Logger *const log = NULL) {
const std::string strValue = getConfEntString(category, key);
if (!Utils::isValidUInt(strValue.c_str())) {
if (!utils::isValidUInt(strValue.c_str())) {
InvalidConfigEntry ex(category.c_str(),
key.c_str(), strValue.c_str());
ex.getMessage() << "Failed to get configuration entry " << category <<
......
......@@ -55,7 +55,7 @@ cap_t castor::server::ProcessCap::getProc() {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to get the capabilities of the process: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
return cap;
......@@ -72,7 +72,7 @@ std::string castor::server::ProcessCap::toText(
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to create string representation of capability state: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
std::string result(text);
......@@ -82,7 +82,7 @@ std::string castor::server::ProcessCap::toText(
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to free string representation of capability state: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
......@@ -114,7 +114,7 @@ cap_t castor::server::ProcessCap::fromText(const std::string &text) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to create capability state from string representation"
": text='" << text << "': " << cta::Utils::errnoToString(errno);
": text='" << text << "': " << cta::utils::errnoToString(errno);
throw ex;
}
......@@ -129,7 +129,7 @@ void castor::server::ProcessCap::setProc(const cap_t cap) {
castor::exception::Exception ex;
ex.getMessage() <<
"Failed to set the capabilities of the process: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
}
......@@ -35,7 +35,7 @@ Errnum::Errnum(int err, std::string what):Exception("") {
}
void Errnum::ErrnumConstructorBottomHalf(const std::string & what) {
m_strerror = Utils::errnoToString(m_errnum);
m_strerror = utils::errnoToString(m_errnum);
std::stringstream w2;
if (what.size())
w2 << what << " ";
......
......@@ -55,7 +55,7 @@ cta::log::SyslogLogger::SyslogLogger(const std::string &socketName,
struct stat fileStatus;
bzero(&fileStatus, sizeof(fileStatus));
if(stat(socketName.c_str(), &fileStatus)) {
const std::string errMsg = cta::Utils::errnoToString(errno);
const std::string errMsg = cta::utils::errnoToString(errno);
cta::exception::Exception ex;
ex.getMessage() << "Failed to instantiate syslog logger: Failed to stat"
" socket \"" << socketName << "\"" ": " << errMsg;
......@@ -378,7 +378,7 @@ void cta::log::SyslogLogger::writeHeader(
std::string cta::log::SyslogLogger::cleanString(const std::string &s,
const bool replaceUnderscores) {
// Trim both left and right white-space
std::string result = Utils::trimString(s);
std::string result = utils::trimString(s);
for (std::string::iterator it = result.begin(); it != result.end(); ++it) {
......
......@@ -58,7 +58,7 @@ cap_t cta::server::ProcessCap::getProc() {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to get the capabilities of the process: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
return cap;
......@@ -75,7 +75,7 @@ std::string cta::server::ProcessCap::toText(
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to create string representation of capability state: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
std::string result(text);
......@@ -85,7 +85,7 @@ std::string cta::server::ProcessCap::toText(
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to free string representation of capability state: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
......@@ -117,7 +117,7 @@ cap_t cta::server::ProcessCap::fromText(const std::string &text) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to create capability state from string representation"
": text='" << text << "': " << cta::Utils::errnoToString(errno);
": text='" << text << "': " << cta::utils::errnoToString(errno);
throw ex;
}
......@@ -132,7 +132,7 @@ void cta::server::ProcessCap::setProc(const cap_t cap) {
cta::exception::Exception ex;
ex.getMessage() <<
"Failed to set the capabilities of the process: "
<< cta::Utils::errnoToString(errno);
<< cta::utils::errnoToString(errno);
throw ex;
}
}
......@@ -101,7 +101,7 @@ static void assertPathContainsValidChars(const std::string &path) {
//------------------------------------------------------------------------------
// assertAbsolutePathSyntax
//------------------------------------------------------------------------------
void cta::Utils::assertAbsolutePathSyntax(const std::string &path) {
void cta::utils::assertAbsolutePathSyntax(const std::string &path) {
try {
assertPathStartsWithASlash(path);
assertPathContainsValidChars(path);
......@@ -117,7 +117,7 @@ void cta::Utils::assertAbsolutePathSyntax(const std::string &path) {
//------------------------------------------------------------------------------
// getEnclosingPath
//------------------------------------------------------------------------------
std::string cta::Utils::getEnclosingPath(const std::string &path) {
std::string cta::utils::getEnclosingPath(const std::string &path) {
if(path == "/") {
throw Exception("Root directory does not have a parent");
}
......@@ -132,7 +132,7 @@ std::string cta::Utils::getEnclosingPath(const std::string &path) {
//------------------------------------------------------------------------------
// getEnclosedName
//------------------------------------------------------------------------------
std::string cta::Utils::getEnclosedName(const std::string &path) {
std::string cta::utils::getEnclosedName(const std::string &path) {
const std::string::size_type last_slash_idx = path.find_last_of('/');
if(std::string::npos == last_slash_idx) {
return path;
......@@ -148,7 +148,7 @@ std::string cta::Utils::getEnclosedName(const std::string &path) {
//-----------------------------------------------------------------------------
// getEnclosedNames
//-----------------------------------------------------------------------------
std::list<std::string> cta::Utils::getEnclosedNames(
std::list<std::string> cta::utils::getEnclosedNames(
const std::list<std::string> &paths) {
std::list<std::string> names;
......@@ -163,7 +163,7 @@ std::list<std::string> cta::Utils::getEnclosedNames(
//-----------------------------------------------------------------------------
// trimSlashes
//-----------------------------------------------------------------------------
std::string cta::Utils::trimSlashes(const std::string &s) {
std::string cta::utils::trimSlashes(const std::string &s) {
// Find first non slash character
size_t beginpos = s.find_first_not_of("/");
std::string::const_iterator it1;
......@@ -188,7 +188,7 @@ std::string cta::Utils::trimSlashes(const std::string &s) {
//-----------------------------------------------------------------------------
// trimFinalSlashes
//-----------------------------------------------------------------------------
std::string cta::Utils::trimFinalSlashes(const std::string &s) {
std::string cta::utils::trimFinalSlashes(const std::string &s) {
// Find last non slash chararacter
std::string::const_iterator it2;
size_t endpos = s.find_last_not_of("/");
......@@ -204,7 +204,7 @@ std::string cta::Utils::trimFinalSlashes(const std::string &s) {
//-----------------------------------------------------------------------------
// splitString
//-----------------------------------------------------------------------------
void cta::Utils::splitString(const std::string &str, const char separator,
void cta::utils::splitString(const std::string &str, const char separator,
std::vector<std::string> &result) {
if(str.empty()) {
......@@ -230,7 +230,7 @@ void cta::Utils::splitString(const std::string &str, const char separator,
//-----------------------------------------------------------------------------
// trimString
//-----------------------------------------------------------------------------
std::string cta::Utils::trimString(const std::string &s) throw() {
std::string cta::utils::trimString(const std::string &s) throw() {
const std::string& spaces="\t\n\v\f\r ";
// Find first non white character
......@@ -257,7 +257,7 @@ std::string cta::Utils::trimString(const std::string &s) throw() {
//-----------------------------------------------------------------------------
// generateUuid
//-----------------------------------------------------------------------------
std::string cta::Utils::generateUuid() {
std::string cta::utils::generateUuid() {
uuid_t uuid;
char str[36 + 1];
......@@ -270,7 +270,7 @@ std::string cta::Utils::generateUuid() {
//-----------------------------------------------------------------------------
// endsWith
//-----------------------------------------------------------------------------
bool cta::Utils::endsWith(const std::string &str, const char c) {
bool cta::utils::endsWith(const std::string &str, const char c) {
if(str.empty()) {
return false;
} else {
......@@ -281,13 +281,13 @@ bool cta::Utils::endsWith(const std::string &str, const char c) {
//------------------------------------------------------------------------------
// setXattr
//------------------------------------------------------------------------------
void cta::Utils::setXattr(const std::string &path, const std::string &name,
void cta::utils::setXattr(const std::string &path, const std::string &name,
const std::string &value) {
if(setxattr(path.c_str(), name.c_str(), value.c_str(), value.length(), 0)) {
const int savedErrno = errno;
std::ostringstream msg;
msg << "Call to setxattr() failed: path=" << path << " name=" << name <<
" value=" << value << ": " << Utils::errnoToString(savedErrno);
" value=" << value << ": " << errnoToString(savedErrno);
throw exception::Exception(msg.str());
}
}
......@@ -295,14 +295,14 @@ void cta::Utils::setXattr(const std::string &path, const std::string &name,
//------------------------------------------------------------------------------
// getXattr
//------------------------------------------------------------------------------
std::string cta::Utils::getXattr(const std::string &path,
std::string cta::utils::getXattr(const std::string &path,
const std::string &name) {
const auto sizeOfValue = getxattr(path.c_str(), name.c_str(), NULL, 0);
if(0 > sizeOfValue) {
const int savedErrno = errno;
std::stringstream msg;
msg << "Call to getxattr() failed: path=" << path << " name=" << name <<
": " << Utils::errnoToString(savedErrno);
": " << errnoToString(savedErrno);
throw exception::Exception(msg.str());
}
......@@ -318,7 +318,7 @@ std::string cta::Utils::getXattr(const std::string &path,
const int savedErrno = errno;
std::stringstream msg;
msg << "Call to getxattr() failed: path=" << path << " name=" << name <<
": " << Utils::errnoToString(savedErrno);
": " << errnoToString(savedErrno);
throw exception::Exception(msg.str());
}
......@@ -328,7 +328,7 @@ std::string cta::Utils::getXattr(const std::string &path,
//------------------------------------------------------------------------------
// errnoToString
//------------------------------------------------------------------------------
std::string cta::Utils::errnoToString(const int errnoValue) throw() {
std::string cta::utils::errnoToString(const int errnoValue) throw() {
char buf[100];
if(!strerror_r_wrapper(errnoValue, buf, sizeof(buf))) {
......@@ -362,7 +362,7 @@ std::string cta::Utils::errnoToString(const int errnoValue) throw() {
//------------------------------------------------------------------------------
// toUint16
//------------------------------------------------------------------------------
uint16_t cta::Utils::toUint16(const std::string &str) {
uint16_t cta::utils::toUint16(const std::string &str) {
if(str.empty()) {
std::ostringstream msg;
msg << "Failed to convert empty string to uint16_t: An empty string is not"
......@@ -398,7 +398,7 @@ uint16_t cta::Utils::toUint16(const std::string &str) {
//------------------------------------------------------------------------------
// toUid
//------------------------------------------------------------------------------
uid_t cta::Utils::toUid(const std::string &str) {
uid_t cta::utils::toUid(const std::string &str) {
if(str.empty()) {
std::ostringstream msg;
msg << "Failed to convert empty string to uid_t: An empty string is not"
......@@ -434,7 +434,7 @@ uid_t cta::Utils::toUid(const std::string &str) {
//------------------------------------------------------------------------------
// toGid
//------------------------------------------------------------------------------
gid_t cta::Utils::toGid(const std::string &str) {
gid_t cta::utils::toGid(const std::string &str) {
if(str.empty()) {
std::ostringstream msg;
msg << "Failed to convert empty string to gid_t: An empty string is not"
......@@ -470,7 +470,7 @@ gid_t cta::Utils::toGid(const std::string &str) {
//------------------------------------------------------------------------------
// isValidUInt
//------------------------------------------------------------------------------
bool cta::Utils::isValidUInt(const std::string &str)
bool cta::utils::isValidUInt(const std::string &str)
throw() {
// An empty string is not a valid unsigned integer
if(str.empty()) {
......@@ -493,7 +493,7 @@ bool cta::Utils::isValidUInt(const std::string &str)
//------------------------------------------------------------------------------
// getAdler32
//------------------------------------------------------------------------------
uint32_t cta::Utils::getAdler32(const uint8_t *buf, const uint32_t len)
uint32_t cta::utils::getAdler32(const uint8_t *buf, const uint32_t len)
throw() {
const uint32_t checksum = adler32(0L, Z_NULL, 0);
return adler32(checksum, (const Bytef*)buf, len);
......@@ -502,7 +502,7 @@ uint32_t cta::Utils::getAdler32(const uint8_t *buf, const uint32_t len)
//------------------------------------------------------------------------------
// getShortHostname
//------------------------------------------------------------------------------
std::string cta::Utils::getShortHostname() {
std::string cta::utils::getShortHostname() {
struct utsname un;
exception::Errnum::throwOnMinusOne(uname (&un));
std::vector<std::string> snn;
......@@ -513,7 +513,7 @@ std::string cta::Utils::getShortHostname() {
//------------------------------------------------------------------------------
// getDumpableProcessAttribute
//------------------------------------------------------------------------------
bool cta::Utils::getDumpableProcessAttribute() {
bool cta::utils::getDumpableProcessAttribute() {
const int rc = prctl(PR_GET_DUMPABLE);
switch(rc) {
case -1:
......@@ -541,7 +541,7 @@ bool cta::Utils::getDumpableProcessAttribute() {
//------------------------------------------------------------------------------
// setDumpableProcessAttribute
//------------------------------------------------------------------------------
void cta::Utils::setDumpableProcessAttribute(const bool dumpable) {
void cta::utils::setDumpableProcessAttribute(const bool dumpable) {
const int rc = prctl(PR_SET_DUMPABLE, dumpable ? 1 : 0);
switch(rc) {
case -1:
......@@ -563,5 +563,3 @@ bool cta::Utils::getDumpableProcessAttribute() {
}
}
}
......@@ -27,11 +27,7 @@
namespace cta {
/**
* Class of utility methods.
*/
class Utils {
public:
namespace utils {
/**
* Throws an exception if the specified absolute path constains a
......@@ -39,7 +35,7 @@ public:
*
* @param path The Absolute path.
*/
static void assertAbsolutePathSyntax(const std::string &path);
void assertAbsolutePathSyntax(const std::string &path);
/**
* Returns the path of the enclosing directory of the specified path.
......@@ -54,7 +50,7 @@ public:
* @param path The path.
* @return The path of the enclosing directory.
*/
static std::string getEnclosingPath(const std::string &path);
std::string getEnclosingPath(const std::string &path);
/**
* Returns the name of the enclosed file or directory of the specified path.
......@@ -62,7 +58,7 @@ public:
* @param path The path.
* @return The name of the enclosed file or directory.
*/
static std::string getEnclosedName(const std::string &path);
std::string getEnclosedName(const std::string &path);
/**
* Returns the names of the enclosed file or directory of each of the
......@@ -72,7 +68,7 @@ public:
* @return The names of the enclosed file or directory of each of the
* specified paths.
*/
static std::list<std::string> getEnclosedNames(
std::list<std::string> getEnclosedNames(
const std::list<std::string> &paths);
/**
......@@ -82,7 +78,7 @@ public:
* @param s The string to be trimmed.
* @return The result of trimming the string.
*/
static std::string trimSlashes(const std::string &s);
std::string trimSlashes(const std::string &s);
/**
* Returns the result of trimming right slashes from the
......@@ -91,7 +87,7 @@ public:
* @param s The string to be trimmed.
* @return The result of trimming the string.
*/
static std::string trimFinalSlashes(const std::string &s);
std::string trimFinalSlashes(const std::string &s);
/**
* Splits the specified string into a vector of strings using the specified
......@@ -104,7 +100,7 @@ public:
* @param result The vector when the result of spliting the string will be
* stored.
*/
static void splitString(const std::string &str, const char separator,
void splitString(const std::string &str, const char separator,
std::vector<std::string> &result);
......@@ -115,7 +111,7 @@ public:
* @param s The string to be trimmed.
* @return The result of trimming the string.
*/
static std::string trimString(const std::string &s) throw();
std::string trimString(const std::string &s) throw();
/**
* Creates and returns an std::string which is the result of replacing each
......@@ -125,14 +121,14 @@ public:
* @param str The original string.
* @return The newly created string with single spaces.
*/
static std::string singleSpaceString(const std::string &str) throw();
std::string singleSpaceString(const std::string &str) throw();
/**
* Returns uuid in the form of a string.
*
* @return uuid in the form of a string.
*/
static std::string generateUuid();
std::string generateUuid();
/**
* Returns true if the specified string ends with the specifie character.
......@@ -141,7 +137,7 @@ public:
* @param c The character to be looked for at the end of the string.
* @return True if the specified string ends with the specified character.
*/
static bool endsWith(const std::string &str, const char c);
bool endsWith(const std::string &str, const char c);
/**
* Returns the string reprsentation of the specified value.
......@@ -149,7 +145,7 @@ public:
* @param value The value whose string representation is to be returned.
* @return The string reprsentation of the specified value.
*/
template <typename T> static std::string toString(const T& value) {
template <typename T> std::string toString(const T& value) {
std::ostringstream oss;
oss << value;
return oss.str();
......@@ -162,7 +158,7 @@ public:
* @param name The name of the extended attribute.
* @param value The value of the extended attribute.
*/
static void setXattr(const std::string &path, const std::string &name,
void setXattr(const std::string &path, const std::string &name,
const std::string &value);
/**
......@@ -172,7 +168,7 @@ public:
* @param name The name of the extended attribute.
* @return The value of the extended attribute.
*/
static std::string getXattr(const std::string &path, const std::string &name);
std::string getXattr(const std::string &path, const std::string &name);
/**
* Determines the string representation of the specified error number.
......@@ -182,7 +178,7 @@ public:
* @param errnoValue The errno value.
* @return The string representation.
*/
static std::string errnoToString(const int errnoValue) throw();
std::string errnoToString(const int errnoValue) throw();
/**
* Converts the specified string to an unsigned integer.
......@@ -190,7 +186,7 @@ public:
* @param str The string.
* @return The unisgned integer.
*/
static uint16_t toUint16(const std::string &str);
uint16_t toUint16(const std::string &str);
/**
* Converts the specified string to a uid.
......@@ -198,7 +194,7 @@ public:
* @param str The string.
* @return The uid.
*/
static uid_t toUid(const std::string &str);
uid_t toUid(const std::string &str);
/**
* Converts the specified string to a gid.
......@@ -206,7 +202,7 @@ public:
* @param str The string.
* @return The gid.
*/
static gid_t toGid(const std::string &str);
gid_t toGid(const std::string &str);
/**
* Checks if the specified string is a valid unsigned integer.
......@@ -214,14 +210,14 @@ public:
* @param str The string to be checked.
* @returns true if the string is a valid unsigned integer, else false.
*/
static bool isValidUInt(const std::string &str) throw();
bool isValidUInt(const std::string &str) throw();
/**
* Gets the short host name from the system
*
* @return the short host name
*/
static std::string getShortHostname();
std::string getShortHostname();
/**
* Returns the alder32 checksum of the specified buffer.
......@@ -230,7 +226,7 @@ public:
* @param len The length of the buffer in bytes.
* @return the alder32 checksum of the specified buffer.
*/
static uint32_t getAdler32(const uint8_t *buf, const uint32_t len) throw();
uint32_t getAdler32(const uint8_t *buf, const uint32_t len) throw();
/**
* Returns true if the attributes of the current process indicate that it will
......@@ -241,7 +237,7 @@ public:
*
* @return true if the current program is dumpable.
*/
static bool getDumpableProcessAttribute();
bool getDumpableProcessAttribute();
/**
* Sets the attributes of the current process to indicate hat it will produce a
......@@ -249,7 +245,8 @@ public:
*
* @param dumpable true if the current program should be dumpable.
*/
static void setDumpableProcessAttribute(const bool dumpable);
}; // class Utils
void setDumpableProcessAttribute(const bool dumpable);
} // namespace utils
} // namespace cta
......@@ -36,7 +36,7 @@ TEST_F(cta_UtilsTest, trimSlashes_emptyString) {
using namespace cta;
const std::string s;
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(s, trimmedString);
}
......@@ -44,7 +44,7 @@ TEST_F(cta_UtilsTest, trimSlashes_noSlashes) {
using namespace cta;
const std::string s("NO_SLASHES");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(s, trimmedString);
}
......@@ -52,7 +52,7 @@ TEST_F(cta_UtilsTest, trimSlashes_oneLeftSlash) {
using namespace cta;
const std::string s("/VALUE");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(std::string("VALUE"), trimmedString);
}
......@@ -60,7 +60,7 @@ TEST_F(cta_UtilsTest, trimSlashes_twoLeftSlashes) {
using namespace cta;
const std::string s("//VALUE");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(std::string("VALUE"), trimmedString);
}
......@@ -68,7 +68,7 @@ TEST_F(cta_UtilsTest, trimSlashes_oneRightSlash) {
using namespace cta;
const std::string s("VALUE/");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(std::string("VALUE"), trimmedString);
}
......@@ -76,7 +76,7 @@ TEST_F(cta_UtilsTest, trimSlashes_twoRightSlashes) {
using namespace cta;
const std::string s("VALUE//");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(std::string("VALUE"), trimmedString);
}
......@@ -85,7 +85,7 @@ TEST_F(cta_UtilsTest,
using namespace cta;
const std::string s("/VALUE/");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(std::string("VALUE"), trimmedString);
}
......@@ -94,7 +94,7 @@ TEST_F(cta_UtilsTest,
using namespace cta;
const std::string s("//VALUE//");
const std::string trimmedString = Utils::trimSlashes(s);
const std::string trimmedString = utils::trimSlashes(s);
ASSERT_EQ(std::string("VALUE"), trimmedString);
}
......@@ -103,7 +103,7 @@ TEST_F(cta_UtilsTest, getEnclosingPath_empty_string) {
const std::string dirPath = "";