Skip to content

Commit db51a56

Browse files
Barthelemyktf
authored andcommitted
CCDB api : Prevent whitespaces in file name (#1774)
The commit also introduces a StringUtils.h with trimming functions.
1 parent 56264a0 commit db51a56

5 files changed

Lines changed: 94 additions & 22 deletions

File tree

CCDB/src/CcdbApi.cxx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <chrono>
1919
#include <TMessage.h>
2020
#include <sstream>
21+
#include <CommonUtils/StringUtils.h>
2122

2223
namespace o2
2324
{
@@ -75,7 +76,9 @@ void CcdbApi::store(TObject* rootObject, std::string path, std::map<std::string,
7576
struct curl_slist* headerlist = nullptr;
7677
static const char buf[] = "Expect:";
7778
// todo : what is the correct file name ?
78-
string tmpFileName = string(rootObject->GetName()) + "_" + getTimestampString(getCurrentTimestamp()) + ".root";
79+
string objectName = string(rootObject->GetName());
80+
utils::trim(objectName);
81+
string tmpFileName = objectName + "_" + getTimestampString(getCurrentTimestamp()) + ".root";
7982
curl_formadd(&formpost,
8083
&lastptr,
8184
CURLFORM_COPYNAME, "send",

CCDB/test/testCcdbApi.cxx

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include <fcntl.h>
2828
#include <TH1F.h>
2929
#include <chrono>
30+
#include <CommonUtils/StringUtils.h>
3031

3132
using namespace std;
3233
using namespace o2::ccdb;
@@ -108,25 +109,6 @@ BOOST_AUTO_TEST_CASE(delete_test)
108109
BOOST_CHECK(h2 == nullptr);
109110
}
110111

111-
/// trim from start (in place)
112-
/// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
113-
static inline void ltrim(std::string& s)
114-
{
115-
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
116-
return !std::isspace(ch);
117-
}));
118-
}
119-
120-
/// trim from end (in place)
121-
/// https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
122-
static inline void rtrim(std::string& s)
123-
{
124-
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
125-
return !std::isspace(ch);
126-
}).base(),
127-
s.end());
128-
}
129-
130112
void countItems(const string& s, int& countObjects, int& countSubfolders)
131113
{
132114
countObjects = 0;
@@ -135,8 +117,8 @@ void countItems(const string& s, int& countObjects, int& countSubfolders)
135117
std::string line;
136118
bool subfolderMode = false;
137119
while (std::getline(ss, line, '\n')) {
138-
ltrim(line);
139-
rtrim(line);
120+
o2::utils::ltrim(line);
121+
o2::utils::rtrim(line);
140122
if (line.length() == 0) {
141123
continue;
142124
}

Common/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Set(HEADERS
1717
include/${MODULE_NAME}/BoostSerializer.h
1818
include/${MODULE_NAME}/ShmManager.h
1919
include/${MODULE_NAME}/RngHelper.h
20+
include/${MODULE_NAME}/StringUtils.h
2021
)
2122

2223
Set(LINKDEF src/CommonUtilsLinkDef.h)
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright CERN and copyright holders of ALICE O2. This software is
2+
// distributed under the terms of the GNU General Public License v3 (GPL
3+
// Version 3), copied verbatim in the file "COPYING".
4+
//
5+
// See http://alice-o2.web.cern.ch/license for full licensing information.
6+
//
7+
// In applying this license CERN does not waive the privileges and immunities
8+
// granted to it by virtue of its status as an Intergovernmental Organization
9+
// or submit itself to any jurisdiction.
10+
11+
///
12+
/// \file StringUtils.h
13+
/// \author Barthelemy von Haller
14+
///
15+
16+
#ifndef ALICEO2_STRINGUTILS_H
17+
#define ALICEO2_STRINGUTILS_H
18+
19+
namespace o2
20+
{
21+
namespace utils
22+
{
23+
24+
// Code for trimming coming from https://stackoverflow.com/questions/216823/whats-the-best-way-to-trim-stdstring
25+
26+
/**
27+
* Trim from start (in place)
28+
* @param s
29+
*/
30+
static inline void ltrim(std::string& s)
31+
{
32+
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) {
33+
return !std::isspace(ch);
34+
}));
35+
}
36+
37+
/** Trim from end (in place)
38+
*
39+
* @param s
40+
*/
41+
static inline void rtrim(std::string& s)
42+
{
43+
s.erase(std::find_if(s.rbegin(), s.rend(), [](int ch) {
44+
return !std::isspace(ch);
45+
}).base(),
46+
s.end());
47+
}
48+
49+
/**
50+
* Trim from both ends (in place)
51+
* @param s
52+
*/
53+
static inline void trim(std::string& s)
54+
{
55+
ltrim(s);
56+
rtrim(s);
57+
}
58+
59+
/**
60+
* Trim from start (copying)
61+
* @param s
62+
* @return
63+
*/
64+
static inline std::string ltrim_copy(std::string s)
65+
{
66+
ltrim(s);
67+
return s;
68+
}
69+
70+
/**
71+
* Trim from end (copying)
72+
* @param s
73+
* @return
74+
*/
75+
static inline std::string rtrim_copy(std::string s)
76+
{
77+
rtrim(s);
78+
return s;
79+
}
80+
81+
} // namespace utils
82+
} // namespace o2
83+
84+
#endif //ALICEO2_STRINGUTILS_H

cmake/O2Dependencies.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,12 @@ o2_define_bucket(
526526
fairmq_bucket
527527
pthread Core Tree XMLParser Hist Net RIO z
528528
${CURL_LIBRARIES}
529+
common_utils_bucket
529530

530531
INCLUDE_DIRECTORIES
531532
${FAIRROOT_INCLUDE_DIR}
532533
${ROOT_INCLUDE_DIR}
534+
${CMAKE_SOURCE_DIR}/Common/Utils/include
533535

534536
SYSTEMINCLUDE_DIRECTORIES
535537
${PROTOBUF_INCLUDE_DIR}

0 commit comments

Comments
 (0)