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
|
/** \file
* \brief Simple expandable array
*
* See Copyright Notice in "iup.h"
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <memory.h>
#include "iup_array.h"
#include "iup_assert.h"
struct _Iarray
{
void* data;
int count;
int max_count;
int elem_size;
int start_count;
};
Iarray* iupArrayCreate(int start_count, int elem_size)
{
Iarray* iarray = (Iarray*)malloc(sizeof(Iarray));
iarray->count = 0;
iarray->elem_size = elem_size;
iarray->max_count = start_count;
iarray->start_count = start_count;
iarray->data = malloc(elem_size*start_count);
iupASSERT(iarray->data!=NULL);
if (!iarray->data)
{
free(iarray);
return NULL;
}
memset(iarray->data, 0, elem_size*start_count);
return iarray;
}
void iupArrayDestroy(Iarray* iarray)
{
iupASSERT(iarray!=NULL);
if (!iarray)
return;
if (iarray->data)
{
memset(iarray->data, 0, iarray->elem_size*iarray->max_count);
free(iarray->data);
}
free(iarray);
}
void* iupArrayGetData(Iarray* iarray)
{
iupASSERT(iarray!=NULL);
if (!iarray)
return NULL;
return iarray->data;
}
void* iupArrayInc(Iarray* iarray)
{
iupASSERT(iarray!=NULL);
if (!iarray)
return NULL;
if (iarray->count >= iarray->max_count)
{
int old_count = iarray->max_count;
iarray->max_count += iarray->start_count;
iarray->data = realloc(iarray->data, iarray->elem_size*iarray->max_count);
iupASSERT(iarray->data!=NULL);
if (!iarray->data)
return NULL;
memset((unsigned char*)iarray->data + iarray->elem_size*old_count, 0, iarray->elem_size*(iarray->max_count-old_count));
}
iarray->count++;
return iarray->data;
}
void* iupArrayAdd(Iarray* iarray, int new_count)
{
iupASSERT(iarray!=NULL);
if (!iarray)
return NULL;
if (iarray->count+new_count > iarray->max_count)
{
int old_count = iarray->max_count;
iarray->max_count += new_count;
iarray->data = realloc(iarray->data, iarray->elem_size*iarray->max_count);
iupASSERT(iarray->data!=NULL);
if (!iarray->data)
return NULL;
memset((unsigned char*)iarray->data + iarray->elem_size*old_count, 0, iarray->elem_size*(iarray->max_count-old_count));
}
iarray->count += new_count;
return iarray->data;
}
int iupArrayCount(Iarray* iarray)
{
iupASSERT(iarray!=NULL);
if (!iarray)
return 0;
return iarray->count;
}
|