2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2002-10-12 11:37:38 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
2008-01-07 19:13:47 +00:00
|
|
|
* of the License, or (at your option) any later version.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2002-10-12 11:37:38 +00:00
|
|
|
*
|
|
|
|
* The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
|
|
|
|
* All rights reserved.
|
|
|
|
* Dynamically sized string ADT
|
|
|
|
*/
|
|
|
|
|
2019-02-18 08:08:12 +11:00
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
2011-02-27 20:37:56 +00:00
|
|
|
*/
|
|
|
|
|
2021-03-24 16:43:17 +11:00
|
|
|
#include <stdio.h>
|
2013-04-01 11:27:47 +00:00
|
|
|
#include <stdlib.h> /* malloc */
|
2002-10-12 11:37:38 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_dynstr.h"
|
2017-04-24 10:42:10 +02:00
|
|
|
#include "BLI_memarena.h"
|
2013-04-01 11:27:47 +00:00
|
|
|
#include "BLI_string.h"
|
2020-03-19 09:33:03 +01:00
|
|
|
#include "BLI_utildefines.h"
|
|
|
|
#include "MEM_guardedalloc.h"
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2008-11-11 15:03:26 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# ifndef vsnprintf
|
|
|
|
# define vsnprintf _vsnprintf
|
|
|
|
# endif
|
2002-11-25 12:02:15 +00:00
|
|
|
#endif
|
|
|
|
|
2011-01-12 10:00:47 +00:00
|
|
|
#ifndef va_copy
|
|
|
|
# ifdef __va_copy
|
2012-05-12 15:02:10 +00:00
|
|
|
# define va_copy(a, b) __va_copy(a, b)
|
2011-01-12 10:00:47 +00:00
|
|
|
# else /* !__va_copy */
|
2012-05-12 15:02:10 +00:00
|
|
|
# define va_copy(a, b) ((a) = (b))
|
2011-01-12 10:00:47 +00:00
|
|
|
# endif /* __va_copy */
|
|
|
|
#endif /* va_copy */
|
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
/***/
|
|
|
|
|
|
|
|
typedef struct DynStrElem DynStrElem;
|
|
|
|
struct DynStrElem {
|
|
|
|
DynStrElem *next;
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
char *str;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct DynStr {
|
|
|
|
DynStrElem *elems, *last;
|
|
|
|
int curlen;
|
2017-04-24 10:42:10 +02:00
|
|
|
MemArena *memarena;
|
2002-10-12 11:37:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***/
|
|
|
|
|
2011-09-28 05:53:40 +00:00
|
|
|
DynStr *BLI_dynstr_new(void)
|
|
|
|
{
|
2012-05-12 15:02:10 +00:00
|
|
|
DynStr *ds = MEM_mallocN(sizeof(*ds), "DynStr");
|
|
|
|
ds->elems = ds->last = NULL;
|
|
|
|
ds->curlen = 0;
|
2017-04-24 10:42:10 +02:00
|
|
|
ds->memarena = NULL;
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:42:10 +02:00
|
|
|
DynStr *BLI_dynstr_new_memarena(void)
|
|
|
|
{
|
|
|
|
DynStr *ds = MEM_mallocN(sizeof(*ds), "DynStr");
|
|
|
|
ds->elems = ds->last = NULL;
|
|
|
|
ds->curlen = 0;
|
|
|
|
ds->memarena = BLI_memarena_new(BLI_MEMARENA_STD_BUFSIZE, __func__);
|
|
|
|
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
|
|
|
|
BLI_INLINE void *dynstr_alloc(DynStr *__restrict ds, size_t size)
|
|
|
|
{
|
|
|
|
return ds->memarena ? BLI_memarena_alloc(ds->memarena, size) : malloc(size);
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:59:31 -05:00
|
|
|
void BLI_dynstr_append(DynStr *__restrict ds, const char *cstr)
|
2011-09-28 05:53:40 +00:00
|
|
|
{
|
2017-04-24 10:42:10 +02:00
|
|
|
DynStrElem *dse = dynstr_alloc(ds, sizeof(*dse));
|
2012-05-12 15:02:10 +00:00
|
|
|
int cstrlen = strlen(cstr);
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2017-04-24 10:42:10 +02:00
|
|
|
dse->str = dynstr_alloc(ds, cstrlen + 1);
|
2012-05-12 15:02:10 +00:00
|
|
|
memcpy(dse->str, cstr, cstrlen + 1);
|
|
|
|
dse->next = NULL;
|
2018-06-17 16:32:54 +02:00
|
|
|
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!ds->last) {
|
2012-05-12 15:02:10 +00:00
|
|
|
ds->last = ds->elems = dse;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
|
|
|
else {
|
2012-05-12 15:02:10 +00:00
|
|
|
ds->last = ds->last->next = dse;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2002-10-12 11:37:38 +00:00
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
ds->curlen += cstrlen;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:59:31 -05:00
|
|
|
void BLI_dynstr_nappend(DynStr *__restrict ds, const char *cstr, int len)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2017-04-24 10:42:10 +02:00
|
|
|
DynStrElem *dse = dynstr_alloc(ds, sizeof(*dse));
|
2012-05-12 15:02:10 +00:00
|
|
|
int cstrlen = BLI_strnlen(cstr, len);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-04-24 10:42:10 +02:00
|
|
|
dse->str = dynstr_alloc(ds, cstrlen + 1);
|
2010-02-26 23:56:16 +00:00
|
|
|
memcpy(dse->str, cstr, cstrlen);
|
|
|
|
dse->str[cstrlen] = '\0';
|
2012-05-12 15:02:10 +00:00
|
|
|
dse->next = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-27 13:16:10 +11:00
|
|
|
if (!ds->last) {
|
2012-05-12 15:02:10 +00:00
|
|
|
ds->last = ds->elems = dse;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
|
|
|
else {
|
2012-05-12 15:02:10 +00:00
|
|
|
ds->last = ds->last->next = dse;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
ds->curlen += cstrlen;
|
2010-02-26 23:56:16 +00:00
|
|
|
}
|
|
|
|
|
2014-06-27 05:59:31 -05:00
|
|
|
void BLI_dynstr_vappendf(DynStr *__restrict ds, const char *__restrict format, va_list args)
|
2008-11-11 15:03:26 +00:00
|
|
|
{
|
|
|
|
char *message, fixedmessage[256];
|
2012-05-12 15:02:10 +00:00
|
|
|
int len = sizeof(fixedmessage);
|
|
|
|
const int maxlen = 65536;
|
2011-01-12 05:49:33 +00:00
|
|
|
int retval;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
while (1) {
|
2011-01-12 05:49:33 +00:00
|
|
|
va_list args_cpy;
|
2019-03-27 13:16:10 +11:00
|
|
|
if (len == sizeof(fixedmessage)) {
|
2012-05-12 15:02:10 +00:00
|
|
|
message = fixedmessage;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
|
|
|
else {
|
2012-05-12 15:02:10 +00:00
|
|
|
message = MEM_callocN(sizeof(char) * len, "BLI_dynstr_appendf");
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2021-06-22 10:42:32 -07:00
|
|
|
/* can't reuse the same args, so work on a copy */
|
2011-01-12 05:49:33 +00:00
|
|
|
va_copy(args_cpy, args);
|
2012-05-12 15:02:10 +00:00
|
|
|
retval = vsnprintf(message, len, format, args_cpy);
|
2011-01-12 05:49:33 +00:00
|
|
|
va_end(args_cpy);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (retval == -1) {
|
2008-11-11 15:03:26 +00:00
|
|
|
/* -1 means not enough space, but on windows it may also mean
|
|
|
|
* there is a formatting error, so we impose a maximum length */
|
2019-03-27 13:16:10 +11:00
|
|
|
if (message != fixedmessage) {
|
2008-11-11 15:03:26 +00:00
|
|
|
MEM_freeN(message);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2012-05-12 15:02:10 +00:00
|
|
|
message = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2008-11-11 15:03:26 +00:00
|
|
|
len *= 2;
|
2012-03-24 06:18:31 +00:00
|
|
|
if (len > maxlen) {
|
2008-11-11 15:03:26 +00:00
|
|
|
fprintf(stderr, "BLI_dynstr_append text too long or format error.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (retval >= len) {
|
2008-11-11 15:03:26 +00:00
|
|
|
/* in C99 the actual length required is returned */
|
2019-03-27 13:16:10 +11:00
|
|
|
if (message != fixedmessage) {
|
2008-11-11 15:03:26 +00:00
|
|
|
MEM_freeN(message);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2012-05-12 15:02:10 +00:00
|
|
|
message = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-07 12:37:15 +00:00
|
|
|
/* retval doesn't include \0 terminator */
|
2012-05-12 15:02:10 +00:00
|
|
|
len = retval + 1;
|
2008-11-11 15:03:26 +00:00
|
|
|
}
|
2019-03-27 13:16:10 +11:00
|
|
|
else {
|
2008-11-11 15:03:26 +00:00
|
|
|
break;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2008-11-11 15:03:26 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (message) {
|
2008-11-11 15:03:26 +00:00
|
|
|
BLI_dynstr_append(ds, message);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-27 13:16:10 +11:00
|
|
|
if (message != fixedmessage) {
|
2008-11-11 15:03:26 +00:00
|
|
|
MEM_freeN(message);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2008-11-11 15:03:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-27 05:59:31 -05:00
|
|
|
void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format, ...)
|
2008-12-29 13:38:08 +00:00
|
|
|
{
|
|
|
|
va_list args;
|
2009-06-20 14:53:30 +00:00
|
|
|
char *message, fixedmessage[256];
|
2012-05-12 15:02:10 +00:00
|
|
|
int len = sizeof(fixedmessage);
|
|
|
|
const int maxlen = 65536;
|
2011-01-14 19:15:06 +00:00
|
|
|
int retval;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-20 14:53:30 +00:00
|
|
|
/* note that it's tempting to just call BLI_dynstr_vappendf here
|
|
|
|
* and avoid code duplication, that crashes on some system because
|
|
|
|
* va_start/va_end have to be called for each vsnprintf call */
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
while (1) {
|
2019-03-27 13:16:10 +11:00
|
|
|
if (len == sizeof(fixedmessage)) {
|
2012-05-12 15:02:10 +00:00
|
|
|
message = fixedmessage;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
|
|
|
else {
|
2012-05-12 15:02:10 +00:00
|
|
|
message = MEM_callocN(sizeof(char) * (len), "BLI_dynstr_appendf");
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-20 14:53:30 +00:00
|
|
|
va_start(args, format);
|
2012-05-12 15:02:10 +00:00
|
|
|
retval = vsnprintf(message, len, format, args);
|
2009-06-20 14:53:30 +00:00
|
|
|
va_end(args);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (retval == -1) {
|
2009-06-20 14:53:30 +00:00
|
|
|
/* -1 means not enough space, but on windows it may also mean
|
|
|
|
* there is a formatting error, so we impose a maximum length */
|
2019-03-27 13:16:10 +11:00
|
|
|
if (message != fixedmessage) {
|
2009-06-20 14:53:30 +00:00
|
|
|
MEM_freeN(message);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2012-05-12 15:02:10 +00:00
|
|
|
message = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2009-06-20 14:53:30 +00:00
|
|
|
len *= 2;
|
2012-03-24 06:18:31 +00:00
|
|
|
if (len > maxlen) {
|
2009-06-20 14:53:30 +00:00
|
|
|
fprintf(stderr, "BLI_dynstr_append text too long or format error.\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-03-24 06:18:31 +00:00
|
|
|
else if (retval >= len) {
|
2009-06-20 14:53:30 +00:00
|
|
|
/* in C99 the actual length required is returned */
|
2019-03-27 13:16:10 +11:00
|
|
|
if (message != fixedmessage) {
|
2009-06-20 14:53:30 +00:00
|
|
|
MEM_freeN(message);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2012-05-12 15:02:10 +00:00
|
|
|
message = NULL;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-04-07 12:37:15 +00:00
|
|
|
/* retval doesn't include \0 terminator */
|
2012-05-12 15:02:10 +00:00
|
|
|
len = retval + 1;
|
2009-06-20 14:53:30 +00:00
|
|
|
}
|
2019-03-27 13:16:10 +11:00
|
|
|
else {
|
2009-06-20 14:53:30 +00:00
|
|
|
break;
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2009-06-20 14:53:30 +00:00
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2012-03-24 06:18:31 +00:00
|
|
|
if (message) {
|
2009-06-20 14:53:30 +00:00
|
|
|
BLI_dynstr_append(ds, message);
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2019-03-27 13:16:10 +11:00
|
|
|
if (message != fixedmessage) {
|
2009-06-20 14:53:30 +00:00
|
|
|
MEM_freeN(message);
|
2019-03-27 13:16:10 +11:00
|
|
|
}
|
2009-06-20 14:53:30 +00:00
|
|
|
}
|
2008-12-29 13:38:08 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 16:37:14 +10:00
|
|
|
int BLI_dynstr_get_len(const DynStr *ds)
|
2011-09-28 05:53:40 +00:00
|
|
|
{
|
2002-10-12 11:37:38 +00:00
|
|
|
return ds->curlen;
|
|
|
|
}
|
|
|
|
|
2021-06-30 16:37:14 +10:00
|
|
|
void BLI_dynstr_get_cstring_ex(const DynStr *__restrict ds, char *__restrict rets)
|
2011-12-17 00:52:36 +00:00
|
|
|
{
|
2011-12-20 02:54:25 +00:00
|
|
|
char *s;
|
2021-06-30 16:37:14 +10:00
|
|
|
const DynStrElem *dse;
|
2011-12-20 02:54:25 +00:00
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
for (s = rets, dse = ds->elems; dse; dse = dse->next) {
|
|
|
|
int slen = strlen(dse->str);
|
2002-10-12 11:37:38 +00:00
|
|
|
|
|
|
|
memcpy(s, dse->str, slen);
|
|
|
|
|
2012-05-12 15:02:10 +00:00
|
|
|
s += slen;
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2014-05-29 20:25:03 +10:00
|
|
|
BLI_assert((s - rets) == ds->curlen);
|
2012-05-12 15:02:10 +00:00
|
|
|
rets[ds->curlen] = '\0';
|
2011-12-20 02:54:25 +00:00
|
|
|
}
|
|
|
|
|
2021-06-30 16:37:14 +10:00
|
|
|
char *BLI_dynstr_get_cstring(const DynStr *ds)
|
2011-12-20 02:54:25 +00:00
|
|
|
{
|
2012-05-12 15:02:10 +00:00
|
|
|
char *rets = MEM_mallocN(ds->curlen + 1, "dynstr_cstring");
|
2011-12-20 02:54:25 +00:00
|
|
|
BLI_dynstr_get_cstring_ex(ds, rets);
|
2002-10-12 11:37:38 +00:00
|
|
|
return rets;
|
|
|
|
}
|
|
|
|
|
2017-04-24 10:42:10 +02:00
|
|
|
void BLI_dynstr_clear(DynStr *ds)
|
|
|
|
{
|
|
|
|
if (ds->memarena) {
|
|
|
|
BLI_memarena_clear(ds->memarena);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
for (DynStrElem *dse_next, *dse = ds->elems; dse; dse = dse_next) {
|
|
|
|
dse_next = dse->next;
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-04-24 10:42:10 +02:00
|
|
|
free(dse->str);
|
|
|
|
free(dse);
|
|
|
|
}
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2017-04-24 10:42:10 +02:00
|
|
|
ds->elems = ds->last = NULL;
|
|
|
|
ds->curlen = 0;
|
|
|
|
}
|
|
|
|
|
2011-12-17 00:52:36 +00:00
|
|
|
void BLI_dynstr_free(DynStr *ds)
|
|
|
|
{
|
2017-04-24 10:42:10 +02:00
|
|
|
if (ds->memarena) {
|
|
|
|
BLI_memarena_free(ds->memarena);
|
2002-10-12 11:37:38 +00:00
|
|
|
}
|
2017-04-24 10:42:10 +02:00
|
|
|
else {
|
|
|
|
BLI_dynstr_clear(ds);
|
|
|
|
}
|
2019-04-17 06:17:24 +02:00
|
|
|
|
2002-10-12 11:37:38 +00:00
|
|
|
MEM_freeN(ds);
|
|
|
|
}
|