gdb/common/gdb_vecs.c - gdb

Functions defined

Source code

  1. /* Some commonly-used VEC types.

  2.    Copyright (C) 2012-2015 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. /* Call xfree for each element of CHAR_PTR_VEC and final VEC_free for
  18.    CHAR_PTR_VEC itself.

  19.    You must not modify CHAR_PTR_VEC after it got registered with this function
  20.    by make_cleanup as the CHAR_PTR_VEC base address may change on its updates.
  21.    Contrary to VEC_free this function does not (cannot) clear the pointer.  */

  22. void
  23. free_char_ptr_vec (VEC (char_ptr) *char_ptr_vec)
  24. {
  25.   int ix;
  26.   char *name;

  27.   for (ix = 0; VEC_iterate (char_ptr, char_ptr_vec, ix, name); ++ix)
  28.     xfree (name);
  29.   VEC_free (char_ptr, char_ptr_vec);
  30. }

  31. /* Worker function to split character delimiter separated string of fields
  32.    STR into a CHAR_PTR_VEC.  */

  33. static void
  34. delim_string_to_char_ptr_vec_append (VEC (char_ptr) **vecp,
  35.                                      const char *str, char delimiter)
  36. {
  37.   do
  38.     {
  39.       size_t this_len;
  40.       char *next_field, *this_field;

  41.       next_field = strchr (str, delimiter);
  42.       if (next_field == NULL)
  43.         this_len = strlen (str);
  44.       else
  45.         {
  46.           this_len = next_field - str;
  47.           next_field++;
  48.         }

  49.       this_field = xmalloc (this_len + 1);
  50.       memcpy (this_field, str, this_len);
  51.       this_field[this_len] = '\0';
  52.       VEC_safe_push (char_ptr, *vecp, this_field);

  53.       str = next_field;
  54.     }
  55.   while (str != NULL);
  56. }

  57. /* Split STR, a list of DELIMITER-separated fields, into a CHAR_PTR_VEC.

  58.    You may modify the returned strings.
  59.    Read free_char_ptr_vec for its cleanup.  */

  60. VEC (char_ptr) *
  61. delim_string_to_char_ptr_vec (const char *str, char delimiter)
  62. {
  63.   VEC (char_ptr) *retval = NULL;

  64.   delim_string_to_char_ptr_vec_append (&retval, str, delimiter);

  65.   return retval;
  66. }

  67. /* Extended version of dirnames_to_char_ptr_vec - additionally if *VECP is
  68.    non-NULL the new list elements from DIRNAMES are appended to the existing
  69.    *VECP list of entries.  *VECP address will be updated by this call.  */

  70. void
  71. dirnames_to_char_ptr_vec_append (VEC (char_ptr) **vecp, const char *dirnames)
  72. {
  73.   delim_string_to_char_ptr_vec_append (vecp, dirnames, DIRNAME_SEPARATOR);
  74. }

  75. /* Split DIRNAMES by DIRNAME_SEPARATOR delimiter and return a list of all the
  76.    elements in their original order.  For empty string ("") DIRNAMES return
  77.    list of one empty string ("") element.

  78.    You may modify the returned strings.
  79.    Read free_char_ptr_vec for its cleanup.  */

  80. VEC (char_ptr) *
  81. dirnames_to_char_ptr_vec (const char *dirnames)
  82. {
  83.   VEC (char_ptr) *retval = NULL;

  84.   dirnames_to_char_ptr_vec_append (&retval, dirnames);

  85.   return retval;
  86. }