Skip to content

Commit 68fce24

Browse files
committed
Refactor some utility code
1 parent ece98b1 commit 68fce24

File tree

3 files changed

+57
-59
lines changed

3 files changed

+57
-59
lines changed

src/util.hpp

Lines changed: 0 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,8 @@ limitations under the License.
1818
#define _util_
1919

2020

21-
#include <cstdlib>
22-
#include <iomanip>
2321
#include <iostream>
2422
#include <sstream>
25-
#include <string>
26-
#include <vector>
2723
#include "types.hpp"
2824

2925

@@ -47,57 +43,4 @@ struct istreambuf : public std::basic_streambuf<T, std::char_traits<T> >
4743
}
4844
};
4945

50-
inline int tokenizeCSV(std::string& s, std::vector<std::string>& tokens, char delim = ',')
51-
{
52-
std::stringstream ss;
53-
char prv = 0;
54-
55-
for (auto& c : s) {
56-
if (c == delim) {
57-
if (prv != '\\') {
58-
std::string tk = ss.str();
59-
tokens.push_back(tk);
60-
ss.str("");
61-
prv = 0;
62-
continue;
63-
}
64-
}
65-
66-
ss << c;
67-
prv = c;
68-
}
69-
70-
std::string tk = ss.str();
71-
72-
if (tk.size() > 0)
73-
tokens.push_back(tk);
74-
75-
return tokens.size();
76-
}
77-
78-
inline std::string formatSize(const std::string& input)
79-
{
80-
double size = atof(input.c_str());;
81-
std::stringstream ss;
82-
std::string s = input;
83-
84-
if (size >= double(1 << 30)) {
85-
size /= double(1024 * 1024 * 1024);
86-
ss << std::fixed << std::setprecision(2) << size << " GiB";
87-
s = ss.str();
88-
}
89-
else if (size >= double(1 << 20)) {
90-
size /= double(1024 * 1024);
91-
ss << std::fixed << std::setprecision(2) << size << " MiB";
92-
s = ss.str();
93-
}
94-
else if (size >= double(1 << 10)) {
95-
size /= double(1024);
96-
ss << std::fixed << std::setprecision(2) << size << " KiB";
97-
s = ss.str();
98-
}
99-
100-
return s;
101-
}
102-
10346
#endif

src/util/Printer.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace kanzi
4545
#ifdef CONCURRENCY_ENABLED
4646
std::lock_guard<std::mutex> lock(_mtx);
4747
#endif
48-
(*_os) << msg ;
48+
(*_os) << msg;
4949
}
5050
}
5151

@@ -63,7 +63,7 @@ namespace kanzi
6363
#ifdef CONCURRENCY_ENABLED
6464
std::lock_guard<std::mutex> lock(_mtx);
6565
#endif
66-
(*_os) << msg ;
66+
(*_os) << msg;
6767
}
6868
}
6969

src/util/strings.hpp

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
#ifndef _strings_
1818
#define _strings_
1919

20+
#include <cstdlib>
21+
#include <iomanip>
2022
#include <sstream>
2123
#include <string>
2224
#include <vector>
@@ -88,5 +90,58 @@ inline void tokenize(const std::string& str, std::vector<std::string>& v, char t
8890
v.push_back(s);
8991
}
9092

93+
inline int tokenizeCSV(std::string& s, std::vector<std::string>& tokens, char delim = ',')
94+
{
95+
std::stringstream ss;
96+
char prv = 0;
97+
98+
for (auto& c : s) {
99+
if (c == delim) {
100+
if (prv != '\\') {
101+
std::string tk = ss.str();
102+
tokens.push_back(tk);
103+
ss.str("");
104+
prv = 0;
105+
continue;
106+
}
107+
}
108+
109+
ss << c;
110+
prv = c;
111+
}
112+
113+
std::string tk = ss.str();
114+
115+
if (tk.size() > 0)
116+
tokens.push_back(tk);
117+
118+
return tokens.size();
119+
}
120+
121+
inline std::string formatSize(const std::string& input)
122+
{
123+
double size = atof(input.c_str());
124+
std::stringstream ss;
125+
std::string s = input;
126+
127+
if (size >= double(1 << 30)) {
128+
size /= double(1024 * 1024 * 1024);
129+
ss << std::fixed << std::setprecision(2) << size << " GiB";
130+
s = ss.str();
131+
}
132+
else if (size >= double(1 << 20)) {
133+
size /= double(1024 * 1024);
134+
ss << std::fixed << std::setprecision(2) << size << " MiB";
135+
s = ss.str();
136+
}
137+
else if (size >= double(1 << 10)) {
138+
size /= double(1024);
139+
ss << std::fixed << std::setprecision(2) << size << " KiB";
140+
s = ss.str();
141+
}
142+
143+
return s;
144+
}
145+
91146
#endif
92147

0 commit comments

Comments
 (0)