project_files/frontlib/util/buffer.h
author Wuzzy <almikes@aol.com>
Wed, 13 Apr 2016 01:26:46 +0200
changeset 11726 8c1a30d4cbc8
parent 10017 de822cd3df3a
permissions -rw-r--r--
Construction Mode: Fix giving round energy boost at first round
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     1
/*
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     2
 * Hedgewars, a free turn based strategy game
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     3
 * Copyright (C) 2012 Simeon Maxein <smaxein@googlemail.com>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     4
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     5
 * This program is free software; you can redistribute it and/or
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     6
 * modify it under the terms of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     7
 * as published by the Free Software Foundation; either version 2
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     8
 * of the License, or (at your option) any later version.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
     9
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    10
 * This program is distributed in the hope that it will be useful,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    13
 * GNU General Public License for more details.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    14
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    15
 * You should have received a copy of the GNU General Public License
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    16
 * along with this program; if not, write to the Free Software
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    18
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    19
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    20
#ifndef BUFFER_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    21
#define BUFFER_H_
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    22
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    23
#include <stdint.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    24
#include <stddef.h>
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    25
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    26
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    27
 * A simple struct to hold both the pointer to an array and its size,
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    28
 * for e.g. conveniently returning it from a function.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    29
 *
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    30
 * Convention: Size is zero iff data is a NULL pointer.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    31
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    32
typedef struct {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    33
    void *data;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    34
    size_t size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    35
} flib_buffer;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    36
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    37
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    38
 * Just like flib_buffer, but the contents are not supposed to be modified.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    39
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    40
typedef struct {
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    41
    const void *data;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    42
    size_t size;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    43
} flib_constbuffer;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    44
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    45
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    46
 * Simple variable-capacity data structure that can be efficiently appended to.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    47
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    48
typedef struct _flib_vector flib_vector;
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    49
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    50
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    51
 * Create a new vector. Needs to be destroyed again later with flib_vector_destroy.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    52
 * May return NULL if memory runs out.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    53
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    54
flib_vector *flib_vector_create();
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    55
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    56
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    57
 * Free the memory of this vector
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    58
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    59
void flib_vector_destroy(flib_vector *vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    60
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    61
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    62
 * Resize the vector. This changes the size, and ensures the capacity is large enough to
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    63
 * for the new size. Can also free memory if the new size is smaller. There is no guarantee
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    64
 * about the contents of extra memory.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    65
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    66
int flib_vector_resize(flib_vector *vec, size_t newSize);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    67
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    68
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    69
 * Append the provided data to the end of the vector, enlarging it as required.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    70
 * The vector remains unchanged if appending fails.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    71
 * Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    72
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    73
int flib_vector_append(flib_vector *vec, const void *data, size_t len);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    74
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    75
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    76
 * Append data from a format string to the buffer (without trailing 0)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    77
 * Returns 0 on success.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    78
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    79
int flib_vector_appendf(flib_vector *vec, const char *template, ...);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    80
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    81
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    82
 * Return a pointer to the current data buffer of the vector. This pointer can
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    83
 * become invalid if the vector size or capacity is changed.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    84
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    85
void *flib_vector_data(flib_vector *vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    86
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    87
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    88
 * Return the current size of the vector.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    89
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    90
size_t flib_vector_size(flib_vector *vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    91
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    92
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    93
 * Return a buffer pointing to the current contents of the vector.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    94
 * These will become invalid if the vector size or capacity is changed.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    95
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    96
flib_buffer flib_vector_as_buffer(flib_vector *vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    97
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    98
/**
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
    99
 * Return a constbuffer pointing to the current contents of the vector.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   100
 * These will become invalid if the vector size or capacity is changed.
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   101
 */
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   102
flib_constbuffer flib_vector_as_constbuffer(flib_vector *vec);
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   103
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 7314
diff changeset
   104
#endif