make_name_indices.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @file make_name_indices.h
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef BADVPN_MAKE_NAME_INDICES_H
  30. #define BADVPN_MAKE_NAME_INDICES_H
  31. #include <stdlib.h>
  32. #include <stddef.h>
  33. #include <misc/strdup.h>
  34. #include <misc/balloc.h>
  35. #include <misc/debug.h>
  36. #include <ncd/NCDStringIndex.h>
  37. static int ncd_make_name_indices (NCDStringIndex *string_index, const char *name, NCD_string_id_t **out_varnames, size_t *out_num_names) WARN_UNUSED;
  38. static size_t split_string_inplace2__indices (char *str, char del)
  39. {
  40. ASSERT(str)
  41. size_t num_extra_parts = 0;
  42. while (*str) {
  43. if (*str == del) {
  44. *str = '\0';
  45. num_extra_parts++;
  46. }
  47. str++;
  48. }
  49. return num_extra_parts;
  50. }
  51. static int ncd_make_name_indices (NCDStringIndex *string_index, const char *name, NCD_string_id_t **out_varnames, size_t *out_num_names)
  52. {
  53. ASSERT(string_index)
  54. ASSERT(name)
  55. ASSERT(out_varnames)
  56. ASSERT(out_num_names)
  57. char *data = b_strdup(name);
  58. if (!data) {
  59. goto fail0;
  60. }
  61. size_t num_names = split_string_inplace2__indices(data, '.') + 1;
  62. NCD_string_id_t *varnames = BAllocArray(num_names, sizeof(varnames[0]));
  63. if (!varnames) {
  64. goto fail1;
  65. }
  66. char *cur = data;
  67. for (size_t i = 0; i < num_names; i++) {
  68. NCD_string_id_t id = NCDStringIndex_Get(string_index, cur);
  69. if (id < 0) {
  70. goto fail2;
  71. }
  72. varnames[i] = id;
  73. cur += strlen(cur) + 1;
  74. }
  75. free(data);
  76. *out_varnames = varnames;
  77. *out_num_names = num_names;
  78. return 1;
  79. fail2:
  80. BFree(varnames);
  81. fail1:
  82. free(data);
  83. fail0:
  84. return 0;
  85. }
  86. #endif