uniqstr.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include "splay-tree.h"
  39. #include "main.h"
  40. #include "uniqstr.h"
  41. /* strdup issue here and gcc-6.3.0 */
  42. #ifndef HAVE_STRDUP
  43. extern char *strdup (const char *str);
  44. #endif
  45. static splay_tree uniqstr_splaytree = NULL;
  46. void
  47. clear_uniqstr (void)
  48. {
  49. uniqstr_splaytree = splay_tree_delete (uniqstr_splaytree);
  50. return;
  51. }
  52. char *
  53. uniqstr (char *str)
  54. {
  55. char *s = NULL;
  56. splay_tree_node spn;
  57. if (str == NULL)
  58. {
  59. return (NULL);
  60. }
  61. if (strlen (str) == 0)
  62. {
  63. return ("");
  64. }
  65. spn = splay_tree_lookup (uniqstr_splaytree, (splay_tree_key) str);
  66. if (spn)
  67. {
  68. return ((char *) spn->key);
  69. }
  70. if (uniqstr_splaytree == NULL)
  71. {
  72. uniqstr_splaytree =
  73. splay_tree_new (splay_tree_compare_strings, splay_tree_free_key,
  74. NULL);
  75. }
  76. s = strdup (str);
  77. splay_tree_insert (uniqstr_splaytree, (splay_tree_key) s, 0);
  78. return (s);
  79. }
  80. /* end */