arg_dstr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * SPDX-FileCopyrightText: 2013-2019 Tom G. Huang
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. /*******************************************************************************
  7. * arg_dstr: Implements the dynamic string utilities
  8. *
  9. * This file is part of the argtable3 library.
  10. *
  11. * Copyright (C) 2013-2019 Tom G. Huang
  12. * <tomghuang@gmail.com>
  13. * All rights reserved.
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. * * Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. * * Redistributions in binary form must reproduce the above copyright
  20. * notice, this list of conditions and the following disclaimer in the
  21. * documentation and/or other materials provided with the distribution.
  22. * * Neither the name of STEWART HEITMANN nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  31. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  32. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  33. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  35. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. ******************************************************************************/
  37. #include "argtable3.h"
  38. #ifndef ARG_AMALGAMATION
  39. #include "argtable3_private.h"
  40. #endif
  41. #include <stdarg.h>
  42. #include <stdlib.h>
  43. #include <string.h>
  44. #if defined(_MSC_VER)
  45. #pragma warning(push)
  46. #pragma warning(disable : 4996)
  47. #endif
  48. #define START_VSNBUFF 16
  49. /*
  50. * This dynamic string module is adapted from TclResult.c in the Tcl library.
  51. * Here is the copyright notice from the library:
  52. *
  53. * This software is copyrighted by the Regents of the University of
  54. * California, Sun Microsystems, Inc., Scriptics Corporation, ActiveState
  55. * Corporation and other parties. The following terms apply to all files
  56. * associated with the software unless explicitly disclaimed in
  57. * individual files.
  58. *
  59. * The authors hereby grant permission to use, copy, modify, distribute,
  60. * and license this software and its documentation for any purpose, provided
  61. * that existing copyright notices are retained in all copies and that this
  62. * notice is included verbatim in any distributions. No written agreement,
  63. * license, or royalty fee is required for any of the authorized uses.
  64. * Modifications to this software may be copyrighted by their authors
  65. * and need not follow the licensing terms described here, provided that
  66. * the new terms are clearly indicated on the first page of each file where
  67. * they apply.
  68. *
  69. * IN NO EVENT SHALL THE AUTHORS OR DISTRIBUTORS BE LIABLE TO ANY PARTY
  70. * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  71. * ARISING OUT OF THE USE OF THIS SOFTWARE, ITS DOCUMENTATION, OR ANY
  72. * DERIVATIVES THEREOF, EVEN IF THE AUTHORS HAVE BEEN ADVISED OF THE
  73. * POSSIBILITY OF SUCH DAMAGE.
  74. *
  75. * THE AUTHORS AND DISTRIBUTORS SPECIFICALLY DISCLAIM ANY WARRANTIES,
  76. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  77. * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT. THIS SOFTWARE
  78. * IS PROVIDED ON AN "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE
  79. * NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR
  80. * MODIFICATIONS.
  81. *
  82. * GOVERNMENT USE: If you are acquiring this software on behalf of the
  83. * U.S. government, the Government shall have only "Restricted Rights"
  84. * in the software and related documentation as defined in the Federal
  85. * Acquisition Regulations (FARs) in Clause 52.227.19 (c) (2). If you
  86. * are acquiring the software on behalf of the Department of Defense, the
  87. * software shall be classified as "Commercial Computer Software" and the
  88. * Government shall have only "Restricted Rights" as defined in Clause
  89. * 252.227-7014 (b) (3) of DFARs. Notwithstanding the foregoing, the
  90. * authors grant the U.S. Government and others acting in its behalf
  91. * permission to use and distribute the software in accordance with the
  92. * terms specified in this license.
  93. */
  94. typedef struct _internal_arg_dstr {
  95. char* data;
  96. arg_dstr_freefn* free_proc;
  97. char sbuf[ARG_DSTR_SIZE + 1];
  98. char* append_data;
  99. int append_data_size;
  100. int append_used;
  101. } _internal_arg_dstr_t;
  102. static void setup_append_buf(arg_dstr_t res, int newSpace);
  103. arg_dstr_t arg_dstr_create(void) {
  104. _internal_arg_dstr_t* h = (_internal_arg_dstr_t*)xmalloc(sizeof(_internal_arg_dstr_t));
  105. memset(h, 0, sizeof(_internal_arg_dstr_t));
  106. h->sbuf[0] = 0;
  107. h->data = h->sbuf;
  108. h->free_proc = ARG_DSTR_STATIC;
  109. return h;
  110. }
  111. void arg_dstr_destroy(arg_dstr_t ds) {
  112. if (ds == NULL)
  113. return;
  114. arg_dstr_reset(ds);
  115. xfree(ds);
  116. return;
  117. }
  118. void arg_dstr_set(arg_dstr_t ds, char* str, arg_dstr_freefn* free_proc) {
  119. int length;
  120. register arg_dstr_freefn* old_free_proc = ds->free_proc;
  121. char* old_result = ds->data;
  122. if (str == NULL) {
  123. ds->sbuf[0] = 0;
  124. ds->data = ds->sbuf;
  125. ds->free_proc = ARG_DSTR_STATIC;
  126. } else if (free_proc == ARG_DSTR_VOLATILE) {
  127. length = (int)strlen(str);
  128. if (length > ARG_DSTR_SIZE) {
  129. ds->data = (char*)xmalloc((unsigned)length + 1);
  130. ds->free_proc = ARG_DSTR_DYNAMIC;
  131. } else {
  132. ds->data = ds->sbuf;
  133. ds->free_proc = ARG_DSTR_STATIC;
  134. }
  135. strcpy(ds->data, str);
  136. } else {
  137. ds->data = str;
  138. ds->free_proc = free_proc;
  139. }
  140. /*
  141. * If the old result was dynamically-allocated, free it up. Do it here,
  142. * rather than at the beginning, in case the new result value was part of
  143. * the old result value.
  144. */
  145. if ((old_free_proc != 0) && (old_result != ds->data)) {
  146. if (old_free_proc == ARG_DSTR_DYNAMIC) {
  147. xfree(old_result);
  148. } else {
  149. (*old_free_proc)(old_result);
  150. }
  151. }
  152. if ((ds->append_data != NULL) && (ds->append_data_size > 0)) {
  153. xfree(ds->append_data);
  154. ds->append_data = NULL;
  155. ds->append_data_size = 0;
  156. }
  157. }
  158. char* arg_dstr_cstr(arg_dstr_t ds) /* Interpreter whose result to return. */
  159. {
  160. return ds->data;
  161. }
  162. void arg_dstr_cat(arg_dstr_t ds, const char* str) {
  163. setup_append_buf(ds, (int)strlen(str) + 1);
  164. memcpy(ds->data + strlen(ds->data), str, strlen(str));
  165. }
  166. void arg_dstr_catc(arg_dstr_t ds, char c) {
  167. setup_append_buf(ds, 2);
  168. memcpy(ds->data + strlen(ds->data), &c, 1);
  169. }
  170. /*
  171. * The logic of the `arg_dstr_catf` function is adapted from the `bformat`
  172. * function in The Better String Library by Paul Hsieh. Here is the copyright
  173. * notice from the library:
  174. *
  175. * Copyright (c) 2014, Paul Hsieh
  176. * All rights reserved.
  177. *
  178. * Redistribution and use in source and binary forms, with or without
  179. * modification, are permitted provided that the following conditions are met:
  180. *
  181. * * Redistributions of source code must retain the above copyright notice, this
  182. * list of conditions and the following disclaimer.
  183. *
  184. * * Redistributions in binary form must reproduce the above copyright notice,
  185. * this list of conditions and the following disclaimer in the documentation
  186. * and/or other materials provided with the distribution.
  187. *
  188. * * Neither the name of bstrlib nor the names of its
  189. * contributors may be used to endorse or promote products derived from
  190. * this software without specific prior written permission.
  191. *
  192. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  193. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  194. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  195. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  196. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  197. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  198. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  199. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  200. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  201. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  202. */
  203. void arg_dstr_catf(arg_dstr_t ds, const char* fmt, ...) {
  204. va_list arglist;
  205. char* buff;
  206. int n, r;
  207. size_t slen;
  208. if (fmt == NULL)
  209. return;
  210. /* Since the length is not determinable beforehand, a search is
  211. performed using the truncating "vsnprintf" call (to avoid buffer
  212. overflows) on increasing potential sizes for the output result. */
  213. if ((n = (int)(2 * strlen(fmt))) < START_VSNBUFF)
  214. n = START_VSNBUFF;
  215. buff = (char*)xmalloc((size_t)(n + 2));
  216. memset(buff, 0, (size_t)(n + 2));
  217. for (;;) {
  218. va_start(arglist, fmt);
  219. r = vsnprintf(buff, (size_t)(n + 1), fmt, arglist);
  220. va_end(arglist);
  221. slen = strlen(buff);
  222. if (slen < (size_t)n)
  223. break;
  224. if (r > n)
  225. n = r;
  226. else
  227. n += n;
  228. xfree(buff);
  229. buff = (char*)xmalloc((size_t)(n + 2));
  230. memset(buff, 0, (size_t)(n + 2));
  231. }
  232. arg_dstr_cat(ds, buff);
  233. xfree(buff);
  234. }
  235. static void setup_append_buf(arg_dstr_t ds, int new_space) {
  236. int total_space;
  237. /*
  238. * Make the append buffer larger, if that's necessary, then copy the
  239. * data into the append buffer and make the append buffer the official
  240. * data.
  241. */
  242. if (ds->data != ds->append_data) {
  243. /*
  244. * If the buffer is too big, then free it up so we go back to a
  245. * smaller buffer. This avoids tying up memory forever after a large
  246. * operation.
  247. */
  248. if (ds->append_data_size > 500) {
  249. xfree(ds->append_data);
  250. ds->append_data = NULL;
  251. ds->append_data_size = 0;
  252. }
  253. ds->append_used = (int)strlen(ds->data);
  254. } else if (ds->data[ds->append_used] != 0) {
  255. /*
  256. * Most likely someone has modified a result created by
  257. * arg_dstr_cat et al. so that it has a different size. Just
  258. * recompute the size.
  259. */
  260. ds->append_used = (int)strlen(ds->data);
  261. }
  262. total_space = new_space + ds->append_used;
  263. if (total_space >= ds->append_data_size) {
  264. char* newbuf;
  265. if (total_space < 100) {
  266. total_space = 200;
  267. } else {
  268. total_space *= 2;
  269. }
  270. newbuf = (char*)xmalloc((unsigned)total_space);
  271. memset(newbuf, 0, (size_t)total_space);
  272. strcpy(newbuf, ds->data);
  273. if (ds->append_data != NULL) {
  274. xfree(ds->append_data);
  275. }
  276. ds->append_data = newbuf;
  277. ds->append_data_size = total_space;
  278. } else if (ds->data != ds->append_data) {
  279. strcpy(ds->append_data, ds->data);
  280. }
  281. arg_dstr_free(ds);
  282. ds->data = ds->append_data;
  283. }
  284. void arg_dstr_free(arg_dstr_t ds) {
  285. if (ds->free_proc != NULL) {
  286. if (ds->free_proc == ARG_DSTR_DYNAMIC) {
  287. xfree(ds->data);
  288. } else {
  289. (*ds->free_proc)(ds->data);
  290. }
  291. ds->free_proc = NULL;
  292. }
  293. }
  294. void arg_dstr_reset(arg_dstr_t ds) {
  295. arg_dstr_free(ds);
  296. if ((ds->append_data != NULL) && (ds->append_data_size > 0)) {
  297. xfree(ds->append_data);
  298. ds->append_data = NULL;
  299. ds->append_data_size = 0;
  300. }
  301. ds->data = ds->sbuf;
  302. ds->sbuf[0] = 0;
  303. }
  304. #if defined(_MSC_VER)
  305. #pragma warning(pop)
  306. #endif