xref: /dragonfly/contrib/gdb-7/gdb/common/gdb_vecs.c (revision cfd1aba3)
1 /* Some commonly-used VEC types.
2 
3    Copyright (C) 2012-2013 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 /* Extended version of dirnames_to_char_ptr_vec - additionally if *VECP is
48    non-NULL the new list elements from DIRNAMES are appended to the existing
49    *VECP list of entries.  *VECP address will be updated by this call.  */
50 
51 void
52 dirnames_to_char_ptr_vec_append (VEC (char_ptr) **vecp, const char *dirnames)
53 {
54   do
55     {
56       size_t this_len;
57       char *next_dir, *this_dir;
58 
59       next_dir = strchr (dirnames, DIRNAME_SEPARATOR);
60       if (next_dir == NULL)
61 	this_len = strlen (dirnames);
62       else
63 	{
64 	  this_len = next_dir - dirnames;
65 	  next_dir++;
66 	}
67 
68       this_dir = xmalloc (this_len + 1);
69       memcpy (this_dir, dirnames, this_len);
70       this_dir[this_len] = '\0';
71       VEC_safe_push (char_ptr, *vecp, this_dir);
72 
73       dirnames = next_dir;
74     }
75   while (dirnames != NULL);
76 }
77 
78 /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the
79    elements in their original order.  For empty string ("") DIRNAMES return
80    list of one empty string ("") element.
81 
82    You may modify the returned strings.
83    Read free_char_ptr_vec for its cleanup.  */
84 
85 VEC (char_ptr) *
86 dirnames_to_char_ptr_vec (const char *dirnames)
87 {
88   VEC (char_ptr) *retval = NULL;
89 
90   dirnames_to_char_ptr_vec_append (&retval, dirnames);
91 
92   return retval;
93 }
94