File: buffer.h

package info (click to toggle)
hedgewars 1.0.2-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 218,948 kB
  • sloc: pascal: 54,829; cpp: 27,221; ansic: 22,649; java: 8,210; haskell: 6,788; xml: 3,076; sh: 580; objc: 113; python: 105; makefile: 33
file content (104 lines) | stat: -rw-r--r-- 3,127 bytes parent folder | download | duplicates (10)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Hedgewars, a free turn based strategy game
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.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
 * of the License, 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#ifndef BUFFER_H_
#define BUFFER_H_

#include <stdint.h>
#include <stddef.h>

/**
 * A simple struct to hold both the pointer to an array and its size,
 * for e.g. conveniently returning it from a function.
 *
 * Convention: Size is zero iff data is a NULL pointer.
 */
typedef struct {
    void *data;
    size_t size;
} flib_buffer;

/**
 * Just like flib_buffer, but the contents are not supposed to be modified.
 */
typedef struct {
    const void *data;
    size_t size;
} flib_constbuffer;

/**
 * Simple variable-capacity data structure that can be efficiently appended to.
 */
typedef struct _flib_vector flib_vector;

/**
 * Create a new vector. Needs to be destroyed again later with flib_vector_destroy.
 * May return NULL if memory runs out.
 */
flib_vector *flib_vector_create();

/**
 * Free the memory of this vector
 */
void flib_vector_destroy(flib_vector *vec);

/**
 * Resize the vector. This changes the size, and ensures the capacity is large enough to
 * for the new size. Can also free memory if the new size is smaller. There is no guarantee
 * about the contents of extra memory.
 */
int flib_vector_resize(flib_vector *vec, size_t newSize);

/**
 * Append the provided data to the end of the vector, enlarging it as required.
 * The vector remains unchanged if appending fails.
 * Returns 0 on success.
 */
int flib_vector_append(flib_vector *vec, const void *data, size_t len);

/**
 * Append data from a format string to the buffer (without trailing 0)
 * Returns 0 on success.
 */
int flib_vector_appendf(flib_vector *vec, const char *template, ...);

/**
 * Return a pointer to the current data buffer of the vector. This pointer can
 * become invalid if the vector size or capacity is changed.
 */
void *flib_vector_data(flib_vector *vec);

/**
 * Return the current size of the vector.
 */
size_t flib_vector_size(flib_vector *vec);

/**
 * Return a buffer pointing to the current contents of the vector.
 * These will become invalid if the vector size or capacity is changed.
 */
flib_buffer flib_vector_as_buffer(flib_vector *vec);

/**
 * Return a constbuffer pointing to the current contents of the vector.
 * These will become invalid if the vector size or capacity is changed.
 */
flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec);

#endif