-
-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Move message init queue to separate file
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
Showing
6 changed files
with
198 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.