mirror of
git://git.gnupg.org/gnupg.git
synced 2024-11-09 21:28:51 +01:00
common: Add simple dynamic array function.
* common/ccparray.c: New. * common/ccparray.h: New. * common/t-ccparray.c: New. * common/Makefile.am (common_sources): Add files. (module_tests): Add test file. (t_ccparray_LDADD): New. Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
parent
f7426b73ce
commit
2421f7f7ed
@ -72,6 +72,7 @@ common_sources = \
|
|||||||
xasprintf.c \
|
xasprintf.c \
|
||||||
xreadline.c \
|
xreadline.c \
|
||||||
membuf.c membuf.h \
|
membuf.c membuf.h \
|
||||||
|
ccparray.c ccparray.h \
|
||||||
iobuf.c iobuf.h \
|
iobuf.c iobuf.h \
|
||||||
ttyio.c ttyio.h \
|
ttyio.c ttyio.h \
|
||||||
asshelp.c asshelp2.c asshelp.h \
|
asshelp.c asshelp2.c asshelp.h \
|
||||||
@ -156,7 +157,7 @@ module_tests = t-stringhelp t-timestuff \
|
|||||||
t-convert t-percent t-gettime t-sysutils t-sexputil \
|
t-convert t-percent t-gettime t-sysutils t-sexputil \
|
||||||
t-session-env t-openpgp-oid t-ssh-utils \
|
t-session-env t-openpgp-oid t-ssh-utils \
|
||||||
t-mapstrings t-zb32 t-mbox-util t-iobuf t-strlist \
|
t-mapstrings t-zb32 t-mbox-util t-iobuf t-strlist \
|
||||||
t-private-keys
|
t-private-keys t-ccparray
|
||||||
if !HAVE_W32CE_SYSTEM
|
if !HAVE_W32CE_SYSTEM
|
||||||
module_tests += t-exechelp
|
module_tests += t-exechelp
|
||||||
endif
|
endif
|
||||||
@ -206,6 +207,7 @@ t_mbox_util_LDADD = $(t_common_ldadd)
|
|||||||
t_iobuf_LDADD = $(t_common_ldadd)
|
t_iobuf_LDADD = $(t_common_ldadd)
|
||||||
t_strlist_LDADD = $(t_common_ldadd)
|
t_strlist_LDADD = $(t_common_ldadd)
|
||||||
t_private_keys_LDADD = $(t_common_ldadd)
|
t_private_keys_LDADD = $(t_common_ldadd)
|
||||||
|
t_ccparray_LDADD = $(t_common_ldadd)
|
||||||
|
|
||||||
# System specific test
|
# System specific test
|
||||||
if HAVE_W32_SYSTEM
|
if HAVE_W32_SYSTEM
|
||||||
|
147
common/ccparray.c
Normal file
147
common/ccparray.c
Normal file
@ -0,0 +1,147 @@
|
|||||||
|
/* 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 <http://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];
|
||||||
|
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;
|
||||||
|
}
|
51
common/ccparray.h
Normal file
51
common/ccparray.h
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
/* 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef GNUPG_COMMON_CCPARRAY_H
|
||||||
|
#define GNUPG_COMMON_CCPARRAY_H
|
||||||
|
|
||||||
|
/* The definition of the structure is private, we only need it here,
|
||||||
|
* so it can be allocated on the stack. */
|
||||||
|
struct _ccparray_private_s
|
||||||
|
{
|
||||||
|
unsigned int count;
|
||||||
|
unsigned int size;
|
||||||
|
int out_of_core;
|
||||||
|
const char **array;
|
||||||
|
};
|
||||||
|
|
||||||
|
typedef struct _ccparray_private_s ccparray_t;
|
||||||
|
|
||||||
|
|
||||||
|
void ccparray_init (ccparray_t *cpa, unsigned int initialsize);
|
||||||
|
void ccparray_put (ccparray_t *cpa, const char *value);
|
||||||
|
const char **ccparray_get (ccparray_t *cpa, size_t *r_nelems);
|
||||||
|
|
||||||
|
|
||||||
|
#endif /*GNUPG_COMMON_CCPARRAY_H*/
|
93
common/t-ccparray.c
Normal file
93
common/t-ccparray.c
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
/* t-ccparray.c - Module test for ccparray.c
|
||||||
|
* Copyright (C) 2016 g10 Code GmbH
|
||||||
|
*
|
||||||
|
* This file is part of GnuPG.
|
||||||
|
*
|
||||||
|
* GnuPG 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 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* GnuPG 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 <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <config.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "util.h"
|
||||||
|
#include "ccparray.h"
|
||||||
|
|
||||||
|
#define pass() do { ; } while(0)
|
||||||
|
#define fail(a) do { fprintf (stderr, "%s:%d: test %d failed\n",\
|
||||||
|
__FILE__,__LINE__, (a)); \
|
||||||
|
exit (1); \
|
||||||
|
} while(0)
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
run_test_1 (void)
|
||||||
|
{
|
||||||
|
ccparray_t ccp;
|
||||||
|
const char **argv;
|
||||||
|
size_t nelem;
|
||||||
|
|
||||||
|
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, &nelem);
|
||||||
|
if (!argv)
|
||||||
|
{
|
||||||
|
fprintf (stderr, "error building array: %s\n", strerror (errno));
|
||||||
|
exit (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nelem != 4)
|
||||||
|
fail (1);
|
||||||
|
|
||||||
|
/* for (i=0; argv[i]; i++) */
|
||||||
|
/* printf ("[%d] = '%s'\n", i, argv[i]); */
|
||||||
|
xfree (argv);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static void
|
||||||
|
run_test_var (int count)
|
||||||
|
{
|
||||||
|
ccparray_t ccp;
|
||||||
|
size_t nelem;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
ccparray_init (&ccp, 0);
|
||||||
|
for (i=0; i < count; i++)
|
||||||
|
ccparray_put (&ccp, "An arg");
|
||||||
|
xfree (ccparray_get (&ccp, &nelem));
|
||||||
|
if (nelem != i)
|
||||||
|
fail (2);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int
|
||||||
|
main (int argc, char **argv)
|
||||||
|
{
|
||||||
|
(void)argc;
|
||||||
|
(void)argv;
|
||||||
|
|
||||||
|
run_test_1 ();
|
||||||
|
run_test_var (0);
|
||||||
|
run_test_var (7);
|
||||||
|
run_test_var (8);
|
||||||
|
run_test_var (9);
|
||||||
|
run_test_var (4096);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user