I’m trying to add some functionality to dunst, a small notification system. I’ve never really done anything in C, but I did manage to do basically what I want in a small file:
#include<stdio.h>
#include<string.h>
main()
{
char str1[128];
char str2[128];
sprintf(str1,"Hello");
sprintf(str2,"world!");
printf("%s %s\n",str1,str2);
}
strcat changes the first string given, which isn’t what I want. In the program, it calculates width for the window by the text given, and I want to append some text(hidden) onto the notification message (cur_msg->msg).
Here’s what the code looks like:
#define MAX(a,b) ((a) > (b) ? (a) : (b))
void
drawmsg(void) {
int width, x, y, height, drawn_msg_count, i;
unsigned int len = list_len(msgqueue);
msg_queue_t *cur_msg = msgqueue;
char hidden[128];
int hidden_count = 0;
int hidden_color_idx = NORM;
dc->x = 0;
dc->y = 0;
dc->h = 0;
/* a height of 0 doesn't make sense, so we define it as 1 */
if(geometry.h == 0) {
geometry.h = 1;
}
height = MIN(geometry.h, len);
drawn_msg_count = height;
hidden_count = len - height;
hidden_count = indicate_hidden && hidden_count > 0 ? hidden_count : 0;
sprintf(hidden, "(%d more)", hidden_count);
if(hidden_count && !single_line)
height++;
if(geometry.mask & WidthValue) {
if(geometry.w == 0) {
width = 0;
for(i = 0; i < height; i++){
width = MAX(width, textw(dc, cur_msg->msg));
if(hidden_count && !single_line)
width = MAX(width, textw(dc,("%s %s",cur_msg->msg,hidden)));
cur_msg = cur_msg->next;
}
} else {
width = geometry.w;
}
} else {
width = scr.dim.w;
}
cur_msg = msgqueue;
if(geometry.mask & XNegative) {
x = (scr.dim.x + (scr.dim.w - width)) + geometry.x;
} else {
x = scr.dim.x + geometry.x;
}
if(geometry.mask & YNegative) {
y = (scr.dim.h + geometry.y) - height*font_h;
} else {
y = 0 + geometry.y;
}
resizedc(dc, width, height*font_h);
XResizeWindow(dc->dpy, win, width, height*font_h);
drawrect(dc, 0, 0, width, height*font_h, True, colors[NORM]->BG);
for(i = 0; i < drawn_msg_count; i++) {
if(cur_msg->start == 0)
cur_msg->start = now;
drawrect(dc, 0, dc->y, width, font_h, True, cur_msg->colors->BG);
if(hidden_count && single_line){
drawtext(dc, ("%s %s",cur_msg->msg,hidden), cur_msg->colors);
} else {
drawtext(dc, cur_msg->msg, cur_msg->colors);
}
dc->y += font_h;
hidden_color_idx = cur_msg->urgency;
cur_msg = cur_msg->next;
}
if(hidden_count && !single_line) {
drawrect(dc, 0, dc->y, width, font_h, True, colors[NORM]->BG);
drawtext(dc, hidden, colors[hidden_color_idx]);
dc->y += font_h;
}
XMoveWindow(dc->dpy, win, x, y);
mapdc(dc, win, width, height*font_h);
}
This is using it as a comma operator from what I’m guessing, and I’d like to use it like printf does, to replace the %s with the strings :/
How do I make it do this properly? If I can get it I think I can get the changes I want to work.
Feel free to shorten the code up, I don’t know what I really need to keep in and what’s okay to remove if I want some help on this.
Did not really read through your code, but it did definitely sound like
sprintto me.int sprintf ( char * str, const char * format, ... );basically printf for strings. Will take the format operators and throw it all together into str.Documentation