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