aboutsummaryrefslogtreecommitdiffstats
path: root/libical/src/libical/icalarray.c
blob: 9aea671b6458c95d3a9542d5cd87683653434aea (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*-
  ======================================================================
  FILE: icalarray.c
  CREATOR: Damon Chaplin 07 March 2001
  
  $Id$
  $Locker$
    
 (C) COPYRIGHT 2001, Ximian, Inc.

 This program is free software; you can redistribute it and/or modify
 it under the terms of either: 

    The LGPL as published by the Free Software Foundation, version
    2.1, available at: http://www.fsf.org/copyleft/lesser.html

  Or:

    The Mozilla Public License Version 1.0. You may obtain a copy of
    the License at http://www.mozilla.org/MPL/


 ======================================================================*/

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#include "icalarray.h"
#include "icalerror.h"


static void icalarray_expand        (icalarray  *array,
                     int         space_needed);


icalarray*
icalarray_new           (int         element_size,
                 int         increment_size)
{
    icalarray *array;

    array = (icalarray*) malloc (sizeof (icalarray));
    if (!array) {
    icalerror_set_errno(ICAL_NEWFAILED_ERROR);
    return NULL;
    }

    array->element_size = element_size;
    array->increment_size = increment_size;
    array->num_elements = 0;
    array->space_allocated = 0;
    array->data = NULL;

    return array;
}


void
icalarray_free          (icalarray  *array)
{
    if (array->data)
    free (array->data);
    free (array);
}


void
icalarray_append        (icalarray  *array,
                 void       *element)
{
    if (array->num_elements >= array->space_allocated)
    icalarray_expand (array, 1);

    memcpy (array->data + array->num_elements * array->element_size, element,
        array->element_size);
    array->num_elements++;
}


void*
icalarray_element_at        (icalarray  *array,
                 int         position)
{
    assert (position >= 0);
    assert (position < array->num_elements);

    return array->data + position * array->element_size;
}


void
icalarray_remove_element_at (icalarray  *array,
                 int         position)
{
    void *dest;
    int elements_to_move;

    assert (position >= 0);
    assert (position < array->num_elements);

    dest = array->data + position * array->element_size;
    elements_to_move = array->num_elements - position - 1;

    if (elements_to_move > 0)
    memmove (dest, dest + array->element_size,
         elements_to_move * array->element_size);

    array->num_elements--;
}


void
icalarray_sort          (icalarray  *array,
                 int           (*compare) (const void *,
                               const void *))
{
    qsort (array->data, array->num_elements, array->element_size, compare);
}


static void
icalarray_expand        (icalarray  *array,
                 int         space_needed)
{
    int new_space_allocated;
    void *new_data;

    new_space_allocated = array->space_allocated + array->increment_size;

    if (space_needed > array->increment_size) 
    new_space_allocated += space_needed;

    new_data = realloc (array->data,
            new_space_allocated * array->element_size);
    if (new_data) {
    array->data = new_data;
    array->space_allocated = new_space_allocated;
    } else {
    icalerror_set_errno(ICAL_ALLOCATION_ERROR);
    }
}