Back to index

lightning-sunbird  0.9+nobinonly
icalarray.c
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*-
00002   ======================================================================
00003   FILE: icalarray.c
00004   CREATOR: Damon Chaplin 07 March 2001
00005   
00006   $Id: icalarray.c,v 1.5 2002/06/28 09:30:15 acampi Exp $
00007   $Locker:  $
00008     
00009  (C) COPYRIGHT 2001, Ximian, Inc.
00010 
00011  This program is free software; you can redistribute it and/or modify
00012  it under the terms of either: 
00013 
00014     The LGPL as published by the Free Software Foundation, version
00015     2.1, available at: http://www.fsf.org/copyleft/lesser.html
00016 
00017   Or:
00018 
00019     The Mozilla Public License Version 1.0. You may obtain a copy of
00020     the License at http://www.mozilla.org/MPL/
00021 
00022 
00023  ======================================================================*/
00024 
00031 #ifdef HAVE_CONFIG_H
00032 #include "config.h"
00033 #endif
00034 
00035 #include <stdlib.h>
00036 #include <string.h>
00037 
00038 #include "icalarray.h"
00039 #include "icalerror.h"
00040 
00041 
00042 static void icalarray_expand              (icalarray    *array,
00043                                     int           space_needed);
00044 
00048 icalarray*
00049 icalarray_new               (int           element_size,
00050                              int           increment_size)
00051 {
00052     icalarray *array;
00053 
00054     array = (icalarray*) malloc (sizeof (icalarray));
00055     if (!array) {
00056        icalerror_set_errno(ICAL_NEWFAILED_ERROR);
00057        return NULL;
00058     }
00059 
00060     array->element_size = element_size;
00061     array->increment_size = increment_size;
00062     array->num_elements = 0;
00063     array->space_allocated = 0;
00064     array->data = NULL;
00065 
00066     return array;
00067 }
00068 
00072 void
00073 icalarray_free                     (icalarray    *array)
00074 {
00075     if (array->data)
00076        free (array->data);
00077     free (array);
00078 }
00079 
00080 
00081 void
00082 icalarray_append            (icalarray    *array,
00083                              void         *element)
00084 {
00085     if (array->num_elements >= array->space_allocated)
00086        icalarray_expand (array, 1);
00087 
00088     memcpy ((char *)(array->data) + ( array->num_elements * array->element_size ), element,
00089            array->element_size);
00090     array->num_elements++;
00091 }
00092 
00093 
00094 void*
00095 icalarray_element_at        (icalarray    *array,
00096                              int           position)
00097 {
00098     assert (position >= 0);
00099     assert (position < array->num_elements);
00100 
00101     return (char *)(array->data) + (position * array->element_size);
00102 }
00103 
00104 
00105 void
00106 icalarray_remove_element_at (icalarray    *array,
00107                              int           position)
00108 {
00109     void *dest;
00110     int elements_to_move;
00111 
00112     assert (position >= 0);
00113     assert (position < array->num_elements);
00114 
00115     dest = (char *)array->data + (position * array->element_size);
00116     elements_to_move = array->num_elements - position - 1;
00117 
00118     if (elements_to_move > 0)
00119        memmove (dest, (char *)dest + array->element_size,
00120                elements_to_move * array->element_size);
00121 
00122     array->num_elements--;
00123 }
00124 
00125 
00126 void
00127 icalarray_sort                     (icalarray    *array,
00128                              int          (*compare) (const void *,
00129                                                     const void *))
00130 {
00131     qsort (array->data, array->num_elements, array->element_size, compare);
00132 }
00133 
00134 
00135 static void
00136 icalarray_expand            (icalarray    *array,
00137                              int           space_needed)
00138 {
00139     int new_space_allocated;
00140     void *new_data;
00141 
00142     new_space_allocated = array->space_allocated + array->increment_size;
00143 
00144     if (space_needed > array->increment_size) 
00145        new_space_allocated += space_needed;
00146 
00147        /*
00148     new_data = realloc (array->data,
00149                      new_space_allocated * array->element_size);
00150        */
00151        new_data = malloc(new_space_allocated * array->element_size);
00152        memcpy(new_data,array->data,array->element_size*array->space_allocated);
00153        free(array->data);
00154 
00155     if (new_data) {
00156        array->data = new_data;
00157        array->space_allocated = new_space_allocated;
00158     } else {
00159        icalerror_set_errno(ICAL_ALLOCATION_ERROR);
00160     }
00161 }
00162 
00163