libexplain
1.4.D001
|
00001 /* 00002 * libexplain - Explain errno values returned by libc functions 00003 * Copyright (C) 2009-2011, 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 it 00007 * under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 3 of the License, or (at your 00009 * option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, but WITHOUT 00012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00014 * 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/ctype.h> 00021 #include <libexplain/ac/stdlib.h> 00022 #include <libexplain/ac/string.h> 00023 00024 #include <libexplain/malloc.h> 00025 #include <libexplain/string_list.h> 00026 #include <libexplain/strndup.h> 00027 00028 00029 void 00030 explain_string_list_constructor(explain_string_list_t *slp) 00031 { 00032 slp->length = 0; 00033 slp->maximum = 0; 00034 slp->string = 0; 00035 } 00036 00037 00038 void 00039 explain_string_list_destructor(explain_string_list_t *slp) 00040 { 00041 size_t j; 00042 00043 for (j = 0; j < slp->length; ++j) 00044 { 00045 free(slp->string[j]); 00046 slp->string[j] = 0; 00047 } 00048 slp->length = 0; 00049 slp->maximum = 0; 00050 if (slp->string) 00051 { 00052 free(slp->string); 00053 slp->string = 0; 00054 } 00055 } 00056 00057 00058 static int 00059 explain_string_list_member(const explain_string_list_t *slp, const char *text) 00060 { 00061 size_t j; 00062 00063 for (j = 0; j < slp->length; ++j) 00064 if (0 == strcmp(slp->string[j], text)) 00065 return 1; 00066 return 0; 00067 } 00068 00069 00070 void 00071 explain_string_list_append_unique(explain_string_list_t *slp, const char *text) 00072 { 00073 if (!explain_string_list_member(slp, text)) 00074 explain_string_list_append(slp, text); 00075 } 00076 00077 00078 void 00079 explain_string_list_append(explain_string_list_t *slp, const char *text) 00080 { 00081 explain_string_list_append_n(slp, text, strlen(text)); 00082 } 00083 00084 00085 void 00086 explain_string_list_append_n(explain_string_list_t *slp, const char *text, 00087 size_t text_size) 00088 { 00089 if (slp->length >= slp->maximum) 00090 { 00091 char **new_string; 00092 size_t j; 00093 00094 slp->maximum = slp->maximum * 2 + 16; 00095 new_string = explain_malloc_or_die(slp->maximum * sizeof(*new_string)); 00096 for (j = 0; j < slp->length; ++j) 00097 new_string[j] = slp->string[j]; 00098 if (slp->string) 00099 free(slp->string); 00100 slp->string = new_string; 00101 } 00102 slp->string[slp->length++] = explain_strndup_or_die(text, text_size); 00103 } 00104 00105 00106 void 00107 explain_string_list_split(explain_string_list_t *result, const char *text) 00108 { 00109 for (;;) 00110 { 00111 unsigned char c; 00112 const char *start; 00113 00114 start = text; 00115 c = *text++; 00116 if (!c) 00117 return; 00118 if (isspace(c)) 00119 continue; 00120 for (;;) 00121 { 00122 c = *text; 00123 if (!c || isspace(c)) 00124 break; 00125 ++text; 00126 } 00127 explain_string_list_append_n(result, start, text - start); 00128 } 00129 } 00130 00131 00132 static int 00133 cmp(const void *a, const void *b) 00134 { 00135 return strcmp(*(const char **)a, *(const char **)b); 00136 } 00137 00138 00139 void 00140 explain_string_list_sort(explain_string_list_t *slp) 00141 { 00142 if (slp->length >= 2) 00143 qsort(slp->string, slp->length, sizeof(slp->string[0]), cmp); 00144 } 00145 00146 00147 /* vim: set ts=8 sw=4 et : */