libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2010, 2013 Peter Miller 00004 * Written by Peter Miller <pmiller@opensource.org.au> 00005 * 00006 * This program is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as 00008 * published by the Free Software Foundation; either version 3 of the 00009 * License, or (at your option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00018 */ 00019 00020 #include <libexplain/ac/stdlib.h> 00021 00022 #include <libexplain/printf_format.h> 00023 00024 00025 void 00026 explain_printf_format_list_constructor(explain_printf_format_list_t *lp) 00027 { 00028 lp->length = 0; 00029 lp->maximum = 0; 00030 lp->list = 0; 00031 } 00032 00033 void 00034 explain_printf_format_list_destructor(explain_printf_format_list_t *lp) 00035 { 00036 if (lp->list) 00037 free(lp->list); 00038 lp->length = 0; 00039 lp->maximum = 0; 00040 lp->list = 0; 00041 } 00042 00043 void 00044 explain_printf_format_list_clear(explain_printf_format_list_t *lp) 00045 { 00046 lp->length = 0; 00047 } 00048 00049 00050 int 00051 explain_printf_format_list_append(explain_printf_format_list_t *lp, 00052 const explain_printf_format_t *datum) 00053 { 00054 if (lp->length >= lp->maximum) 00055 { 00056 size_t j; 00057 size_t new_maximum; 00058 size_t nbytes; 00059 explain_printf_format_t *new_list; 00060 00061 new_maximum = lp->maximum * 2 + 8; 00062 nbytes = new_maximum * sizeof(lp->list[0]); 00063 new_list = malloc(nbytes); 00064 if (!new_list) 00065 return -1; 00066 for (j = 0; j < lp->length; ++j) 00067 new_list[j] = lp->list[j]; 00068 if (lp->list) 00069 free(lp->list); 00070 lp->list = new_list; 00071 lp->maximum = new_maximum; 00072 } 00073 lp->list[lp->length++] = *datum; 00074 return 0; 00075 } 00076 00077 00078 static int 00079 cmp(const void *va, const void *vb) 00080 { 00081 const explain_printf_format_t *a = va; 00082 const explain_printf_format_t *b = vb; 00083 return (a->index - b->index); 00084 } 00085 00086 00087 void 00088 explain_printf_format_list_sort(explain_printf_format_list_t *lp) 00089 { 00090 if (lp->length >= 2) 00091 qsort(lp->list, lp->length, sizeof(lp->list[0]), cmp); 00092 } 00093 00094 00095 /* vim: set ts=8 sw=4 et : */