109 lines
2.7 KiB
C
109 lines
2.7 KiB
C
#ifndef UC_STRVEC_H_
|
|
#define UC_STRVEC_H_
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
#ifdef __cplusplus
|
|
extern "C" {
|
|
#endif
|
|
/** A Vector of strings.*/
|
|
struct UCStrv
|
|
{
|
|
/** number of strings */
|
|
size_t cnt;
|
|
/** allocated room in strings (internal use) */
|
|
size_t allocated;
|
|
/** Array of strings */
|
|
char **strings;
|
|
};
|
|
|
|
/** Initialize a UCStrv
|
|
*
|
|
* @param strv
|
|
*/
|
|
void uc_strv_init(struct UCStrv *strv);
|
|
|
|
/** static initializer. Use as: struct UCStrv foo = UC_STRV_INITIALIZER;
|
|
*/
|
|
#define UC_STRV_INITIALIZER {0, 0, NULL}
|
|
|
|
/**
|
|
* Grow the UCStrv so it can hold n more items
|
|
*
|
|
* @param strv UCStrv to grow
|
|
* @param cnt expand strvec to hold this cnt more items
|
|
* @return 0 on success, otherwise failure
|
|
**/
|
|
int uc_strv_grow(struct UCStrv *strv, size_t cnt);
|
|
|
|
/**
|
|
* Expand the UCStrv to hold attleast 1 more item if needed
|
|
*
|
|
* @param strv UCStrv to expand
|
|
* @return 0 on success, otherwise failure
|
|
**/
|
|
int uc_strv_check_expand(struct UCStrv *strv);
|
|
|
|
/**
|
|
* Append a string to the UCStrv, the string will be copied in
|
|
*
|
|
* @param strv UCStrv to append to
|
|
* @param str String to append, the string will be copied.
|
|
* @return 0 on success, otherwise failure
|
|
**/
|
|
int uc_strv_append(struct UCStrv *strv, const char *restrict str);
|
|
|
|
/**
|
|
* Append a string to the strvec, nocopy variang.
|
|
* The string shold be dynamically allocated, as
|
|
* destroying the UCStrv will free() all items.
|
|
*
|
|
* @param strv UCStrv to add to
|
|
* @param str the string to append
|
|
* @return 0 on success, otherwise failure
|
|
**/
|
|
int uc_strv_append_nocopy(struct UCStrv *strv, char *restrict str);
|
|
|
|
/**
|
|
* Append a NULL pointer to the UCStrv, (cnt is not increased,
|
|
* so another _appended item will overwrite this NULL terminator)
|
|
*
|
|
* @param strv UCStrv to append to
|
|
* @return 0 on success, otherwise failure
|
|
**/
|
|
int uc_strv_null_terminate(struct UCStrv *strv);
|
|
|
|
/**
|
|
* free all strings in this UCStrv, cnt will be 0 after this
|
|
* @param strv UCStrv to clear
|
|
*/
|
|
void uc_strv_clear(struct UCStrv *strv);
|
|
|
|
/**
|
|
* free all elements as well as the underlying array, the UCStrv is
|
|
* unusable after this call until it is initialized again.
|
|
* (strv itself is not free'd)
|
|
*
|
|
* @param strv UCStrv to destroy
|
|
*/
|
|
void uc_strv_destroy(struct UCStrv *strv);
|
|
|
|
/**
|
|
* Append all the elments from b to a
|
|
* If copy is 1, the appended elements will be copied when appended
|
|
*
|
|
* @param a destination UCStrv
|
|
* @param b source UCStrv
|
|
* @param copy 1 for making a copy of the string when appending it
|
|
* 0 to just append the same pointer in b to a
|
|
* @return 0 on success, != 0 if appending/allocation fails
|
|
*/
|
|
int uc_strv_append_all(struct UCStrv *restrict a, struct UCStrv *restrict b, int copy);
|
|
|
|
#ifdef __cplusplus
|
|
}
|
|
#endif
|
|
|
|
#endif
|