su_strlst.c 16.5 KB
Newer Older
Pekka Pessi's avatar
Pekka Pessi committed
1 2 3
/*
 * This file is part of the Sofia-SIP package
 *
4
 * Copyright (C) 2006 Nokia Corporation.
Pekka Pessi's avatar
Pekka Pessi committed
5 6 7
 *
 * Contact: Pekka Pessi <pekka.pessi@nokia.com>
 *
8
 * This library is free software; you can redistribute it and/or
Pekka Pessi's avatar
Pekka Pessi committed
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
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 */

/**@defgroup su_strlst String Lists
 *
 * Lists of strings.
 *
 * String lists using #su_home_t.
 *
 */

/**@ingroup su_strlst
 * @CFILE su_strlst.c
 * @brief String lists.
 *
 * The string lists can be used to concatenate a large number of strings, or
 * split a string to smaller pieces (e.g., lines).
 *
 * Example of concatenating a number of strings to @a s:
 * @code
 * su_strlst_t *l = su_strlist_create(home);
43 44 45 46
 * su_strlst_append(l, "=============");
 * su_slprintf(l, "a is: %d", a)
 * su_slprintf(l, "b is: %d", b)
 * su_slprintf(l, "c is: %d", c)
Pekka Pessi's avatar
Pekka Pessi committed
47
 * su_strlst_append(l, "------------");
48 49
 * su_slprintf(l, "total: %d", a + b + c));
 * su_strlst_append(l, "=============");
Pekka Pessi's avatar
Pekka Pessi committed
50 51 52 53 54 55
 * s = su_strlst_join(l, "\n");
 * @endcode
 *
 * Another example, splitting a string into lines and counting the number of
 * non-empty ones:
 * @code
56
 * usize_t i, n;
Pekka Pessi's avatar
Pekka Pessi committed
57 58 59 60
 * su_strlst_t *l;
 * 
 * l = su_strlst_split(NULL, buf, "\n");
 * 
61 62 63
 * nonempty = 0;
 * 
 * for (i = 0; i < su_strlst_len(l); i++) {
Pekka Pessi's avatar
Pekka Pessi committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
 *   n = strcspn(su_strlst_item(l, i), " \t");
 *   if (su_strlst_item(l, i)[n])
 *     nonempty++;
 * }
 * 
 * su_strlst_destroy(l);
 * @endcode
 *
 * @author Pekka Pessi <Pekka.Pessi@nokia.com>
 * 
 * @date Created: Fri May  3 09:22:59 2002 ppessi
 */

#include "config.h"

79 80 81
#include "sofia-sip/su_config.h"
#include "sofia-sip/su_strlst.h"

Pekka Pessi's avatar
Pekka Pessi committed
82 83 84 85 86 87 88 89
#include <stdlib.h>
#include <stddef.h>
#include <memory.h>
#include <limits.h>
#include <string.h>

#include <assert.h>

90 91 92 93 94 95 96
#if defined(va_copy)
/* Xyzzy */
#elif defined(__va_copy)
#define va_copy(dst, src) __va_copy((dst), (src))
#else
#define va_copy(dst, src) (memcpy(&(dst), &(src), sizeof (va_list)))
#endif
Pekka Pessi's avatar
Pekka Pessi committed
97 98 99 100 101 102

enum { N = 8 };

struct su_strlst_s
{
  su_home_t    sl_home[1];
103 104 105
  size_t       sl_size;
  size_t       sl_len;
  size_t       sl_total;
Pekka Pessi's avatar
Pekka Pessi committed
106 107 108
  char const **sl_list;
};

109 110 111 112 113 114
/** Create a list with initial values */
static
su_strlst_t *su_strlst_vcreate_with_by(su_home_t *home,
				       char const *value,
				       va_list va,
				       int deeply)
Pekka Pessi's avatar
Pekka Pessi committed
115 116
{
  su_strlst_t *self;
117
  size_t i, n, m;
118 119 120
  size_t total, size;

  m = 0, total = 0;
Pekka Pessi's avatar
Pekka Pessi committed
121

122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  /* Count arguments and their length */
  if (value) {
    char const *s;
    va_list va0;

    va_copy(va0, va);
    for (s = value; s; m++, s = va_arg(va0, char *))
      total += strlen(s);
    va_end(va0);
  }

  for (n = N; m > n; n *= 2)
    ;

  size = sizeof(*self) + n * sizeof(*self->sl_list);
  if (deeply)
    size += total + m;

  self = su_home_clone(home, size);
Pekka Pessi's avatar
Pekka Pessi committed
141
  if (self) {
142
    self->sl_size = n;
Pekka Pessi's avatar
Pekka Pessi committed
143
    self->sl_list = (char const **)(self + 1);
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
    self->sl_len = m;
    self->sl_total = total;

    if (deeply) {
      char *s = (char *)(self->sl_list + self->sl_size);
      char *end = s + total + m;

      for (i = 0; i < m; i++) {
	assert(s);
	self->sl_list[i] = s;
	s = memccpy(s, value, '\0', end - s);
	value = va_arg(va, char const *);
      }
    }
    else {
      for (i = 0; i < m; i++) {
	self->sl_list[i] = value;
	value = va_arg(va, char const *);
      }
    }
Pekka Pessi's avatar
Pekka Pessi committed
164
  }
165

Pekka Pessi's avatar
Pekka Pessi committed
166 167 168
  return self;
}

169 170 171 172 173 174 175 176
/** Create an empty string list.
 *
 * The list is initially empty. The memory home for the list is cloned from
 * @a home.
 *
 */
su_strlst_t *su_strlst_create(su_home_t *home)
{
177 178 179 180 181 182 183 184
  su_strlst_t *self;

  self = su_home_clone(home, sizeof(*self) + N * sizeof(*self->sl_list));
  if (self) {
    self->sl_size = N;
    self->sl_list = (char const **)(self + 1);
  }
  return self;
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
}

/** Create a string list with initial values.
 *
 * The list is initialized with strings in argument. The argument list is
 * terminated with NULL. The memory home for the list is cloned from @a
 * home.
 */
su_strlst_t *su_strlst_create_with(su_home_t *home, char const *value, ...)
{
  va_list va;
  su_strlst_t *l;

  va_start(va, value);
  l = su_strlst_vcreate_with_by(home, value, va, 0);
  va_end(va);

  return l;
}

/** Create a string list with initial values.
 *
 * The string list is initialized with strings from @a va_list @a va. The
 * argument list is terminated with NULL. The memory home for the list is
 * cloned from @a home.
 */
su_strlst_t *su_strlst_vcreate_with(su_home_t *home,
				    char const *value,
				    va_list va)
{
  return su_strlst_vcreate_with_by(home, value, va, 0);
}

/** Create a string list with duplicatedd initial values.
 *
 * The list is initialized with copies of strings in argument list. The
 * argument list is terminated with NULL. The memory home for the list is
 * cloned from @a home.
 */
su_strlst_t *su_strlst_create_with_dup(su_home_t *home, char const *value, ...)
{
  va_list va;
  su_strlst_t *l;

  va_start(va, value);
  l = su_strlst_vcreate_with_by(home, value, va, 1);
  va_end(va);

  return l;
}

/** Create a string list with duplicates of initial values.
 *
 * The string list is initialized with copies of strings from @a va_list @a
 * va. The argument list is terminated with NULL. The memory home for the
 * list is cloned from @a home.
 */
su_strlst_t *su_strlst_vcreate_with_dup(su_home_t *home,
					char const *value,
					va_list va)
{
  return su_strlst_vcreate_with_by(home, value, va, 1);
}


Pekka Pessi's avatar
Pekka Pessi committed
250 251 252 253 254 255 256
/** Copy a string list */
static
su_strlst_t *su_strlst_copy_by(su_home_t *home, 
			       su_strlst_t const *orig, 
			       int deeply)
{
  su_strlst_t *self;
257
  size_t N, i, size, deepsize = 0;
Pekka Pessi's avatar
Pekka Pessi committed
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277

  if (orig == NULL)
    return NULL;

  N = orig->sl_size;

  if (deeply)
    deepsize = orig->sl_len + orig->sl_total;

  size = sizeof(*self) + N * sizeof(self->sl_list[0]) + deepsize;

  self = su_home_clone(home, size);
  if (self) {
    self->sl_size = N;
    self->sl_list = (char const **)(self + 1);

    self->sl_len = N = orig->sl_len;
    self->sl_total = orig->sl_total;

    if (deeply) {
278
      char *s = (char *)(self->sl_list + self->sl_size);
Pekka Pessi's avatar
Pekka Pessi committed
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 317 318 319 320 321
      char *end = s + deepsize;
      for (i = 0; i < N; i++) {
	self->sl_list[i] = s;
	s = memccpy(s, orig->sl_list[i], '\0', end - s);
	assert(s);
      }
    }
    else {
      for (i = 0; i < N; i++) {
	self->sl_list[i] = orig->sl_list[i];
      }
    }
  }

  return self;
}

/** Shallow copy a string list. */
su_strlst_t *su_strlst_copy(su_home_t *home, su_strlst_t const *orig)
{
  return su_strlst_copy_by(home, orig, 0);
}

/** Deep copy a string list. */
su_strlst_t *su_strlst_dup(su_home_t *home, su_strlst_t const *orig)
{
  return su_strlst_copy_by(home, orig, 1);
}

/** Destroy a string list.
 *
 * The function su_strlst_destroy() destroys a list of strings and frees all
 * duplicated strings belonging to it.
 */
void su_strlst_destroy(su_strlst_t *self)
{
  su_home_zap(self->sl_home);
}

/** Increase the list size for next item, if necessary. */
static int su_strlst_increase(su_strlst_t *self)
{
  if (self->sl_size <= self->sl_len + 1) {
322
    size_t size = 2 * self->sl_size * sizeof(self->sl_list[0]);
Pekka Pessi's avatar
Pekka Pessi committed
323 324 325
    char const **list;

    if (self->sl_list != (char const **)(self + 1)) {
326
      list = su_realloc(self->sl_home, (void *)self->sl_list, size);
Pekka Pessi's avatar
Pekka Pessi committed
327
    } else {
328
      list = su_alloc(self->sl_home, size);
Pekka Pessi's avatar
Pekka Pessi committed
329 330 331 332 333 334 335 336
      if (list)
	memcpy(list, self->sl_list, self->sl_len * sizeof(*self->sl_list));
    }
    
    if (!list)
      return 0;

    self->sl_list = list;
337
    self->sl_size = 2 * self->sl_size;
Pekka Pessi's avatar
Pekka Pessi committed
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
  }

  return 1;
}

/**Duplicate and append a string to list. 
 *
 * @param self  pointer to a string list object
 * @param str   string to be duplicated and appended
 *
 * @return
 * Pointer to duplicated string, if successful, or NULL upon an error.
 */
char *su_strlst_dup_append(su_strlst_t *self, char const *str)
{
353 354
  size_t len;

Pekka Pessi's avatar
Pekka Pessi committed
355 356 357
  if (str == NULL)
    str = "";

358 359 360
  len = strlen(str);

  if (self && su_strlst_increase(self)) {
Pekka Pessi's avatar
Pekka Pessi committed
361 362 363 364 365 366 367 368 369 370 371 372
    char *retval = su_alloc(self->sl_home, len + 1);
    if (retval) {
      memcpy(retval, str, len);
      retval[len] = 0;
      self->sl_list[self->sl_len++] = retval;
      self->sl_total += len;
    }
    return retval;
  }
  return NULL;
}

373
/**Append a string to list.
Pekka Pessi's avatar
Pekka Pessi committed
374
 *
375 376
 * The string is not copied, and it @b must not be modified while string
 * list exists.
Pekka Pessi's avatar
Pekka Pessi committed
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
 *
 * @param self  pointer to a string list object
 * @param str   string to be appended
 *
 * @return
 * Pointer to string, if successful, or NULL upon an error.
 */
char const *su_strlst_append(su_strlst_t *self, char const *str)
{
  if (str == NULL)
    str = "";

  if (self && su_strlst_increase(self)) {
    self->sl_list[self->sl_len++] = str;
    self->sl_total += strlen(str);
    return str;
  }
  return NULL;
}

397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
/**Append a formatted string to the list.
 *
 * Format a string according to a @a fmt like printf(). The resulting string
 * is copied to a memory area freshly allocated from a the memory home of
 * the list and appended to the string list.
 *
 * @param self  pointer to a string list object
 * @param fmt format string
 * @param ... argument list (must match with the @a fmt format string)
 *
 * @return A pointer to a fresh copy of formatting result, or NULL upon an
 * error.
 */
char const *su_slprintf(su_strlst_t *self, char const *fmt, ...)
{
  char const *str;
  va_list ap;
  va_start(ap, fmt);
  str = su_slvprintf(self, fmt, ap);
  va_end(ap);

  return str;
}

/**Append a formatted string to the list.
 *
 * Format a string according to a @a fmt like vprintf(). The resulting
 * string is copied to a memory area freshly allocated from a the memory
 * home of the list and appended to the string list.
 *
 * @param self  pointer to a string list object
428 429
 * @param fmt  format string
 * @param ap   stdarg argument list (must match with the @a fmt format string)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
 *
 * @return A pointer to a fresh copy of formatting result, or NULL upon an
 * error.
 */
char const *su_slvprintf(su_strlst_t *self, char const *fmt, va_list ap)
{
  char *str = NULL;

  if (self && su_strlst_increase(self)) {
    str = su_vsprintf(self->sl_home, fmt, ap);
    if (str) {
      self->sl_list[self->sl_len++] = str;
      self->sl_total += strlen(str);
    }
  }
  return str;
}

Pekka Pessi's avatar
Pekka Pessi committed
448 449 450 451 452 453 454 455 456 457
/**Returns a numbered item from the list of strings. The numbering starts from
 * 0.
 * 
 * @param self  pointer to a string list object
 * @param i     string index
 *
 * @return
 * Pointer to string, if item exists, or NULL if index is out of bounds or
 * list does not exist.
 */
Michael Jerris's avatar
Michael Jerris committed
458
char const *su_strlst_item(su_strlst_t const *self, usize_t i)
Pekka Pessi's avatar
Pekka Pessi committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
{
  if (self && i < self->sl_len)
    return self->sl_list[i];
  else
    return NULL;
}

/**Sets a item to the list of strings. 
 *
 * Note that the item numbering starts from 0.
 * 
 * @param self  pointer to a string list object
 * @param i     string index
 * @param s     string to be set as item @a i
 *
 * @return
 * Pointer to string, if item exists, or NULL if index is out of bounds or
 * list does not exist.
 */
478
char const *su_strlst_set_item(su_strlst_t *self, usize_t i, char const *s)
Pekka Pessi's avatar
Pekka Pessi committed
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
{
  char const *old = NULL;

  if (self == NULL)
    return NULL;
  else if (i == self->sl_len)
    return (void)su_strlst_append(self, s), NULL;
  else if (i > self->sl_len)
    return NULL;

  if (s == NULL)
    s = "";

  old = self->sl_list[i];

  self->sl_list[i] = s;

  return old;
}

/**Removes a numbered item from the list of strings. The numbering starts from
 * 0. The caller is responsible of reclaiming memory used by the removed string.
 * 
 * @param self  pointer to a string list object
 * @param i     string index
 *
 * @return
 * Pointer to string, if item exists, or NULL if index is out of bounds or
 * list does not exist.
 */
509
SU_DLL char const *su_strlst_remove(su_strlst_t *self, usize_t i)
Pekka Pessi's avatar
Pekka Pessi committed
510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
{
  if (self && i < self->sl_len) {
    char const *s = self->sl_list[i];

    memmove(&self->sl_list[i], &self->sl_list[i + 1], 
	    &self->sl_list[self->sl_len] - &self->sl_list[i]);

    self->sl_len--;

    return s;
  }
  else
    return NULL;
}



/** Concatenate list of strings to one string.
 *
 * The function su_strlst_join() concatenates the list of strings. Between
 * each string in list it uses @a sep. The separator is not inserted after
 * the last string in list, but one can always append an empty string to the
 * list.
 *
 * The string is allocated from the memory @a home. If @a home is NULL, the
 * string is allocated using malloc().
 *
 * @param self  pointer to a string list object
 * @param home  home pointer
 * @param sep   separator (may be NULL)
 *
 * @return 
 *
 * The function su_strlst_join() returns a concatenation of the strings in
 * list, or NULL upon an error.
 */
char *su_strlst_join(su_strlst_t *self, su_home_t *home, char const *sep)
{
  if (!sep)
    sep = "";

  if (self && self->sl_len > 0) {
552 553 554 555 556
    size_t seplen = strlen(sep);
    size_t total = self->sl_total + seplen * (self->sl_len - 1);
    char *retval;

    retval = su_alloc(home, total + 1);
Pekka Pessi's avatar
Pekka Pessi committed
557 558 559

    if (retval) {
      char *s = retval;
560
      size_t i = 0, len;
Pekka Pessi's avatar
Pekka Pessi committed
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578

      for (;;) {
	len = strlen(self->sl_list[i]);
	memcpy(s, self->sl_list[i], len), s += len;
	if (++i >= self->sl_len)
	  break;
	memcpy(s, sep, seplen),	s += seplen;
      }
      *s = '\0';
      assert(s == retval + total);
    }

    return retval;
  }

  return su_strdup(home, "");
}

579
su_inline
Pekka Pessi's avatar
Pekka Pessi committed
580 581 582
su_strlst_t *
su_strlst_split0(su_strlst_t *l, char *str, char const *sep)
{
583
  size_t n = sep ? strlen(sep) : 0;
Pekka Pessi's avatar
Pekka Pessi committed
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  char *s;

  if (n > 0) {
    while ((s = strstr(str, sep))) {
      *s = '\0';
      if (!su_strlst_append(l, str))
	return NULL;
      str = s + n;
    }
  }

  if (!su_strlst_append(l, str))
    return NULL;

  return l;
}

/**Split a string. 
 *
 * Splits a string to substrings. It returns a string list object. The
 * string to be split is not copied but instead modified in place. Use
 * su_strlst_dup_split() if you do not want to modify @a str.
 *
 * @param home  home pointer
 * @param str     string to be split
 * @param sep   separator between substrings
 *
 * @return
 * Pointer to list of strings, if successful, or NULL upon an error.
 */
su_strlst_t *
su_strlst_split(su_home_t *home, char *str, char const *sep)
{
  if (str) {
    su_strlst_t *l = su_strlst_create(home);

    if (!su_strlst_split0(l, str, sep))
      su_strlst_destroy(l), l = NULL;

    return l;
  }
  return NULL;
}

/**Duplicate and split a string. 
 *
 * Duplicates a string and splits the result to substrings. It returns a
 * string list object. The string to be splitted is not modified.
 *
 * @param home  home pointer
 * @param cstr  string to be split
 * @param sep   separator between substrings
 *
 * @return
 * Pointer to list of strings, if successful, or NULL upon an error.
 */
su_strlst_t *su_strlst_dup_split(su_home_t *home, 
				 char const *cstr, 
				 char const *sep)
{
  if (cstr) {
    su_strlst_t *l = su_strlst_create(home);

    if (l) {
      char *s = su_strdup(su_strlst_home(l), cstr);

      if (s && !su_strlst_split0(l, s, sep))
	su_strlst_destroy(l), l = NULL;
    }

    return l;
  }
  return NULL;
}

/** Get number of items in list. 
 *
 * The function su_strlst_len() returns the number of items in the 
 * string list.
 * 
 */
665
usize_t su_strlst_len(su_strlst_t const *l)
Pekka Pessi's avatar
Pekka Pessi committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
{
  return l ? l->sl_len : 0;
}

/**Get a string array from list. 
 *
 * The function su_strlst_get_array() returns an array of strings. The
 * length of the array is always one longer than the length of the string
 * list, and the last string in the returned array is always NULL.
 * 
 * @param self pointer to a string list object
 *
 * @return
 * Pointer to array of strings, or NULL if error occurred.
 */
char const **su_strlst_get_array(su_strlst_t *self)
{
  if (self) {
684 685 686 687
    char const **retval;
    size_t size = sizeof(retval[0]) * (self->sl_len + 1);

    retval = su_alloc(self->sl_home, size);
Pekka Pessi's avatar
Pekka Pessi committed
688 689
    
    if (retval) {
690
      memcpy(retval, self->sl_list, sizeof(retval[0]) * self->sl_len);
Pekka Pessi's avatar
Pekka Pessi committed
691
      retval[self->sl_len] = NULL;
692
      return retval;
Pekka Pessi's avatar
Pekka Pessi committed
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712
    }
  }

  return NULL;
}

/**Free a string array. 
 *
 * The function su_strlst_free_array() discards a string array allocated
 * with su_strlst_get_array().
 * 
 * @param self  pointer to a string list object
 * @param array  string array to be freed
 *
 */
void su_strlst_free_array(su_strlst_t *self, char const **array)
{
  if (array)
    su_free(self->sl_home, (void *)array);
}