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