iovec-wrapper: introduce iovw_replace_string_field()
This commit is contained in:
@@ -59,7 +59,7 @@ int iovw_put(struct iovec_wrapper *iovw, void *data, size_t len) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
|
||||
int iovw_put_string_field_full(struct iovec_wrapper *iovw, bool replace, const char *field, const char *value) {
|
||||
_cleanup_free_ char *x = NULL;
|
||||
int r;
|
||||
|
||||
@@ -69,6 +69,14 @@ int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const c
|
||||
if (!x)
|
||||
return -ENOMEM;
|
||||
|
||||
if (replace)
|
||||
FOREACH_ARRAY(iovec, iovw->iovec, iovw->count)
|
||||
if (memory_startswith(iovec->iov_base, iovec->iov_len, field)) {
|
||||
iovec->iov_len = strlen(x);
|
||||
free_and_replace(iovec->iov_base, x);
|
||||
return 0;
|
||||
}
|
||||
|
||||
r = iovw_put(iovw, x, strlen(x));
|
||||
if (r >= 0)
|
||||
TAKE_PTR(x);
|
||||
|
||||
@@ -33,7 +33,13 @@ static inline bool iovw_isempty(const struct iovec_wrapper *iovw) {
|
||||
return !iovw || iovw->count == 0;
|
||||
}
|
||||
|
||||
int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value);
|
||||
int iovw_put_string_field_full(struct iovec_wrapper *iovw, bool replace, const char *field, const char *value);
|
||||
static inline int iovw_put_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
|
||||
return iovw_put_string_field_full(iovw, false, field, value);
|
||||
}
|
||||
static inline int iovw_replace_string_field(struct iovec_wrapper *iovw, const char *field, const char *value) {
|
||||
return iovw_put_string_field_full(iovw, true, field, value);
|
||||
}
|
||||
int iovw_put_string_field_free(struct iovec_wrapper *iovw, const char *field, char *value);
|
||||
void iovw_rebase(struct iovec_wrapper *iovw, void *old, void *new);
|
||||
size_t iovw_size(const struct iovec_wrapper *iovw);
|
||||
|
||||
Reference in New Issue
Block a user