Commit | Line | Data |
---|---|---|
48faced0 DE |
1 | /* Some commonly-used VEC types. |
2 | ||
32d0add0 | 3 | Copyright (C) 2012-2015 Free Software Foundation, Inc. |
48faced0 DE |
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 | ||
727605ca | 20 | #include "common-defs.h" |
48faced0 DE |
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 | ||
749234e5 DE |
42 | /* Worker function to split character delimiter separated string of fields |
43 | STR into a CHAR_PTR_VEC. */ | |
48faced0 | 44 | |
749234e5 DE |
45 | static void |
46 | delim_string_to_char_ptr_vec_append (VEC (char_ptr) **vecp, | |
47 | const char *str, char delimiter) | |
48faced0 DE |
48 | { |
49 | do | |
50 | { | |
51 | size_t this_len; | |
749234e5 | 52 | char *next_field, *this_field; |
48faced0 | 53 | |
749234e5 DE |
54 | next_field = strchr (str, delimiter); |
55 | if (next_field == NULL) | |
56 | this_len = strlen (str); | |
48faced0 DE |
57 | else |
58 | { | |
749234e5 DE |
59 | this_len = next_field - str; |
60 | next_field++; | |
48faced0 DE |
61 | } |
62 | ||
749234e5 DE |
63 | this_field = xmalloc (this_len + 1); |
64 | memcpy (this_field, str, this_len); | |
65 | this_field[this_len] = '\0'; | |
66 | VEC_safe_push (char_ptr, *vecp, this_field); | |
48faced0 | 67 | |
749234e5 | 68 | str = next_field; |
48faced0 | 69 | } |
749234e5 DE |
70 | while (str != NULL); |
71 | } | |
72 | ||
73 | /* Split STR, a list of DELIMITER-separated fields, into a CHAR_PTR_VEC. | |
74 | ||
75 | You may modify the returned strings. | |
76 | Read free_char_ptr_vec for its cleanup. */ | |
77 | ||
78 | VEC (char_ptr) * | |
79 | delim_string_to_char_ptr_vec (const char *str, char delimiter) | |
80 | { | |
81 | VEC (char_ptr) *retval = NULL; | |
82 | ||
83 | delim_string_to_char_ptr_vec_append (&retval, str, delimiter); | |
84 | ||
85 | return retval; | |
86 | } | |
87 | ||
88 | /* Extended version of dirnames_to_char_ptr_vec - additionally if *VECP is | |
89 | non-NULL the new list elements from DIRNAMES are appended to the existing | |
90 | *VECP list of entries. *VECP address will be updated by this call. */ | |
91 | ||
92 | void | |
93 | dirnames_to_char_ptr_vec_append (VEC (char_ptr) **vecp, const char *dirnames) | |
94 | { | |
95 | delim_string_to_char_ptr_vec_append (vecp, dirnames, DIRNAME_SEPARATOR); | |
48faced0 DE |
96 | } |
97 | ||
98 | /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the | |
99 | elements in their original order. For empty string ("") DIRNAMES return | |
100 | list of one empty string ("") element. | |
101 | ||
102 | You may modify the returned strings. | |
103 | Read free_char_ptr_vec for its cleanup. */ | |
104 | ||
105 | VEC (char_ptr) * | |
106 | dirnames_to_char_ptr_vec (const char *dirnames) | |
107 | { | |
108 | VEC (char_ptr) *retval = NULL; | |
109 | ||
110 | dirnames_to_char_ptr_vec_append (&retval, dirnames); | |
111 | ||
112 | return retval; | |
113 | } |