On 03/16/2010 03:05 PM, Denys Vlasenko wrote:
+static vector_string_t split(const std::string&s, char delim)
+{
- std::stringstream ss(s);
- std::string item;
- std::vectorstd::string elems;
- while (std::getline(ss, item, delim))
- elems.push_back(item);
- return elems;
+}
Use of C++ streams usually adds on the order of 10k of code.
We need to talk later whether we want to go entirely C.
For now, personally I try to minimize usage of C++ constructs.
Here, looks like you can as easily perform splitting using, say, loop
with strchr('\n') inside.
Agreed. I rewrote this routine to use strtok_r.
Eh, since I am late in commenting, I can't ask *you* to do it a bit
differently, I guess I have to propose the my own patch on top.
Here is it.
Sometime ago we talked about const string parameters and decided
to generally use const char*, not const string&. The patch below
uses that convention. It also fixes a check for enabled plugins
and does a fem other small tweaks.
Please review...
--
vda
diff -urpN abrt/src/CLI/report.cpp abrt.1/src/CLI/report.cpp
--- abrt/src/CLI/report.cpp 2010-03-18 13:33:59.000000000 +0100
+++ abrt.1/src/CLI/report.cpp 2010-03-18 13:58:31.841874936 +0100
@@ -431,29 +431,24 @@ static void read_from_stdin(const char *
if (NULL == fgets(result, result_size, stdin))
result[0] = '\0';
// Remove the newline from the login.
- char *newline = strchr(result, '\n');
- if (newline)
- *newline = '\0';
+ strchrnul(result, '\n')[0] = '\0';
}
/** Splits a string into substrings using chosen delimiters.
* @param delim
- * Specifies a set of characters that delimit the
+ * Specifies a set of characters that delimit the
* tokens in the parsed string
*/
-static vector_string_t split(const std::string &s, const char *delim)
+static vector_string_t split(const char *s, const char delim)
{
std::vector
std::string elems;
- char str[s.length() + 1];
- /* str is modified by the following strtok_r,
- so s.c_str() cannot be used directly */
- strcpy(str, s.c_str());
- char *saveptr, *token;
- token = strtok_r(str, delim, &saveptr);
- while (token != NULL)
+ while (1)
{
- elems.push_back(token);
- token = strtok_r(NULL, delim, &saveptr);
+ const char *end = strchrnul(s, delim);
+ elems.push_back(std::string(s, end - s));
+ if (*end == '\0')
+ break;
+ s = end + 1;
}
return elems;
}
@@ -499,7 +494,7 @@ static vector_string_t get_enabled_repor
}
/* Reporters found, now parse the list. */
- vector_string_t reporter_vec = split(reporters_iter->second, ",");
+ vector_string_t reporter_vec = split(reporters_iter->second.c_str(), ',');
// Get informations about all plugins.
map_map_string_t plugins = call_GetPluginsInfo();
@@ -508,7 +503,7 @@ static vector_string_t get_enabled_repor
for (it = plugins.begin(); it != itend; ++it)
{
// Skip disabled plugins.
- if (0 != strcmp(it->second["Enabled"].c_str(), "yes"))
+ if (string_to_bool(it->second["Enabled"].c_str()) != true)
continue;
// Skip nonReporter plugins.
if (0 != strcmp(it->second["Type"].c_str(), "Reporter"))