rcutils
master
C API providing common utilities and data structures.
|
#include <string.h>
#include "rcutils/allocator.h"
#include "rcutils/error_handling.h"
#include "rcutils/macros.h"
#include "rcutils/qsort.h"
#include "rcutils/types/rcutils_ret.h"
#include "rcutils/visibility_control.h"
Go to the source code of this file.
Classes | |
struct | rcutils_string_array_t |
Typedefs | |
typedef struct rcutils_string_array_t | rcutils_string_array_t |
Functions | |
rcutils_string_array_t | rcutils_get_zero_initialized_string_array (void) |
Return an empty string array struct. More... | |
rcutils_ret_t | rcutils_string_array_init (rcutils_string_array_t *string_array, size_t size, const rcutils_allocator_t *allocator) |
Initialize a string array with a given size. More... | |
rcutils_ret_t | rcutils_string_array_fini (rcutils_string_array_t *string_array) |
Finalize a string array, reclaiming all resources. More... | |
rcutils_ret_t | rcutils_string_array_cmp (const rcutils_string_array_t *lhs, const rcutils_string_array_t *rhs, int *res) |
Compare two string arrays. More... | |
rcutils_ret_t | rcutils_string_array_resize (rcutils_string_array_t *string_array, size_t new_size) |
Resize a string array, reclaiming removed resources. More... | |
int | rcutils_string_array_sort_compare (const void *lhs, const void *rhs) |
Lexicographic comparer for pointers to string pointers. More... | |
rcutils_ret_t | rcutils_string_array_sort (rcutils_string_array_t *string_array) |
Sort a string array according to lexicographical order. More... | |
typedef struct rcutils_string_array_t rcutils_string_array_t |
rcutils_string_array_t rcutils_get_zero_initialized_string_array | ( | void | ) |
Return an empty string array struct.
This function returns an empty and zero initialized string array struct. Calling rcutils_string_array_fini() on any non-initialized instance leads to undefined behavior. Every instance of string_array_t has to either be zero_initialized with this function or manually allocated.
Example:
rcutils_ret_t rcutils_string_array_init | ( | rcutils_string_array_t * | string_array, |
size_t | size, | ||
const rcutils_allocator_t * | allocator | ||
) |
Initialize a string array with a given size.
This function will initialize a given, zero initialized, string array to a given size.
Note that putting a string into the array gives owenship to the array.
Example:
rcutils_ret_t rcutils_string_array_fini | ( | rcutils_string_array_t * | string_array | ) |
Finalize a string array, reclaiming all resources.
This function reclaims any memory owned by the string array, including the strings it references.
The allocator used to initialize the string array is used to deallocate each string in the array and the array of strings itself.
[in,out] | string_array | object to be finalized |
RCUTILS_RET_OK
if successful, or RCUTILS_RET_INVALID_ARGUMENT
for invalid arguments, or RCUTILS_RET_ERROR
if an unknown error occurs rcutils_ret_t rcutils_string_array_cmp | ( | const rcutils_string_array_t * | lhs, |
const rcutils_string_array_t * | rhs, | ||
int * | res | ||
) |
Compare two string arrays.
The two string arrays are compared according to lexicographical order.
[in] | lhs | The first string array. |
[in] | rhs | The second string array. |
[out] | res | Negative value if lhs appears before rhs in lexicographical order. Zero if lhs and rhs are equal. Positive value if lhs appears after rhs in lexographical order. \return RCUTILS_RET_OKif successful, or \return RCUTILS_RET_INVALID_ARGUMENTif any argument is NULL, or \return RCUTILS_RET_INVALID_ARGUMENTif lhs->dataor rhs->datais NULL, or \return RCUTILS_RET_ERROR` if an unknown error occurs. |
rcutils_ret_t rcutils_string_array_resize | ( | rcutils_string_array_t * | string_array, |
size_t | new_size | ||
) |
Resize a string array, reclaiming removed resources.
This function changes the size of an existing string array. If the new size is larger, new entries are added to the end of the array and are zero- initialized. If the new size is smaller, entries are removed from the end of the array and their resources reclaimed.
string_array
remains unchanged and should still be reclaimed with rcutils_string_array_fini.[in,out] | string_array | object to be resized. |
[in] | new_size | the size the array should be changed to. |
RCUTILS_RET_OK
if successful, or RCUTILS_RET_INVALID_ARGUMENT
for invalid arguments, or RCUTILS_RET_BAD_ALLOC
if memory allocation fails, or RCUTILS_RET_ERROR
if an unknown error occurs. int rcutils_string_array_sort_compare | ( | const void * | lhs, |
const void * | rhs | ||
) |
Lexicographic comparer for pointers to string pointers.
This functions compares pointers to string pointers lexicographically ascending.
[in] | lhs | pointer to the first string pointer. |
[in] | rhs | pointer to the second string pointer. |
|
inline |
Sort a string array according to lexicographical order.
This function changes the order of the entries in a string array so that they are in lexicographically ascending order. Empty entries are placed at the end of the array.
[in,out] | string_array | object whose elements should be sorted. |
RCUTILS_RET_OK
if successful, or RCUTILS_RET_INVALID_ARGUMENT
for invalid arguments, or RCUTILS_RET_ERROR
if an unknown error occurs.