lib/buffer: retun size of the buffer and data

Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
Karel Zak 2021-08-05 16:05:58 +02:00
parent 2e03758dc5
commit f60dc88848
4 changed files with 28 additions and 17 deletions

View File

@ -23,6 +23,7 @@ int ul_buffer_append_data(struct ul_buffer *buf, const char *data, size_t sz);
int ul_buffer_append_string(struct ul_buffer *buf, const char *str);
int ul_buffer_append_ntimes(struct ul_buffer *buf, size_t n, const char *str);
int ul_buffer_set_data(struct ul_buffer *buf, const char *data, size_t sz);
char *ul_buffer_get_data(struct ul_buffer *buf);
char *ul_buffer_get_data(struct ul_buffer *buf, size_t *sz);
size_t ul_buffer_get_bufsiz(struct ul_buffer *buf);
#endif /* UTIL_LINUX_BUFFER */

View File

@ -119,16 +119,26 @@ int ul_buffer_set_data(struct ul_buffer *buf, const char *data, size_t sz)
return ul_buffer_append_data(buf, data, sz);
}
char *ul_buffer_get_data(struct ul_buffer *buf)
char *ul_buffer_get_data(struct ul_buffer *buf, size_t *sz)
{
if (sz)
*sz = buf->end - buf->begin;
return buf->begin;
}
/* size of allocated area (!= size of stored data */
size_t ul_buffer_get_bufsiz(struct ul_buffer *buf)
{
return buf->sz;
}
#ifdef TEST_PROGRAM_BUFFER
int main(void)
{
struct ul_buffer buf = UL_INIT_BUFFER;
char *str;
size_t sz = 0;
ul_buffer_set_chunksize(&buf, 16);
@ -140,22 +150,22 @@ int main(void)
ul_buffer_append_string(&buf, "=");
ul_buffer_append_string(&buf, "bbb");
str = ul_buffer_get_data(&buf);
printf("data '%s'\n", str);
str = ul_buffer_get_data(&buf, &sz);
printf("data [%zu] '%s'\n", sz, str);
ul_buffer_reset_data(&buf);
ul_buffer_append_string(&buf, "This is really long string to test the buffer function.");
ul_buffer_append_string(&buf, " YES!");
str = ul_buffer_get_data(&buf);
printf("data '%s'\n", str);
str = ul_buffer_get_data(&buf, &sz);
printf("data [%zu] '%s'\n", sz, str);
ul_buffer_free_data(&buf);
str = strdup("foo");
ul_buffer_refer_string(&buf, str);
ul_buffer_append_data(&buf, ",", 1);
ul_buffer_append_string(&buf, "bar");
str = ul_buffer_get_data(&buf);
printf("data '%s'\n", str);
str = ul_buffer_get_data(&buf, &sz);
printf("data [%zu] '%s'\n", sz, str);
ul_buffer_free_data(&buf);
}

View File

@ -224,7 +224,7 @@ int mnt_optstr_append_option(char **optstr, const char *name, const char *value)
rc = __buffer_append_option(&buf, name, nsz, value, vsz);
*optstr = ul_buffer_get_data(&buf);
*optstr = ul_buffer_get_data(&buf, NULL);
return rc;
}
/**
@ -261,7 +261,7 @@ int mnt_optstr_prepend_option(char **optstr, const char *name, const char *value
free(*optstr);
}
*optstr = ul_buffer_get_data(&buf);
*optstr = ul_buffer_get_data(&buf, NULL);
return rc;
}
@ -558,11 +558,11 @@ int mnt_split_optstr(const char *optstr, char **user, char **vfs,
}
if (vfs)
*vfs = rc ? NULL : ul_buffer_get_data(&xvfs);
*vfs = rc ? NULL : ul_buffer_get_data(&xvfs, NULL);
if (fs)
*fs = rc ? NULL : ul_buffer_get_data(&xfs);
*fs = rc ? NULL : ul_buffer_get_data(&xfs, NULL);
if (user)
*user = rc ? NULL : ul_buffer_get_data(&xuser);
*user = rc ? NULL : ul_buffer_get_data(&xuser, NULL);
if (rc) {
ul_buffer_free_data(&xvfs);
ul_buffer_free_data(&xfs);
@ -626,7 +626,7 @@ int mnt_optstr_get_options(const char *optstr, char **subset,
break;
}
*subset = rc ? NULL : ul_buffer_get_data(&buf);
*subset = rc ? NULL : ul_buffer_get_data(&buf, NULL);
if (rc)
ul_buffer_free_data(&buf);
return rc;
@ -834,7 +834,7 @@ int mnt_optstr_apply_flags(char **optstr, unsigned long flags,
goto err;
}
*optstr = ul_buffer_get_data(&buf);
*optstr = ul_buffer_get_data(&buf, NULL);
}
DBG(CXT, ul_debug("new optstr '%s'", *optstr));

View File

@ -820,7 +820,7 @@ static char *device_get_data(
if (i + 1 < n)
ul_buffer_append_data(&buf, "\n", 1);
}
str = ul_buffer_get_data(&buf);
str = ul_buffer_get_data(&buf, NULL);
break;
}
case COL_FSROOTS:
@ -838,7 +838,7 @@ static char *device_get_data(
if (i + 1 < n)
ul_buffer_append_data(&buf, "\n", 1);
}
str = ul_buffer_get_data(&buf);
str = ul_buffer_get_data(&buf, NULL);
break;
}
case COL_LABEL: