make use of asprintf

This commit is contained in:
Sascha Kruse 2012-10-08 08:33:11 +02:00
parent d9d2ccb3d4
commit fa3e498c4e
2 changed files with 22 additions and 30 deletions

51
dunst.c
View File

@ -410,34 +410,25 @@ void update_draw_txt_buf(notification * n, int max_width)
while(isspace(*msg)) while(isspace(*msg))
msg++; msg++;
if (!n->draw_txt_buf.txt) { if (n->draw_txt_buf.txt)
int dup_len = strlen(" () ") + digit_count(INT_MAX); free(n->draw_txt_buf.txt);
int msg_len = strlen(msg) + 2; /* 2 == surrounding spaces */
int age_len = strlen(" ( h 60m 60s old ) ")
+ digit_count(INT_MAX); /* could be INT_MAX hours */
int line_length = dup_len + msg_len + age_len;
line_length += sizeof(" ( more ) ") + digit_count(INT_MAX); char *buf;
n->draw_txt_buf.txt = calloc(line_length, sizeof(char));
n->draw_txt_buf.bufsize = line_length;
}
char *buf = n->draw_txt_buf.txt;
int bufsize = n->draw_txt_buf.bufsize;
char *next = buf;
memset(buf, '\0', bufsize);
/* print dup_count */ /* print dup_count */
if (n->dup_count > 0) { if (n->dup_count > 0) {
snprintf(next, bufsize - strlen(buf), "(%d) ", n->dup_count); asprintf(&buf, "(%d)", n->dup_count);
next = buf + strlen(buf); } else {
buf = strdup("");
} }
/* print msg */ /* print msg */
strncat(buf, msg, bufsize - strlen(buf)); {
next = buf + strlen(buf); char *new_buf;
asprintf(&new_buf, "%s %s", buf, msg);
free(buf);
buf = new_buf;
}
/* print age */ /* print age */
int hours, minutes, seconds; int hours, minutes, seconds;
@ -448,20 +439,22 @@ void update_draw_txt_buf(notification * n, int max_width)
minutes = t_delta / 60 % 60; minutes = t_delta / 60 % 60;
seconds = t_delta % 60; seconds = t_delta % 60;
char *new_buf;
if (hours > 0) { if (hours > 0) {
snprintf(next, bufsize - strlen(buf), asprintf(&new_buf, "%s (%dh %dm %ds old)", buf, hours,
" (%dh %dm %ds old)", hours, minutes, seconds); minutes, seconds);
} else if (minutes > 0) { } else if (minutes > 0) {
snprintf(next, bufsize - strlen(buf), " (%dm %ds old)", asprintf(&new_buf, "%s (%dm %ds old)", buf, minutes, seconds);
minutes, seconds);
} else { } else {
snprintf(next, bufsize - strlen(buf), " (%ds old)", asprintf(&new_buf, "%s (%ds old)", buf, seconds);
seconds);
} }
free(buf);
buf = new_buf;
} }
n->draw_txt_buf.line_count = n->draw_txt_buf.line_count = do_word_wrap(buf, max_width);
do_word_wrap(n->draw_txt_buf.txt, max_width); n->draw_txt_buf.txt = buf;
} }
char *draw_txt_get_line(draw_txt * dt, int line) char *draw_txt_get_line(draw_txt * dt, int line)

View File

@ -33,7 +33,6 @@ typedef struct _screen_info {
typedef struct _draw_txt { typedef struct _draw_txt {
char *txt; char *txt;
int line_count; int line_count;
int bufsize;
} draw_txt; } draw_txt;
typedef struct _notification { typedef struct _notification {