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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
|
/* argvfactory.c - Creating argv vectors
* Copyright (C) 1999 Michael Roth <mroth@gnupg.org>
*
* This file is part of pgpgpg.
*
* pgpgpg 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.
*
* pgpgpg 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
*/
#include "includes.h"
#include "argvfactory.h"
/*****************************************************************************
*
* Function : argv_factory_init
*
* Purpose : Initializes an ArgvFactory structure
*
* Input : af - A pointer to an uninitialized ArgvFactory structure to
* initialize.
*
*****************************************************************************/
void argv_factory_init(ArgvFactory *af)
{
assert(af != NULL);
af->size = 64;
af->used = 0;
af->argv = (char **)malloc(af->size * sizeof(char *));
assert(af->argv != NULL);
*af->argv = NULL;
}
/*****************************************************************************
*
* Function : argv_factory_release
*
* Purpose : Releases all memory associated with an ArgvFactory structure.
*
* Input : af - A pointer to the ArgvFactory structure to release.
*
* Notes : Don't use `af' after releasing.
*
*****************************************************************************/
void argv_factory_release(ArgvFactory *af)
{
assert(af != NULL);
assert(af->argv != NULL);
assert(af->used+1 <= af->size);
assert(af->argv[af->used] == NULL);
/* FIXME: Free the strings in the argv vector !!! */
free(af->argv);
}
/*****************************************************************************
*
* Function : argv_factory_add
*
* Purpose : Adds a argument to the argv vector managed by a ArgvFactory
* structure.
*
* Input : af - A pointer to an ArgvFactory which manages an argv vector.
* arg - The string to add to the argv vector managed by an
* ArgvFactory. The string will be duplicated.
*
* Notes : You could reuse or free the memory pointed to by `arg' after the
* call to argv_factory_add().
*
*****************************************************************************/
void argv_factory_add(ArgvFactory *af, char *arg)
{
assert(af != NULL);
assert(af->argv != NULL);
assert(af->used+1 <= af->size);
assert(af->argv[af->used] == NULL);
assert(arg != NULL);
if (af->used + 1 == af->size)
{
af->size *= 2;
af->argv = realloc(af->argv, af->size * sizeof(char *));
assert(af->argv != NULL);
}
af->argv[af->used++] = strdup(arg);
af->argv[af->used] = NULL;
assert(af->argv[af->used-1]);
}
/*****************************************************************************
*
* Function : argv_factory_append
*
* Purpose : Concatenate a second argv vector managed by an ArgvFactory to the
* first argv vector managed by an ArgvFactory structure.
*
* Input : dest - The ArgvFactory to which the `src' ArgvFactorys argv vector
* will be appended.
* src - The ArgvFactory which manage the argv vector which will be
* appended to the `dest' ArgvFactory argv vector. The strings
* in the `src' ArgvFactory argv vector will be duplicated.
*
* Notes : You can reuse or free the ArgvFactory `src' after a call to
* argv_factory_append().
*
*****************************************************************************/
void argv_factory_append(ArgvFactory *dest, ArgvFactory *src)
{
char **argv;
assert(dest != NULL);
assert(dest->argv != NULL);
assert(dest->used+1 <= dest->size);
assert(dest->argv[dest->used] == NULL);
assert(src != NULL);
assert(src->argv != NULL);
assert(src->used+1 <= src->size);
assert(src->argv[src->used] == NULL);
for (argv=src->argv; *argv; ++argv)
{
if (dest->used + 1 == dest->size)
{
dest->used *= 2;
dest->argv = realloc(dest->argv, dest->size * sizeof(char *));
assert(dest->argv != NULL);
}
dest->argv[dest->used++] = strdup(*argv);
dest->argv[dest->used] = NULL;
assert(dest->argv[dest->used-1]);
}
}
|