2007-07-27 05:35:13 -04:00
|
|
|
/** String handling functions
|
|
|
|
* @file */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-12-18 10:56:59 -05:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE /* XXX: fseeko, ftello */
|
|
|
|
#endif
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdarg.h>
|
2005-12-18 10:56:59 -05:00
|
|
|
#include <stdio.h>
|
2005-09-15 09:58:31 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2008-10-18 21:36:00 -04:00
|
|
|
#include <limits.h>
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
#include "elinks.h"
|
|
|
|
|
|
|
|
#include "util/conv.h"
|
|
|
|
#include "util/error.h"
|
|
|
|
#include "util/memdebug.h"
|
|
|
|
#include "util/memory.h"
|
|
|
|
#include "util/string.h"
|
|
|
|
#include "util/snprintf.h"
|
|
|
|
|
|
|
|
|
|
|
|
/* This file looks to be slowly being overloaded by a lot of various stuff,
|
|
|
|
* like memory managment, stubs, tools, granular and non-granular strings,
|
2019-04-21 06:27:40 -04:00
|
|
|
* struct string object... Perhaps util/memory.* and util/stubs.* (stubs.h
|
2005-09-15 09:58:31 -04:00
|
|
|
* probably included in elinks.h, it's important enough) would be nice to
|
|
|
|
* have. --pasky */
|
|
|
|
|
|
|
|
|
|
|
|
#define string_assert(f, l, x, o) \
|
|
|
|
if ((assert_failed = !(x))) { \
|
|
|
|
errfile = f, errline = l, \
|
2007-03-11 04:41:58 -04:00
|
|
|
elinks_internal("[%s] assertion %s failed!", o, #x); \
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG_MEMLEAK
|
|
|
|
|
|
|
|
unsigned char *
|
2007-01-02 14:26:55 -05:00
|
|
|
debug_memacpy(const unsigned char *f, int l, const unsigned char *src, int len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
unsigned char *m;
|
|
|
|
|
|
|
|
string_assert(f, l, len >= 0, "memacpy");
|
|
|
|
if_assert_failed len = 0;
|
|
|
|
|
|
|
|
m = debug_mem_alloc(f, l, len + 1);
|
|
|
|
if (!m) return NULL;
|
|
|
|
|
|
|
|
if (src && len) memcpy(m, src, len);
|
|
|
|
m[len] = '\0';
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *
|
2007-01-02 14:26:55 -05:00
|
|
|
debug_stracpy(const unsigned char *f, int l, const unsigned char *src)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
string_assert(f, l, src, "stracpy");
|
|
|
|
if_assert_failed return NULL;
|
|
|
|
|
|
|
|
return debug_memacpy(f, l, src, strlen(src));
|
|
|
|
}
|
|
|
|
|
|
|
|
#else /* DEBUG_MEMLEAK */
|
|
|
|
|
|
|
|
unsigned char *
|
2007-01-02 14:26:55 -05:00
|
|
|
memacpy(const unsigned char *src, int len)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
unsigned char *m;
|
|
|
|
|
|
|
|
assertm(len >= 0, "[memacpy]");
|
|
|
|
if_assert_failed { len = 0; }
|
|
|
|
|
|
|
|
m = mem_alloc(len + 1);
|
|
|
|
if (!m) return NULL;
|
|
|
|
|
|
|
|
if (src && len) memcpy(m, src, len);
|
|
|
|
m[len] = 0;
|
|
|
|
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *
|
2007-01-02 14:26:55 -05:00
|
|
|
stracpy(const unsigned char *src)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
assertm(src, "[stracpy]");
|
|
|
|
if_assert_failed return NULL;
|
|
|
|
|
|
|
|
return memacpy(src, strlen(src));
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DEBUG_MEMLEAK */
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2007-01-27 18:39:45 -05:00
|
|
|
add_to_strn(unsigned char **dst, const unsigned char *src)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
unsigned char *newdst;
|
|
|
|
int dstlen;
|
|
|
|
int srclen;
|
|
|
|
|
|
|
|
assertm(*dst && src, "[add_to_strn]");
|
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
dstlen = strlen(*dst);
|
|
|
|
srclen = strlen(src) + 1; /* Include the NUL char! */
|
|
|
|
newdst = mem_realloc(*dst, dstlen + srclen);
|
|
|
|
if (!newdst) return;
|
|
|
|
|
|
|
|
memcpy(newdst + dstlen, src, srclen);
|
|
|
|
*dst = newdst;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *
|
2007-01-01 17:47:38 -05:00
|
|
|
insert_in_string(unsigned char **dst, int pos,
|
|
|
|
const unsigned char *seq, int seqlen)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int dstlen = strlen(*dst);
|
|
|
|
unsigned char *string = mem_realloc(*dst, dstlen + seqlen + 1);
|
|
|
|
|
|
|
|
if (!string) return NULL;
|
|
|
|
|
|
|
|
memmove(string + pos + seqlen, string + pos, dstlen - pos + 1);
|
|
|
|
memcpy(string + pos, seq, seqlen);
|
|
|
|
*dst = string;
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *
|
2007-01-27 18:42:08 -05:00
|
|
|
straconcat(const unsigned char *str, ...)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
2007-01-27 18:42:08 -05:00
|
|
|
const unsigned char *a;
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned char *s;
|
|
|
|
unsigned int len;
|
|
|
|
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(str != NULL, "[straconcat]");
|
2005-09-15 09:58:31 -04:00
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
len = strlen(str);
|
|
|
|
s = mem_alloc(len + 1);
|
|
|
|
if (!s) return NULL;
|
|
|
|
|
|
|
|
if (len) memcpy(s, str, len);
|
|
|
|
|
|
|
|
va_start(ap, str);
|
2007-01-27 18:42:08 -05:00
|
|
|
while ((a = va_arg(ap, const unsigned char *))) {
|
2005-09-15 09:58:31 -04:00
|
|
|
unsigned int l = strlen(a);
|
|
|
|
unsigned char *ns;
|
|
|
|
|
|
|
|
if (!l) continue;
|
|
|
|
|
|
|
|
ns = mem_realloc(s, len + 1 + l);
|
|
|
|
if (!ns) {
|
|
|
|
mem_free(s);
|
|
|
|
va_end(ap);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
s = ns;
|
|
|
|
memcpy(s + len, a, l);
|
|
|
|
len += l;
|
|
|
|
}
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
s[len] = '\0';
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2007-01-27 18:43:49 -05:00
|
|
|
xstrcmp(const unsigned char *s1, const unsigned char *s2)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
if (!s1) return -!!s2;
|
|
|
|
if (!s2) return 1;
|
|
|
|
return strcmp(s1, s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned char *
|
|
|
|
safe_strncpy(unsigned char *dst, const unsigned char *src, size_t dst_size)
|
|
|
|
{
|
|
|
|
assertm(dst && src && dst_size > 0, "[safe_strncpy]");
|
|
|
|
if_assert_failed return NULL;
|
|
|
|
|
|
|
|
strncpy(dst, src, dst_size);
|
|
|
|
dst[dst_size - 1] = '\0';
|
|
|
|
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define strlcmp_device(c,s1,n1,s2,n2,t1,t2) { \
|
|
|
|
size_t p; \
|
|
|
|
int d; \
|
|
|
|
\
|
|
|
|
/* XXX: The return value is inconsistent. Hrmpf. Making it consistent
|
|
|
|
* would make the @n1 != @n2 case significantly more expensive, though.
|
|
|
|
* So noone should better rely on the return value actually meaning
|
|
|
|
* anything quantitatively. --pasky */ \
|
|
|
|
\
|
|
|
|
if (!s1 || !s2) \
|
|
|
|
return 1; \
|
|
|
|
\
|
|
|
|
/* n1,n2 is unsigned, so don't assume -1 < 0 ! >:) */ \
|
|
|
|
\
|
|
|
|
/* TODO: Don't precompute strlen()s but rather make the loop smarter.
|
|
|
|
* --pasky */ \
|
|
|
|
if (n1 == -1) n1 = strlen(s1); \
|
|
|
|
if (n2 == -1) n2 = strlen(s2); \
|
|
|
|
\
|
|
|
|
string_assert(errfile, errline, n1 >= 0 && n2 >= 0, c); \
|
|
|
|
\
|
|
|
|
d = n1 - n2; \
|
|
|
|
if (d) return d; \
|
|
|
|
\
|
|
|
|
for (p = 0; p < n1 && s1[p] && s2[p]; p++) { \
|
|
|
|
d = t1 - t2; \
|
|
|
|
if (d) return d; \
|
|
|
|
} \
|
|
|
|
return 0; \
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
elinks_strlcmp(const unsigned char *s1, size_t n1,
|
|
|
|
const unsigned char *s2, size_t n2)
|
|
|
|
{
|
|
|
|
strlcmp_device("strlcmp", s1, n1, s2, n2, s1[p], s2[p]);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
elinks_strlcasecmp(const unsigned char *s1, size_t n1,
|
2008-10-18 21:36:00 -04:00
|
|
|
const unsigned char *s2, size_t n2,
|
|
|
|
const int locale_indep)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2008-10-18 21:36:00 -04:00
|
|
|
if (locale_indep) {
|
|
|
|
strlcmp_device("strlcasecmp", s1, n1, s2, n2, c_toupper(s1[p]), c_toupper(s2[p]));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
strlcmp_device("strlcasecmp", s1, n1, s2, n2, toupper(s1[p]), toupper(s2[p]));
|
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
}
|
|
|
|
|
2009-06-03 15:09:20 -04:00
|
|
|
/* strlcasestr - adapted from c_strcasestr */
|
|
|
|
char *
|
|
|
|
elinks_strlcasestr(const char *haystack, const int haystackl,
|
|
|
|
const char *needle, const int needlel)
|
|
|
|
{
|
|
|
|
size_t haystack_length = haystackl == -1 ? strlen(haystack) : haystackl;
|
|
|
|
size_t needle_length = needlel == -1 ? strlen(needle) : needlel;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (haystack_length < needle_length)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = haystack_length - needle_length + 1; i; i--) {
|
|
|
|
if (!c_strncasecmp(haystack, needle, needle_length))
|
|
|
|
return (char *) haystack;
|
|
|
|
haystack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-02 15:15:38 -05:00
|
|
|
int
|
|
|
|
c_strcasecmp(const char *s1, const char *s2)
|
2008-10-18 21:36:00 -04:00
|
|
|
{
|
2008-11-02 15:15:38 -05:00
|
|
|
for (;; s1++, s2++) {
|
|
|
|
unsigned char c1 = c_tolower(*(const unsigned char *) s1);
|
|
|
|
unsigned char c2 = c_tolower(*(const unsigned char *) s2);
|
|
|
|
|
|
|
|
if (c1 != c2)
|
|
|
|
return (c1 < c2) ? -1: +1;
|
|
|
|
if (c1 == '\0')
|
|
|
|
return 0;
|
|
|
|
}
|
2008-10-18 21:36:00 -04:00
|
|
|
}
|
|
|
|
|
2008-11-02 15:15:38 -05:00
|
|
|
int c_strncasecmp(const char *s1, const char *s2, size_t n)
|
2008-10-18 21:36:00 -04:00
|
|
|
{
|
2008-11-02 15:15:38 -05:00
|
|
|
for (; n > 0; n--, s1++, s2++) {
|
|
|
|
unsigned char c1 = c_tolower(*(const unsigned char *) s1);
|
|
|
|
unsigned char c2 = c_tolower(*(const unsigned char *) s2);
|
|
|
|
|
|
|
|
if (c1 != c2)
|
|
|
|
return (c1 < c2) ? -1: +1;
|
|
|
|
if (c1 == '\0')
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 0;
|
2008-10-18 21:36:00 -04:00
|
|
|
}
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2008-11-01 13:18:06 -04:00
|
|
|
/* c_strcasestr - adapted from src/osdep/stub.c */
|
|
|
|
char * c_strcasestr(const char *haystack, const char *needle)
|
|
|
|
{
|
|
|
|
size_t haystack_length = strlen(haystack);
|
|
|
|
size_t needle_length = strlen(needle);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (haystack_length < needle_length)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = haystack_length - needle_length + 1; i; i--) {
|
|
|
|
if (!c_strncasecmp(haystack, needle, needle_length))
|
|
|
|
return (char *) haystack;
|
|
|
|
haystack++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2005-09-15 09:58:31 -04:00
|
|
|
/* The new string utilities: */
|
|
|
|
|
|
|
|
/* TODO Currently most of the functions use add_bytes_to_string() as a backend
|
|
|
|
* instead we should optimize each function. */
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
NONSTATIC_INLINE struct string *
|
2005-09-15 09:58:31 -04:00
|
|
|
#ifdef DEBUG_MEMLEAK
|
2019-04-21 06:27:40 -04:00
|
|
|
init_string__(const unsigned char *file, int line, struct string *string)
|
2005-09-15 09:58:31 -04:00
|
|
|
#else
|
2019-04-21 06:27:40 -04:00
|
|
|
init_string(struct string *string)
|
2005-09-15 09:58:31 -04:00
|
|
|
#endif
|
|
|
|
{
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(string != NULL, "[init_string]");
|
2005-09-15 09:58:31 -04:00
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
string->length = 0;
|
|
|
|
#ifdef DEBUG_MEMLEAK
|
|
|
|
string->source = debug_mem_alloc(file, line, STRING_GRANULARITY + 1);
|
|
|
|
#else
|
|
|
|
string->source = mem_alloc(STRING_GRANULARITY + 1);
|
|
|
|
#endif
|
|
|
|
if (!string->source) return NULL;
|
|
|
|
|
|
|
|
*string->source = '\0';
|
|
|
|
|
|
|
|
set_string_magic(string);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2009-03-28 14:15:08 -04:00
|
|
|
NONSTATIC_INLINE void
|
2019-04-21 06:27:40 -04:00
|
|
|
done_string(struct string *string)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(string != NULL, "[done_string]");
|
2005-09-15 09:58:31 -04:00
|
|
|
if_assert_failed { return; }
|
|
|
|
|
|
|
|
if (string->source) {
|
|
|
|
/* We only check the magic if we have to free anything so
|
|
|
|
* that done_string() can be called multiple times without
|
|
|
|
* blowing up something */
|
|
|
|
check_string_magic(string);
|
|
|
|
mem_free(string->source);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Blast everything including the magic */
|
|
|
|
memset(string, 0, sizeof(*string));
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
NONSTATIC_INLINE struct string *
|
|
|
|
add_to_string(struct string *string, const unsigned char *source)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
assertm(string && source, "[add_to_string]");
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
if (!*source) return string;
|
|
|
|
|
|
|
|
return add_bytes_to_string(string, source, strlen(source));
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
NONSTATIC_INLINE struct string *
|
|
|
|
add_crlf_to_string(struct string *string)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(string != NULL, "[add_crlf_to_string]");
|
2005-09-15 09:58:31 -04:00
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
if (!realloc_string(string, string->length + 2))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string->source[string->length++] = ASCII_CR;
|
|
|
|
string->source[string->length++] = ASCII_LF;
|
|
|
|
string->source[string->length] = '\0';
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
NONSTATIC_INLINE struct string *
|
|
|
|
add_string_to_string(struct string *string, const struct string *from)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
assertm(string && from, "[add_string_to_string]");
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
check_string_magic(from);
|
|
|
|
|
2007-03-18 14:11:46 -04:00
|
|
|
if (!from->length) return string; /* optimization only */
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
return add_bytes_to_string(string, from->source, from->length);
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_file_to_string(struct string *string, const unsigned char *filename)
|
2005-12-18 10:56:59 -05:00
|
|
|
{
|
|
|
|
FILE *file;
|
|
|
|
off_t filelen;
|
|
|
|
int newlength;
|
|
|
|
|
|
|
|
assertm(string && filename, "[add_file_to_string]");
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
file = fopen(filename, "rb");
|
|
|
|
if (!file) return NULL;
|
|
|
|
|
|
|
|
if (fseeko(file, 0, SEEK_END)) goto err;
|
|
|
|
|
|
|
|
filelen = ftello(file);
|
|
|
|
if (filelen == -1) goto err;
|
|
|
|
|
|
|
|
if (fseeko(file, 0, SEEK_SET)) goto err;
|
|
|
|
|
|
|
|
newlength = string->length + filelen;
|
|
|
|
if (!realloc_string(string, newlength)) goto err;
|
|
|
|
|
|
|
|
string->length += fread(string->source + string->length, 1,
|
|
|
|
(size_t) filelen, file);
|
|
|
|
string->source[string->length] = 0;
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
if (string->length != newlength) goto err;
|
|
|
|
|
|
|
|
return string;
|
|
|
|
|
|
|
|
err:
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
string_concat(struct string *string, ...)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
va_list ap;
|
2007-01-27 19:04:00 -05:00
|
|
|
const unsigned char *source;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(string != NULL, "[string_concat]");
|
2005-09-15 09:58:31 -04:00
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
va_start(ap, string);
|
2007-01-27 19:04:00 -05:00
|
|
|
while ((source = va_arg(ap, const unsigned char *)))
|
2005-09-15 09:58:31 -04:00
|
|
|
if (*source)
|
|
|
|
add_to_string(string, source);
|
|
|
|
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string */
|
2019-04-21 06:27:40 -04:00
|
|
|
NONSTATIC_INLINE struct string *
|
|
|
|
add_char_to_string(struct string *string, unsigned char character)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
assertm(string && character, "[add_char_to_string]");
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
if (!realloc_string(string, string->length + 1))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
string->source[string->length++] = character;
|
|
|
|
string->source[string->length] = '\0';
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
NONSTATIC_INLINE struct string *
|
|
|
|
add_xchar_to_string(struct string *string, unsigned char character, int times)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int newlength;
|
|
|
|
|
|
|
|
assertm(string && character && times >= 0, "[add_xchar_to_string]");
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
if (!times) return string;
|
|
|
|
|
|
|
|
newlength = string->length + times;
|
|
|
|
if (!realloc_string(string, newlength))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
memset(string->source + string->length, character, times);
|
|
|
|
string->length = newlength;
|
|
|
|
string->source[newlength] = '\0';
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 05:35:13 -04:00
|
|
|
/** Add printf()-style format string to @a string. */
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
|
|
|
add_format_to_string(struct string *string, const unsigned char *format, ...)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
int newlength;
|
|
|
|
int width;
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
assertm(string && format, "[add_format_to_string]");
|
|
|
|
if_assert_failed { return NULL; }
|
|
|
|
|
|
|
|
check_string_magic(string);
|
|
|
|
|
|
|
|
va_start(ap, format);
|
2007-09-03 13:53:27 -04:00
|
|
|
width = vsnprintf(NULL, 0, format, ap);
|
|
|
|
va_end(ap);
|
2005-09-15 09:58:31 -04:00
|
|
|
if (width <= 0) return NULL;
|
|
|
|
|
|
|
|
newlength = string->length + width;
|
|
|
|
if (!realloc_string(string, newlength))
|
|
|
|
return NULL;
|
|
|
|
|
2007-09-03 13:53:27 -04:00
|
|
|
va_start(ap, format);
|
2005-09-15 09:58:31 -04:00
|
|
|
vsnprintf(&string->source[string->length], width + 1, format, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
string->length = newlength;
|
|
|
|
string->source[newlength] = '\0';
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *
|
2007-07-26 15:39:08 -04:00
|
|
|
add_to_string_list(LIST_OF(struct string_list_item) *list,
|
|
|
|
const unsigned char *source, int length)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
|
|
|
struct string_list_item *item;
|
2019-04-21 06:27:40 -04:00
|
|
|
struct string *string;
|
2005-09-15 09:58:31 -04:00
|
|
|
|
|
|
|
assertm(list && source, "[add_to_string_list]");
|
|
|
|
if_assert_failed return NULL;
|
|
|
|
|
|
|
|
item = mem_alloc(sizeof(*item));
|
|
|
|
if (!item) return NULL;
|
|
|
|
|
|
|
|
string = &item->string;
|
|
|
|
if (length < 0) length = strlen(source);
|
|
|
|
|
|
|
|
if (!init_string(string)
|
|
|
|
|| !add_bytes_to_string(string, source, length)) {
|
|
|
|
done_string(string);
|
|
|
|
mem_free(item);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
add_to_list_end(*list, item);
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2007-07-27 07:14:00 -04:00
|
|
|
/** @relates string_list_item */
|
2005-09-15 09:58:31 -04:00
|
|
|
void
|
2007-07-26 15:39:08 -04:00
|
|
|
free_string_list(LIST_OF(struct string_list_item) *list)
|
2005-09-15 09:58:31 -04:00
|
|
|
{
|
2007-03-11 06:22:02 -04:00
|
|
|
assertm(list != NULL, "[free_string_list]");
|
2005-09-15 09:58:31 -04:00
|
|
|
if_assert_failed return;
|
|
|
|
|
|
|
|
while (!list_empty(*list)) {
|
|
|
|
struct string_list_item *item = list->next;
|
|
|
|
|
|
|
|
del_from_list(item);
|
|
|
|
done_string(&item->string);
|
|
|
|
mem_free(item);
|
|
|
|
}
|
|
|
|
}
|