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 308 309 310 311 312 313 314 315 316
|
/* Copyright (c) 2007 James Antill -- See LICENSE file for terms. */
#ifndef USTR_SET_H
#error " You should have already included ustr-set.h, or just include ustr.h."
#endif
USTR_CONF_i_PROTO
int ustrp__set_undef(struct Ustr_pool *p, struct Ustr **ps1, size_t nlen)
{
struct Ustr *s1 = USTR_NULL;
struct Ustr *ret = USTR_NULL;
size_t clen = 0;
size_t sz = 0;
size_t oh = 0;
size_t osz = 0;
size_t nsz = 0;
int alloc = USTR_FALSE;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
s1 = *ps1;
clen = ustr_len(s1);
if (nlen == clen)
{
if (ustr_owner(s1))
return (USTR_TRUE);
}
else if (ustr__rw_mod(s1, nlen, &sz, &oh, &osz, &nsz, &alloc))
{
if (nlen > clen)
return (ustrp__add_undef(p, ps1, (nlen - clen)));
else
return (ustrp__del(p, ps1, (clen - nlen)));
}
else if (ustr_limited(s1))
goto fail_enomem;
if (!(ret = ustrp__dupx_undef(p, USTR__DUPX_FROM(s1), nlen)))
goto fail_enomem;
ustrp__sc_free2(p, ps1, ret);
return (USTR_TRUE);
fail_enomem:
ustr_setf_enomem_err(s1);
return (USTR_FALSE);
}
USTR_CONF_I_PROTO int ustr_set_undef(struct Ustr **ps1, size_t nlen)
{ return (ustrp__set_undef(0, ps1, nlen)); }
USTR_CONF_I_PROTO
int ustrp_set_undef(struct Ustr_pool *p, struct Ustrp **ps1, size_t nlen)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set_undef(p, &tmp, nlen);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO int ustrp__set_empty(struct Ustr_pool *p, struct Ustr **ps1)
{
struct Ustr *ret = USTR_NULL;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (ustr_sized(*ps1) && ustr_owner(*ps1))
return (ustrp__del(p, ps1, ustr_len(*ps1)));
if (!(ret = ustrp__dupx_empty(p, USTR__DUPX_FROM(*ps1))))
{
ustr_setf_enomem_err(*ps1);
return (USTR_FALSE);
}
ustrp__sc_free2(p, ps1, ret);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_set_empty(struct Ustr **ps1)
{ return (ustrp__set_empty(0, ps1)); }
USTR_CONF_I_PROTO int ustrp_set_empty(struct Ustr_pool *p, struct Ustrp **ps1)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set_empty(p, &tmp);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO int ustrp__set_buf(struct Ustr_pool *p, struct Ustr **ps1,
const void *buf, size_t len)
{
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (!ustrp__set_undef(p, ps1, len))
return (USTR_FALSE);
ustr__memcpy(*ps1, 0, buf, len);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO
int ustr_set_buf(struct Ustr **ps1, const void *buf, size_t len)
{ return (ustrp__set_buf(0, ps1, buf, len)); }
USTR_CONF_I_PROTO int ustrp_set_buf(struct Ustr_pool *p, struct Ustrp **ps1,
const void *buf, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set_buf(p, &tmp, buf, len);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO
int ustrp__set(struct Ustr_pool *p, struct Ustr **ps1, const struct Ustr *s2)
{
struct Ustr *ret = USTR_NULL;
USTR_ASSERT(ps1 &&
ustrp__assert_valid(!!p, *ps1) && ustrp__assert_valid(!!p, s2));
if (*ps1 == s2)
return (USTR_TRUE);
if (ustr__treat_as_buf(*ps1, 0, ustr_len(s2)))
return (ustrp__set_buf(p, ps1, ustr_cstr(s2), ustr_len(s2)));
if (!(ret = ustrp__dupx(p, USTR__DUPX_FROM(*ps1), s2)))
{
ustr_setf_enomem_err(*ps1);
return (USTR_FALSE);
}
ustrp__sc_free2(p, ps1, ret);
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_set(struct Ustr **ps1, const struct Ustr *s2)
{ return (ustrp__set(0, ps1, s2)); }
USTR_CONF_I_PROTO
int ustrp_set(struct Ustr_pool *p, struct Ustrp **ps1, const struct Ustrp *s2)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set(p, &tmp, &s2->s);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO
int ustrp__set_subustr(struct Ustr_pool *p, struct Ustr **ps1,
const struct Ustr *s2, size_t pos, size_t len)
{
size_t clen = 0;
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (!len)
return (ustrp__del(p, ps1, ustr_len(*ps1)));
clen = ustrp__assert_valid_subustr(!!p, s2, pos, len);
if (!clen)
return (USTR_FALSE);
if (len == clen)
return (ustrp__set(p, ps1, s2));
if ((*ps1 == s2) && ustr_owner(s2) && ustr_alloc(s2))
{ /* only one reference, so we can't take _cstr() before we realloc */
--pos;
ustrp__del(p, ps1, clen - (pos + len)); /* delete bit after */
ustrp__del_subustr(p, ps1, 1, pos); /* delete bit before */
return (USTR_TRUE);
}
return (ustrp__set_buf(p, ps1, ustr_cstr(s2) + pos - 1, len));
}
USTR_CONF_I_PROTO int ustr_set_subustr(struct Ustr **ps1, const struct Ustr *s2,
size_t pos, size_t len)
{ return (ustrp__set_subustr(0, ps1, s2, pos, len)); }
USTR_CONF_I_PROTO
int ustrp_set_subustrp(struct Ustr_pool *p, struct Ustrp **ps1,
const struct Ustrp *s2, size_t pos, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set_subustr(p, &tmp, &s2->s, pos, len);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_i_PROTO int ustrp__set_rep_chr(struct Ustr_pool *p, struct Ustr **ps1,
char chr, size_t len)
{
USTR_ASSERT(ps1 && ustrp__assert_valid(!!p, *ps1));
if (!ustrp__set_undef(p, ps1, len))
return (USTR_FALSE);
ustr__memset(*ps1, 0, chr, len);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO
int ustr_set_rep_chr(struct Ustr **ps1, char chr, size_t len)
{ return (ustrp__set_rep_chr(0, ps1, chr, len)); }
USTR_CONF_I_PROTO int ustrp_set_rep_chr(struct Ustr_pool *p, struct Ustrp **ps1,
char chr, size_t len)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set_rep_chr(p, &tmp, chr, len);
*ps1 = USTRP(tmp);
return (ret);
}
#ifdef USTR_FMT_H
# if USTR_CONF_HAVE_VA_COPY
USTR_CONF_i_PROTO
int ustrp__set_vfmt_lim(struct Ustr_pool *p, struct Ustr **ps1, size_t lim,
const char *fmt, va_list ap)
{ /* NOTE: Copy and pasted so we can use ustrp_set_undef() */
va_list nap;
int rc = -1;
char buf[USTR__SNPRINTF_LOCAL];
USTR__VA_COPY(nap, ap);
rc = USTR_CONF_VSNPRINTF_BEG(buf, sizeof(buf), fmt, nap);
va_end(nap);
if ((rc == -1) && ((rc = ustr__retard_vfmt_ret(fmt, ap)) == -1))
return (USTR_FALSE);
if (lim && ((size_t)rc > lim))
rc = lim;
if ((size_t)rc < sizeof(buf)) /* everything is done */
return (ustrp__set_buf(p, ps1, buf, rc));
if (!ustrp__set_undef(p, ps1, rc))
return (USTR_FALSE);
USTR_CONF_VSNPRINTF_END(ustr_wstr(*ps1), rc + 1, fmt, ap);
USTR_ASSERT(ustrp__assert_valid(!!p, *ps1));
return (USTR_TRUE);
}
USTR_CONF_I_PROTO int ustr_set_vfmt_lim(struct Ustr **ps1, size_t lim,
const char *fmt, va_list ap)
{ return (ustrp__set_vfmt_lim(0, ps1, lim, fmt, ap)); }
USTR_CONF_I_PROTO
int ustrp_set_vfmt_lim(struct Ustr_pool *p,struct Ustrp **ps1, size_t lim,
const char *fmt, va_list ap)
{
struct Ustr *tmp = &(*ps1)->s;
int ret = ustrp__set_vfmt_lim(p, &tmp, lim, fmt, ap);
*ps1 = USTRP(tmp);
return (ret);
}
USTR_CONF_I_PROTO
int ustr_set_fmt_lim(struct Ustr **ps1, size_t lim, const char *fmt, ...)
{
va_list ap;
int ret = USTR_FALSE;
va_start(ap, fmt);
ret = ustr_set_vfmt_lim(ps1, lim, fmt, ap);
va_end(ap);
return (ret);
}
USTR_CONF_I_PROTO
int ustrp_set_fmt_lim(struct Ustr_pool *p, struct Ustrp **ps1, size_t lim,
const char*fmt, ...)
{
va_list ap;
int ret = USTR_FALSE;
va_start(ap, fmt);
ret = ustrp_set_vfmt_lim(p, ps1, lim, fmt, ap);
va_end(ap);
return (ret);
}
USTR_CONF_I_PROTO int ustr_set_vfmt(struct Ustr **ps1,
const char *fmt, va_list ap)
{ return (ustr_set_vfmt_lim(ps1, 0, fmt, ap)); }
USTR_CONF_I_PROTO int ustrp_set_vfmt(struct Ustr_pool *p, struct Ustrp **ps1,
const char *fmt, va_list ap)
{ return (ustrp_set_vfmt_lim(p, ps1, 0, fmt, ap)); }
USTR_CONF_I_PROTO int ustr_set_fmt(struct Ustr **ps1, const char *fmt, ...)
{
va_list ap;
int ret = USTR_FALSE;
va_start(ap, fmt);
ret = ustr_set_vfmt(ps1, fmt, ap);
va_end(ap);
return (ret);
}
USTR_CONF_I_PROTO int ustrp_set_fmt(struct Ustr_pool *p, struct Ustrp **ps1,
const char *fmt, ...)
{
va_list ap;
int ret = USTR_FALSE;
va_start(ap, fmt);
ret = ustrp_set_vfmt(p, ps1, fmt, ap);
va_end(ap);
return (ret);
}
# endif
#endif
|