guowenxue
2020-08-21 02f4d9518378031c63df7a36c49d8b2eabdaab90
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
164
165
166
/*********************************************************************************
 *      Copyright:  (C) 2012 Guo Wenxue<guowenxue@gmail.com>  
 *                  All rights reserved.
 *
 *       Filename:  cp_array.c
 *    Description:  This file is a dynamic array implement
 *                 
 *        Version:  1.0.0(12/20/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "12/20/2012 01:48:27 PM"
 *                 
 ********************************************************************************/
 
#include <stdlib.h>
#include "cp_array.h"
#include "cp_common.h"
#include "cp_logger.h"
 
CP_ARRAY *cp_array_init(CP_ARRAY *array, int size)
{
    int        i;
 
    if( !array )
    {
        if( !(array=t_malloc(sizeof(*array))) )
        {
            return NULL;
        }
        else
        {
            memset(array, 0, sizeof(*array));
            array->flag |= CP_ARRAY_FLAG_MALLOC; 
        }
    }
    else
    {
        /* array is a variable,so clear it */
        memset(array, 0, sizeof(*array));
    }
 
    array->size = size;
    if( !array->data && !(array->data=(void **)malloc(array->size*sizeof(void *))) )
    {
        cp_array_term(array);
        return NULL;
    }
 
    for(i=0; i<array->size; i++)
    {
        array->data[i] = NULL;
    }
 
    return array;
}
 
void cp_array_term(CP_ARRAY *array)
{
    if(!array)
        return; 
 
    if(array->data)
        t_free(array->data);
 
    if(array->flag&CP_ARRAY_FLAG_MALLOC)
    {
        t_free(array);
    }
 
    return ;
}
 
 
int cp_array_add(CP_ARRAY *array, void *data)
{
    int              i;
    void             *entry;
 
    if(!array || !data)
    {
        log_err("Invalude input arguments\n");
        return -1;
    }
 
    /* array already full */
    if(array->items >= array->size)
    {
        log_err("array is full,can not add data [%p]\n", data);
        return -2;
    }
    
    /* We don't start the array from 0 but 1 */
    cp_list_array_for_each(array, i, entry)
    {
        if( !entry )
        {
            log_dbg("Array[%d] add data[%p] ok\n", i, data);
            array->data[i] = data; 
            array->items ++;
            break;
        }
    }
 
    return i;
}
 
void cp_array_rm_byindex(CP_ARRAY *array, int index)
{
    /* We don't start the array from 0 but 1 */
    if(!array || index<0)
    {
        log_err("Invalude input arguments\n");
        return;
    }
 
    if(array->data[index])
    { 
        array->items --;
        array->data[index] = NULL;
    }
}
 
int cp_array_rm_bydata(CP_ARRAY *array, void *data)
{
    int              i, rv = -3;
    void             *entry;
 
    if(!array || !data)
    {
        log_err("Invalude input arguments\n");
        return -1;
    }
 
    if(array->items <= 0)
    {
        log_err("array is empty,can not remove data [%p]\n", data);
        return -2;
    }
 
    /* We don't start the array from 0 but 1 */
    cp_list_array_for_each(array, i, entry)
    {
        if( entry == data )
        {
            array->items --;
            array->data[i] = NULL;
            rv = 0;
            break;
        }
    }
 
    return rv;
}
 
void cp_array_travel(CP_ARRAY *array)
{
    int              i;
    void             *data;
 
    /* We don't start the array from 0 but 1 */
    cp_list_array_for_each(array, i, data)
    {
        log_dbg("array data[%d] save data [%p]\n", i, data);
    }
 
    return ;
}