-
Notifications
You must be signed in to change notification settings - Fork 101
/
char_array.c
247 lines (206 loc) · 7.45 KB
/
char_array.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2018 Open Source Robotics Foundation, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <stdarg.h>
#include "rcutils/error_handling.h"
#include "rcutils/types/char_array.h"
#define MIN(a, b) ((a) < (b) ? (a) : (b))
rcutils_char_array_t
rcutils_get_zero_initialized_char_array(void)
{
static rcutils_char_array_t char_array = {
.buffer = NULL,
.owns_buffer = true,
.buffer_length = 0u,
.buffer_capacity = 0u
};
char_array.allocator = rcutils_get_zero_initialized_allocator();
return char_array;
}
rcutils_ret_t
rcutils_char_array_init(
rcutils_char_array_t * char_array,
size_t buffer_capacity,
const rcutils_allocator_t * allocator)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(char_array, RCUTILS_RET_ERROR);
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
allocator, "char array has no valid allocator",
return RCUTILS_RET_ERROR);
char_array->owns_buffer = true;
char_array->buffer_length = 0lu;
char_array->buffer_capacity = buffer_capacity;
char_array->allocator = *allocator;
if (buffer_capacity > 0lu) {
char_array->buffer =
(char *)allocator->allocate(buffer_capacity * sizeof(char), allocator->state);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
char_array->buffer,
"failed to allocate memory for char array",
char_array->buffer_capacity = 0lu;
char_array->buffer_length = 0lu;
return RCUTILS_RET_BAD_ALLOC);
char_array->buffer[0] = '\0';
}
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_char_array_fini(rcutils_char_array_t * char_array)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(char_array, RCUTILS_RET_ERROR);
if (char_array->owns_buffer) {
rcutils_allocator_t * allocator = &char_array->allocator;
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
allocator, "char array has no valid allocator",
return RCUTILS_RET_ERROR);
allocator->deallocate(char_array->buffer, allocator->state);
}
char_array->buffer = NULL;
char_array->buffer_length = 0lu;
char_array->buffer_capacity = 0lu;
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_char_array_resize(rcutils_char_array_t * char_array, size_t new_size)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(char_array, RCUTILS_RET_ERROR);
if (0lu == new_size) {
RCUTILS_SET_ERROR_MSG("new size of char_array has to be greater than zero");
return RCUTILS_RET_INVALID_ARGUMENT;
}
rcutils_allocator_t * allocator = &char_array->allocator;
RCUTILS_CHECK_ALLOCATOR_WITH_MSG(
allocator, "char array has no valid allocator",
return RCUTILS_RET_ERROR);
if (new_size == char_array->buffer_capacity) {
// nothing to do here
return RCUTILS_RET_OK;
}
char * old_buf = char_array->buffer;
size_t old_size = char_array->buffer_capacity;
size_t old_length = char_array->buffer_length;
if (char_array->owns_buffer) { // we own the buffer, we can do whatever we want
char * new_buf = rcutils_reallocf(char_array->buffer, new_size * sizeof(char), allocator);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
new_buf,
"failed to reallocate memory for char array",
return RCUTILS_RET_BAD_ALLOC);
char_array->buffer = new_buf;
} else { // we don't realloc memory we don't own. instead, we alloc some new space
rcutils_ret_t ret = rcutils_char_array_init(char_array, new_size, allocator);
if (ret != RCUTILS_RET_OK) {
return ret;
}
size_t n = MIN(new_size, old_size);
memcpy(char_array->buffer, old_buf, n);
char_array->buffer[n - 1] = '\0'; // always have an ending
}
char_array->buffer_capacity = new_size;
char_array->buffer_length = MIN(new_size, old_length);
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_char_array_expand_as_needed(rcutils_char_array_t * char_array, size_t new_size)
{
RCUTILS_CHECK_ARGUMENT_FOR_NULL(char_array, RCUTILS_RET_ERROR);
if (new_size <= char_array->buffer_capacity) {
return RCUTILS_RET_OK;
}
// Make sure we expand by at least 1.5x the old capacity
size_t minimum_size = char_array->buffer_capacity + (char_array->buffer_capacity >> 1);
if (new_size < minimum_size) {
new_size = minimum_size;
}
return rcutils_char_array_resize(char_array, new_size);
}
static int
_rcutils_char_array_vsprintf(rcutils_char_array_t * char_array, const char * format, va_list args)
{
va_list args_clone;
va_copy(args_clone, args);
// when doing size calculation, remember the return value of vsnprintf excludes terminating null
// byte
int size = vsnprintf(char_array->buffer, char_array->buffer_capacity, format, args_clone);
va_end(args_clone);
return size;
}
rcutils_ret_t
rcutils_char_array_vsprintf(rcutils_char_array_t * char_array, const char * format, va_list args)
{
int size = _rcutils_char_array_vsprintf(char_array, format, args);
if (size < 0) {
RCUTILS_SET_ERROR_MSG("vsprintf on char array failed");
return RCUTILS_RET_ERROR;
}
size_t new_size = (size_t) size + 1; // with the terminating null byte
if (new_size > char_array->buffer_capacity) {
rcutils_ret_t ret = rcutils_char_array_expand_as_needed(char_array, new_size);
if (ret != RCUTILS_RET_OK) {
RCUTILS_SET_ERROR_MSG("char array failed to expand");
return ret;
}
if (_rcutils_char_array_vsprintf(char_array, format, args) != size) {
if (rcutils_char_array_fini(char_array) == RCUTILS_RET_OK) {
RCUTILS_SET_ERROR_MSG("vsprintf on resized char array failed");
} else {
RCUTILS_SET_ERROR_MSG("vsprintf on resized char array failed; clean up failed too");
}
return RCUTILS_RET_ERROR;
}
}
char_array->buffer_length = new_size;
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_char_array_memcpy(rcutils_char_array_t * char_array, const char * src, size_t n)
{
rcutils_ret_t ret = rcutils_char_array_expand_as_needed(char_array, n);
if (ret != RCUTILS_RET_OK) {
RCUTILS_SET_ERROR_MSG("char array failed to expand");
return ret;
}
memcpy(char_array->buffer, src, n);
char_array->buffer_length = n;
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_char_array_strcpy(rcutils_char_array_t * char_array, const char * src)
{
return rcutils_char_array_memcpy(char_array, src, strlen(src) + 1);
}
rcutils_ret_t
rcutils_char_array_strncat(rcutils_char_array_t * char_array, const char * src, size_t n)
{
size_t current_strlen;
if (char_array->buffer_length == 0) {
current_strlen = 0;
} else {
// The buffer length always contains the trailing \0, so the strlen is one less than that.
current_strlen = char_array->buffer_length - 1;
}
size_t new_length = current_strlen + n + 1;
rcutils_ret_t ret = rcutils_char_array_expand_as_needed(char_array, new_length);
if (ret != RCUTILS_RET_OK) {
RCUTILS_SET_ERROR_MSG("char array failed to expand");
return ret;
}
memcpy(char_array->buffer + current_strlen, src, n);
char_array->buffer[new_length - 1] = '\0';
char_array->buffer_length = new_length;
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_char_array_strcat(rcutils_char_array_t * char_array, const char * src)
{
return rcutils_char_array_strncat(char_array, src, strlen(src));
}