xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/gdb_vecs.cc (revision f8cf1a9151c7af1cb0bd8b09c13c66bca599c027)
1 /* Some commonly-used VEC types.
2 
3    Copyright (C) 2012-2024 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 "gdb_vecs.h"
21 #include "host-defs.h"
22 
23 /* Worker function to split character delimiter separated string of fields
24    STR into a char pointer vector.  */
25 
26 static void
27 delim_string_to_char_ptr_vec_append
28   (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *str,
29    char delimiter)
30 {
31   do
32     {
33       size_t this_len;
34       const char *next_field;
35       char *this_field;
36 
37       next_field = strchr (str, delimiter);
38       if (next_field == NULL)
39 	this_len = strlen (str);
40       else
41 	{
42 	  this_len = next_field - str;
43 	  next_field++;
44 	}
45 
46       this_field = (char *) xmalloc (this_len + 1);
47       memcpy (this_field, str, this_len);
48       this_field[this_len] = '\0';
49       vecp->emplace_back (this_field);
50 
51       str = next_field;
52     }
53   while (str != NULL);
54 }
55 
56 /* See gdb_vecs.h.  */
57 
58 std::vector<gdb::unique_xmalloc_ptr<char>>
59 delim_string_to_char_ptr_vec (const char *str, char delimiter)
60 {
61   std::vector<gdb::unique_xmalloc_ptr<char>> retval;
62 
63   delim_string_to_char_ptr_vec_append (&retval, str, delimiter);
64 
65   return retval;
66 }
67 
68 /* See gdb_vecs.h.  */
69 
70 void
71 dirnames_to_char_ptr_vec_append
72   (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *dirnames)
73 {
74   delim_string_to_char_ptr_vec_append (vecp, dirnames, DIRNAME_SEPARATOR);
75 }
76 
77 /* See gdb_vecs.h.  */
78 
79 std::vector<gdb::unique_xmalloc_ptr<char>>
80 dirnames_to_char_ptr_vec (const char *dirnames)
81 {
82   std::vector<gdb::unique_xmalloc_ptr<char>> retval;
83 
84   dirnames_to_char_ptr_vec_append (&retval, dirnames);
85 
86   return retval;
87 }
88