Line data Source code
1 : #include "precizer.h"
2 :
3 : /**
4 : * @brief Adds a new string to a dynamically allocated NULL-terminated array of strings.
5 : *
6 : * @details This function dynamically appends a string to an array of strings.
7 : * The array must be NULL-terminated to allow safe iteration.
8 : * It reallocates memory to accommodate the new entry, ensuring the array
9 : * remains NULL-terminated after adding the new string.
10 : *
11 : * @param array_ptr Pointer to the dynamic array of strings (NULL-terminated).
12 : * The array will be reallocated to fit the new string.
13 : * @param new_string The string to append to the array.
14 : *
15 : * @return SUCCESS if the string was successfully added.
16 : * FAILURE if memory allocation fails at any point.
17 : *
18 : * @note The provided array must be initialized properly as either NULL or a valid
19 : * NULL-terminated array of strings before calling this function.
20 : *
21 : * @warning Memory allocated by this function is freed by calling the free_config()
22 : * function upon program termination.
23 : */
24 132 : Return add_string_to_array(
25 : char ***array_ptr,
26 : const char *new_string)
27 : {
28 : /// The status that will be passed to return() before exiting.
29 : /// By default, the function worked without errors.
30 132 : Return status = SUCCESS;
31 :
32 132 : if(array_ptr == NULL)
33 : {
34 0 : report("Invalid parameter: array_ptr is NULL");
35 0 : provide(FAILURE);
36 : }
37 :
38 132 : if(new_string == NULL)
39 : {
40 0 : report("Invalid parameter: new_string is NULL");
41 0 : provide(FAILURE);
42 : }
43 :
44 : // Calculate the size of the current string array
45 132 : size_t size = 0;
46 132 : char **array = *array_ptr;
47 :
48 132 : if(array != NULL)
49 : {
50 101 : while(array[size] != NULL)
51 : {
52 51 : size++;
53 : }
54 : }
55 :
56 : // Increase the size of the array by 1 and copy existing strings into it
57 : // Use a temporary variable to realloc the array
58 132 : char **tmp = (char **)realloc(array,(size + 2) * sizeof(char *));
59 :
60 132 : if(tmp == NULL)
61 : {
62 : // Reallocation failed, free the original array
63 0 : report("Memory allocation failed, requested size: %zu bytes",(size + 2) * sizeof(char *));
64 :
65 0 : for(size_t i = 0; i < size; i++)
66 : {
67 0 : free(array[i]);
68 : }
69 0 : free(array);
70 0 : provide(FAILURE);
71 : } else {
72 132 : array = tmp;
73 : }
74 :
75 : // Allocate memory for the new string and copy the new string into it
76 132 : array[size] = (char *)malloc((strlen(new_string) + 1) * sizeof(char));
77 :
78 132 : if(array[size] == NULL)
79 : {
80 0 : report("Memory allocation failed, requested size: %zu bytes",(strlen(new_string) + 1) * sizeof(char));
81 :
82 : // Reallocation failed, free the original array
83 0 : for(size_t i = 0; i < size; i++)
84 : {
85 0 : free(array[i]);
86 : }
87 0 : free(array);
88 0 : provide(FAILURE);
89 : }
90 :
91 132 : strcpy(array[size],new_string);
92 :
93 : // Set the last element to NULL
94 132 : array[size + 1] = NULL;
95 :
96 : // Update the array pointer in the calling function
97 132 : *array_ptr = array;
98 :
99 132 : provide(status);
100 : }
|