bstring.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /**
  2. * @file bstring.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_BSTRING_H
  30. #define BADVPN_BSTRING_H
  31. #include <misc/debug.h>
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #define BSTRING_TYPE_STATIC 5
  35. #define BSTRING_TYPE_DYNAMIC 7
  36. #define BSTRING_TYPE_EXTERNAL 11
  37. #define BSTRING_STATIC_SIZE 23
  38. #define BSTRING_STATIC_MAX (BSTRING_STATIC_SIZE - 1)
  39. typedef struct {
  40. union {
  41. struct {
  42. char type;
  43. char static_string[BSTRING_STATIC_SIZE];
  44. } us;
  45. struct {
  46. char type;
  47. char *dynamic_string;
  48. } ud;
  49. struct {
  50. char type;
  51. const char *external_string;
  52. } ue;
  53. } u;
  54. } BString;
  55. static void BString__assert (BString *o)
  56. {
  57. switch (o->u.us.type) {
  58. case BSTRING_TYPE_STATIC:
  59. case BSTRING_TYPE_DYNAMIC:
  60. case BSTRING_TYPE_EXTERNAL:
  61. return;
  62. }
  63. ASSERT(0);
  64. }
  65. static int BString_Init (BString *o, const char *str)
  66. {
  67. if (strlen(str) <= BSTRING_STATIC_MAX) {
  68. strcpy(o->u.us.static_string, str);
  69. o->u.us.type = BSTRING_TYPE_STATIC;
  70. } else {
  71. if (!(o->u.ud.dynamic_string = malloc(strlen(str) + 1))) {
  72. return 0;
  73. }
  74. strcpy(o->u.ud.dynamic_string, str);
  75. o->u.ud.type = BSTRING_TYPE_DYNAMIC;
  76. }
  77. BString__assert(o);
  78. return 1;
  79. }
  80. static void BString_InitStatic (BString *o, const char *str)
  81. {
  82. ASSERT(strlen(str) <= BSTRING_STATIC_MAX)
  83. strcpy(o->u.us.static_string, str);
  84. o->u.us.type = BSTRING_TYPE_STATIC;
  85. BString__assert(o);
  86. }
  87. static void BString_InitExternal (BString *o, const char *str)
  88. {
  89. o->u.ue.external_string = str;
  90. o->u.ue.type = BSTRING_TYPE_EXTERNAL;
  91. BString__assert(o);
  92. }
  93. static void BString_InitAllocated (BString *o, char *str)
  94. {
  95. o->u.ud.dynamic_string = str;
  96. o->u.ud.type = BSTRING_TYPE_DYNAMIC;
  97. BString__assert(o);
  98. }
  99. static void BString_Free (BString *o)
  100. {
  101. BString__assert(o);
  102. if (o->u.ud.type == BSTRING_TYPE_DYNAMIC) {
  103. free(o->u.ud.dynamic_string);
  104. }
  105. }
  106. static const char * BString_Get (BString *o)
  107. {
  108. BString__assert(o);
  109. switch (o->u.us.type) {
  110. case BSTRING_TYPE_STATIC: return o->u.us.static_string;
  111. case BSTRING_TYPE_DYNAMIC: return o->u.ud.dynamic_string;
  112. case BSTRING_TYPE_EXTERNAL: return o->u.ue.external_string;
  113. }
  114. ASSERT(0);
  115. return NULL;
  116. }
  117. #endif