Skip to content

Commit

Permalink
refactor: Move message init queue to separate file
Browse files Browse the repository at this point in the history
This functionality was awkwardly placed between main.c and toxic.c.
Now it resides on its own as a stand-alone unit.

Also improved documentation and naming scheme, and added some
extra null checks.
  • Loading branch information
JFreegman committed Mar 2, 2024
1 parent f4148ae commit b3efee9
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 123 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ LDFLAGS += ${USER_LDFLAGS}

OBJ = autocomplete.o avatars.o bootstrap.o chat.o chat_commands.o conference.o configdir.o curl_util.o execute.o
OBJ += file_transfers.o friendlist.o global_commands.o conference_commands.o groupchats.o groupchat_commands.o help.o
OBJ += input.o line_info.o log.o main.o message_queue.o misc_tools.o name_lookup.o notify.o prompt.o qr_code.o settings.o
OBJ += term_mplex.o toxic.o toxic_strings.o windows.o
OBJ += init_queue.o input.o line_info.o log.o main.o message_queue.o misc_tools.o name_lookup.o notify.o prompt.o qr_code.o
OBJ += settings.o term_mplex.o toxic.o toxic_strings.o windows.o

# Check if debug build is enabled
RELEASE := $(shell if [ -z "$(ENABLE_RELEASE)" ] || [ "$(ENABLE_RELEASE)" = "0" ] ; then echo disabled ; else echo enabled ; fi)
Expand Down
81 changes: 81 additions & 0 deletions src/init_queue.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* init_queue.c
*
* Copyright (C) 2024 Toxic All Rights Reserved.
*
* This file is part of Toxic. Toxic is free software licensed
* under the GNU General Public License 3.0.
*/

#include "init_queue.h"

#include <stdint.h>
#include <stdlib.h>

#include "line_info.h"
#include "toxic.h"
#include "toxic_constants.h"
#include "windows.h"

Init_Queue *init_queue_new(void)
{
Init_Queue *init_q = (Init_Queue *) calloc(1, sizeof(Init_Queue));
return init_q;
}

void init_queue_free(Init_Queue *init_q)
{
if (init_q == NULL) {
return;
}

for (uint16_t i = 0; i < init_q->count; ++i) {
free(init_q->messages[i]);
}

free(init_q->messages);
free(init_q);
}

void init_queue_print(const Init_Queue *init_q, ToxWindow *window, const Client_Config *c_config)
{
if (init_q == NULL) {
return;
}

for (uint16_t i = 0; i < init_q->count; ++i) {
line_info_add(window, c_config, NULL, NULL, NULL, SYS_MSG, 0, 0, "%s", init_q->messages[i]);
}
}

void init_queue_add(Init_Queue *init_q, const char *message, ...)
{
if (init_q == NULL) {
return;
}

char format_message[MAX_STR_SIZE] = {0};

va_list args;
va_start(args, message);
vsnprintf(format_message, sizeof(format_message), message, args);
va_end(args);

const uint16_t i = init_q->count;

char **temp_messages = realloc(init_q->messages, sizeof(char *) * (i + 1));

if (temp_messages == NULL) {
exit_toxic_err(FATALERR_MEMORY, "Failed in init_queue_add");
}

temp_messages[i] = malloc(MAX_STR_SIZE);

if (temp_messages[i] == NULL) {
exit_toxic_err(FATALERR_MEMORY, "Failed in init_queue_add");
}

snprintf(temp_messages[i], MAX_STR_SIZE, "%s", format_message);

init_q->messages = temp_messages;
++init_q->count;
}
55 changes: 55 additions & 0 deletions src/init_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* init_queue.h
*
* Copyright (C) 2024 Toxic All Rights Reserved.
*
* This file is part of Toxic. Toxic is free software licensed
* under the GNU General Public License 3.0.
*/

#ifndef INIT_QUEUE_H
#define INIT_QUEUE_H

#include <stdint.h>

#include "toxic.h"
#include "windows.h"

typedef struct Init_Queue {
char **messages;
uint16_t count;
} Init_Queue;

/*
* Adds `message` to `init_q`.
*
* `init_queue_new()` must be called before using this function.
*
* If `init_q` is NULL this function has no effect.
*/
__attribute__((format(printf, 2, 3)))
void init_queue_add(Init_Queue *init_q, const char *message, ...);

/*
* Frees all memory associated with init_q including the init_q itself.
*
* If `init_q` is NULL this function has no effect.
*/
void init_queue_free(Init_Queue *init_q);

/*
* Prints all messages in `init_q` to `window`.
*
* If `init_q` is NULL this function has no effect.
*/
void init_queue_print(const Init_Queue *init_q, ToxWindow *window, const Client_Config *c_config);

/*
* Returns a new Init_Queue object on success.
* Returns NULL on memory allocation error.
*
* The caller is responsible for freeing the memory associated with the returned
* object with `init_queue_free()`.
*/
Init_Queue *init_queue_new(void);

#endif // INIT_QUEUE_H
Loading

0 comments on commit b3efee9

Please sign in to comment.