Branch data 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 : 258 : Return add_string_to_array(
25 : : char ***array_ptr,
26 : : const char *new_string)
27 : : {
28 : : /* Status returned by this function through provide()
29 : : Default value assumes successful completion */
30 : 258 : Return status = SUCCESS;
31 : :
32 [ - + ]: 258 : if(array_ptr == NULL)
33 : : {
34 : 0 : report("Invalid parameter: array_ptr is NULL");
35 : 0 : provide(FAILURE);
36 : : }
37 : :
38 [ - + ]: 258 : 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 : 258 : size_t size = 0;
46 : 258 : char **array = *array_ptr;
47 : :
48 [ + + ]: 258 : if(array != NULL)
49 : : {
50 [ + + ]: 191 : while(array[size] != NULL)
51 : : {
52 : 97 : 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 : 258 : char **tmp = (char **)realloc(array,(size + 2) * sizeof(char *));
59 : :
60 [ - + ]: 258 : 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 : free_string_array(array);
66 : 0 : *array_ptr = NULL;
67 : 0 : provide(FAILURE);
68 : : } else {
69 : 258 : array = tmp;
70 : : }
71 : :
72 : : // Allocate memory for the new string and copy the new string into it
73 : 258 : array[size] = (char *)malloc((strlen(new_string) + 1) * sizeof(char));
74 : :
75 [ - + ]: 258 : if(array[size] == NULL)
76 : : {
77 : 0 : report("Memory allocation failed, requested size: %zu bytes",(strlen(new_string) + 1) * sizeof(char));
78 : :
79 : : // Reallocation failed, free the original array
80 : 0 : free_string_array(array);
81 : 0 : *array_ptr = NULL;
82 : 0 : provide(FAILURE);
83 : : }
84 : :
85 : 258 : strcpy(array[size],new_string);
86 : :
87 : : // Set the last element to NULL
88 : 258 : array[size + 1] = NULL;
89 : :
90 : : // Update the array pointer in the calling function
91 : 258 : *array_ptr = array;
92 : :
93 : 258 : provide(status);
94 : : }
|