dpus.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2021
  3. *
  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. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * These are the four essential freedoms with GNU GPL software:
  18. * 1: freedom to run the program, for any purpose
  19. * 2: freedom to study how the program works, and change it to make it do what you wish
  20. * 3: freedom to redistribute copies to help your Free Software friends
  21. * 4: freedom to distribute copies of your modified versions to your Free Software friends
  22. * , ,
  23. * / \
  24. * ((__-^^-,-^^-__))
  25. * `-_---' `---_-'
  26. * `--|o` 'o|--'
  27. * \ ` /
  28. * ): :(
  29. * :o_o:
  30. * "-"
  31. */
  32. #include "config.h"
  33. /* needed to get strdup()
  34. #define _GNU_SOURCE
  35. */
  36. #include <stdio.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include "splay-tree.h"
  40. #include "dp.h"
  41. #include "dpus.h"
  42. #include "dpmem.h"
  43. /* strdup issue here and gcc-6.3.0
  44. #ifndef HAVE_STRDUP
  45. extern char *strdup (const char *str);
  46. #endif
  47. */
  48. static splay_tree uniqstr_splaytree = NULL;
  49. void dp_clear_uniqstr(void)
  50. {
  51. uniqstr_splaytree = splay_tree_delete(uniqstr_splaytree);
  52. return;
  53. }
  54. char *dp_uniqstr(char *str)
  55. {
  56. char *s = NULL;
  57. splay_tree_node spn;
  58. if (str == NULL) {
  59. return (NULL);
  60. }
  61. if (strlen(str) == 0) {
  62. return ((char *)"");
  63. }
  64. spn = splay_tree_lookup(uniqstr_splaytree, (splay_tree_key) str);
  65. if (spn) {
  66. return ((char *)spn->key);
  67. }
  68. if (uniqstr_splaytree == NULL) {
  69. uniqstr_splaytree = splay_tree_new(splay_tree_compare_strings, splay_tree_free_key, NULL);
  70. }
  71. s = (char *)dp_calloc(1, (strlen(str) + 1));
  72. strcpy(s, str);
  73. splay_tree_insert(uniqstr_splaytree, (splay_tree_key) s, 0);
  74. return (s);
  75. }
  76. /* end */