summaryrefslogtreecommitdiffstats
path: root/src/libply/ply-array.c
blob: 00f4a534781afa58bc30c7639b4de192914528af (plain)
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
/* ply-array.c - linked array implementation
 *
 * Copyright (C) 2008 Red Hat, Inc.
 *
 * 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, 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.
 *
 * Written by: Ray Strode <rstrode@redhat.com>
 */
#include "config.h"
#include "ply-array.h"

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

#include "ply-buffer.h"

static const void *null_terminator = NULL;

struct _ply_array
{
  ply_buffer_t *buffer;
};

ply_array_t *
ply_array_new (void)
{
  ply_array_t *array;

  array = calloc (1, sizeof (ply_array_t));

  array->buffer = ply_buffer_new ();

  ply_buffer_append_bytes (array->buffer, &null_terminator, sizeof (const void *));

  return array;
}

void
ply_array_free (ply_array_t *array)
{
  if (array == NULL)
    return;

  ply_buffer_free (array->buffer);

  free (array);
}

int
ply_array_get_size (ply_array_t *array)
{
  int size;

  size = (ply_buffer_get_size (array->buffer) / sizeof (const void *)) - 1;

  return size;
}

void
ply_array_add_element (ply_array_t *array,
                       const void  *data)
{
  /* Temporarily remove NULL terminator
   */
  ply_buffer_remove_bytes_at_end (array->buffer, sizeof (const void *));

  ply_buffer_append_bytes (array->buffer, &data, sizeof (const void *));

  /* Add NULL terminator back
   */
  ply_buffer_append_bytes (array->buffer, &null_terminator, sizeof (const void *));
}

void * const *
ply_array_get_elements (ply_array_t *array)
{
  return (void * const *) ply_buffer_get_bytes (array->buffer);
}

void **
ply_array_steal_elements (ply_array_t *array)
{
  void **data;

  data = (void **) ply_buffer_steal_bytes (array->buffer);

  ply_buffer_append_bytes (array->buffer, &null_terminator, sizeof (const void *));

  return data;
}

#ifdef PLY_ARRAY_ENABLE_TEST
#include <stdio.h>

int
main (int    argc,
      char **argv)
{
  ply_array_t *array;
  int i;
  char **data;

  array = ply_array_new ();

  ply_array_add_element (array, "foo");
  ply_array_add_element (array, "bar");
  ply_array_add_element (array, "baz");
  ply_array_add_element (array, "qux");

  data = (char **) ply_array_get_elements (array);
  for (i = 0; data[i] != NULL; i++)
    {
      printf ("element '%d' has data '%s'\n", i, data[i]);
      i++;
    }

  ply_array_free (array);
  return 0;
}

#endif
/* vim: set ts=4 sw=4 expandtab autoindent cindent cino={.5s,(0: */