Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 4 | * |
| 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 | |
| 24 | PJ_DEF(void) pj_array_insert( void *array, |
| 25 | unsigned elem_size, |
| 26 | unsigned count, |
| 27 | unsigned pos, |
| 28 | const void *value) |
| 29 | { |
Benny Prijono | 54a4f3d | 2007-02-20 14:58:40 +0000 | [diff] [blame] | 30 | if (count && pos < count) { |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 31 | 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 | |
| 38 | PJ_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 | |
| 51 | PJ_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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 58 | const char *char_array = (const char*)array; |
Benny Prijono | 5dcb38d | 2005-11-21 01:55:47 +0000 | [diff] [blame] | 59 | 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 | |