| 1 | /* Copyright (C) 2007 MySQL AB |
| 2 | Use is subject to license terms |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; version 2 of the License. |
| 7 | |
| 8 | This program is distributed in the hope that it will be useful, |
| 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | GNU General Public License for more details. |
| 12 | |
| 13 | You should have received a copy of the GNU General Public License |
| 14 | along with this program; if not, write to the Free Software |
| 15 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */ |
| 16 | |
| 17 | #include "mysys_priv.h" |
| 18 | #include <m_string.h> /* strcmp() */ |
| 19 | |
| 20 | |
| 21 | /** |
| 22 | Append str to array, or move to the end if it already exists |
| 23 | |
| 24 | @param str String to be appended |
| 25 | @param array The array, terminated by a NULL element, all unused elements |
| 26 | pre-initialized to NULL |
| 27 | @param size Size of the array; array must be terminated by a NULL |
| 28 | pointer, so can hold size - 1 elements |
| 29 | |
| 30 | @retval FALSE Success |
| 31 | @retval TRUE Failure, array is full |
| 32 | */ |
| 33 | |
| 34 | my_bool array_append_string_unique(const char *str, |
| 35 | const char **array, size_t size) |
| 36 | { |
| 37 | const char **p; |
| 38 | /* end points at the terminating NULL element */ |
| 39 | const char **end= array + size - 1; |
| 40 | DBUG_ASSERT(*end == NULL); |
| 41 | |
| 42 | for (p= array; *p; ++p) |
| 43 | { |
| 44 | if (strcmp(*p, str) == 0) |
| 45 | break; |
| 46 | } |
| 47 | if (p >= end) |
| 48 | return TRUE; /* Array is full */ |
| 49 | |
| 50 | DBUG_ASSERT(*p == NULL || strcmp(*p, str) == 0); |
| 51 | |
| 52 | while (*(p + 1)) |
| 53 | { |
| 54 | *p= *(p + 1); |
| 55 | ++p; |
| 56 | } |
| 57 | |
| 58 | DBUG_ASSERT(p < end); |
| 59 | *p= str; |
| 60 | |
| 61 | return FALSE; /* Success */ |
| 62 | } |
| 63 | |