blob: 06a3ad1ab4e76358c00ccd4506d1d5049dbabe97 [file] [log] [blame]
Benny Prijonodd859a62005-11-01 16:42:51 +00001/* $Header: /pjproject-0.3/pjlib/src/pj/array.c 5 10/14/05 12:26a Bennylp $ */
2/* $Log: /pjproject-0.3/pjlib/src/pj/array.c $
3 *
4 * 5 10/14/05 12:26a Bennylp
5 * Finished error code framework, some fixes in ioqueue, etc. Pretty
6 * major.
7 *
8 * 4 9/17/05 10:37a Bennylp
9 * Major reorganization towards version 0.3.
10 *
11 */
12#include <pj/array.h>
13#include <pj/string.h>
14#include <pj/assert.h>
15#include <pj/errno.h>
16
17PJ_DEF(void) pj_array_insert( void *array,
18 unsigned elem_size,
19 unsigned count,
20 unsigned pos,
21 const void *value)
22{
23 if (count && pos < count-1) {
24 pj_memmove( (char*)array + (pos+1)*elem_size,
25 (char*)array + pos*elem_size,
26 (count-pos)*elem_size);
27 }
28 pj_memmove((char*)array + pos*elem_size, value, elem_size);
29}
30
31PJ_DEF(void) pj_array_erase( void *array,
32 unsigned elem_size,
33 unsigned count,
34 unsigned pos)
35{
36 pj_assert(count != 0);
37 if (pos < count-1) {
38 pj_memmove( (char*)array + pos*elem_size,
39 (char*)array + (pos+1)*elem_size,
40 (count-pos-1)*elem_size);
41 }
42}
43
44PJ_DEF(pj_status_t) pj_array_find( const void *array,
45 unsigned elem_size,
46 unsigned count,
47 pj_status_t (*matching)(const void *value),
48 void **result)
49{
50 unsigned i;
51 const char *char_array = array;
52 for (i=0; i<count; ++i) {
53 if ( (*matching)(char_array) == PJ_SUCCESS) {
54 if (result) {
55 *result = (void*)char_array;
56 }
57 return PJ_SUCCESS;
58 }
59 char_array += elem_size;
60 }
61 return PJ_ENOTFOUND;
62}
63