mirror of
https://gitlab.freedesktop.org/plymouth/plymouth.git
synced 2026-05-05 18:08:01 +02:00
buffer: Add APIs for editing bytes in buffer
This commit adds a few functions for getting at and using the size, bytes, and capacity of a buffer. This will make it easier to modify utf8 string on the side and and update the buffer size afterward.
This commit is contained in:
parent
73093a8411
commit
804a78860a
2 changed files with 31 additions and 0 deletions
|
|
@ -224,6 +224,20 @@ ply_buffer_append_from_fd (ply_buffer_t *buffer,
|
|||
ply_buffer_append_bytes (buffer, bytes, bytes_read);
|
||||
}
|
||||
|
||||
void
|
||||
ply_buffer_set_bytes (ply_buffer_t *buffer,
|
||||
void *bytes,
|
||||
size_t number_of_bytes,
|
||||
size_t capacity)
|
||||
{
|
||||
if (buffer->data != bytes)
|
||||
free (buffer->data);
|
||||
|
||||
buffer->data = bytes;
|
||||
buffer->size = number_of_bytes;
|
||||
buffer->capacity = capacity;
|
||||
}
|
||||
|
||||
const char *
|
||||
ply_buffer_get_bytes (ply_buffer_t *buffer)
|
||||
{
|
||||
|
|
@ -246,6 +260,12 @@ ply_buffer_steal_bytes (ply_buffer_t *buffer)
|
|||
return bytes;
|
||||
}
|
||||
|
||||
size_t
|
||||
ply_buffer_get_capacity (ply_buffer_t *buffer)
|
||||
{
|
||||
return buffer->capacity;
|
||||
}
|
||||
|
||||
size_t
|
||||
ply_buffer_get_size (ply_buffer_t *buffer)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -44,12 +44,23 @@ __attribute__((__format__ (__printf__, 2, 3)))
|
|||
void ply_buffer_append_with_non_literal_format_string (ply_buffer_t *buffer,
|
||||
const char *format,
|
||||
...);
|
||||
void ply_buffer_set_bytes (ply_buffer_t *buffer,
|
||||
void *bytes,
|
||||
size_t number_of_bytes,
|
||||
size_t capacity);
|
||||
void ply_buffer_remove_bytes (ply_buffer_t *buffer,
|
||||
size_t number_of_bytes);
|
||||
void ply_buffer_remove_bytes_at_end (ply_buffer_t *buffer,
|
||||
size_t number_of_bytes);
|
||||
const char *ply_buffer_get_bytes (ply_buffer_t *buffer);
|
||||
size_t ply_buffer_get_capacity (ply_buffer_t *buffer);
|
||||
char *ply_buffer_steal_bytes (ply_buffer_t *buffer);
|
||||
#define ply_buffer_borrow_bytes(buffer, bytes, size, capacity) \
|
||||
for (bool _ran = false; *bytes = (char *) ply_buffer_get_bytes (buffer), \
|
||||
*size = ply_buffer_get_size (buffer), \
|
||||
*capacity = ply_buffer_get_capacity (buffer), \
|
||||
!_ran; \
|
||||
ply_buffer_set_bytes (buffer, *bytes, *size, *capacity), _ran = true)
|
||||
size_t ply_buffer_get_size (ply_buffer_t *buffer);
|
||||
void ply_buffer_clear (ply_buffer_t *buffer);
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue