Add delim_string_to_char_ptr_vec.
[deliverable/binutils-gdb.git] / gdb / common / gdb_vecs.c
1 /* Some commonly-used VEC types.
2
3 Copyright (C) 2012-2014 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 #ifdef GDBSERVER
21 #include "server.h"
22 #else
23 #include "defs.h"
24 #endif
25
26 #include "gdb_vecs.h"
27 #include "host-defs.h"
28
29 /* Call xfree for each element of CHAR_PTR_VEC and final VEC_free for
30 CHAR_PTR_VEC itself.
31
32 You must not modify CHAR_PTR_VEC after it got registered with this function
33 by make_cleanup as the CHAR_PTR_VEC base address may change on its updates.
34 Contrary to VEC_free this function does not (cannot) clear the pointer. */
35
36 void
37 free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec)
38 {
39 int ix;
40 char *name;
41
42 for (ix = 0; VEC_iterate (char_ptr, char_ptr_vec, ix, name); ++ix)
43 xfree (name);
44 VEC_free (char_ptr, char_ptr_vec);
45 }
46
47 /* Worker function to split character delimiter separated string of fields
48 STR into a CHAR_PTR_VEC. */
49
50 static void
51 delim_string_to_char_ptr_vec_append (VEC (char_ptr) **vecp,
52 const char *str, char delimiter)
53 {
54 do
55 {
56 size_t this_len;
57 char *next_field, *this_field;
58
59 next_field = strchr (str, delimiter);
60 if (next_field == NULL)
61 this_len = strlen (str);
62 else
63 {
64 this_len = next_field - str;
65 next_field++;
66 }
67
68 this_field = xmalloc (this_len + 1);
69 memcpy (this_field, str, this_len);
70 this_field[this_len] = '\0';
71 VEC_safe_push (char_ptr, *vecp, this_field);
72
73 str = next_field;
74 }
75 while (str != NULL);
76 }
77
78 /* Split STR, a list of DELIMITER-separated fields, into a CHAR_PTR_VEC.
79
80 You may modify the returned strings.
81 Read free_char_ptr_vec for its cleanup. */
82
83 VEC (char_ptr) *
84 delim_string_to_char_ptr_vec (const char *str, char delimiter)
85 {
86 VEC (char_ptr) *retval = NULL;
87
88 delim_string_to_char_ptr_vec_append (&retval, str, delimiter);
89
90 return retval;
91 }
92
93 /* Extended version of dirnames_to_char_ptr_vec - additionally if *VECP is
94 non-NULL the new list elements from DIRNAMES are appended to the existing
95 *VECP list of entries. *VECP address will be updated by this call. */
96
97 void
98 dirnames_to_char_ptr_vec_append (VEC (char_ptr) **vecp, const char *dirnames)
99 {
100 delim_string_to_char_ptr_vec_append (vecp, dirnames, DIRNAME_SEPARATOR);
101 }
102
103 /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the
104 elements in their original order. For empty string ("") DIRNAMES return
105 list of one empty string ("") element.
106
107 You may modify the returned strings.
108 Read free_char_ptr_vec for its cleanup. */
109
110 VEC (char_ptr) *
111 dirnames_to_char_ptr_vec (const char *dirnames)
112 {
113 VEC (char_ptr) *retval = NULL;
114
115 dirnames_to_char_ptr_vec_append (&retval, dirnames);
116
117 return retval;
118 }
This page took 0.047949 seconds and 4 git commands to generate.