gdb_vecs.cc 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* Some commonly-used VEC types.
  2. Copyright (C) 2012-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "common-defs.h"
  15. #include "gdb_vecs.h"
  16. #include "host-defs.h"
  17. /* Worker function to split character delimiter separated string of fields
  18. STR into a char pointer vector. */
  19. static void
  20. delim_string_to_char_ptr_vec_append
  21. (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *str,
  22. char delimiter)
  23. {
  24. do
  25. {
  26. size_t this_len;
  27. const char *next_field;
  28. char *this_field;
  29. next_field = strchr (str, delimiter);
  30. if (next_field == NULL)
  31. this_len = strlen (str);
  32. else
  33. {
  34. this_len = next_field - str;
  35. next_field++;
  36. }
  37. this_field = (char *) xmalloc (this_len + 1);
  38. memcpy (this_field, str, this_len);
  39. this_field[this_len] = '\0';
  40. vecp->emplace_back (this_field);
  41. str = next_field;
  42. }
  43. while (str != NULL);
  44. }
  45. /* See gdb_vecs.h. */
  46. std::vector<gdb::unique_xmalloc_ptr<char>>
  47. delim_string_to_char_ptr_vec (const char *str, char delimiter)
  48. {
  49. std::vector<gdb::unique_xmalloc_ptr<char>> retval;
  50. delim_string_to_char_ptr_vec_append (&retval, str, delimiter);
  51. return retval;
  52. }
  53. /* See gdb_vecs.h. */
  54. void
  55. dirnames_to_char_ptr_vec_append
  56. (std::vector<gdb::unique_xmalloc_ptr<char>> *vecp, const char *dirnames)
  57. {
  58. delim_string_to_char_ptr_vec_append (vecp, dirnames, DIRNAME_SEPARATOR);
  59. }
  60. /* See gdb_vecs.h. */
  61. std::vector<gdb::unique_xmalloc_ptr<char>>
  62. dirnames_to_char_ptr_vec (const char *dirnames)
  63. {
  64. std::vector<gdb::unique_xmalloc_ptr<char>> retval;
  65. dirnames_to_char_ptr_vec_append (&retval, dirnames);
  66. return retval;
  67. }