NCDFastNames.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /**
  2. * @file NCDFastNames.c
  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. #include <string.h>
  30. #include <misc/balloc.h>
  31. #include "NCDFastNames.h"
  32. static size_t count_names (const char *str, size_t str_len)
  33. {
  34. size_t count = 1;
  35. while (str_len > 0) {
  36. if (*str == '.') {
  37. count++;
  38. }
  39. str++;
  40. str_len--;
  41. }
  42. return count;
  43. }
  44. static int add_name (NCDFastNames *o, NCDStringIndex *string_index, const char *str, size_t str_len, const char *remain, size_t remain_len)
  45. {
  46. ASSERT(str)
  47. ASSERT(!!o->dynamic_names == (o->num_names > NCD_NUM_FAST_NAMES))
  48. NCD_string_id_t id = NCDStringIndex_GetBin(string_index, str, str_len);
  49. if (id < 0) {
  50. return 0;
  51. }
  52. if (o->num_names < NCD_NUM_FAST_NAMES) {
  53. o->static_names[o->num_names++] = id;
  54. return 1;
  55. }
  56. if (o->num_names == NCD_NUM_FAST_NAMES) {
  57. size_t num_more = (!remain ? 0 : count_names(remain, remain_len));
  58. size_t num_all = o->num_names + 1 + num_more;
  59. if (!(o->dynamic_names = BAllocArray(num_all, sizeof(o->dynamic_names[0])))) {
  60. return 0;
  61. }
  62. memcpy(o->dynamic_names, o->static_names, NCD_NUM_FAST_NAMES * sizeof(o->dynamic_names[0]));
  63. }
  64. o->dynamic_names[o->num_names++] = id;
  65. return 1;
  66. }
  67. int NCDFastNames_Init (NCDFastNames *o, NCDStringIndex *string_index, const char *str, size_t str_len)
  68. {
  69. ASSERT(str)
  70. o->num_names = 0;
  71. o->dynamic_names = NULL;
  72. size_t i = 0;
  73. while (i < str_len) {
  74. if (str[i] == '.') {
  75. if (!add_name(o, string_index, str, i, str + (i + 1), str_len - (i + 1))) {
  76. goto fail;
  77. }
  78. str += i + 1;
  79. str_len -= i + 1;
  80. i = 0;
  81. continue;
  82. }
  83. i++;
  84. }
  85. if (!add_name(o, string_index, str, i, NULL, 0)) {
  86. goto fail;
  87. }
  88. return 1;
  89. fail:
  90. BFree(o->dynamic_names);
  91. return 0;
  92. }
  93. void NCDFastNames_Free (NCDFastNames *o)
  94. {
  95. if (o->dynamic_names) {
  96. BFree(o->dynamic_names);
  97. }
  98. }
  99. size_t NCDFastNames_GetNumNames (NCDFastNames *o)
  100. {
  101. ASSERT(o->num_names > 0)
  102. return o->num_names;
  103. }
  104. NCD_string_id_t * NCDFastNames_GetNames (NCDFastNames *o)
  105. {
  106. return o->dynamic_names ? o->dynamic_names : o->static_names;
  107. }