From d4bc19d4303bdb3bc8a35535b9467bedb23f6185 Mon Sep 17 00:00:00 2001 From: Barthelemy Date: Fri, 8 Mar 2019 09:42:39 +0100 Subject: [PATCH 1/3] CCDB api : Prevent whitespaces in file name The commit also introduces a StringUtils.h with trimming functions. --- CCDB/src/CcdbApi.cxx | 5 +- CCDB/test/testCcdbApi.cxx | 24 +----- Common/Utils/CMakeLists.txt | 1 + .../Utils/include/CommonUtils/StringUtils.h | 73 +++++++++++++++++++ cmake/O2Dependencies.cmake | 2 + 5 files changed, 83 insertions(+), 22 deletions(-) create mode 100644 Common/Utils/include/CommonUtils/StringUtils.h diff --git a/CCDB/src/CcdbApi.cxx b/CCDB/src/CcdbApi.cxx index 7355a24f4ca7e..c5dfebb6c9808 100644 --- a/CCDB/src/CcdbApi.cxx +++ b/CCDB/src/CcdbApi.cxx @@ -18,6 +18,7 @@ #include #include #include +#include namespace o2 { @@ -75,7 +76,9 @@ void CcdbApi::store(TObject* rootObject, std::string path, std::mapGetName()) + "_" + getTimestampString(getCurrentTimestamp()) + ".root"; + string objectName = string(rootObject->GetName()); + utils::trim(objectName); + string tmpFileName = objectName + "_" + getTimestampString(getCurrentTimestamp()) + ".root"; curl_formadd(&formpost, &lastptr, CURLFORM_COPYNAME, "send", diff --git a/CCDB/test/testCcdbApi.cxx b/CCDB/test/testCcdbApi.cxx index fb5b475cbbaa2..a3ccce7e71305 100644 --- a/CCDB/test/testCcdbApi.cxx +++ b/CCDB/test/testCcdbApi.cxx @@ -27,6 +27,7 @@ #include #include #include +#include using namespace std; using namespace o2::ccdb; @@ -108,25 +109,6 @@ BOOST_AUTO_TEST_CASE(delete_test) BOOST_CHECK(h2 == nullptr); } -/// trim from start (in place) -/// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring -static inline void ltrim(std::string& s) -{ - s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { - return !std::isspace(ch); - })); -} - -/// trim from end (in place) -/// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring -static inline void rtrim(std::string& s) -{ - s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { - return !std::isspace(ch); - }).base(), - s.end()); -} - void countItems(const string& s, int& countObjects, int& countSubfolders) { countObjects = 0; @@ -135,8 +117,8 @@ void countItems(const string& s, int& countObjects, int& countSubfolders) std::string line; bool subfolderMode = false; while (std::getline(ss, line, '\n')) { - ltrim(line); - rtrim(line); + o2::utils::ltrim(line); + o2::utils::rtrim(line); if (line.length() == 0) { continue; } diff --git a/Common/Utils/CMakeLists.txt b/Common/Utils/CMakeLists.txt index c5450aeb1c55c..6e84216839d7a 100644 --- a/Common/Utils/CMakeLists.txt +++ b/Common/Utils/CMakeLists.txt @@ -17,6 +17,7 @@ Set(HEADERS include/${MODULE_NAME}/BoostSerializer.h include/${MODULE_NAME}/ShmManager.h include/${MODULE_NAME}/RngHelper.h + include/${MODULE_NAME}/StringUtils.h ) Set(LINKDEF src/CommonUtilsLinkDef.h) diff --git a/Common/Utils/include/CommonUtils/StringUtils.h b/Common/Utils/include/CommonUtils/StringUtils.h new file mode 100644 index 0000000000000..572cee39390b2 --- /dev/null +++ b/Common/Utils/include/CommonUtils/StringUtils.h @@ -0,0 +1,73 @@ +// Copyright CERN and copyright holders of ALICE O2. This software is +// distributed under the terms of the GNU General Public License v3 (GPL +// Version 3), copied verbatim in the file "COPYING". +// +// See http://alice-o2.web.cern.ch/license for full licensing information. +// +// In applying this license CERN does not waive the privileges and immunities +// granted to it by virtue of its status as an Intergovernmental Organization +// or submit itself to any jurisdiction. + +/// +/// \file StringUtils.h +/// \author Barthelemy von Haller +/// + +#ifndef ALICEO2_STRINGUTILS_H +#define ALICEO2_STRINGUTILS_H + +namespace o2 +{ +namespace utils +{ + +// Code for trimming coming from https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring + +/** + * trim from start (in place) + * @param s + */ +static inline void ltrim(std::string &s) { + s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { + return !std::isspace(ch); + })); +} + +/** trim from end (in place) + * + * @param s + */ +static inline void rtrim(std::string &s) { + s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { + return !std::isspace(ch); + }).base(), s.end()); +} + +/** + * trim from both ends (in place) + * @param s + */ +static inline void trim(std::string& s) +{ + ltrim(s); + rtrim(s); +} + +// trim from start (copying) +static inline std::string ltrim_copy(std::string s) +{ + ltrim(s); + return s; +} + +// trim from end (copying) +static inline std::string rtrim_copy(std::string s) +{ + rtrim(s); + return s; +} + +} // namespace utils +} // namespace o2 + +#endif //ALICEO2_STRINGUTILS_H diff --git a/cmake/O2Dependencies.cmake b/cmake/O2Dependencies.cmake index 3e8e1147d01e9..649f67a16cd82 100644 --- a/cmake/O2Dependencies.cmake +++ b/cmake/O2Dependencies.cmake @@ -513,10 +513,12 @@ o2_define_bucket( fairmq_bucket pthread Core Tree XMLParser Hist Net RIO z ${CURL_LIBRARIES} + common_utils_bucket INCLUDE_DIRECTORIES ${FAIRROOT_INCLUDE_DIR} ${ROOT_INCLUDE_DIR} + ${CMAKE_SOURCE_DIR}/Common/Utils/include SYSTEMINCLUDE_DIRECTORIES ${PROTOBUF_INCLUDE_DIR} From 651cc777fb54f20a8d79def954578d9d48dbbfca Mon Sep 17 00:00:00 2001 From: Barthelemy Date: Fri, 8 Mar 2019 12:50:53 +0100 Subject: [PATCH 2/3] fix formatting --- Common/Utils/include/CommonUtils/StringUtils.h | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/Common/Utils/include/CommonUtils/StringUtils.h b/Common/Utils/include/CommonUtils/StringUtils.h index 572cee39390b2..d91d9e8dbf02b 100644 --- a/Common/Utils/include/CommonUtils/StringUtils.h +++ b/Common/Utils/include/CommonUtils/StringUtils.h @@ -27,20 +27,23 @@ namespace utils * trim from start (in place) * @param s */ -static inline void ltrim(std::string &s) { +static inline void ltrim(std::string& s) +{ s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { - return !std::isspace(ch); - })); + return !std::isspace(ch); + })); } /** trim from end (in place) * * @param s */ -static inline void rtrim(std::string &s) { +static inline void rtrim(std::string& s) +{ s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) { - return !std::isspace(ch); - }).base(), s.end()); + return !std::isspace(ch); + }).base(), + s.end()); } /** From 1e80bf40d7ed3714e91f735428a027e2b5ede532 Mon Sep 17 00:00:00 2001 From: Barthelemy Date: Fri, 8 Mar 2019 13:47:57 +0100 Subject: [PATCH 3/3] missing doxygen --- Common/Utils/include/CommonUtils/StringUtils.h | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Common/Utils/include/CommonUtils/StringUtils.h b/Common/Utils/include/CommonUtils/StringUtils.h index d91d9e8dbf02b..e51682ec79ada 100644 --- a/Common/Utils/include/CommonUtils/StringUtils.h +++ b/Common/Utils/include/CommonUtils/StringUtils.h @@ -24,7 +24,7 @@ namespace utils // Code for trimming coming from https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring /** - * trim from start (in place) + * Trim from start (in place) * @param s */ static inline void ltrim(std::string& s) @@ -34,7 +34,7 @@ static inline void ltrim(std::string& s) })); } -/** trim from end (in place) +/** Trim from end (in place) * * @param s */ @@ -47,7 +47,7 @@ static inline void rtrim(std::string& s) } /** - * trim from both ends (in place) + * Trim from both ends (in place) * @param s */ static inline void trim(std::string& s) @@ -56,14 +56,22 @@ static inline void trim(std::string& s) rtrim(s); } -// trim from start (copying) +/** + * Trim from start (copying) + * @param s + * @return + */ static inline std::string ltrim_copy(std::string s) { ltrim(s); return s; } -// trim from end (copying) +/** + * Trim from end (copying) + * @param s + * @return + */ static inline std::string rtrim_copy(std::string s) { rtrim(s);