vcgus.c 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. * SPDX-License-Identifier: GPL-3.0+
  33. * License-Filename: LICENSE
  34. */
  35. #include "config.h"
  36. /* needed to get strdup()
  37. #define _GNU_SOURCE
  38. */
  39. #include <stdio.h>
  40. #include <string.h>
  41. #include <stdlib.h>
  42. #include <zlib.h>
  43. #include "splay-tree.h"
  44. #include "main.h"
  45. #include "vcg.h"
  46. #include "vcgus.h"
  47. #include "dpmem.h"
  48. /* strdup issue here and gcc-6.3.0
  49. #ifndef HAVE_STRDUP
  50. extern char *strdup (const char *str);
  51. #endif
  52. */
  53. static splay_tree uniqstr_splaytree = NULL;
  54. void vcg_clear_uniqstr(void)
  55. {
  56. uniqstr_splaytree = splay_tree_delete(uniqstr_splaytree);
  57. return;
  58. }
  59. char *vcg_uniqstr(char *str)
  60. {
  61. char *s = NULL;
  62. splay_tree_node spn;
  63. if (str == NULL) {
  64. return (NULL);
  65. }
  66. if (strlen(str) == 0) {
  67. return ("");
  68. }
  69. spn = splay_tree_lookup(uniqstr_splaytree, (splay_tree_key) str);
  70. if (spn) {
  71. return ((char *)spn->key);
  72. }
  73. if (uniqstr_splaytree == NULL) {
  74. uniqstr_splaytree = splay_tree_new(splay_tree_compare_strings, splay_tree_free_key, NULL);
  75. }
  76. s = dp_calloc(1, (strlen(str) + 1));
  77. if (s == NULL) {
  78. return (NULL);
  79. }
  80. strcpy(s, str);
  81. splay_tree_insert(uniqstr_splaytree, (splay_tree_key) s, 0);
  82. return (s);
  83. }
  84. /* end */