| 1 | /* Some commonly-used VEC types. |
| 2 | |
| 3 | Copyright (C) 2012-2018 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GDB. |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation; either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
| 19 | |
| 20 | #include "common-defs.h" |
| 21 | #include "gdb_vecs.h" |
| 22 | #include "host-defs.h" |
| 23 | |
| 24 | /* Call xfree for each element of CHAR_PTR_VEC and final VEC_free for |
| 25 | CHAR_PTR_VEC itself. |
| 26 | |
| 27 | You must not modify CHAR_PTR_VEC after it got registered with this function |
| 28 | by make_cleanup as the CHAR_PTR_VEC base address may change on its updates. |
| 29 | Contrary to VEC_free this function does not (cannot) clear the pointer. */ |
| 30 | |
| 31 | void |
| 32 | free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec) |
| 33 | { |
| 34 | int ix; |
| 35 | char *name; |
| 36 | |
| 37 | for (ix = 0; VEC_iterate (char_ptr, char_ptr_vec, ix, name); ++ix) |
| 38 | xfree (name); |
| 39 | VEC_free (char_ptr, char_ptr_vec); |
| 40 | } |
| 41 | |
| 42 | /* Worker function to split character delimiter separated string of fields |
| 43 | STR into a CHAR_PTR_VEC. */ |
| 44 | |
| 45 | static void |
| 46 | delim_string_to_char_ptr_vec_append (VEC (char_ptr) **vecp, |
| 47 | const char *str, char delimiter) |
| 48 | { |
| 49 | do |
| 50 | { |
| 51 | size_t this_len; |
| 52 | const char *next_field; |
| 53 | char *this_field; |
| 54 | |
| 55 | next_field = strchr (str, delimiter); |
| 56 | if (next_field == NULL) |
| 57 | this_len = strlen (str); |
| 58 | else |
| 59 | { |
| 60 | this_len = next_field - str; |
| 61 | next_field++; |
| 62 | } |
| 63 | |
| 64 | this_field = (char *) xmalloc (this_len + 1); |
| 65 | memcpy (this_field, str, this_len); |
| 66 | this_field[this_len] = '\0'; |
| 67 | VEC_safe_push (char_ptr, *vecp, this_field); |
| 68 | |
| 69 | str = next_field; |
| 70 | } |
| 71 | while (str != NULL); |
| 72 | } |
| 73 | |
| 74 | /* Split STR, a list of DELIMITER-separated fields, into a CHAR_PTR_VEC. |
| 75 | |
| 76 | You may modify the returned strings. |
| 77 | Read free_char_ptr_vec for its cleanup. */ |
| 78 | |
| 79 | VEC (char_ptr) * |
| 80 | delim_string_to_char_ptr_vec (const char *str, char delimiter) |
| 81 | { |
| 82 | VEC (char_ptr) *retval = NULL; |
| 83 | |
| 84 | delim_string_to_char_ptr_vec_append (&retval, str, delimiter); |
| 85 | |
| 86 | return retval; |
| 87 | } |
| 88 | |
| 89 | /* Extended version of dirnames_to_char_ptr_vec - additionally if *VECP is |
| 90 | non-NULL the new list elements from DIRNAMES are appended to the existing |
| 91 | *VECP list of entries. *VECP address will be updated by this call. */ |
| 92 | |
| 93 | void |
| 94 | dirnames_to_char_ptr_vec_append (VEC (char_ptr) **vecp, const char *dirnames) |
| 95 | { |
| 96 | delim_string_to_char_ptr_vec_append (vecp, dirnames, DIRNAME_SEPARATOR); |
| 97 | } |
| 98 | |
| 99 | /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the |
| 100 | elements in their original order. For empty string ("") DIRNAMES return |
| 101 | list of one empty string ("") element. |
| 102 | |
| 103 | You may modify the returned strings. |
| 104 | Read free_char_ptr_vec for its cleanup. */ |
| 105 | |
| 106 | VEC (char_ptr) * |
| 107 | dirnames_to_char_ptr_vec (const char *dirnames) |
| 108 | { |
| 109 | VEC (char_ptr) *retval = NULL; |
| 110 | |
| 111 | dirnames_to_char_ptr_vec_append (&retval, dirnames); |
| 112 | |
| 113 | return retval; |
| 114 | } |