blob: fb6a17641103ba88bee1ca0f9dc6094b3a3524b5 [file] [log] [blame]
Benny Prijono4766ffe2005-11-01 17:56:59 +00001/* $Id$
2 *
Benny Prijonodd859a62005-11-01 16:42:51 +00003 */
4#include <pj/array.h>
5#include <pj/string.h>
6#include <pj/assert.h>
7#include <pj/errno.h>
8
9PJ_DEF(void) pj_array_insert( void *array,
10 unsigned elem_size,
11 unsigned count,
12 unsigned pos,
13 const void *value)
14{
15 if (count && pos < count-1) {
16 pj_memmove( (char*)array + (pos+1)*elem_size,
17 (char*)array + pos*elem_size,
18 (count-pos)*elem_size);
19 }
20 pj_memmove((char*)array + pos*elem_size, value, elem_size);
21}
22
23PJ_DEF(void) pj_array_erase( void *array,
24 unsigned elem_size,
25 unsigned count,
26 unsigned pos)
27{
28 pj_assert(count != 0);
29 if (pos < count-1) {
30 pj_memmove( (char*)array + pos*elem_size,
31 (char*)array + (pos+1)*elem_size,
32 (count-pos-1)*elem_size);
33 }
34}
35
36PJ_DEF(pj_status_t) pj_array_find( const void *array,
37 unsigned elem_size,
38 unsigned count,
39 pj_status_t (*matching)(const void *value),
40 void **result)
41{
42 unsigned i;
43 const char *char_array = array;
44 for (i=0; i<count; ++i) {
45 if ( (*matching)(char_array) == PJ_SUCCESS) {
46 if (result) {
47 *result = (void*)char_array;
48 }
49 return PJ_SUCCESS;
50 }
51 char_array += elem_size;
52 }
53 return PJ_ENOTFOUND;
54}
55