forked from ros2/rcutils
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstring_array.c
More file actions
138 lines (124 loc) · 3.94 KB
/
string_array.c
File metadata and controls
138 lines (124 loc) · 3.94 KB
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
// Copyright 2017 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.
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdlib.h>
#include <string.h>
#include "rcutils/allocator.h"
#include "rcutils/error_handling.h"
#include "rcutils/types/string_array.h"
#include "rcutils/types/rcutils_ret.h"
rcutils_string_array_t
rcutils_get_zero_initialized_string_array(void)
{
static rcutils_string_array_t array = {
.size = 0,
.data = NULL,
};
array.allocator = rcutils_get_zero_initialized_allocator();
return array;
}
rcutils_ret_t
rcutils_string_array_init(
rcutils_string_array_t * string_array,
size_t size,
const rcutils_allocator_t * allocator)
{
if (NULL == allocator) {
RCUTILS_SET_ERROR_MSG("allocator is null");
return RCUTILS_RET_INVALID_ARGUMENT;
}
if (NULL == string_array) {
RCUTILS_SET_ERROR_MSG("string_array is null");
return RCUTILS_RET_INVALID_ARGUMENT;
}
string_array->size = size;
string_array->data = allocator->zero_allocate(size, sizeof(char *), allocator->state);
if (NULL == string_array->data) {
RCUTILS_SET_ERROR_MSG("failed to allocator string array");
return RCUTILS_RET_BAD_ALLOC;
}
string_array->allocator = *allocator;
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_string_array_fini(rcutils_string_array_t * string_array)
{
if (NULL == string_array) {
RCUTILS_SET_ERROR_MSG("string_array is null");
return RCUTILS_RET_INVALID_ARGUMENT;
}
if (NULL == string_array->data) {
return RCUTILS_RET_OK;
}
rcutils_allocator_t * allocator = &string_array->allocator;
if (!rcutils_allocator_is_valid(allocator)) {
RCUTILS_SET_ERROR_MSG("allocator is invalid");
return RCUTILS_RET_INVALID_ARGUMENT;
}
size_t i;
for (i = 0; i < string_array->size; ++i) {
allocator->deallocate(string_array->data[i], allocator->state);
string_array->data[i] = NULL;
}
allocator->deallocate(string_array->data, allocator->state);
string_array->data = NULL;
return RCUTILS_RET_OK;
}
rcutils_ret_t
rcutils_string_array_cmp(
const rcutils_string_array_t * lhs,
const rcutils_string_array_t * rhs,
int * res)
{
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
lhs, "lhs string array is null", return RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
rhs, "rhs string array is null", return RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
lhs->data, "lhs->data is null", return RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
rhs->data, "rhs->data is null", return RCUTILS_RET_INVALID_ARGUMENT);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
res, "res argument is null", return RCUTILS_RET_INVALID_ARGUMENT);
size_t smallest_size = lhs->size;
if (rhs->size < smallest_size) {
smallest_size = rhs->size;
}
for (size_t i = 0; i < smallest_size; ++i) {
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
lhs->data[i], "lhs array element is null", return RCUTILS_RET_ERROR);
RCUTILS_CHECK_FOR_NULL_WITH_MSG(
rhs->data[i], "rhs array element is null", return RCUTILS_RET_ERROR);
// Loop until we find a pair of strings that are not equal
int strcmp_res = strcmp(lhs->data[i], rhs->data[i]);
if (0 != strcmp_res) {
*res = strcmp_res;
return RCUTILS_RET_OK;
}
}
// If all strings equal, compare array sizes
*res = 0;
if (lhs->size < rhs->size) {
*res = -1;
} else if (lhs->size > rhs->size) {
*res = 1;
}
return RCUTILS_RET_OK;
}
#ifdef __cplusplus
}
#endif