The source code and dockerfile for the GSW2024 AI Lab.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

195 lines
5.4 KiB

4 weeks ago
  1. /**
  2. @file
  3. @ingroup cstringstream
  4. @brief Simple string streams in C.
  5. @author Fabio Somenzi
  6. @copyright@parblock
  7. Copyright (c) 2014-2015, Regents of the University of Colorado
  8. All rights reserved.
  9. Redistribution and use in source and binary forms, with or without
  10. modification, are permitted provided that the following conditions
  11. are met:
  12. Redistributions of source code must retain the above copyright
  13. notice, this list of conditions and the following disclaimer.
  14. Redistributions in binary form must reproduce the above copyright
  15. notice, this list of conditions and the following disclaimer in the
  16. documentation and/or other materials provided with the distribution.
  17. Neither the name of the University of Colorado nor the names of its
  18. contributors may be used to endorse or promote products derived from
  19. this software without specific prior written permission.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  23. FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  24. COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  25. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  26. BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  27. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  28. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  30. ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  31. POSSIBILITY OF SUCH DAMAGE.
  32. @endparblock
  33. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #include <string.h>
  37. #include "cstringstream.h"
  38. /**
  39. * @brief Type of a simple extensible string buffer.
  40. */
  41. struct _cstringstream {
  42. size_t capacity; /**< elements allocated */
  43. size_t inUse; /**< elements currently in use */
  44. char * data; /**< actual data */
  45. };
  46. cstringstream newStringStream(void) {
  47. cstringstream ss;
  48. ss = (cstringstream) malloc(sizeof(struct _cstringstream));
  49. if (!ss) return NULL;
  50. ss->capacity = 1; /* parsimonious */
  51. ss->inUse = 0;
  52. ss->data = (char *) malloc(sizeof(char) * ss->capacity);
  53. if (!ss->data) {
  54. free(ss);
  55. return NULL;
  56. }
  57. return ss;
  58. }
  59. void deleteStringStream(cstringstream ss) {
  60. if (ss) {
  61. free(ss->data);
  62. free(ss);
  63. }
  64. }
  65. int clearStringStream(cstringstream ss) {
  66. if (!ss) return -1;
  67. ss->inUse = 0;
  68. return 0;
  69. }
  70. cstringstream copyStringStream(const_cstringstream src) {
  71. cstringstream dest;
  72. if (!src) return 0;
  73. dest = newStringStream();
  74. if (!dest) return 0;
  75. if (resizeStringStream(dest, src->inUse)) {
  76. deleteStringStream(dest);
  77. return 0;
  78. }
  79. strncpy(dest->data, src->data, src->inUse);
  80. return dest;
  81. }
  82. int resizeStringStream(cstringstream ss, size_t newSize) {
  83. if (newSize > ss->capacity) {
  84. /* To avoid too many calls to realloc, we choose the larger of
  85. * twice the current size and the new requested size. */
  86. size_t newCapacity = 2 * ss->capacity;
  87. if (newCapacity < newSize)
  88. newCapacity = newSize;
  89. char * tmp = (char *) realloc(ss->data, newCapacity * sizeof(char));
  90. /* If the allocation fails, leave the array alone. */
  91. if (!tmp) return -1;
  92. ss->data = tmp;
  93. ss->capacity = newCapacity;
  94. }
  95. /* Here we are guaranteed that newSize <= ss->capacity. */
  96. ss->inUse = newSize;
  97. return 0;
  98. }
  99. int sizeStringStream(const_cstringstream ss, size_t * num) {
  100. if (!ss || !num) return -1;
  101. *num = ss->inUse;
  102. return 0;
  103. }
  104. int getStringStream(const_cstringstream ss, size_t index, char * c) {
  105. if (!ss || !c || index >= ss->inUse) return -1;
  106. *c = ss->data[index];
  107. return 0;
  108. }
  109. int appendCharStringStream(cstringstream ss, char c) {
  110. if (!ss) return -1;
  111. if (resizeStringStream(ss, ss->inUse + 1)) return -1;
  112. /* Now we have space. */
  113. ss->data[ss->inUse-1] = c;
  114. return 0;
  115. }
  116. int appendStringStringStream(cstringstream ss, char const * s) {
  117. if (!ss) return -1;
  118. size_t len = strlen(s);
  119. if (resizeStringStream(ss, ss->inUse + len)) return -1;
  120. /* Now we have space. */
  121. strncpy(ss->data + ss->inUse - len, s, len);
  122. return 0;
  123. }
  124. int appendIntStringStream(cstringstream ss, int d) {
  125. char str[256];
  126. if (!ss) return -1;
  127. sprintf(str, "%d", d);
  128. return appendStringStringStream(ss, str);
  129. }
  130. int appendUnsignedStringStream(cstringstream ss, unsigned u) {
  131. char str[256];
  132. if (!ss) return -1;
  133. sprintf(str, "%u", u);
  134. return appendStringStringStream(ss, str);
  135. }
  136. int appendLongStringStream(cstringstream ss, long ld) {
  137. char str[256];
  138. if (!ss) return -1;
  139. sprintf(str, "%ld", ld);
  140. return appendStringStringStream(ss, str);
  141. }
  142. int appendUnsignedLongStringStream(cstringstream ss, unsigned long lu) {
  143. char str[256];
  144. if (!ss) return -1;
  145. sprintf(str, "%lu", lu);
  146. return appendStringStringStream(ss, str);
  147. }
  148. int appendDoubleStringStream(cstringstream ss, double g) {
  149. char str[256];
  150. if (!ss) return -1;
  151. sprintf(str, "%g", g);
  152. return appendStringStringStream(ss, str);
  153. }
  154. int putStringStream(cstringstream ss, size_t index, char c) {
  155. if (!ss || index >= ss->inUse) return -1;
  156. ss->data[index] = c;
  157. return 0;
  158. }
  159. char * stringFromStringStream(const_cstringstream ss) {
  160. if (!ss) return 0;
  161. char * str = (char *) malloc(sizeof(char) * (ss->inUse + 1));
  162. if (!str) return 0;
  163. strncpy(str, ss->data, ss->inUse);
  164. str[ss->inUse] = '\0';
  165. return str;
  166. }