15 #include "eckit/utils/StringTools.h"
19 #include "eckit/exception/Exceptions.h"
20 #include "eckit/filesystem/PathName.h"
21 #include "eckit/io/FileHandle.h"
22 #include "eckit/log/CodeLocation.h"
23 #include "eckit/log/Log.h"
24 #include "eckit/utils/Translator.h"
25 #include "eckit/utils/Regex.h"
30 using namespace eckit;
34 std::vector<std::string> StringTool::readLines(
const PathName fileName,
bool logging)
36 std::string s = readFile(fileName, logging);
37 return StringTools::split(
"\n", s);
40 std::string StringTool::readFile(
const PathName fileName,
bool logging)
42 const size_t CHUNK_SIZE = 1024;
43 char buffer[CHUNK_SIZE];
45 FileHandle
f(fileName);
49 size_t read, totalRead = 0;
51 while ( (read =
f.read(buffer,
sizeof(buffer) /
sizeof(
char))) > 0 )
54 ret.append(std::string(
static_cast<char*
>(buffer), read));
58 Log::info() <<
"Read " << totalRead <<
" bytes from file " << fileName <<
"[" << ret <<
"]" << std::endl;
64 int StringTool::shell(std::string cmd,
const CodeLocation& where,
bool assertSuccess)
66 std::string
c =
"/bin/sh -c \"" + cmd +
"\"";
68 Log::info() <<
"Executing '" +
c +
"' ";
69 Log::info() <<
" " << where.file() <<
" +" << where.line();
70 Log::info() << std::endl;
72 int rc = system(
c.c_str());
74 if (assertSuccess &&
rc != 0)
76 throw eckit::SeriousBug(std::string(
" \"") + cmd +
"\" failed. " + where.file() +
" +" + Translator<int, std::string>()(where.line()));
84 for (
size_t i = 0;
i < s.size(); ++
i)
90 bool StringTool::isInQuotes(
const std::string& value)
92 return value.size() > 1
93 && ((value[0] ==
'"' && value[value.size() - 1] ==
'"')
94 || (value[0] ==
'\'' && value[value.size() - 1] ==
'\''));
97 std::string StringTool::unQuote(
const std::string& value)
101 if (isInQuotes(value))
102 return value.substr(1, value.size() - 2);
106 std::string StringTool::double_as_string(
double m)
108 char buf[
sizeof(double) + 1];
109 memset(buf, 0,
sizeof(buf));
110 memcpy(buf,
reinterpret_cast<char *
>(&
m),
sizeof(
double));
111 return std::string(buf,
sizeof(
double));
114 double StringTool::cast_as_double(
const std::string& value)
116 char buf[
sizeof(double)];
117 memset(buf,
' ',
sizeof(
double));
119 ASSERT(value.size() <=
sizeof(
double));
121 strncpy(buf +
sizeof(
double) - value.size(), value.c_str(), value.size());
122 return *
reinterpret_cast<double *
>(buf);
133 double StringTool::translate(
const std::string& v)
135 return isInQuotes(v) ? cast_as_double(unQuote(v)) : Translator<std::string, double>()(v);
140 bool StringTool::match(
const std::string& regex,
const std::string& s)
142 return Regex(regex).match(s);
145 bool StringTool::matchAny(
const std::vector<std::string>& regs,
const std::string& s)
147 for (
size_t i = 0;
i < regs.size(); ++
i)
148 if (match(regs[
i], s))
153 ostream&
operator<<(std::ostream& s,
const std::vector<std::string>& st)
156 for (std::vector<std::string>::const_iterator it = st.begin(); it != st.end(); ++it)
170 case STRING:
return double_as_string(
d);
173 s <<
d;
return s.str();
176 ASSERT(0 &&
"Type not known.");
182 std::string StringTool::patchTimeForMars(
const std::string& ss)
185 if (v.size() == 5) v = std::string(
"0") + v;
190 Log::debug() <<
"StringTool::patchTimeForMars: stripping seconds from TIME: '"
191 << s <<
"' => '" << v <<
"'" << std::endl;
196 bool StringTool::isSelectStatement(
const std::string& s) {
return StringTool::match(
"select", eckit::StringTools::lower(
eckit::StringTools::trim(s))); }
std::string trim(const std::string &str)
ostream & operator<<(std::ostream &s, const std::vector< std::string > &st)