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
|
/*
* medussa - a distributed cracking system
* Copyright (C) 1999 Kostas Evangelinos <kos@bastard.net>
*
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*
* $Id: array.c,v 1.5 1999/12/20 19:46:35 kos Exp $
*
*/
#include <stdlib.h>
#include <strings.h>
#include <assert.h>
#include <stdio.h>
#include "array.h"
#include "xmalloc.h"
array *
array_init(int size) {
array *l;
l = (array *)xcalloc(1, sizeof(array));
l->allocelem = ARRAY_CHUNK;
l->nelem = 0;
l->sizelem = size;
l->data = xcalloc(l->allocelem, l->sizelem);
return l;
}
int
array_zero(array *l) {
l->nelem = 0;
return 0;
}
array *
array_init_hint(array *l, int size, int hint) {
l->allocelem = hint;
l->nelem = 0;
l->sizelem = size;
l->data = xcalloc(l->allocelem, l->sizelem);
return l;
}
int
array_add(array *l, void *data) {
void *new;
/* If we hit the ceiling, do stuff */
if(array_nelems(l) == l->allocelem) {
new = xrealloc(l->data, (l->allocelem + ARRAY_CHUNK) * l->sizelem);
l->data = new;
l->allocelem += ARRAY_CHUNK;
}
memcpy(l->data + (l->nelem*l->sizelem), data, l->sizelem);
l->nelem++;
return 0;
}
int
array_nuke(array *l) {
if(!(l->data))
return 1;
xfree(l->data);
return 0;
}
void *
array_get(array *l, int which) {
if(which >= l->nelem || which < 0)
return NULL;
return l->data + (which * l->sizelem);
}
int
array_delete(array *l, int which) {
void *p;
if(which > l->nelem)
return 1;
p = array_get(l, which);
memcpy(p, p+l->sizelem, (l->nelem*l->sizelem)-((which+1)*l->sizelem));
l->nelem--;
return 0;
}
#ifdef ARRAY_DEBUG
int
main(int argc, char **argv) {
array *l;
int i;
char elem[256];
l = array_init(256);
strcpy(elem, "element 1");
assert(array_add(l, (char *)elem) == 0);
strcpy(elem, "element 2");
assert(array_add(l, (char *)elem) == 0);
strcpy(elem, "element 3");
assert(array_add(l, (char *)elem) == 0);
strcpy(elem, "element 4");
assert(array_add(l, (char *)elem) == 0);
for(i=0; i<array_nelems(l); i++)
printf("%s\n", (char *)array_get(l, i));
array_delete(l, 2);
for(i=0; i<array_nelems(l); i++)
printf("%s\n", (char *)array_get(l, i));
array_nuke(l);
exit(0);
}
#endif
|