Debian Codegraph @

libcdata/libcdata_btree_values_list.c

raw symbols SCIP
1 /*
2 * Balanced tree values list functions
3 *
4 * Copyright (C) 2006-2024, Joachim Metz <[email protected]>
5 *
6 * Refer to AUTHORS for acknowledgements.
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22 #include <common.h>
23 #include <memory.h>
24 #include <types.h>
25
26 #include "libcdata_btree_values_list.h"
27 #include "libcdata_libcerror.h"
28 #include "libcdata_list.h"
29 #include "libcdata_list_element.h"
30 #include "libcdata_types.h"
31
32 /* Frees a B-tree values list
33 * Returns 1 if successful or -1 on error
34 */
35 int libcdata_btree_values_list_free(
36 libcdata_list_t **values_list,
37 libcerror_error_t **error )
38 {
39 static char *function = "libcdata_btree_values_list_free";
40 int result = 1;
41
42 if( values_list == NULL )
43 {
44 libcerror_error_set(
45 error,
46 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
47 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
48 "%s: invalid values list.",
49 function );
50
51 return( -1 );
52 }
53 if( libcdata_list_free(
54 values_list,
55 NULL,
56 error ) != 1 )
57 {
58 libcerror_error_set(
59 error,
60 LIBCERROR_ERROR_DOMAIN_RUNTIME,
61 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
62 "%s: unable to free values list.",
63 function );
64
65 result = -1;
66 }
67 return( result );
68 }
69
70 /* Removes an element from the values list
71 * Returns 1 if successful or -1 on error
72 */
73 int libcdata_btree_values_list_remove_element(
74 libcdata_list_t *values_list,
75 libcdata_list_element_t **values_list_element,
76 libcerror_error_t **error )
77 {
78 static char *function = "libcdata_btree_values_list_remove_element";
79
80 if( values_list == NULL )
81 {
82 libcerror_error_set(
83 error,
84 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
85 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
86 "%s: invalid values list.",
87 function );
88
89 return( -1 );
90 }
91 if( values_list_element == NULL )
92 {
93 libcerror_error_set(
94 error,
95 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
96 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
97 "%s: invalid values list element.",
98 function );
99
100 return( -1 );
101 }
102 if( libcdata_list_remove_element(
103 values_list,
104 *values_list_element,
105 error ) != 1 )
106 {
107 libcerror_error_set(
108 error,
109 LIBCERROR_ERROR_DOMAIN_RUNTIME,
110 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
111 "%s: unable to remove element from values list.",
112 function );
113
114 return( -1 );
115 }
116 if( libcdata_list_element_free(
117 values_list_element,
118 NULL,
119 error ) != 1 )
120 {
121 libcerror_error_set(
122 error,
123 LIBCERROR_ERROR_DOMAIN_RUNTIME,
124 LIBCERROR_RUNTIME_ERROR_FINALIZE_FAILED,
125 "%s: unable to free element.",
126 function );
127
128 return( -1 );
129 }
130 return( 1 );
131 }
132
133 /* Replaces the values list element with the previous element
134 * If available replacement_value is set to the value of the previous element
135 * Returns 1 if successful or -1 on error
136 */
137 int libcdata_btree_values_list_replace_element_with_previous(
138 libcdata_list_t *values_list,
139 libcdata_list_element_t **values_list_element,
140 intptr_t **replacement_value,
141 libcerror_error_t **error )
142 {
143 libcdata_list_element_t *previous_element = NULL;
144 static char *function = "libcdata_btree_values_list_replace_element_with_previous";
145
146 if( values_list == NULL )
147 {
148 libcerror_error_set(
149 error,
150 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
151 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
152 "%s: invalid values list.",
153 function );
154
155 return( -1 );
156 }
157 if( values_list_element == NULL )
158 {
159 libcerror_error_set(
160 error,
161 LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
162 LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
163 "%s: invalid values list element.",
164 function );
165
166 return( -1 );
167 }
168 if( libcdata_list_element_get_previous_element(
169 *values_list_element,
170 &previous_element,
171 error ) != 1 )
172 {
173 libcerror_error_set(
174 error,
175 LIBCERROR_ERROR_DOMAIN_RUNTIME,
176 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
177 "%s: unable to retrieve previous element from values list element.",
178 function );
179
180 return( -1 );
181 }
182 if( previous_element != NULL )
183 {
184 if( libcdata_list_element_get_value(
185 previous_element,
186 replacement_value,
187 error ) != 1 )
188 {
189 libcerror_error_set(
190 error,
191 LIBCERROR_ERROR_DOMAIN_RUNTIME,
192 LIBCERROR_RUNTIME_ERROR_GET_FAILED,
193 "%s: unable to retrieve value from previous element.",
194 function );
195
196 return( -1 );
197 }
198 }
199 if( libcdata_btree_values_list_remove_element(
200 values_list,
201 values_list_element,
202 error ) != 1 )
203 {
204 libcerror_error_set(
205 error,
206 LIBCERROR_ERROR_DOMAIN_RUNTIME,
207 LIBCERROR_RUNTIME_ERROR_REMOVE_FAILED,
208 "%s: unable to remove element from values list.",
209 function );
210
211 return( -1 );
212 }
213 *values_list_element = previous_element;
214
215 return( 1 );
216 }
217