libmp3splt
src/splt_array.c
Go to the documentation of this file.
00001 /**********************************************************
00002  *
00003  * libmp3splt -- library based on mp3splt,
00004  *               for mp3/ogg splitting without decoding
00005  *
00006  * Copyright (c) 2002-2005 M. Trotta - <mtrotta@users.sourceforge.net>
00007  * Copyright (c) 2005-2011 Alexandru Munteanu - io_fx@yahoo.fr
00008  *
00009  * http://mp3splt.sourceforge.net
00010  *
00011  *********************************************************/
00012 
00013 /**********************************************************
00014  *
00015  * This program is free software; you can redistribute it and/or
00016  * modify it under the terms of the GNU General Public License
00017  * as published by the Free Software Foundation; either version 2
00018  * of the License, or (at your option) any later version.
00019  *
00020  * This program is distributed in the hope that it will be useful,
00021  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023  * GNU General Public License for more details.
00024  *
00025  * You should have received a copy of the GNU General Public License
00026  * along with this program; if not, write to the Free Software
00027  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
00028  * 02111-1307,
00029  * USA.
00030  *
00031  *********************************************************/
00032 
00039 #include <stdio.h>
00040 #include <stdlib.h>
00041 #include <assert.h>
00042 
00043 #include "splt_array.h"
00044 
00045 splt_array *splt_array_new()
00046 {
00047   splt_array *array = malloc(sizeof(splt_array));
00048   if (array == NULL)
00049   {
00050     return NULL;
00051   }
00052 
00053   array->elements = NULL;
00054   array->number_of_elements = 0;
00055 
00056   return array;
00057 }
00058 
00059 int splt_array_append(splt_array *array, void *element)
00060 {
00061   if (!array || !element)
00062   {
00063     return 2;
00064   }
00065 
00066   if (array->number_of_elements == 0)
00067   {
00068     array->elements = malloc(sizeof(element));
00069     if (!array->elements)
00070     {
00071       return -1;
00072     }
00073     array->elements[0] = element;
00074     array->number_of_elements++;
00075   }
00076   else
00077   {
00078     size_t malloc_number = sizeof(element) * (array->number_of_elements + 1);
00079     void **new_elements = realloc(array->elements, malloc_number);
00080     if (! new_elements)
00081     {
00082       return -1;
00083     }
00084     array->elements = new_elements;
00085     array->elements[array->number_of_elements] = element;
00086     array->number_of_elements++;
00087   }
00088 
00089   return 0;
00090 }
00091 
00092 void splt_array_clear(splt_array *array)
00093 {
00094   if (!array)
00095   {
00096     return;
00097   }
00098 
00099   if (array->elements)
00100   {
00101     free(array->elements);
00102     array->elements = NULL;
00103     array->number_of_elements = 0;
00104   }
00105 }
00106 
00107 void splt_array_free(splt_array **array)
00108 {
00109   if (!array)
00110   {
00111     return;
00112   }
00113 
00114   if (*array)
00115   {
00116     splt_array_clear(*array);
00117     free(*array);
00118     *array = NULL;
00119   }
00120 }
00121 
00122 void *splt_array_get(splt_array *array, int index)
00123 {
00124   if (!array)
00125   {
00126     return NULL;
00127   }
00128 
00129   if (index < 0 || index >= array->number_of_elements)
00130   {
00131     return NULL;
00132   }
00133 
00134   return array->elements[index];
00135 }
00136 
00137 int splt_array_length(splt_array *array)
00138 {
00139   if (!array)
00140   {
00141     return -1;
00142   }
00143 
00144   return array->number_of_elements;
00145 }
00146