Rework constructors/destructors

This commit is contained in:
Přemysl Eric Janouch 2017-06-22 22:45:25 +02:00
parent 68bc297809
commit a2611cdc3c
Signed by: p
GPG Key ID: B715679E3A361BE6
3 changed files with 36 additions and 61 deletions

View File

@ -1467,15 +1467,12 @@ struct formatter
size_t items_alloc; ///< Items allocated size_t items_alloc; ///< Items allocated
}; };
static void static struct formatter
formatter_init (struct formatter *self, formatter_make (struct app_context *ctx, struct server *s)
struct app_context *ctx, struct server *s)
{ {
memset (self, 0, sizeof *self); struct formatter self = { .ctx = ctx, .s = s };
self->ctx = ctx; self.items = xcalloc (sizeof *self.items, (self.items_alloc = 16));
self->s = s; return self;
self->items = xcalloc (sizeof *self->items, (self->items_alloc = 16));
self->items_len = 0;
} }
static void static void
@ -3749,8 +3746,7 @@ buffer_update_time (struct app_context *ctx, time_t now, FILE *stream,
return; return;
} }
struct formatter f; struct formatter f = formatter_make (ctx, NULL);
formatter_init (&f, ctx, NULL);
formatter_add (&f, "#a#s\n", ATTR_DATE_CHANGE, buf); formatter_add (&f, "#a#s\n", ATTR_DATE_CHANGE, buf);
formatter_flush (&f, stream, flush_opts); formatter_flush (&f, stream, flush_opts);
// Flush the trailing formatting reset item // Flush the trailing formatting reset item
@ -3805,8 +3801,7 @@ buffer_line_display (struct app_context *ctx,
CALL (ctx->input, hide); CALL (ctx->input, hide);
struct formatter f; struct formatter f = formatter_make (ctx, NULL);
formatter_init (&f, ctx, NULL);
buffer_line_write_time (&f, line, stdout, 0); buffer_line_write_time (&f, line, stdout, 0);
// Ignore all formatting for messages coming from other buffers, that is // Ignore all formatting for messages coming from other buffers, that is
@ -3827,8 +3822,7 @@ static void
buffer_line_write_to_backlog (struct app_context *ctx, buffer_line_write_to_backlog (struct app_context *ctx,
struct buffer_line *line, FILE *log_file, int flush_opts) struct buffer_line *line, FILE *log_file, int flush_opts)
{ {
struct formatter f; struct formatter f = formatter_make (ctx, NULL);
formatter_init (&f, ctx, NULL);
buffer_line_write_time (&f, line, log_file, flush_opts); buffer_line_write_time (&f, line, log_file, flush_opts);
buffer_line_flush (line, &f, log_file, flush_opts); buffer_line_flush (line, &f, log_file, flush_opts);
} }
@ -3840,9 +3834,7 @@ buffer_line_write_to_log (struct app_context *ctx,
if (line->flags & BUFFER_LINE_SKIP_FILE) if (line->flags & BUFFER_LINE_SKIP_FILE)
return; return;
struct formatter f; struct formatter f = formatter_make (ctx, NULL);
formatter_init (&f, ctx, NULL);
struct tm current; struct tm current;
char buf[20]; char buf[20];
if (!gmtime_r (&line->when, &current)) if (!gmtime_r (&line->when, &current))
@ -3922,8 +3914,7 @@ log_full (struct app_context *ctx, struct server *s, struct buffer *buffer,
va_list ap; va_list ap;
va_start (ap, format); va_start (ap, format);
struct formatter f; struct formatter f = formatter_make (ctx, s);
formatter_init (&f, ctx, s);
formatter_addv (&f, format, &ap); formatter_addv (&f, format, &ap);
log_formatter (ctx, buffer, flags, &f); log_formatter (ctx, buffer, flags, &f);
@ -4105,8 +4096,7 @@ buffer_remove (struct app_context *ctx, struct buffer *buffer)
static void static void
buffer_print_read_marker (struct app_context *ctx, FILE *stream, int flush_opts) buffer_print_read_marker (struct app_context *ctx, FILE *stream, int flush_opts)
{ {
struct formatter f; struct formatter f = formatter_make (ctx, NULL);
formatter_init (&f, ctx, NULL);
formatter_add (&f, "#a-- -- -- ---\n", ATTR_READ_MARKER); formatter_add (&f, "#a-- -- -- ---\n", ATTR_READ_MARKER);
formatter_flush (&f, stream, flush_opts); formatter_flush (&f, stream, flush_opts);
// Flush the trailing formatting reset item // Flush the trailing formatting reset item
@ -6020,8 +6010,7 @@ irc_is_highlight (struct server *s, const char *message)
// Strip formatting from the message so that it doesn't interfere // Strip formatting from the message so that it doesn't interfere
// with nickname detection (color sequences in particular) // with nickname detection (color sequences in particular)
struct formatter f; struct formatter f = formatter_make (s->ctx, NULL);
formatter_init (&f, s->ctx, NULL);
formatter_parse_mirc (&f, message); formatter_parse_mirc (&f, message);
struct str stripped = str_make (); struct str stripped = str_make ();
@ -6576,8 +6565,7 @@ irc_handle_kick (struct server *s, const struct irc_message *msg)
if (buffer) if (buffer)
{ {
struct formatter f; struct formatter f = formatter_make (s->ctx, s);
formatter_init (&f, s->ctx, s);
formatter_add (&f, "#a<--#r #N #a#s#r #n", formatter_add (&f, "#a<--#r #N #a#s#r #n",
ATTR_PART, msg->prefix, ATTR_PART, "has kicked", target); ATTR_PART, msg->prefix, ATTR_PART, "has kicked", target);
if (message) if (message)
@ -6844,8 +6832,7 @@ irc_handle_part (struct server *s, const struct irc_message *msg)
if (buffer) if (buffer)
{ {
struct formatter f; struct formatter f = formatter_make (s->ctx, s);
formatter_init (&f, s->ctx, s);
formatter_add (&f, "#a<--#r #N #a#s#r #S", formatter_add (&f, "#a<--#r #N #a#s#r #S",
ATTR_PART, msg->prefix, ATTR_PART, "has left", channel_name); ATTR_PART, msg->prefix, ATTR_PART, "has left", channel_name);
if (message) if (message)
@ -6907,8 +6894,7 @@ irc_handle_ctcp_request (struct server *s,
return; return;
} }
struct formatter f; struct formatter f = formatter_make (s->ctx, s);
formatter_init (&f, s->ctx, s);
formatter_add (&f, "CTCP requested by #n", msg->prefix); formatter_add (&f, "CTCP requested by #n", msg->prefix);
if (irc_is_channel (s, irc_skip_statusmsg (s, target))) if (irc_is_channel (s, irc_skip_statusmsg (s, target)))
formatter_add (&f, " (to #S)", target); formatter_add (&f, " (to #S)", target);
@ -7001,8 +6987,7 @@ static void
log_quit (struct server *s, log_quit (struct server *s,
struct buffer *buffer, const char *prefix, const char *reason) struct buffer *buffer, const char *prefix, const char *reason)
{ {
struct formatter f; struct formatter f = formatter_make (s->ctx, s);
formatter_init (&f, s->ctx, s);
formatter_add (&f, "#a<--#r #N #a#s#r", formatter_add (&f, "#a<--#r #N #a#s#r",
ATTR_PART, prefix, ATTR_PART, "has quit"); ATTR_PART, prefix, ATTR_PART, "has quit");
if (reason) if (reason)
@ -13868,8 +13853,7 @@ format_input_and_die (struct app_context *ctx)
char buf[513]; char buf[513];
while (fgets (buf, sizeof buf, stdin)) while (fgets (buf, sizeof buf, stdin))
{ {
struct formatter f; struct formatter f = formatter_make (ctx, NULL);
formatter_init (&f, ctx, NULL);
formatter_add (&f, "#m", buf); formatter_add (&f, "#m", buf);
formatter_flush (&f, stdout, FLUSH_OPT_NOWRAP); formatter_flush (&f, stdout, FLUSH_OPT_NOWRAP);
formatter_free (&f); formatter_free (&f);

20
kike.c
View File

@ -1739,16 +1739,14 @@ struct mode_processor
struct strv *output_params; ///< Similarly for "*_params" struct strv *output_params; ///< Similarly for "*_params"
}; };
static void static struct mode_processor
mode_processor_init (struct mode_processor *self) mode_processor_make (void)
{ {
memset (self, 0, sizeof *self); return (struct mode_processor)
{
self->added = str_make (); .added = str_make (), .added_params = strv_make (),
self->removed = str_make (); .removed = str_make (), .removed_params = strv_make (),
};
self->added_params = strv_make ();
self->removed_params = strv_make ();
} }
static void static void
@ -1990,9 +1988,7 @@ static void
irc_handle_chan_mode_change irc_handle_chan_mode_change
(struct client *c, struct channel *chan, char *params[]) (struct client *c, struct channel *chan, char *params[])
{ {
struct mode_processor p; struct mode_processor p = mode_processor_make ();
mode_processor_init (&p);
p.params = params; p.params = params;
p.channel = chan; p.channel = chan;
p.c = c; p.c = c;

View File

@ -83,11 +83,10 @@ struct plugin
struct str write_buffer; ///< Output yet to be sent out struct str write_buffer; ///< Output yet to be sent out
}; };
static void static struct plugin *
plugin_init (struct plugin *self) plugin_new (void)
{ {
memset (self, 0, sizeof *self); struct plugin *self = xcalloc (1, sizeof *self);
self->pid = -1; self->pid = -1;
self->queued_output = str_make (); self->queued_output = str_make ();
@ -95,10 +94,11 @@ plugin_init (struct plugin *self)
self->read_buffer = str_make (); self->read_buffer = str_make ();
self->write_fd = -1; self->write_fd = -1;
self->write_buffer = str_make (); self->write_buffer = str_make ();
return self;
} }
static void static void
plugin_free (struct plugin *self) plugin_destroy (struct plugin *self)
{ {
soft_assert (self->pid == -1); soft_assert (self->pid == -1);
free (self->name); free (self->name);
@ -113,6 +113,8 @@ plugin_free (struct plugin *self)
if (!self->initialized) if (!self->initialized)
str_free (&self->queued_output); str_free (&self->queued_output);
free (self);
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -192,13 +194,8 @@ bot_context_free (struct bot_context *self)
str_free (&self->read_buffer); str_free (&self->read_buffer);
// TODO: terminate the plugins properly before this is called // TODO: terminate the plugins properly before this is called
struct plugin *link, *tmp; LIST_FOR_EACH (struct plugin, link, self->plugins)
for (link = self->plugins; link; link = tmp) plugin_destroy (link);
{
tmp = link->next;
plugin_free (link);
free (link);
}
if (self->irc_fd != -1) if (self->irc_fd != -1)
{ {
@ -1099,8 +1096,7 @@ plugin_launch (struct bot_context *ctx, const char *name, struct error **e)
xclose (stdin_pipe[0]); xclose (stdin_pipe[0]);
xclose (stdout_pipe[1]); xclose (stdout_pipe[1]);
struct plugin *plugin = xmalloc (sizeof *plugin); struct plugin *plugin = plugin_new ();
plugin_init (plugin);
plugin->ctx = ctx; plugin->ctx = ctx;
plugin->pid = pid; plugin->pid = pid;
plugin->name = xstrdup (name); plugin->name = xstrdup (name);
@ -1876,8 +1872,7 @@ on_plugin_death (struct plugin *plugin, int status)
plugin->read_fd = -1; plugin->read_fd = -1;
LIST_UNLINK (ctx->plugins, plugin); LIST_UNLINK (ctx->plugins, plugin);
plugin_free (plugin); plugin_destroy (plugin);
free (plugin);
// Living child processes block us from quitting // Living child processes block us from quitting
try_finish_quit (ctx); try_finish_quit (ctx);