summaryrefslogtreecommitdiff
path: root/src/libply/ply-buffer.h
blob: fc7f4a8966034a0f90f5d1349a1ba9edefcd4f0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* ply-buffer.h - facilities for buffering data
 *
 * Copyright (C) 2008 Ray Strode <rstrode@redhat.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 */
#ifndef PLY_BUFFER_H
#define PLY_BUFFER_H

#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>

typedef struct _ply_buffer ply_buffer_t;

#ifndef PLY_HIDE_FUNCTION_DECLARATIONS
ply_buffer_t *ply_buffer_new (void);
void ply_buffer_free (ply_buffer_t *buffer);
void ply_buffer_append_bytes (ply_buffer_t *buffer,
                              const void   *bytes,
                              size_t        number_of_bytes);

void ply_buffer_append_from_fd (ply_buffer_t *buffer,
                                int           fd);
#define ply_buffer_append(buffer, format, args ...)                             \
        ply_buffer_append_with_non_literal_format_string (buffer,              \
                                                          format "", ## args)
__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

#endif /* PLY_BUFFER_H */