Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_dynstr.c « intern « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bc61282fa201e3201ccffdfd6c33c01f672a46fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * 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
 * of the License, or (at your option) any later version.
 *
 * 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,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 */

/** \file
 * \ingroup bli
 * Dynamically sized string ADT.
 */

#include <stdio.h>
#include <stdlib.h> /* malloc */
#include <string.h>

#include "BLI_dynstr.h"
#include "BLI_memarena.h"
#include "BLI_string.h"
#include "BLI_utildefines.h"
#include "MEM_guardedalloc.h"

#ifdef _WIN32
#  ifndef vsnprintf
#    define vsnprintf _vsnprintf
#  endif
#endif

#ifndef va_copy
#  ifdef __va_copy
#    define va_copy(a, b) __va_copy(a, b)
#  else /* !__va_copy */
#    define va_copy(a, b) ((a) = (b))
#  endif /* __va_copy */
#endif   /* va_copy */

/***/

typedef struct DynStrElem DynStrElem;
struct DynStrElem {
  DynStrElem *next;

  char *str;
};

struct DynStr {
  DynStrElem *elems, *last;
  int curlen;
  MemArena *memarena;
};

/***/

DynStr *BLI_dynstr_new(void)
{
  DynStr *ds = MEM_mallocN(sizeof(*ds), "DynStr");
  ds->elems = ds->last = NULL;
  ds->curlen = 0;
  ds->memarena = NULL;

  return ds;
}

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);
}

void BLI_dynstr_append(DynStr *__restrict ds, const char *cstr)
{
  DynStrElem *dse = dynstr_alloc(ds, sizeof(*dse));
  int cstrlen = strlen(cstr);

  dse->str = dynstr_alloc(ds, cstrlen + 1);
  memcpy(dse->str, cstr, cstrlen + 1);
  dse->next = NULL;

  if (!ds->last) {
    ds->last = ds->elems = dse;
  }
  else {
    ds->last = ds->last->next = dse;
  }

  ds->curlen += cstrlen;
}

void BLI_dynstr_nappend(DynStr *__restrict ds, const char *cstr, int len)
{
  DynStrElem *dse = dynstr_alloc(ds, sizeof(*dse));
  int cstrlen = BLI_strnlen(cstr, len);

  dse->str = dynstr_alloc(ds, cstrlen + 1);
  memcpy(dse->str, cstr, cstrlen);
  dse->str[cstrlen] = '\0';
  dse->next = NULL;

  if (!ds->last) {
    ds->last = ds->elems = dse;
  }
  else {
    ds->last = ds->last->next = dse;
  }

  ds->curlen += cstrlen;
}

void BLI_dynstr_vappendf(DynStr *__restrict ds, const char *__restrict format, va_list args)
{
  char *message, fixedmessage[256];
  int len = sizeof(fixedmessage);
  const int maxlen = 65536;
  int retval;

  while (1) {
    va_list args_cpy;
    if (len == sizeof(fixedmessage)) {
      message = fixedmessage;
    }
    else {
      message = MEM_callocN(sizeof(char) * len, "BLI_dynstr_appendf");
    }

    /* can't reuse the same args, so work on a copy */
    va_copy(args_cpy, args);
    retval = vsnprintf(message, len, format, args_cpy);
    va_end(args_cpy);

    if (retval == -1) {
      /* -1 means not enough space, but on windows it may also mean
       * there is a formatting error, so we impose a maximum length */
      if (message != fixedmessage) {
        MEM_freeN(message);
      }
      message = NULL;

      len *= 2;
      if (len > maxlen) {
        fprintf(stderr, "BLI_dynstr_append text too long or format error.\n");
        break;
      }
    }
    else if (retval >= len) {
      /* in C99 the actual length required is returned */
      if (message != fixedmessage) {
        MEM_freeN(message);
      }
      message = NULL;

      /* retval doesn't include \0 terminator */
      len = retval + 1;
    }
    else {
      break;
    }
  }

  if (message) {
    BLI_dynstr_append(ds, message);

    if (message != fixedmessage) {
      MEM_freeN(message);
    }
  }
}

void BLI_dynstr_appendf(DynStr *__restrict ds, const char *__restrict format, ...)
{
  va_list args;
  char *message, fixedmessage[256];
  int len = sizeof(fixedmessage);
  const int maxlen = 65536;
  int retval;

  /* 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 */

  while (1) {
    if (len == sizeof(fixedmessage)) {
      message = fixedmessage;
    }
    else {
      message = MEM_callocN(sizeof(char) * (len), "BLI_dynstr_appendf");
    }

    va_start(args, format);
    retval = vsnprintf(message, len, format, args);
    va_end(args);

    if (retval == -1) {
      /* -1 means not enough space, but on windows it may also mean
       * there is a formatting error, so we impose a maximum length */
      if (message != fixedmessage) {
        MEM_freeN(message);
      }
      message = NULL;

      len *= 2;
      if (len > maxlen) {
        fprintf(stderr, "BLI_dynstr_append text too long or format error.\n");
        break;
      }
    }
    else if (retval >= len) {
      /* in C99 the actual length required is returned */
      if (message != fixedmessage) {
        MEM_freeN(message);
      }
      message = NULL;

      /* retval doesn't include \0 terminator */
      len = retval + 1;
    }
    else {
      break;
    }
  }

  if (message) {
    BLI_dynstr_append(ds, message);

    if (message != fixedmessage) {
      MEM_freeN(message);
    }
  }
}

int BLI_dynstr_get_len(const DynStr *ds)
{
  return ds->curlen;
}

void BLI_dynstr_get_cstring_ex(const DynStr *__restrict ds, char *__restrict rets)
{
  char *s;
  const DynStrElem *dse;

  for (s = rets, dse = ds->elems; dse; dse = dse->next) {
    int slen = strlen(dse->str);

    memcpy(s, dse->str, slen);

    s += slen;
  }
  BLI_assert((s - rets) == ds->curlen);
  rets[ds->curlen] = '\0';
}

char *BLI_dynstr_get_cstring(const DynStr *ds)
{
  char *rets = MEM_mallocN(ds->curlen + 1, "dynstr_cstring");
  BLI_dynstr_get_cstring_ex(ds, rets);
  return rets;
}

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;

      free(dse->str);
      free(dse);
    }
  }

  ds->elems = ds->last = NULL;
  ds->curlen = 0;
}

void BLI_dynstr_free(DynStr *ds)
{
  if (ds->memarena) {
    BLI_memarena_free(ds->memarena);
  }
  else {
    BLI_dynstr_clear(ds);
  }

  MEM_freeN(ds);
}