mirror of
https://github.com/Llewellynvdm/conky.git
synced 2024-06-10 19:22:20 +00:00
Refactor mysql.cc (#493)
* Refactor mysql.cc * Comparison of array temp not equal to a null pointer * Switch and compare to C NULL instead std::nullptr_t to avoid compile error * Use parentheses around addrs * Remove parentheses around addrs * Rebase
This commit is contained in:
parent
81fd3e2a67
commit
8dbb623255
22
src/mysql.cc
22
src/mysql.cc
|
@ -46,6 +46,7 @@ conky::simple_config_setting<std::string> db("mysql_db", "mysql", false);
|
|||
|
||||
void print_mysql(struct text_object *obj, char *p, int p_max_size) {
|
||||
MYSQL *conn = mysql_init(nullptr);
|
||||
MYSQL_RES *res = nullptr;
|
||||
|
||||
if (conn == nullptr) {
|
||||
NORM_ERR("Can't initialize MySQL");
|
||||
|
@ -57,22 +58,16 @@ void print_mysql(struct text_object *obj, char *p, int p_max_size) {
|
|||
password.get(*state).c_str(), db.get(*state).c_str(),
|
||||
port.get(*state), nullptr, 0)) {
|
||||
NORM_ERR("MySQL: %s", mysql_error(conn));
|
||||
mysql_close(conn);
|
||||
mysql_library_end();
|
||||
return;
|
||||
goto error;
|
||||
}
|
||||
if (mysql_query(conn, obj->data.s)) {
|
||||
NORM_ERR("MySQL: %s", mysql_error(conn));
|
||||
mysql_close(conn);
|
||||
mysql_library_end();
|
||||
return;
|
||||
goto error;
|
||||
}
|
||||
MYSQL_RES *res = mysql_use_result(conn);
|
||||
res = mysql_use_result(conn);
|
||||
if (res == nullptr) {
|
||||
NORM_ERR("MySQL: %s", mysql_error(conn));
|
||||
mysql_close(conn);
|
||||
mysql_library_end();
|
||||
return;
|
||||
goto error;
|
||||
}
|
||||
MYSQL_ROW row = mysql_fetch_row(res);
|
||||
if (row) {
|
||||
|
@ -80,7 +75,12 @@ void print_mysql(struct text_object *obj, char *p, int p_max_size) {
|
|||
} else {
|
||||
NORM_ERR("MySQL: '%s' returned no results", obj->data.s);
|
||||
}
|
||||
mysql_free_result(res);
|
||||
|
||||
error:
|
||||
if (nullptr != res) {
|
||||
mysql_free_result(res);
|
||||
}
|
||||
mysql_close(conn);
|
||||
mysql_library_end();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -250,7 +250,7 @@ void print_addrs(struct text_object *obj, char *p, int p_max_size) {
|
|||
|
||||
if (!ns) return;
|
||||
|
||||
if (nullptr != ns->addrs && strlen(ns->addrs) > 2) {
|
||||
if (0 != ns->addrs[0] && strlen(ns->addrs) > 2) {
|
||||
ns->addrs[strlen(ns->addrs) - 2] = 0; /* remove ", " from end of string */
|
||||
strncpy(p, ns->addrs, p_max_size);
|
||||
} else {
|
||||
|
|
|
@ -131,7 +131,7 @@ static void update_user_time(char *tty) {
|
|||
|
||||
tty_user_time(temp, tty);
|
||||
|
||||
if (temp != nullptr) {
|
||||
if (*temp != 0) {
|
||||
free_and_zero(current_info->users.ctime);
|
||||
current_info->users.ctime = (char *)malloc(text_buffer_size.get(*state));
|
||||
strncpy(current_info->users.ctime, temp, text_buffer_size.get(*state));
|
||||
|
@ -148,7 +148,7 @@ int update_users(void) {
|
|||
int t;
|
||||
users_alloc(current_info);
|
||||
user_name(temp);
|
||||
if (temp != nullptr) {
|
||||
if (*temp != 0) {
|
||||
free_and_zero(current_info->users.names);
|
||||
current_info->users.names = (char *)malloc(text_buffer_size.get(*state));
|
||||
strncpy(current_info->users.names, temp, text_buffer_size.get(*state));
|
||||
|
@ -168,7 +168,7 @@ int update_users(void) {
|
|||
}
|
||||
temp[0] = 0;
|
||||
user_term(temp);
|
||||
if (temp != nullptr) {
|
||||
if (*temp != 0) {
|
||||
free_and_zero(current_info->users.terms);
|
||||
current_info->users.terms = (char *)malloc(text_buffer_size.get(*state));
|
||||
strncpy(current_info->users.terms, temp, text_buffer_size.get(*state));
|
||||
|
@ -178,7 +178,7 @@ int update_users(void) {
|
|||
strncpy(current_info->users.terms, "broken", text_buffer_size.get(*state));
|
||||
}
|
||||
user_time(temp);
|
||||
if (temp != nullptr) {
|
||||
if (*temp != 0) {
|
||||
free_and_zero(current_info->users.times);
|
||||
current_info->users.times = (char *)malloc(text_buffer_size.get(*state));
|
||||
strncpy(current_info->users.times, temp, text_buffer_size.get(*state));
|
||||
|
|
Loading…
Reference in New Issue
Block a user