/* Core Foundation arrays. Copyright (C) 2001 Johan Rydberg. All Rights Reserved. This file is part of Crust. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include "CFArray.h" #include static void destroy_array (void *instp); static CFTypeID CFArrayTypeID; /* CoreFoundation runtime class for CFArray. */ static CFRuntimeClass CFArrayRuntimeClass = { 0, /* version */ "CFArray", /* Name of class. */ sizeof (struct CFArray), /* Instance size. */ 0, /* Init function. */ destroy_array /* Final function. */ }; static void initialize_array_runtime (void) __attribute__ ((constructor)); /* Initialize runtime class for CFArray. This has a "constructor" attribute so that is should be called at initialization time. */ static void initialize_array_runtime (void) { CFArrayTypeID = CFRuntimeRegisterClass (&CFArrayRuntimeClass); } /* Called when we lost all references to INST. */ void destroy_array (void *inst) { CFArrayRef array = (CFArrayRef) inst; free (array->array); } /* Create a new array from C array ARRAY. COUNT is number of elements in ARRAY. ARRAY can be NULL if count is zero. */ CFArrayRef CFArrayCreate (int count, CFTypeRef *carray) { CFArrayRef array; array = CFRuntimeCreateInstance (CFArrayTypeID); if (count) { array->array = malloc (count * sizeof (CFTypeRef)); memcpy (array->array, carray, count * sizeof (CFTypeRef)); array->count = count; array->max_count = count; } else { /* We reserv space for 20 elements at start. */ count = 20; array->array = malloc (count * sizeof (CFTypeRef)); array->max_count = count; } return array; } /* Returns number of elements in ARRAY. */ int CFArrayCount (CFArrayRef array) { return array->count; } /* Returns element at POSITION in ARRAY. */ CFTypeRef CFArrayAt (CFArrayRef array, int position) { if (position < 0 || position >= array->count) return 0; return array->array [position]; } /* Set element at POSITION in ARRAY to VALUE. POSITION may not be greater than current number of elements + 1. */ void CFArraySetAt (CFArrayRef array, int position, CFTypeRef value) { if (position < 0 || position > array->count) return; /* Check if we must allocate more memory. */ if(position == array->count && array->count == array->max_count) array->array = realloc (array->array, array->max_count <<= 1); array->array [position] = value; array->count++; } /* Convenience function for inserting VALUE at the end of ARRAY. */ void CFArrayAppend (CFArrayRef array, CFTypeRef value) { CFArraySetAt (array, CFArrayCount (array), value); } /* Return position for VALUE in ARRAY. Returns a negative value if VALUE was not to be found in ARRAY. */ int CFArrayValueAt (CFArrayRef array, CFTypeRef value) { int i; for (i = 0; i < array->count; i++) if (array->array [i] == value) return i; return -1; }