blob: 832afee72903fbce3188b18cf57054f58dc0b324 [file] [log] [blame]
Benny Prijono5dcb38d2005-11-21 01:55:47 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5dcb38d2005-11-21 01:55:47 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include <pj/array.h>
20#include <pj/string.h>
21#include <pj/assert.h>
22#include <pj/errno.h>
23
24PJ_DEF(void) pj_array_insert( void *array,
25 unsigned elem_size,
26 unsigned count,
27 unsigned pos,
28 const void *value)
29{
Benny Prijono54a4f3d2007-02-20 14:58:40 +000030 if (count && pos < count) {
Benny Prijono5dcb38d2005-11-21 01:55:47 +000031 pj_memmove( (char*)array + (pos+1)*elem_size,
32 (char*)array + pos*elem_size,
33 (count-pos)*elem_size);
34 }
35 pj_memmove((char*)array + pos*elem_size, value, elem_size);
36}
37
38PJ_DEF(void) pj_array_erase( void *array,
39 unsigned elem_size,
40 unsigned count,
41 unsigned pos)
42{
43 pj_assert(count != 0);
44 if (pos < count-1) {
45 pj_memmove( (char*)array + pos*elem_size,
46 (char*)array + (pos+1)*elem_size,
47 (count-pos-1)*elem_size);
48 }
49}
50
51PJ_DEF(pj_status_t) pj_array_find( const void *array,
52 unsigned elem_size,
53 unsigned count,
54 pj_status_t (*matching)(const void *value),
55 void **result)
56{
57 unsigned i;
Benny Prijonof260e462007-04-30 21:03:32 +000058 const char *char_array = (const char*)array;
Benny Prijono5dcb38d2005-11-21 01:55:47 +000059 for (i=0; i<count; ++i) {
60 if ( (*matching)(char_array) == PJ_SUCCESS) {
61 if (result) {
62 *result = (void*)char_array;
63 }
64 return PJ_SUCCESS;
65 }
66 char_array += elem_size;
67 }
68 return PJ_ENOTFOUND;
69}
70