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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/* ccparray.c - A simple dynamic array for character pointer.
* Copyright (C) 2016 g10 Code GmbH
*
* This file is part of GnuPG.
*
* This file is free software; you can redistribute it and/or modify
* it under the terms of either
*
* - the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 3 of the License, or (at
* your option) any later version.
*
* or
*
* - 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.
*
* or both in parallel, as here.
*
* This file 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, see <https://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
#include "util.h"
#include "ccparray.h"
/* A simple implementation of a dynamic array of const char pointers.
* The example code:
*
* ccparray_t ccp;
* const char **argv;
* int i;
*
* ccparray_init (&ccp, 0);
* ccparray_put (&ccp, "First arg");
* ccparray_put (&ccp, "Second arg");
* ccparray_put (&ccp, NULL);
* ccparray_put (&ccp, "Fourth arg");
* argv = ccparray_get (&ccp, NULL);
* if (!argv)
* die ("error building array: %s\n", strerror (errno));
* for (i=0; argv[i]; i++)
* printf ("[%d] = '%s'\n", i, argv[i]);
* xfree (argv);
*
* will result in this output:
*
* [0] = 'First arg'
* [1] = 'Second arg'
*
* Note that allocation errors are detected but only returned with the
* final ccparray_get(); this helps not to clutter the code with out
* of core checks.
*/
void
ccparray_init (ccparray_t *cpa, unsigned int initialsize)
{
if (!initialsize)
cpa->size = 16;
else if (initialsize < (1<<16))
cpa->size = initialsize;
else
cpa->size = (1<<16);
cpa->count = 0;
cpa->out_of_core = 0;
cpa->array = xtrycalloc (cpa->size, sizeof *cpa->array);
if (!cpa->array)
cpa->out_of_core = errno;
}
void
ccparray_put (ccparray_t *cpa, const char *value)
{
if (cpa->out_of_core)
return;
if (cpa->count + 1 >= cpa->size)
{
const char **newarray;
size_t n, newsize;
if (cpa->size < 8)
newsize = 16;
else if (cpa->size < 4096)
newsize = 2 * cpa->size;
else if (cpa->size < (1<<16))
newsize = cpa->size + 2048;
else
{
cpa->out_of_core = ENOMEM;
return;
}
newarray = xtrycalloc (newsize, sizeof *newarray);
if (!newarray)
{
cpa->out_of_core = errno ? errno : ENOMEM;
return;
}
for (n=0; n < cpa->size; n++)
newarray[n] = cpa->array[n];
xfree (cpa->array);
cpa->array = newarray;
cpa->size = newsize;
}
cpa->array[cpa->count++] = value;
}
const char **
ccparray_get (ccparray_t *cpa, size_t *r_count)
{
const char **result;
if (cpa->out_of_core)
{
if (cpa->array)
{
xfree (cpa->array);
cpa->array = NULL;
}
gpg_err_set_errno (cpa->out_of_core);
return NULL;
}
result= cpa->array;
if (r_count)
*r_count = cpa->count;
cpa->array = NULL;
cpa->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */
return result;
}
|