1/* GLIB - Library of useful routines for C programming 2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public 15 * License along with this library; if not, write to the 16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17 * Boston, MA 02111-1307, USA. 18 */ 19 20/* 21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS 22 * file for a list of people on the GLib Team. See the ChangeLog 23 * files for a list of changes. These files are distributed with 24 * GLib at ftp://ftp.gtk.org/pub/gtk/. 25 */ 26 27/* 28 * Modified by Bruno Haible for use as a gnulib module. 29 */ 30 31/* 32 * MT safe 33 */ 34 35#include "config.h" 36 37#if 0 38#define _GNU_SOURCE /* For stpcpy */ 39#endif 40 41#include <stdarg.h> 42#include <stdio.h> 43#include <stdlib.h> 44#include <string.h> 45#include <locale.h> 46#include <errno.h> 47#include <ctype.h> /* For tolower() */ 48#if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL) 49#include <signal.h> 50#endif 51 52#include "glib.h" 53#if 0 54#include "gprintf.h" 55#include "gprintfint.h" 56 57#include "galias.h" 58 59#ifdef G_OS_WIN32 60#include <windows.h> 61#endif 62#endif 63 64/* do not include <unistd.h> in this place since it 65 * interferes with g_strsignal() on some OSes 66 */ 67 68static const guint16 ascii_table_data[256] = { 69 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 70 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004, 71 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 72 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 73 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 74 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 75 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 76 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 77 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253, 78 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 79 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 80 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 81 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073, 82 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 83 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 84 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004 85 /* the upper 128 are all zeroes */ 86}; 87 88const guint16 * const g_ascii_table = ascii_table_data; 89 90gchar* 91g_strdup (const gchar *str) 92{ 93 gchar *new_str; 94 gsize length; 95 96 if (str) 97 { 98 length = strlen (str) + 1; 99 new_str = g_new (char, length); 100 memcpy (new_str, str, length); 101 } 102 else 103 new_str = NULL; 104 105 return new_str; 106} 107 108#if 0 109 110gpointer 111g_memdup (gconstpointer mem, 112 guint byte_size) 113{ 114 gpointer new_mem; 115 116 if (mem) 117 { 118 new_mem = g_malloc (byte_size); 119 memcpy (new_mem, mem, byte_size); 120 } 121 else 122 new_mem = NULL; 123 124 return new_mem; 125} 126 127#endif 128 129gchar* 130g_strndup (const gchar *str, 131 gsize n) 132{ 133 gchar *new_str; 134 135 if (str) 136 { 137 new_str = g_new (gchar, n + 1); 138 strncpy (new_str, str, n); 139 new_str[n] = '\0'; 140 } 141 else 142 new_str = NULL; 143 144 return new_str; 145} 146 147#if 0 148 149gchar* 150g_strnfill (gsize length, 151 gchar fill_char) 152{ 153 gchar *str; 154 155 str = g_new (gchar, length + 1); 156 memset (str, (guchar)fill_char, length); 157 str[length] = '\0'; 158 159 return str; 160} 161 162#endif 163 164/** 165 * g_stpcpy: 166 * @dest: destination buffer. 167 * @src: source string. 168 * 169 * Copies a nul-terminated string into the dest buffer, include the 170 * trailing nul, and return a pointer to the trailing nul byte. 171 * This is useful for concatenating multiple strings together 172 * without having to repeatedly scan for the end. 173 * 174 * Return value: a pointer to trailing nul byte. 175 **/ 176gchar * 177g_stpcpy (gchar *dest, 178 const gchar *src) 179{ 180#ifdef HAVE_STPCPY 181 g_return_val_if_fail (dest != NULL, NULL); 182 g_return_val_if_fail (src != NULL, NULL); 183 return stpcpy (dest, src); 184#else 185 register gchar *d = dest; 186 register const gchar *s = src; 187 188 g_return_val_if_fail (dest != NULL, NULL); 189 g_return_val_if_fail (src != NULL, NULL); 190 do 191 *d++ = *s; 192 while (*s++ != '\0'); 193 194 return d - 1; 195#endif 196} 197 198gchar* 199g_strdup_vprintf (const gchar *format, 200 va_list args) 201{ 202 gchar *string = NULL; 203 204 g_vasprintf (&string, format, args); 205 206 return string; 207} 208 209gchar* 210g_strdup_printf (const gchar *format, 211 ...) 212{ 213 gchar *buffer; 214 va_list args; 215 216 va_start (args, format); 217 buffer = g_strdup_vprintf (format, args); 218 va_end (args); 219 220 return buffer; 221} 222 223gchar* 224g_strconcat (const gchar *string1, ...) 225{ 226 gsize l; 227 va_list args; 228 gchar *s; 229 gchar *concat; 230 gchar *ptr; 231 232 if (!string1) 233 return NULL; 234 235 l = 1 + strlen (string1); 236 va_start (args, string1); 237 s = va_arg (args, gchar*); 238 while (s) 239 { 240 l += strlen (s); 241 s = va_arg (args, gchar*); 242 } 243 va_end (args); 244 245 concat = g_new (gchar, l); 246 ptr = concat; 247 248 ptr = g_stpcpy (ptr, string1); 249 va_start (args, string1); 250 s = va_arg (args, gchar*); 251 while (s) 252 { 253 ptr = g_stpcpy (ptr, s); 254 s = va_arg (args, gchar*); 255 } 256 va_end (args); 257 258 return concat; 259} 260 261#if 0 262 263/** 264 * g_strtod: 265 * @nptr: the string to convert to a numeric value. 266 * @endptr: if non-%NULL, it returns the character after 267 * the last character used in the conversion. 268 * 269 * Converts a string to a #gdouble value. 270 * It calls the standard strtod() function to handle the conversion, but 271 * if the string is not completely converted it attempts the conversion 272 * again with g_ascii_strtod(), and returns the best match. 273 * 274 * This function should seldomly be used. The normal situation when reading 275 * numbers not for human consumption is to use g_ascii_strtod(). Only when 276 * you know that you must expect both locale formatted and C formatted numbers 277 * should you use this. Make sure that you don't pass strings such as comma 278 * separated lists of values, since the commas may be interpreted as a decimal 279 * point in some locales, causing unexpected results. 280 * 281 * Return value: the #gdouble value. 282 **/ 283gdouble 284g_strtod (const gchar *nptr, 285 gchar **endptr) 286{ 287 gchar *fail_pos_1; 288 gchar *fail_pos_2; 289 gdouble val_1; 290 gdouble val_2 = 0; 291 292 g_return_val_if_fail (nptr != NULL, 0); 293 294 fail_pos_1 = NULL; 295 fail_pos_2 = NULL; 296 297 val_1 = strtod (nptr, &fail_pos_1); 298 299 if (fail_pos_1 && fail_pos_1[0] != 0) 300 val_2 = g_ascii_strtod (nptr, &fail_pos_2); 301 302 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2) 303 { 304 if (endptr) 305 *endptr = fail_pos_1; 306 return val_1; 307 } 308 else 309 { 310 if (endptr) 311 *endptr = fail_pos_2; 312 return val_2; 313 } 314} 315 316/** 317 * g_ascii_strtod: 318 * @nptr: the string to convert to a numeric value. 319 * @endptr: if non-%NULL, it returns the character after 320 * the last character used in the conversion. 321 * 322 * Converts a string to a #gdouble value. 323 * This function behaves like the standard strtod() function 324 * does in the C locale. It does this without actually 325 * changing the current locale, since that would not be 326 * thread-safe. 327 * 328 * This function is typically used when reading configuration 329 * files or other non-user input that should be locale independent. 330 * To handle input from the user you should normally use the 331 * locale-sensitive system strtod() function. 332 * 333 * To convert from a #gdouble to a string in a locale-insensitive 334 * way, use g_ascii_dtostr(). 335 * 336 * If the correct value would cause overflow, plus or minus %HUGE_VAL 337 * is returned (according to the sign of the value), and %ERANGE is 338 * stored in %errno. If the correct value would cause underflow, 339 * zero is returned and %ERANGE is stored in %errno. 340 * 341 * This function resets %errno before calling strtod() so that 342 * you can reliably detect overflow and underflow. 343 * 344 * Return value: the #gdouble value. 345 **/ 346gdouble 347g_ascii_strtod (const gchar *nptr, 348 gchar **endptr) 349{ 350 gchar *fail_pos; 351 gdouble val; 352 struct lconv *locale_data; 353 const char *decimal_point; 354 int decimal_point_len; 355 const char *p, *decimal_point_pos; 356 const char *end = NULL; /* Silence gcc */ 357 int strtod_errno; 358 359 g_return_val_if_fail (nptr != NULL, 0); 360 361 fail_pos = NULL; 362 363 locale_data = localeconv (); 364 decimal_point = locale_data->decimal_point; 365 decimal_point_len = strlen (decimal_point); 366 367 g_assert (decimal_point_len != 0); 368 369 decimal_point_pos = NULL; 370 end = NULL; 371 372 if (decimal_point[0] != '.' || 373 decimal_point[1] != 0) 374 { 375 p = nptr; 376 /* Skip leading space */ 377 while (g_ascii_isspace (*p)) 378 p++; 379 380 /* Skip leading optional sign */ 381 if (*p == '+' || *p == '-') 382 p++; 383 384 if (p[0] == '0' && 385 (p[1] == 'x' || p[1] == 'X')) 386 { 387 p += 2; 388 /* HEX - find the (optional) decimal point */ 389 390 while (g_ascii_isxdigit (*p)) 391 p++; 392 393 if (*p == '.') 394 decimal_point_pos = p++; 395 396 while (g_ascii_isxdigit (*p)) 397 p++; 398 399 if (*p == 'p' || *p == 'P') 400 p++; 401 if (*p == '+' || *p == '-') 402 p++; 403 while (g_ascii_isdigit (*p)) 404 p++; 405 406 end = p; 407 } 408 else if (g_ascii_isdigit (*p) || *p == '.') 409 { 410 while (g_ascii_isdigit (*p)) 411 p++; 412 413 if (*p == '.') 414 decimal_point_pos = p++; 415 416 while (g_ascii_isdigit (*p)) 417 p++; 418 419 if (*p == 'e' || *p == 'E') 420 p++; 421 if (*p == '+' || *p == '-') 422 p++; 423 while (g_ascii_isdigit (*p)) 424 p++; 425 426 end = p; 427 } 428 /* For the other cases, we need not convert the decimal point */ 429 } 430 431 if (decimal_point_pos) 432 { 433 char *copy, *c; 434 435 /* We need to convert the '.' to the locale specific decimal point */ 436 copy = g_malloc (end - nptr + 1 + decimal_point_len); 437 438 c = copy; 439 memcpy (c, nptr, decimal_point_pos - nptr); 440 c += decimal_point_pos - nptr; 441 memcpy (c, decimal_point, decimal_point_len); 442 c += decimal_point_len; 443 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1)); 444 c += end - (decimal_point_pos + 1); 445 *c = 0; 446 447 errno = 0; 448 val = strtod (copy, &fail_pos); 449 strtod_errno = errno; 450 451 if (fail_pos) 452 { 453 if (fail_pos - copy > decimal_point_pos - nptr) 454 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1); 455 else 456 fail_pos = (char *)nptr + (fail_pos - copy); 457 } 458 459 g_free (copy); 460 461 } 462 else if (end) 463 { 464 char *copy; 465 466 copy = g_malloc (end - (char *)nptr + 1); 467 memcpy (copy, nptr, end - nptr); 468 *(copy + (end - (char *)nptr)) = 0; 469 470 errno = 0; 471 val = strtod (copy, &fail_pos); 472 strtod_errno = errno; 473 474 if (fail_pos) 475 { 476 fail_pos = (char *)nptr + (fail_pos - copy); 477 } 478 479 g_free (copy); 480 } 481 else 482 { 483 errno = 0; 484 val = strtod (nptr, &fail_pos); 485 strtod_errno = errno; 486 } 487 488 if (endptr) 489 *endptr = fail_pos; 490 491 errno = strtod_errno; 492 493 return val; 494} 495 496 497/** 498 * g_ascii_dtostr: 499 * @buffer: A buffer to place the resulting string in 500 * @buf_len: The length of the buffer. 501 * @d: The #gdouble to convert 502 * 503 * Converts a #gdouble to a string, using the '.' as 504 * decimal point. 505 * 506 * This functions generates enough precision that converting 507 * the string back using g_ascii_strtod() gives the same machine-number 508 * (on machines with IEEE compatible 64bit doubles). It is 509 * guaranteed that the size of the resulting string will never 510 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes. 511 * 512 * Return value: The pointer to the buffer with the converted string. 513 **/ 514gchar * 515g_ascii_dtostr (gchar *buffer, 516 gint buf_len, 517 gdouble d) 518{ 519 return g_ascii_formatd (buffer, buf_len, "%.17g", d); 520} 521 522/** 523 * g_ascii_formatd: 524 * @buffer: A buffer to place the resulting string in 525 * @buf_len: The length of the buffer. 526 * @format: The printf()-style format to use for the 527 * code to use for converting. 528 * @d: The #gdouble to convert 529 * 530 * Converts a #gdouble to a string, using the '.' as 531 * decimal point. To format the number you pass in 532 * a printf()-style format string. Allowed conversion 533 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'. 534 * 535 * If you just want to want to serialize the value into a 536 * string, use g_ascii_dtostr(). 537 * 538 * Return value: The pointer to the buffer with the converted string. 539 **/ 540gchar * 541g_ascii_formatd (gchar *buffer, 542 gint buf_len, 543 const gchar *format, 544 gdouble d) 545{ 546 struct lconv *locale_data; 547 const char *decimal_point; 548 int decimal_point_len; 549 gchar *p; 550 int rest_len; 551 gchar format_char; 552 553 g_return_val_if_fail (buffer != NULL, NULL); 554 g_return_val_if_fail (format[0] == '%', NULL); 555 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL); 556 557 format_char = format[strlen (format) - 1]; 558 559 g_return_val_if_fail (format_char == 'e' || format_char == 'E' || 560 format_char == 'f' || format_char == 'F' || 561 format_char == 'g' || format_char == 'G', 562 NULL); 563 564 if (format[0] != '%') 565 return NULL; 566 567 if (strpbrk (format + 1, "'l%")) 568 return NULL; 569 570 if (!(format_char == 'e' || format_char == 'E' || 571 format_char == 'f' || format_char == 'F' || 572 format_char == 'g' || format_char == 'G')) 573 return NULL; 574 575 576 _g_snprintf (buffer, buf_len, format, d); 577 578 locale_data = localeconv (); 579 decimal_point = locale_data->decimal_point; 580 decimal_point_len = strlen (decimal_point); 581 582 g_assert (decimal_point_len != 0); 583 584 if (decimal_point[0] != '.' || 585 decimal_point[1] != 0) 586 { 587 p = buffer; 588 589 while (g_ascii_isspace (*p)) 590 p++; 591 592 if (*p == '+' || *p == '-') 593 p++; 594 595 while (isdigit ((guchar)*p)) 596 p++; 597 598 if (strncmp (p, decimal_point, decimal_point_len) == 0) 599 { 600 *p = '.'; 601 p++; 602 if (decimal_point_len > 1) { 603 rest_len = strlen (p + (decimal_point_len-1)); 604 memmove (p, p + (decimal_point_len-1), 605 rest_len); 606 p[rest_len] = 0; 607 608 } 609 } 610 } 611 612 return buffer; 613} 614 615static guint64 616g_parse_long_long (const gchar *nptr, 617 gchar **endptr, 618 guint base, 619 gboolean *negative) 620{ 621 /* this code is based on on the strtol(3) code from GNU libc released under 622 * the GNU General Public License. 623 * 624 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02 625 * Free Software Foundation, Inc. 626 */ 627#define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \ 628 (c) == '\r' || (c) == '\t' || (c) == '\v') 629#define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z') 630#define ISLOWER(c) ((c) >= 'a' && (c) <= 'z') 631#define ISALPHA(c) (ISUPPER (c) || ISLOWER (c)) 632#define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c)) 633#define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c)) 634 gboolean overflow; 635 guint64 cutoff; 636 guint64 cutlim; 637 guint64 ui64; 638 const gchar *s, *save; 639 guchar c; 640 641 g_return_val_if_fail (nptr != NULL, 0); 642 643 if (base == 1 || base > 36) 644 { 645 errno = EINVAL; 646 return 0; 647 } 648 649 save = s = nptr; 650 651 /* Skip white space. */ 652 while (ISSPACE (*s)) 653 ++s; 654 655 if (G_UNLIKELY (!*s)) 656 goto noconv; 657 658 /* Check for a sign. */ 659 *negative = FALSE; 660 if (*s == '-') 661 { 662 *negative = TRUE; 663 ++s; 664 } 665 else if (*s == '+') 666 ++s; 667 668 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */ 669 if (*s == '0') 670 { 671 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X') 672 { 673 s += 2; 674 base = 16; 675 } 676 else if (base == 0) 677 base = 8; 678 } 679 else if (base == 0) 680 base = 10; 681 682 /* Save the pointer so we can check later if anything happened. */ 683 save = s; 684 cutoff = G_MAXUINT64 / base; 685 cutlim = G_MAXUINT64 % base; 686 687 overflow = FALSE; 688 ui64 = 0; 689 c = *s; 690 for (; c; c = *++s) 691 { 692 if (c >= '0' && c <= '9') 693 c -= '0'; 694 else if (ISALPHA (c)) 695 c = TOUPPER (c) - 'A' + 10; 696 else 697 break; 698 if (c >= base) 699 break; 700 /* Check for overflow. */ 701 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim)) 702 overflow = TRUE; 703 else 704 { 705 ui64 *= base; 706 ui64 += c; 707 } 708 } 709 710 /* Check if anything actually happened. */ 711 if (s == save) 712 goto noconv; 713 714 /* Store in ENDPTR the address of one character 715 past the last character we converted. */ 716 if (endptr) 717 *endptr = (gchar*) s; 718 719 if (G_UNLIKELY (overflow)) 720 { 721 errno = ERANGE; 722 return G_MAXUINT64; 723 } 724 725 return ui64; 726 727 noconv: 728 /* We must handle a special case here: the base is 0 or 16 and the 729 first two characters are '0' and 'x', but the rest are no 730 hexadecimal digits. This is no error case. We return 0 and 731 ENDPTR points to the `x`. */ 732 if (endptr) 733 { 734 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X' 735 && save[-2] == '0') 736 *endptr = (gchar*) &save[-1]; 737 else 738 /* There was no number to convert. */ 739 *endptr = (gchar*) nptr; 740 } 741 return 0; 742} 743 744/** 745 * g_ascii_strtoull: 746 * @nptr: the string to convert to a numeric value. 747 * @endptr: if non-%NULL, it returns the character after 748 * the last character used in the conversion. 749 * @base: to be used for the conversion, 2..36 or 0 750 * 751 * Converts a string to a #guint64 value. 752 * This function behaves like the standard strtoull() function 753 * does in the C locale. It does this without actually 754 * changing the current locale, since that would not be 755 * thread-safe. 756 * 757 * This function is typically used when reading configuration 758 * files or other non-user input that should be locale independent. 759 * To handle input from the user you should normally use the 760 * locale-sensitive system strtoull() function. 761 * 762 * If the correct value would cause overflow, %G_MAXUINT64 763 * is returned, and %ERANGE is stored in %errno. If the base is 764 * outside the valid range, zero is returned, and %EINVAL is stored 765 * in %errno. If the string conversion fails, zero is returned, and 766 * @endptr returns @nptr (if @endptr is non-%NULL). 767 * 768 * Return value: the #guint64 value or zero on error. 769 * 770 * Since: 2.2 771 **/ 772guint64 773g_ascii_strtoull (const gchar *nptr, 774 gchar **endptr, 775 guint base) 776{ 777 gboolean negative; 778 guint64 result; 779 780 result = g_parse_long_long (nptr, endptr, base, &negative); 781 782 /* Return the result of the appropriate sign. */ 783 return negative ? -result : result; 784} 785 786/** 787 * g_ascii_strtoll: 788 * @nptr: the string to convert to a numeric value. 789 * @endptr: if non-%NULL, it returns the character after 790 * the last character used in the conversion. 791 * @base: to be used for the conversion, 2..36 or 0 792 * 793 * Converts a string to a #gint64 value. 794 * This function behaves like the standard strtoll() function 795 * does in the C locale. It does this without actually 796 * changing the current locale, since that would not be 797 * thread-safe. 798 * 799 * This function is typically used when reading configuration 800 * files or other non-user input that should be locale independent. 801 * To handle input from the user you should normally use the 802 * locale-sensitive system strtoll() function. 803 * 804 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64 805 * is returned, and %ERANGE is stored in %errno. If the base is 806 * outside the valid range, zero is returned, and %EINVAL is stored 807 * in %errno. If the string conversion fails, zero is returned, and 808 * @endptr returns @nptr (if @endptr is non-%NULL). 809 * 810 * Return value: the #gint64 value or zero on error. 811 * 812 * Since: 2.12 813 **/ 814gint64 815g_ascii_strtoll (const gchar *nptr, 816 gchar **endptr, 817 guint base) 818{ 819 gboolean negative; 820 guint64 result; 821 822 result = g_parse_long_long (nptr, endptr, base, &negative); 823 824 if (negative && result > (guint64) G_MININT64) 825 { 826 errno = ERANGE; 827 return G_MININT64; 828 } 829 else if (!negative && result > (guint64) G_MAXINT64) 830 { 831 errno = ERANGE; 832 return G_MAXINT64; 833 } 834 else 835 return (gint64) result; 836} 837 838G_CONST_RETURN gchar* 839g_strerror (gint errnum) 840{ 841 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT; 842 char *msg; 843 int saved_errno = errno; 844 845#ifdef HAVE_STRERROR 846 const char *msg_locale; 847 848 msg_locale = strerror (errnum); 849 if (g_get_charset (NULL)) 850 { 851 errno = saved_errno; 852 return msg_locale; 853 } 854 else 855 { 856 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL); 857 if (msg_utf8) 858 { 859 /* Stick in the quark table so that we can return a static result 860 */ 861 GQuark msg_quark = g_quark_from_string (msg_utf8); 862 g_free (msg_utf8); 863 864 msg_utf8 = (gchar *) g_quark_to_string (msg_quark); 865 errno = saved_errno; 866 return msg_utf8; 867 } 868 } 869#elif NO_SYS_ERRLIST 870 switch (errnum) 871 { 872#ifdef E2BIG 873 case E2BIG: return "argument list too long"; 874#endif 875#ifdef EACCES 876 case EACCES: return "permission denied"; 877#endif 878#ifdef EADDRINUSE 879 case EADDRINUSE: return "address already in use"; 880#endif 881#ifdef EADDRNOTAVAIL 882 case EADDRNOTAVAIL: return "can't assign requested address"; 883#endif 884#ifdef EADV 885 case EADV: return "advertise error"; 886#endif 887#ifdef EAFNOSUPPORT 888 case EAFNOSUPPORT: return "address family not supported by protocol family"; 889#endif 890#ifdef EAGAIN 891 case EAGAIN: return "try again"; 892#endif 893#ifdef EALIGN 894 case EALIGN: return "EALIGN"; 895#endif 896#ifdef EALREADY 897 case EALREADY: return "operation already in progress"; 898#endif 899#ifdef EBADE 900 case EBADE: return "bad exchange descriptor"; 901#endif 902#ifdef EBADF 903 case EBADF: return "bad file number"; 904#endif 905#ifdef EBADFD 906 case EBADFD: return "file descriptor in bad state"; 907#endif 908#ifdef EBADMSG 909 case EBADMSG: return "not a data message"; 910#endif 911#ifdef EBADR 912 case EBADR: return "bad request descriptor"; 913#endif 914#ifdef EBADRPC 915 case EBADRPC: return "RPC structure is bad"; 916#endif 917#ifdef EBADRQC 918 case EBADRQC: return "bad request code"; 919#endif 920#ifdef EBADSLT 921 case EBADSLT: return "invalid slot"; 922#endif 923#ifdef EBFONT 924 case EBFONT: return "bad font file format"; 925#endif 926#ifdef EBUSY 927 case EBUSY: return "mount device busy"; 928#endif 929#ifdef ECHILD 930 case ECHILD: return "no children"; 931#endif 932#ifdef ECHRNG 933 case ECHRNG: return "channel number out of range"; 934#endif 935#ifdef ECOMM 936 case ECOMM: return "communication error on send"; 937#endif 938#ifdef ECONNABORTED 939 case ECONNABORTED: return "software caused connection abort"; 940#endif 941#ifdef ECONNREFUSED 942 case ECONNREFUSED: return "connection refused"; 943#endif 944#ifdef ECONNRESET 945 case ECONNRESET: return "connection reset by peer"; 946#endif 947#if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK)) 948 case EDEADLK: return "resource deadlock avoided"; 949#endif 950#ifdef EDEADLOCK 951 case EDEADLOCK: return "resource deadlock avoided"; 952#endif 953#ifdef EDESTADDRREQ 954 case EDESTADDRREQ: return "destination address required"; 955#endif 956#ifdef EDIRTY 957 case EDIRTY: return "mounting a dirty fs w/o force"; 958#endif 959#ifdef EDOM 960 case EDOM: return "math argument out of range"; 961#endif 962#ifdef EDOTDOT 963 case EDOTDOT: return "cross mount point"; 964#endif 965#ifdef EDQUOT 966 case EDQUOT: return "disk quota exceeded"; 967#endif 968#ifdef EDUPPKG 969 case EDUPPKG: return "duplicate package name"; 970#endif 971#ifdef EEXIST 972 case EEXIST: return "file already exists"; 973#endif 974#ifdef EFAULT 975 case EFAULT: return "bad address in system call argument"; 976#endif 977#ifdef EFBIG 978 case EFBIG: return "file too large"; 979#endif 980#ifdef EHOSTDOWN 981 case EHOSTDOWN: return "host is down"; 982#endif 983#ifdef EHOSTUNREACH 984 case EHOSTUNREACH: return "host is unreachable"; 985#endif 986#ifdef EIDRM 987 case EIDRM: return "identifier removed"; 988#endif 989#ifdef EINIT 990 case EINIT: return "initialization error"; 991#endif 992#ifdef EINPROGRESS 993 case EINPROGRESS: return "operation now in progress"; 994#endif 995#ifdef EINTR 996 case EINTR: return "interrupted system call"; 997#endif 998#ifdef EINVAL 999 case EINVAL: return "invalid argument"; 1000#endif 1001#ifdef EIO 1002 case EIO: return "I/O error"; 1003#endif 1004#ifdef EISCONN 1005 case EISCONN: return "socket is already connected"; 1006#endif 1007#ifdef EISDIR 1008 case EISDIR: return "is a directory"; 1009#endif 1010#ifdef EISNAME 1011 case EISNAM: return "is a name file"; 1012#endif 1013#ifdef ELBIN 1014 case ELBIN: return "ELBIN"; 1015#endif 1016#ifdef EL2HLT 1017 case EL2HLT: return "level 2 halted"; 1018#endif 1019#ifdef EL2NSYNC 1020 case EL2NSYNC: return "level 2 not synchronized"; 1021#endif 1022#ifdef EL3HLT 1023 case EL3HLT: return "level 3 halted"; 1024#endif 1025#ifdef EL3RST 1026 case EL3RST: return "level 3 reset"; 1027#endif 1028#ifdef ELIBACC 1029 case ELIBACC: return "can not access a needed shared library"; 1030#endif 1031#ifdef ELIBBAD 1032 case ELIBBAD: return "accessing a corrupted shared library"; 1033#endif 1034#ifdef ELIBEXEC 1035 case ELIBEXEC: return "can not exec a shared library directly"; 1036#endif 1037#ifdef ELIBMAX 1038 case ELIBMAX: return "attempting to link in more shared libraries than system limit"; 1039#endif 1040#ifdef ELIBSCN 1041 case ELIBSCN: return ".lib section in a.out corrupted"; 1042#endif 1043#ifdef ELNRNG 1044 case ELNRNG: return "link number out of range"; 1045#endif 1046#ifdef ELOOP 1047 case ELOOP: return "too many levels of symbolic links"; 1048#endif 1049#ifdef EMFILE 1050 case EMFILE: return "too many open files"; 1051#endif 1052#ifdef EMLINK 1053 case EMLINK: return "too many links"; 1054#endif 1055#ifdef EMSGSIZE 1056 case EMSGSIZE: return "message too long"; 1057#endif 1058#ifdef EMULTIHOP 1059 case EMULTIHOP: return "multihop attempted"; 1060#endif 1061#ifdef ENAMETOOLONG 1062 case ENAMETOOLONG: return "file name too long"; 1063#endif 1064#ifdef ENAVAIL 1065 case ENAVAIL: return "not available"; 1066#endif 1067#ifdef ENET 1068 case ENET: return "ENET"; 1069#endif 1070#ifdef ENETDOWN 1071 case ENETDOWN: return "network is down"; 1072#endif 1073#ifdef ENETRESET 1074 case ENETRESET: return "network dropped connection on reset"; 1075#endif 1076#ifdef ENETUNREACH 1077 case ENETUNREACH: return "network is unreachable"; 1078#endif 1079#ifdef ENFILE 1080 case ENFILE: return "file table overflow"; 1081#endif 1082#ifdef ENOANO 1083 case ENOANO: return "anode table overflow"; 1084#endif 1085#if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR)) 1086 case ENOBUFS: return "no buffer space available"; 1087#endif 1088#ifdef ENOCSI 1089 case ENOCSI: return "no CSI structure available"; 1090#endif 1091#ifdef ENODATA 1092 case ENODATA: return "no data available"; 1093#endif 1094#ifdef ENODEV 1095 case ENODEV: return "no such device"; 1096#endif 1097#ifdef ENOENT 1098 case ENOENT: return "no such file or directory"; 1099#endif 1100#ifdef ENOEXEC 1101 case ENOEXEC: return "exec format error"; 1102#endif 1103#ifdef ENOLCK 1104 case ENOLCK: return "no locks available"; 1105#endif 1106#ifdef ENOLINK 1107 case ENOLINK: return "link has be severed"; 1108#endif 1109#ifdef ENOMEM 1110 case ENOMEM: return "not enough memory"; 1111#endif 1112#ifdef ENOMSG 1113 case ENOMSG: return "no message of desired type"; 1114#endif 1115#ifdef ENONET 1116 case ENONET: return "machine is not on the network"; 1117#endif 1118#ifdef ENOPKG 1119 case ENOPKG: return "package not installed"; 1120#endif 1121#ifdef ENOPROTOOPT 1122 case ENOPROTOOPT: return "bad proocol option"; 1123#endif 1124#ifdef ENOSPC 1125 case ENOSPC: return "no space left on device"; 1126#endif 1127#ifdef ENOSR 1128 case ENOSR: return "out of stream resources"; 1129#endif 1130#ifdef ENOSTR 1131 case ENOSTR: return "not a stream device"; 1132#endif 1133#ifdef ENOSYM 1134 case ENOSYM: return "unresolved symbol name"; 1135#endif 1136#ifdef ENOSYS 1137 case ENOSYS: return "function not implemented"; 1138#endif 1139#ifdef ENOTBLK 1140 case ENOTBLK: return "block device required"; 1141#endif 1142#ifdef ENOTCONN 1143 case ENOTCONN: return "socket is not connected"; 1144#endif 1145#ifdef ENOTDIR 1146 case ENOTDIR: return "not a directory"; 1147#endif 1148#ifdef ENOTEMPTY 1149 case ENOTEMPTY: return "directory not empty"; 1150#endif 1151#ifdef ENOTNAM 1152 case ENOTNAM: return "not a name file"; 1153#endif 1154#ifdef ENOTSOCK 1155 case ENOTSOCK: return "socket operation on non-socket"; 1156#endif 1157#ifdef ENOTTY 1158 case ENOTTY: return "inappropriate device for ioctl"; 1159#endif 1160#ifdef ENOTUNIQ 1161 case ENOTUNIQ: return "name not unique on network"; 1162#endif 1163#ifdef ENXIO 1164 case ENXIO: return "no such device or address"; 1165#endif 1166#ifdef EOPNOTSUPP 1167 case EOPNOTSUPP: return "operation not supported on socket"; 1168#endif 1169#ifdef EPERM 1170 case EPERM: return "not owner"; 1171#endif 1172#ifdef EPFNOSUPPORT 1173 case EPFNOSUPPORT: return "protocol family not supported"; 1174#endif 1175#ifdef EPIPE 1176 case EPIPE: return "broken pipe"; 1177#endif 1178#ifdef EPROCLIM 1179 case EPROCLIM: return "too many processes"; 1180#endif 1181#ifdef EPROCUNAVAIL 1182 case EPROCUNAVAIL: return "bad procedure for program"; 1183#endif 1184#ifdef EPROGMISMATCH 1185 case EPROGMISMATCH: return "program version wrong"; 1186#endif 1187#ifdef EPROGUNAVAIL 1188 case EPROGUNAVAIL: return "RPC program not available"; 1189#endif 1190#ifdef EPROTO 1191 case EPROTO: return "protocol error"; 1192#endif 1193#ifdef EPROTONOSUPPORT 1194 case EPROTONOSUPPORT: return "protocol not suppored"; 1195#endif 1196#ifdef EPROTOTYPE 1197 case EPROTOTYPE: return "protocol wrong type for socket"; 1198#endif 1199#ifdef ERANGE 1200 case ERANGE: return "math result unrepresentable"; 1201#endif 1202#if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED)) 1203 case EREFUSED: return "EREFUSED"; 1204#endif 1205#ifdef EREMCHG 1206 case EREMCHG: return "remote address changed"; 1207#endif 1208#ifdef EREMDEV 1209 case EREMDEV: return "remote device"; 1210#endif 1211#ifdef EREMOTE 1212 case EREMOTE: return "pathname hit remote file system"; 1213#endif 1214#ifdef EREMOTEIO 1215 case EREMOTEIO: return "remote i/o error"; 1216#endif 1217#ifdef EREMOTERELEASE 1218 case EREMOTERELEASE: return "EREMOTERELEASE"; 1219#endif 1220#ifdef EROFS 1221 case EROFS: return "read-only file system"; 1222#endif 1223#ifdef ERPCMISMATCH 1224 case ERPCMISMATCH: return "RPC version is wrong"; 1225#endif 1226#ifdef ERREMOTE 1227 case ERREMOTE: return "object is remote"; 1228#endif 1229#ifdef ESHUTDOWN 1230 case ESHUTDOWN: return "can't send afer socket shutdown"; 1231#endif 1232#ifdef ESOCKTNOSUPPORT 1233 case ESOCKTNOSUPPORT: return "socket type not supported"; 1234#endif 1235#ifdef ESPIPE 1236 case ESPIPE: return "invalid seek"; 1237#endif 1238#ifdef ESRCH 1239 case ESRCH: return "no such process"; 1240#endif 1241#ifdef ESRMNT 1242 case ESRMNT: return "srmount error"; 1243#endif 1244#ifdef ESTALE 1245 case ESTALE: return "stale remote file handle"; 1246#endif 1247#ifdef ESUCCESS 1248 case ESUCCESS: return "Error 0"; 1249#endif 1250#ifdef ETIME 1251 case ETIME: return "timer expired"; 1252#endif 1253#ifdef ETIMEDOUT 1254 case ETIMEDOUT: return "connection timed out"; 1255#endif 1256#ifdef ETOOMANYREFS 1257 case ETOOMANYREFS: return "too many references: can't splice"; 1258#endif 1259#ifdef ETXTBSY 1260 case ETXTBSY: return "text file or pseudo-device busy"; 1261#endif 1262#ifdef EUCLEAN 1263 case EUCLEAN: return "structure needs cleaning"; 1264#endif 1265#ifdef EUNATCH 1266 case EUNATCH: return "protocol driver not attached"; 1267#endif 1268#ifdef EUSERS 1269 case EUSERS: return "too many users"; 1270#endif 1271#ifdef EVERSION 1272 case EVERSION: return "version mismatch"; 1273#endif 1274#if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN)) 1275 case EWOULDBLOCK: return "operation would block"; 1276#endif 1277#ifdef EXDEV 1278 case EXDEV: return "cross-domain link"; 1279#endif 1280#ifdef EXFULL 1281 case EXFULL: return "message tables full"; 1282#endif 1283 } 1284#else /* NO_SYS_ERRLIST */ 1285 extern int sys_nerr; 1286 extern char *sys_errlist[]; 1287 1288 if ((errnum > 0) && (errnum <= sys_nerr)) 1289 return sys_errlist [errnum]; 1290#endif /* NO_SYS_ERRLIST */ 1291 1292 msg = g_static_private_get (&msg_private); 1293 if (!msg) 1294 { 1295 msg = g_new (gchar, 64); 1296 g_static_private_set (&msg_private, msg, g_free); 1297 } 1298 1299 _g_sprintf (msg, "unknown error (%d)", errnum); 1300 1301 errno = saved_errno; 1302 return msg; 1303} 1304 1305G_CONST_RETURN gchar* 1306g_strsignal (gint signum) 1307{ 1308 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT; 1309 char *msg; 1310 1311#ifdef HAVE_STRSIGNAL 1312 const char *msg_locale; 1313 1314#if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN) 1315extern const char *strsignal(int); 1316#else 1317 /* this is declared differently (const) in string.h on BeOS */ 1318 extern char *strsignal (int sig); 1319#endif /* !G_OS_BEOS && !G_WITH_CYGWIN */ 1320 msg_locale = strsignal (signum); 1321 if (g_get_charset (NULL)) 1322 return msg_locale; 1323 else 1324 { 1325 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL); 1326 if (msg_utf8) 1327 { 1328 /* Stick in the quark table so that we can return a static result 1329 */ 1330 GQuark msg_quark = g_quark_from_string (msg_utf8); 1331 g_free (msg_utf8); 1332 1333 return g_quark_to_string (msg_quark); 1334 } 1335 } 1336#elif NO_SYS_SIGLIST 1337 switch (signum) 1338 { 1339#ifdef SIGHUP 1340 case SIGHUP: return "Hangup"; 1341#endif 1342#ifdef SIGINT 1343 case SIGINT: return "Interrupt"; 1344#endif 1345#ifdef SIGQUIT 1346 case SIGQUIT: return "Quit"; 1347#endif 1348#ifdef SIGILL 1349 case SIGILL: return "Illegal instruction"; 1350#endif 1351#ifdef SIGTRAP 1352 case SIGTRAP: return "Trace/breakpoint trap"; 1353#endif 1354#ifdef SIGABRT 1355 case SIGABRT: return "IOT trap/Abort"; 1356#endif 1357#ifdef SIGBUS 1358 case SIGBUS: return "Bus error"; 1359#endif 1360#ifdef SIGFPE 1361 case SIGFPE: return "Floating point exception"; 1362#endif 1363#ifdef SIGKILL 1364 case SIGKILL: return "Killed"; 1365#endif 1366#ifdef SIGUSR1 1367 case SIGUSR1: return "User defined signal 1"; 1368#endif 1369#ifdef SIGSEGV 1370 case SIGSEGV: return "Segmentation fault"; 1371#endif 1372#ifdef SIGUSR2 1373 case SIGUSR2: return "User defined signal 2"; 1374#endif 1375#ifdef SIGPIPE 1376 case SIGPIPE: return "Broken pipe"; 1377#endif 1378#ifdef SIGALRM 1379 case SIGALRM: return "Alarm clock"; 1380#endif 1381#ifdef SIGTERM 1382 case SIGTERM: return "Terminated"; 1383#endif 1384#ifdef SIGSTKFLT 1385 case SIGSTKFLT: return "Stack fault"; 1386#endif 1387#ifdef SIGCHLD 1388 case SIGCHLD: return "Child exited"; 1389#endif 1390#ifdef SIGCONT 1391 case SIGCONT: return "Continued"; 1392#endif 1393#ifdef SIGSTOP 1394 case SIGSTOP: return "Stopped (signal)"; 1395#endif 1396#ifdef SIGTSTP 1397 case SIGTSTP: return "Stopped"; 1398#endif 1399#ifdef SIGTTIN 1400 case SIGTTIN: return "Stopped (tty input)"; 1401#endif 1402#ifdef SIGTTOU 1403 case SIGTTOU: return "Stopped (tty output)"; 1404#endif 1405#ifdef SIGURG 1406 case SIGURG: return "Urgent condition"; 1407#endif 1408#ifdef SIGXCPU 1409 case SIGXCPU: return "CPU time limit exceeded"; 1410#endif 1411#ifdef SIGXFSZ 1412 case SIGXFSZ: return "File size limit exceeded"; 1413#endif 1414#ifdef SIGVTALRM 1415 case SIGVTALRM: return "Virtual time alarm"; 1416#endif 1417#ifdef SIGPROF 1418 case SIGPROF: return "Profile signal"; 1419#endif 1420#ifdef SIGWINCH 1421 case SIGWINCH: return "Window size changed"; 1422#endif 1423#ifdef SIGIO 1424 case SIGIO: return "Possible I/O"; 1425#endif 1426#ifdef SIGPWR 1427 case SIGPWR: return "Power failure"; 1428#endif 1429#ifdef SIGUNUSED 1430 case SIGUNUSED: return "Unused signal"; 1431#endif 1432 } 1433#else /* NO_SYS_SIGLIST */ 1434 1435#ifdef NO_SYS_SIGLIST_DECL 1436 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/ 1437#endif 1438 1439 return (char*) /* this function should return const --josh */ sys_siglist [signum]; 1440#endif /* NO_SYS_SIGLIST */ 1441 1442 msg = g_static_private_get (&msg_private); 1443 if (!msg) 1444 { 1445 msg = g_new (gchar, 64); 1446 g_static_private_set (&msg_private, msg, g_free); 1447 } 1448 1449 _g_sprintf (msg, "unknown signal (%d)", signum); 1450 1451 return msg; 1452} 1453 1454/* Functions g_strlcpy and g_strlcat were originally developed by 1455 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code. 1456 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3 1457 * for more information. 1458 */ 1459 1460#ifdef HAVE_STRLCPY 1461/* Use the native ones, if available; they might be implemented in assembly */ 1462gsize 1463g_strlcpy (gchar *dest, 1464 const gchar *src, 1465 gsize dest_size) 1466{ 1467 g_return_val_if_fail (dest != NULL, 0); 1468 g_return_val_if_fail (src != NULL, 0); 1469 1470 return strlcpy (dest, src, dest_size); 1471} 1472 1473gsize 1474g_strlcat (gchar *dest, 1475 const gchar *src, 1476 gsize dest_size) 1477{ 1478 g_return_val_if_fail (dest != NULL, 0); 1479 g_return_val_if_fail (src != NULL, 0); 1480 1481 return strlcat (dest, src, dest_size); 1482} 1483 1484#else /* ! HAVE_STRLCPY */ 1485/* g_strlcpy 1486 * 1487 * Copy string src to buffer dest (of buffer size dest_size). At most 1488 * dest_size-1 characters will be copied. Always NUL terminates 1489 * (unless dest_size == 0). This function does NOT allocate memory. 1490 * Unlike strncpy, this function doesn't pad dest (so it's often faster). 1491 * Returns size of attempted result, strlen(src), 1492 * so if retval >= dest_size, truncation occurred. 1493 */ 1494gsize 1495g_strlcpy (gchar *dest, 1496 const gchar *src, 1497 gsize dest_size) 1498{ 1499 register gchar *d = dest; 1500 register const gchar *s = src; 1501 register gsize n = dest_size; 1502 1503 g_return_val_if_fail (dest != NULL, 0); 1504 g_return_val_if_fail (src != NULL, 0); 1505 1506 /* Copy as many bytes as will fit */ 1507 if (n != 0 && --n != 0) 1508 do 1509 { 1510 register gchar c = *s++; 1511 1512 *d++ = c; 1513 if (c == 0) 1514 break; 1515 } 1516 while (--n != 0); 1517 1518 /* If not enough room in dest, add NUL and traverse rest of src */ 1519 if (n == 0) 1520 { 1521 if (dest_size != 0) 1522 *d = 0; 1523 while (*s++) 1524 ; 1525 } 1526 1527 return s - src - 1; /* count does not include NUL */ 1528} 1529 1530/* g_strlcat 1531 * 1532 * Appends string src to buffer dest (of buffer size dest_size). 1533 * At most dest_size-1 characters will be copied. 1534 * Unlike strncat, dest_size is the full size of dest, not the space left over. 1535 * This function does NOT allocate memory. 1536 * This always NUL terminates (unless siz == 0 or there were no NUL characters 1537 * in the dest_size characters of dest to start with). 1538 * Returns size of attempted result, which is 1539 * MIN (dest_size, strlen (original dest)) + strlen (src), 1540 * so if retval >= dest_size, truncation occurred. 1541 */ 1542gsize 1543g_strlcat (gchar *dest, 1544 const gchar *src, 1545 gsize dest_size) 1546{ 1547 register gchar *d = dest; 1548 register const gchar *s = src; 1549 register gsize bytes_left = dest_size; 1550 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */ 1551 1552 g_return_val_if_fail (dest != NULL, 0); 1553 g_return_val_if_fail (src != NULL, 0); 1554 1555 /* Find the end of dst and adjust bytes left but don't go past end */ 1556 while (*d != 0 && bytes_left-- != 0) 1557 d++; 1558 dlength = d - dest; 1559 bytes_left = dest_size - dlength; 1560 1561 if (bytes_left == 0) 1562 return dlength + strlen (s); 1563 1564 while (*s != 0) 1565 { 1566 if (bytes_left != 1) 1567 { 1568 *d++ = *s; 1569 bytes_left--; 1570 } 1571 s++; 1572 } 1573 *d = 0; 1574 1575 return dlength + (s - src); /* count does not include NUL */ 1576} 1577#endif /* ! HAVE_STRLCPY */ 1578 1579/** 1580 * g_ascii_strdown: 1581 * @str: a string. 1582 * @len: length of @str in bytes, or -1 if @str is nul-terminated. 1583 * 1584 * Converts all upper case ASCII letters to lower case ASCII letters. 1585 * 1586 * Return value: a newly-allocated string, with all the upper case 1587 * characters in @str converted to lower case, with 1588 * semantics that exactly match g_ascii_tolower(). (Note 1589 * that this is unlike the old g_strdown(), which modified 1590 * the string in place.) 1591 **/ 1592gchar* 1593g_ascii_strdown (const gchar *str, 1594 gssize len) 1595{ 1596 gchar *result, *s; 1597 1598 g_return_val_if_fail (str != NULL, NULL); 1599 1600 if (len < 0) 1601 len = strlen (str); 1602 1603 result = g_strndup (str, len); 1604 for (s = result; *s; s++) 1605 *s = g_ascii_tolower (*s); 1606 1607 return result; 1608} 1609 1610#endif 1611 1612/** 1613 * g_ascii_strup: 1614 * @str: a string. 1615 * @len: length of @str in bytes, or -1 if @str is nul-terminated. 1616 * 1617 * Converts all lower case ASCII letters to upper case ASCII letters. 1618 * 1619 * Return value: a newly allocated string, with all the lower case 1620 * characters in @str converted to upper case, with 1621 * semantics that exactly match g_ascii_toupper(). (Note 1622 * that this is unlike the old g_strup(), which modified 1623 * the string in place.) 1624 **/ 1625gchar* 1626g_ascii_strup (const gchar *str, 1627 gssize len) 1628{ 1629 gchar *result, *s; 1630 1631 g_return_val_if_fail (str != NULL, NULL); 1632 1633 if (len < 0) 1634 len = strlen (str); 1635 1636 result = g_strndup (str, len); 1637 for (s = result; *s; s++) 1638 *s = g_ascii_toupper (*s); 1639 1640 return result; 1641} 1642 1643#if 0 1644 1645/** 1646 * g_strdown: 1647 * @string: the string to convert. 1648 * 1649 * Converts a string to lower case. 1650 * 1651 * Return value: the string 1652 * 1653 * Deprecated:2.2: This function is totally broken for the reasons discussed 1654 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown() 1655 * instead. 1656 **/ 1657gchar* 1658g_strdown (gchar *string) 1659{ 1660 register guchar *s; 1661 1662 g_return_val_if_fail (string != NULL, NULL); 1663 1664 s = (guchar *) string; 1665 1666 while (*s) 1667 { 1668 if (isupper (*s)) 1669 *s = tolower (*s); 1670 s++; 1671 } 1672 1673 return (gchar *) string; 1674} 1675 1676/** 1677 * g_strup: 1678 * @string: the string to convert. 1679 * 1680 * Converts a string to upper case. 1681 * 1682 * Return value: the string 1683 * 1684 * Deprecated:2.2: This function is totally broken for the reasons discussed 1685 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead. 1686 **/ 1687gchar* 1688g_strup (gchar *string) 1689{ 1690 register guchar *s; 1691 1692 g_return_val_if_fail (string != NULL, NULL); 1693 1694 s = (guchar *) string; 1695 1696 while (*s) 1697 { 1698 if (islower (*s)) 1699 *s = toupper (*s); 1700 s++; 1701 } 1702 1703 return (gchar *) string; 1704} 1705 1706gchar* 1707g_strreverse (gchar *string) 1708{ 1709 g_return_val_if_fail (string != NULL, NULL); 1710 1711 if (*string) 1712 { 1713 register gchar *h, *t; 1714 1715 h = string; 1716 t = string + strlen (string) - 1; 1717 1718 while (h < t) 1719 { 1720 register gchar c; 1721 1722 c = *h; 1723 *h = *t; 1724 h++; 1725 *t = c; 1726 t--; 1727 } 1728 } 1729 1730 return string; 1731} 1732 1733/** 1734 * g_ascii_tolower: 1735 * @c: any character. 1736 * 1737 * Convert a character to ASCII lower case. 1738 * 1739 * Unlike the standard C library tolower() function, this only 1740 * recognizes standard ASCII letters and ignores the locale, returning 1741 * all non-ASCII characters unchanged, even if they are lower case 1742 * letters in a particular character set. Also unlike the standard 1743 * library function, this takes and returns a char, not an int, so 1744 * don't call it on %EOF but no need to worry about casting to #guchar 1745 * before passing a possibly non-ASCII character in. 1746 * 1747 * Return value: the result of converting @c to lower case. 1748 * If @c is not an ASCII upper case letter, 1749 * @c is returned unchanged. 1750 **/ 1751gchar 1752g_ascii_tolower (gchar c) 1753{ 1754 return g_ascii_isupper (c) ? c - 'A' + 'a' : c; 1755} 1756 1757#endif 1758 1759/** 1760 * g_ascii_toupper: 1761 * @c: any character. 1762 * 1763 * Convert a character to ASCII upper case. 1764 * 1765 * Unlike the standard C library toupper() function, this only 1766 * recognizes standard ASCII letters and ignores the locale, returning 1767 * all non-ASCII characters unchanged, even if they are upper case 1768 * letters in a particular character set. Also unlike the standard 1769 * library function, this takes and returns a char, not an int, so 1770 * don't call it on %EOF but no need to worry about casting to #guchar 1771 * before passing a possibly non-ASCII character in. 1772 * 1773 * Return value: the result of converting @c to upper case. 1774 * If @c is not an ASCII lower case letter, 1775 * @c is returned unchanged. 1776 **/ 1777gchar 1778g_ascii_toupper (gchar c) 1779{ 1780 return g_ascii_islower (c) ? c - 'a' + 'A' : c; 1781} 1782 1783#if 0 1784 1785/** 1786 * g_ascii_digit_value: 1787 * @c: an ASCII character. 1788 * 1789 * Determines the numeric value of a character as a decimal 1790 * digit. Differs from g_unichar_digit_value() because it takes 1791 * a char, so there's no worry about sign extension if characters 1792 * are signed. 1793 * 1794 * Return value: If @c is a decimal digit (according to 1795 * g_ascii_isdigit()), its numeric value. Otherwise, -1. 1796 **/ 1797int 1798g_ascii_digit_value (gchar c) 1799{ 1800 if (g_ascii_isdigit (c)) 1801 return c - '0'; 1802 return -1; 1803} 1804 1805/** 1806 * g_ascii_xdigit_value: 1807 * @c: an ASCII character. 1808 * 1809 * Determines the numeric value of a character as a hexidecimal 1810 * digit. Differs from g_unichar_xdigit_value() because it takes 1811 * a char, so there's no worry about sign extension if characters 1812 * are signed. 1813 * 1814 * Return value: If @c is a hex digit (according to 1815 * g_ascii_isxdigit()), its numeric value. Otherwise, -1. 1816 **/ 1817int 1818g_ascii_xdigit_value (gchar c) 1819{ 1820 if (c >= 'A' && c <= 'F') 1821 return c - 'A' + 10; 1822 if (c >= 'a' && c <= 'f') 1823 return c - 'a' + 10; 1824 return g_ascii_digit_value (c); 1825} 1826 1827/** 1828 * g_ascii_strcasecmp: 1829 * @s1: string to compare with @s2. 1830 * @s2: string to compare with @s1. 1831 * 1832 * Compare two strings, ignoring the case of ASCII characters. 1833 * 1834 * Unlike the BSD strcasecmp() function, this only recognizes standard 1835 * ASCII letters and ignores the locale, treating all non-ASCII 1836 * bytes as if they are not letters. 1837 * 1838 * This function should be used only on strings that are known to be 1839 * in encodings where the bytes corresponding to ASCII letters always 1840 * represent themselves. This includes UTF-8 and the ISO-8859-* 1841 * charsets, but not for instance double-byte encodings like the 1842 * Windows Codepage 932, where the trailing bytes of double-byte 1843 * characters include all ASCII letters. If you compare two CP932 1844 * strings using this function, you will get false matches. 1845 * 1846 * Return value: 0 if the strings match, a negative value if @s1 < @s2, 1847 * or a positive value if @s1 > @s2. 1848 **/ 1849gint 1850g_ascii_strcasecmp (const gchar *s1, 1851 const gchar *s2) 1852{ 1853 gint c1, c2; 1854 1855 g_return_val_if_fail (s1 != NULL, 0); 1856 g_return_val_if_fail (s2 != NULL, 0); 1857 1858 while (*s1 && *s2) 1859 { 1860 c1 = (gint)(guchar) TOLOWER (*s1); 1861 c2 = (gint)(guchar) TOLOWER (*s2); 1862 if (c1 != c2) 1863 return (c1 - c2); 1864 s1++; s2++; 1865 } 1866 1867 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2)); 1868} 1869 1870/** 1871 * g_ascii_strncasecmp: 1872 * @s1: string to compare with @s2. 1873 * @s2: string to compare with @s1. 1874 * @n: number of characters to compare. 1875 * 1876 * Compare @s1 and @s2, ignoring the case of ASCII characters and any 1877 * characters after the first @n in each string. 1878 * 1879 * Unlike the BSD strcasecmp() function, this only recognizes standard 1880 * ASCII letters and ignores the locale, treating all non-ASCII 1881 * characters as if they are not letters. 1882 * 1883 * The same warning as in g_ascii_strcasecmp() applies: Use this 1884 * function only on strings known to be in encodings where bytes 1885 * corresponding to ASCII letters always represent themselves. 1886 * 1887 * Return value: 0 if the strings match, a negative value if @s1 < @s2, 1888 * or a positive value if @s1 > @s2. 1889 **/ 1890gint 1891g_ascii_strncasecmp (const gchar *s1, 1892 const gchar *s2, 1893 gsize n) 1894{ 1895 gint c1, c2; 1896 1897 g_return_val_if_fail (s1 != NULL, 0); 1898 g_return_val_if_fail (s2 != NULL, 0); 1899 1900 while (n && *s1 && *s2) 1901 { 1902 n -= 1; 1903 c1 = (gint)(guchar) TOLOWER (*s1); 1904 c2 = (gint)(guchar) TOLOWER (*s2); 1905 if (c1 != c2) 1906 return (c1 - c2); 1907 s1++; s2++; 1908 } 1909 1910 if (n) 1911 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2)); 1912 else 1913 return 0; 1914} 1915 1916/** 1917 * g_strcasecmp: 1918 * @s1: a string. 1919 * @s2: a string to compare with @s1. 1920 * 1921 * A case-insensitive string comparison, corresponding to the standard 1922 * strcasecmp() function on platforms which support it. 1923 * 1924 * Return value: 0 if the strings match, a negative value if @s1 < @s2, 1925 * or a positive value if @s1 > @s2. 1926 * 1927 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function 1928 * is deprecated and how to replace it. 1929 **/ 1930gint 1931g_strcasecmp (const gchar *s1, 1932 const gchar *s2) 1933{ 1934#ifdef HAVE_STRCASECMP 1935 g_return_val_if_fail (s1 != NULL, 0); 1936 g_return_val_if_fail (s2 != NULL, 0); 1937 1938 return strcasecmp (s1, s2); 1939#else 1940 gint c1, c2; 1941 1942 g_return_val_if_fail (s1 != NULL, 0); 1943 g_return_val_if_fail (s2 != NULL, 0); 1944 1945 while (*s1 && *s2) 1946 { 1947 /* According to A. Cox, some platforms have islower's that 1948 * don't work right on non-uppercase 1949 */ 1950 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1; 1951 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2; 1952 if (c1 != c2) 1953 return (c1 - c2); 1954 s1++; s2++; 1955 } 1956 1957 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2)); 1958#endif 1959} 1960 1961/** 1962 * g_strncasecmp: 1963 * @s1: a string. 1964 * @s2: a string to compare with @s1. 1965 * @n: the maximum number of characters to compare. 1966 * 1967 * A case-insensitive string comparison, corresponding to the standard 1968 * strncasecmp() function on platforms which support it. 1969 * It is similar to g_strcasecmp() except it only compares the first @n 1970 * characters of the strings. 1971 * 1972 * Return value: 0 if the strings match, a negative value if @s1 < @s2, 1973 * or a positive value if @s1 > @s2. 1974 * 1975 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the 1976 * comparison by calling toupper()/tolower(). These functions are 1977 * locale-specific and operate on single bytes. However, it is impossible 1978 * to handle things correctly from an I18N standpoint by operating on 1979 * bytes, since characters may be multibyte. Thus g_strncasecmp() is 1980 * broken if your string is guaranteed to be ASCII, since it's 1981 * locale-sensitive, and it's broken if your string is localized, since 1982 * it doesn't work on many encodings at all, including UTF-8, EUC-JP, 1983 * etc. 1984 * 1985 * There are therefore two replacement functions: g_ascii_strncasecmp(), 1986 * which only works on ASCII and is not locale-sensitive, and 1987 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8. 1988 **/ 1989gint 1990g_strncasecmp (const gchar *s1, 1991 const gchar *s2, 1992 guint n) 1993{ 1994#ifdef HAVE_STRNCASECMP 1995 return strncasecmp (s1, s2, n); 1996#else 1997 gint c1, c2; 1998 1999 g_return_val_if_fail (s1 != NULL, 0); 2000 g_return_val_if_fail (s2 != NULL, 0); 2001 2002 while (n && *s1 && *s2) 2003 { 2004 n -= 1; 2005 /* According to A. Cox, some platforms have islower's that 2006 * don't work right on non-uppercase 2007 */ 2008 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1; 2009 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2; 2010 if (c1 != c2) 2011 return (c1 - c2); 2012 s1++; s2++; 2013 } 2014 2015 if (n) 2016 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2)); 2017 else 2018 return 0; 2019#endif 2020} 2021 2022gchar* 2023g_strdelimit (gchar *string, 2024 const gchar *delimiters, 2025 gchar new_delim) 2026{ 2027 register gchar *c; 2028 2029 g_return_val_if_fail (string != NULL, NULL); 2030 2031 if (!delimiters) 2032 delimiters = G_STR_DELIMITERS; 2033 2034 for (c = string; *c; c++) 2035 { 2036 if (strchr (delimiters, *c)) 2037 *c = new_delim; 2038 } 2039 2040 return string; 2041} 2042 2043gchar* 2044g_strcanon (gchar *string, 2045 const gchar *valid_chars, 2046 gchar substitutor) 2047{ 2048 register gchar *c; 2049 2050 g_return_val_if_fail (string != NULL, NULL); 2051 g_return_val_if_fail (valid_chars != NULL, NULL); 2052 2053 for (c = string; *c; c++) 2054 { 2055 if (!strchr (valid_chars, *c)) 2056 *c = substitutor; 2057 } 2058 2059 return string; 2060} 2061 2062gchar* 2063g_strcompress (const gchar *source) 2064{ 2065 const gchar *p = source, *octal; 2066 gchar *dest = g_malloc (strlen (source) + 1); 2067 gchar *q = dest; 2068 2069 while (*p) 2070 { 2071 if (*p == '\\') 2072 { 2073 p++; 2074 switch (*p) 2075 { 2076 case '\0': 2077 g_warning ("g_strcompress: trailing \\"); 2078 goto out; 2079 case '0': case '1': case '2': case '3': case '4': 2080 case '5': case '6': case '7': 2081 *q = 0; 2082 octal = p; 2083 while ((p < octal + 3) && (*p >= '0') && (*p <= '7')) 2084 { 2085 *q = (*q * 8) + (*p - '0'); 2086 p++; 2087 } 2088 q++; 2089 p--; 2090 break; 2091 case 'b': 2092 *q++ = '\b'; 2093 break; 2094 case 'f': 2095 *q++ = '\f'; 2096 break; 2097 case 'n': 2098 *q++ = '\n'; 2099 break; 2100 case 'r': 2101 *q++ = '\r'; 2102 break; 2103 case 't': 2104 *q++ = '\t'; 2105 break; 2106 default: /* Also handles \" and \\ */ 2107 *q++ = *p; 2108 break; 2109 } 2110 } 2111 else 2112 *q++ = *p; 2113 p++; 2114 } 2115out: 2116 *q = 0; 2117 2118 return dest; 2119} 2120 2121gchar * 2122g_strescape (const gchar *source, 2123 const gchar *exceptions) 2124{ 2125 const guchar *p; 2126 gchar *dest; 2127 gchar *q; 2128 guchar excmap[256]; 2129 2130 g_return_val_if_fail (source != NULL, NULL); 2131 2132 p = (guchar *) source; 2133 /* Each source byte needs maximally four destination chars (\777) */ 2134 q = dest = g_malloc (strlen (source) * 4 + 1); 2135 2136 memset (excmap, 0, 256); 2137 if (exceptions) 2138 { 2139 guchar *e = (guchar *) exceptions; 2140 2141 while (*e) 2142 { 2143 excmap[*e] = 1; 2144 e++; 2145 } 2146 } 2147 2148 while (*p) 2149 { 2150 if (excmap[*p]) 2151 *q++ = *p; 2152 else 2153 { 2154 switch (*p) 2155 { 2156 case '\b': 2157 *q++ = '\\'; 2158 *q++ = 'b'; 2159 break; 2160 case '\f': 2161 *q++ = '\\'; 2162 *q++ = 'f'; 2163 break; 2164 case '\n': 2165 *q++ = '\\'; 2166 *q++ = 'n'; 2167 break; 2168 case '\r': 2169 *q++ = '\\'; 2170 *q++ = 'r'; 2171 break; 2172 case '\t': 2173 *q++ = '\\'; 2174 *q++ = 't'; 2175 break; 2176 case '\\': 2177 *q++ = '\\'; 2178 *q++ = '\\'; 2179 break; 2180 case '"': 2181 *q++ = '\\'; 2182 *q++ = '"'; 2183 break; 2184 default: 2185 if ((*p < ' ') || (*p >= 0177)) 2186 { 2187 *q++ = '\\'; 2188 *q++ = '0' + (((*p) >> 6) & 07); 2189 *q++ = '0' + (((*p) >> 3) & 07); 2190 *q++ = '0' + ((*p) & 07); 2191 } 2192 else 2193 *q++ = *p; 2194 break; 2195 } 2196 } 2197 p++; 2198 } 2199 *q = 0; 2200 return dest; 2201} 2202 2203gchar* 2204g_strchug (gchar *string) 2205{ 2206 guchar *start; 2207 2208 g_return_val_if_fail (string != NULL, NULL); 2209 2210 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++) 2211 ; 2212 2213 g_memmove (string, start, strlen ((gchar *) start) + 1); 2214 2215 return string; 2216} 2217 2218gchar* 2219g_strchomp (gchar *string) 2220{ 2221 gsize len; 2222 2223 g_return_val_if_fail (string != NULL, NULL); 2224 2225 len = strlen (string); 2226 while (len--) 2227 { 2228 if (g_ascii_isspace ((guchar) string[len])) 2229 string[len] = '\0'; 2230 else 2231 break; 2232 } 2233 2234 return string; 2235} 2236 2237/** 2238 * g_strsplit: 2239 * @string: a string to split. 2240 * @delimiter: a string which specifies the places at which to split the string. 2241 * The delimiter is not included in any of the resulting strings, unless 2242 * @max_tokens is reached. 2243 * @max_tokens: the maximum number of pieces to split @string into. If this is 2244 * less than 1, the string is split completely. 2245 * 2246 * Splits a string into a maximum of @max_tokens pieces, using the given 2247 * @delimiter. If @max_tokens is reached, the remainder of @string is appended 2248 * to the last token. 2249 * 2250 * As a special case, the result of splitting the empty string "" is an empty 2251 * vector, not a vector containing a single string. The reason for this 2252 * special case is that being able to represent a empty vector is typically 2253 * more useful than consistent handling of empty elements. If you do need 2254 * to represent empty elements, you'll need to check for the empty string 2255 * before calling g_strsplit(). 2256 * 2257 * Return value: a newly-allocated %NULL-terminated array of strings. Use 2258 * g_strfreev() to free it. 2259 **/ 2260gchar** 2261g_strsplit (const gchar *string, 2262 const gchar *delimiter, 2263 gint max_tokens) 2264{ 2265 GSList *string_list = NULL, *slist; 2266 gchar **str_array, *s; 2267 guint n = 0; 2268 const gchar *remainder; 2269 2270 g_return_val_if_fail (string != NULL, NULL); 2271 g_return_val_if_fail (delimiter != NULL, NULL); 2272 g_return_val_if_fail (delimiter[0] != '\0', NULL); 2273 2274 if (max_tokens < 1) 2275 max_tokens = G_MAXINT; 2276 2277 remainder = string; 2278 s = strstr (remainder, delimiter); 2279 if (s) 2280 { 2281 gsize delimiter_len = strlen (delimiter); 2282 2283 while (--max_tokens && s) 2284 { 2285 gsize len; 2286 gchar *new_string; 2287 2288 len = s - remainder; 2289 new_string = g_new (gchar, len + 1); 2290 strncpy (new_string, remainder, len); 2291 new_string[len] = 0; 2292 string_list = g_slist_prepend (string_list, new_string); 2293 n++; 2294 remainder = s + delimiter_len; 2295 s = strstr (remainder, delimiter); 2296 } 2297 } 2298 if (*string) 2299 { 2300 n++; 2301 string_list = g_slist_prepend (string_list, g_strdup (remainder)); 2302 } 2303 2304 str_array = g_new (gchar*, n + 1); 2305 2306 str_array[n--] = NULL; 2307 for (slist = string_list; slist; slist = slist->next) 2308 str_array[n--] = slist->data; 2309 2310 g_slist_free (string_list); 2311 2312 return str_array; 2313} 2314 2315/** 2316 * g_strsplit_set: 2317 * @string: The string to be tokenized 2318 * @delimiters: A nul-terminated string containing bytes that are used 2319 * to split the string. 2320 * @max_tokens: The maximum number of tokens to split @string into. 2321 * If this is less than 1, the string is split completely 2322 * 2323 * Splits @string into a number of tokens not containing any of the characters 2324 * in @delimiter. A token is the (possibly empty) longest string that does not 2325 * contain any of the characters in @delimiters. If @max_tokens is reached, the 2326 * remainder is appended to the last token. 2327 * 2328 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a 2329 * %NULL-terminated vector containing the three strings "abc", "def", 2330 * and "ghi". 2331 * 2332 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated 2333 * vector containing the four strings "", "def", "ghi", and "". 2334 * 2335 * As a special case, the result of splitting the empty string "" is an empty 2336 * vector, not a vector containing a single string. The reason for this 2337 * special case is that being able to represent a empty vector is typically 2338 * more useful than consistent handling of empty elements. If you do need 2339 * to represent empty elements, you'll need to check for the empty string 2340 * before calling g_strsplit_set(). 2341 * 2342 * Note that this function works on bytes not characters, so it can't be used 2343 * to delimit UTF-8 strings for anything but ASCII characters. 2344 * 2345 * Return value: a newly-allocated %NULL-terminated array of strings. Use 2346 * g_strfreev() to free it. 2347 * 2348 * Since: 2.4 2349 **/ 2350gchar ** 2351g_strsplit_set (const gchar *string, 2352 const gchar *delimiters, 2353 gint max_tokens) 2354{ 2355 gboolean delim_table[256]; 2356 GSList *tokens, *list; 2357 gint n_tokens; 2358 const gchar *s; 2359 const gchar *current; 2360 gchar *token; 2361 gchar **result; 2362 2363 g_return_val_if_fail (string != NULL, NULL); 2364 g_return_val_if_fail (delimiters != NULL, NULL); 2365 2366 if (max_tokens < 1) 2367 max_tokens = G_MAXINT; 2368 2369 if (*string == '\0') 2370 { 2371 result = g_new (char *, 1); 2372 result[0] = NULL; 2373 return result; 2374 } 2375 2376 memset (delim_table, FALSE, sizeof (delim_table)); 2377 for (s = delimiters; *s != '\0'; ++s) 2378 delim_table[*(guchar *)s] = TRUE; 2379 2380 tokens = NULL; 2381 n_tokens = 0; 2382 2383 s = current = string; 2384 while (*s != '\0') 2385 { 2386 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens) 2387 { 2388 gchar *token; 2389 2390 token = g_strndup (current, s - current); 2391 tokens = g_slist_prepend (tokens, token); 2392 ++n_tokens; 2393 2394 current = s + 1; 2395 } 2396 2397 ++s; 2398 } 2399 2400 token = g_strndup (current, s - current); 2401 tokens = g_slist_prepend (tokens, token); 2402 ++n_tokens; 2403 2404 result = g_new (gchar *, n_tokens + 1); 2405 2406 result[n_tokens] = NULL; 2407 for (list = tokens; list != NULL; list = list->next) 2408 result[--n_tokens] = list->data; 2409 2410 g_slist_free (tokens); 2411 2412 return result; 2413} 2414 2415/** 2416 * g_strfreev: 2417 * @str_array: a %NULL-terminated array of strings to free. 2418 2419 * Frees a %NULL-terminated array of strings, and the array itself. 2420 * If called on a %NULL value, g_strfreev() simply returns. 2421 **/ 2422void 2423g_strfreev (gchar **str_array) 2424{ 2425 if (str_array) 2426 { 2427 int i; 2428 2429 for(i = 0; str_array[i] != NULL; i++) 2430 g_free(str_array[i]); 2431 2432 g_free (str_array); 2433 } 2434} 2435 2436/** 2437 * g_strdupv: 2438 * @str_array: %NULL-terminated array of strings. 2439 * 2440 * Copies %NULL-terminated array of strings. The copy is a deep copy; 2441 * the new array should be freed by first freeing each string, then 2442 * the array itself. g_strfreev() does this for you. If called 2443 * on a %NULL value, g_strdupv() simply returns %NULL. 2444 * 2445 * Return value: a new %NULL-terminated array of strings. 2446 **/ 2447gchar** 2448g_strdupv (gchar **str_array) 2449{ 2450 if (str_array) 2451 { 2452 gint i; 2453 gchar **retval; 2454 2455 i = 0; 2456 while (str_array[i]) 2457 ++i; 2458 2459 retval = g_new (gchar*, i + 1); 2460 2461 i = 0; 2462 while (str_array[i]) 2463 { 2464 retval[i] = g_strdup (str_array[i]); 2465 ++i; 2466 } 2467 retval[i] = NULL; 2468 2469 return retval; 2470 } 2471 else 2472 return NULL; 2473} 2474 2475gchar* 2476g_strjoinv (const gchar *separator, 2477 gchar **str_array) 2478{ 2479 gchar *string; 2480 gchar *ptr; 2481 2482 g_return_val_if_fail (str_array != NULL, NULL); 2483 2484 if (separator == NULL) 2485 separator = ""; 2486 2487 if (*str_array) 2488 { 2489 gint i; 2490 gsize len; 2491 gsize separator_len; 2492 2493 separator_len = strlen (separator); 2494 /* First part, getting length */ 2495 len = 1 + strlen (str_array[0]); 2496 for (i = 1; str_array[i] != NULL; i++) 2497 len += strlen (str_array[i]); 2498 len += separator_len * (i - 1); 2499 2500 /* Second part, building string */ 2501 string = g_new (gchar, len); 2502 ptr = g_stpcpy (string, *str_array); 2503 for (i = 1; str_array[i] != NULL; i++) 2504 { 2505 ptr = g_stpcpy (ptr, separator); 2506 ptr = g_stpcpy (ptr, str_array[i]); 2507 } 2508 } 2509 else 2510 string = g_strdup (""); 2511 2512 return string; 2513} 2514 2515gchar* 2516g_strjoin (const gchar *separator, 2517 ...) 2518{ 2519 gchar *string, *s; 2520 va_list args; 2521 gsize len; 2522 gsize separator_len; 2523 gchar *ptr; 2524 2525 if (separator == NULL) 2526 separator = ""; 2527 2528 separator_len = strlen (separator); 2529 2530 va_start (args, separator); 2531 2532 s = va_arg (args, gchar*); 2533 2534 if (s) 2535 { 2536 /* First part, getting length */ 2537 len = 1 + strlen (s); 2538 2539 s = va_arg (args, gchar*); 2540 while (s) 2541 { 2542 len += separator_len + strlen (s); 2543 s = va_arg (args, gchar*); 2544 } 2545 va_end (args); 2546 2547 /* Second part, building string */ 2548 string = g_new (gchar, len); 2549 2550 va_start (args, separator); 2551 2552 s = va_arg (args, gchar*); 2553 ptr = g_stpcpy (string, s); 2554 2555 s = va_arg (args, gchar*); 2556 while (s) 2557 { 2558 ptr = g_stpcpy (ptr, separator); 2559 ptr = g_stpcpy (ptr, s); 2560 s = va_arg (args, gchar*); 2561 } 2562 } 2563 else 2564 string = g_strdup (""); 2565 2566 va_end (args); 2567 2568 return string; 2569} 2570 2571#endif 2572 2573/** 2574 * g_strstr_len: 2575 * @haystack: a string. 2576 * @haystack_len: the maximum length of @haystack. 2577 * @needle: the string to search for. 2578 * 2579 * Searches the string @haystack for the first occurrence 2580 * of the string @needle, limiting the length of the search 2581 * to @haystack_len. 2582 * 2583 * Return value: a pointer to the found occurrence, or 2584 * %NULL if not found. 2585 **/ 2586gchar * 2587g_strstr_len (const gchar *haystack, 2588 gssize haystack_len, 2589 const gchar *needle) 2590{ 2591 g_return_val_if_fail (haystack != NULL, NULL); 2592 g_return_val_if_fail (needle != NULL, NULL); 2593 2594 if (haystack_len < 0) 2595 return strstr (haystack, needle); 2596 else 2597 { 2598 const gchar *p = haystack; 2599 gsize needle_len = strlen (needle); 2600 const gchar *end; 2601 gsize i; 2602 2603 if (needle_len == 0) 2604 return (gchar *)haystack; 2605 2606 if (haystack_len < needle_len) 2607 return NULL; 2608 2609 end = haystack + haystack_len - needle_len; 2610 2611 while (*p && p <= end) 2612 { 2613 for (i = 0; i < needle_len; i++) 2614 if (p[i] != needle[i]) 2615 goto next; 2616 2617 return (gchar *)p; 2618 2619 next: 2620 p++; 2621 } 2622 2623 return NULL; 2624 } 2625} 2626 2627#if 0 2628 2629/** 2630 * g_strrstr: 2631 * @haystack: a nul-terminated string. 2632 * @needle: the nul-terminated string to search for. 2633 * 2634 * Searches the string @haystack for the last occurrence 2635 * of the string @needle. 2636 * 2637 * Return value: a pointer to the found occurrence, or 2638 * %NULL if not found. 2639 **/ 2640gchar * 2641g_strrstr (const gchar *haystack, 2642 const gchar *needle) 2643{ 2644 gsize i; 2645 gsize needle_len; 2646 gsize haystack_len; 2647 const gchar *p; 2648 2649 g_return_val_if_fail (haystack != NULL, NULL); 2650 g_return_val_if_fail (needle != NULL, NULL); 2651 2652 needle_len = strlen (needle); 2653 haystack_len = strlen (haystack); 2654 2655 if (needle_len == 0) 2656 return (gchar *)haystack; 2657 2658 if (haystack_len < needle_len) 2659 return NULL; 2660 2661 p = haystack + haystack_len - needle_len; 2662 2663 while (p >= haystack) 2664 { 2665 for (i = 0; i < needle_len; i++) 2666 if (p[i] != needle[i]) 2667 goto next; 2668 2669 return (gchar *)p; 2670 2671 next: 2672 p--; 2673 } 2674 2675 return NULL; 2676} 2677 2678/** 2679 * g_strrstr_len: 2680 * @haystack: a nul-terminated string. 2681 * @haystack_len: the maximum length of @haystack. 2682 * @needle: the nul-terminated string to search for. 2683 * 2684 * Searches the string @haystack for the last occurrence 2685 * of the string @needle, limiting the length of the search 2686 * to @haystack_len. 2687 * 2688 * Return value: a pointer to the found occurrence, or 2689 * %NULL if not found. 2690 **/ 2691gchar * 2692g_strrstr_len (const gchar *haystack, 2693 gssize haystack_len, 2694 const gchar *needle) 2695{ 2696 g_return_val_if_fail (haystack != NULL, NULL); 2697 g_return_val_if_fail (needle != NULL, NULL); 2698 2699 if (haystack_len < 0) 2700 return g_strrstr (haystack, needle); 2701 else 2702 { 2703 gsize needle_len = strlen (needle); 2704 const gchar *haystack_max = haystack + haystack_len; 2705 const gchar *p = haystack; 2706 gsize i; 2707 2708 while (p < haystack_max && *p) 2709 p++; 2710 2711 if (p < haystack + needle_len) 2712 return NULL; 2713 2714 p -= needle_len; 2715 2716 while (p >= haystack) 2717 { 2718 for (i = 0; i < needle_len; i++) 2719 if (p[i] != needle[i]) 2720 goto next; 2721 2722 return (gchar *)p; 2723 2724 next: 2725 p--; 2726 } 2727 2728 return NULL; 2729 } 2730} 2731 2732 2733/** 2734 * g_str_has_suffix: 2735 * @str: a nul-terminated string. 2736 * @suffix: the nul-terminated suffix to look for. 2737 * 2738 * Looks whether the string @str ends with @suffix. 2739 * 2740 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise. 2741 * 2742 * Since: 2.2 2743 **/ 2744gboolean 2745g_str_has_suffix (const gchar *str, 2746 const gchar *suffix) 2747{ 2748 int str_len; 2749 int suffix_len; 2750 2751 g_return_val_if_fail (str != NULL, FALSE); 2752 g_return_val_if_fail (suffix != NULL, FALSE); 2753 2754 str_len = strlen (str); 2755 suffix_len = strlen (suffix); 2756 2757 if (str_len < suffix_len) 2758 return FALSE; 2759 2760 return strcmp (str + str_len - suffix_len, suffix) == 0; 2761} 2762 2763/** 2764 * g_str_has_prefix: 2765 * @str: a nul-terminated string. 2766 * @prefix: the nul-terminated prefix to look for. 2767 * 2768 * Looks whether the string @str begins with @prefix. 2769 * 2770 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise. 2771 * 2772 * Since: 2.2 2773 **/ 2774gboolean 2775g_str_has_prefix (const gchar *str, 2776 const gchar *prefix) 2777{ 2778 int str_len; 2779 int prefix_len; 2780 2781 g_return_val_if_fail (str != NULL, FALSE); 2782 g_return_val_if_fail (prefix != NULL, FALSE); 2783 2784 str_len = strlen (str); 2785 prefix_len = strlen (prefix); 2786 2787 if (str_len < prefix_len) 2788 return FALSE; 2789 2790 return strncmp (str, prefix, prefix_len) == 0; 2791} 2792 2793 2794/** 2795 * g_strip_context: 2796 * @msgid: a string 2797 * @msgval: another string 2798 * 2799 * An auxiliary function for gettext() support (see Q_()). 2800 * 2801 * Return value: @msgval, unless @msgval is identical to @msgid and contains 2802 * a '|' character, in which case a pointer to the substring of msgid after 2803 * the first '|' character is returned. 2804 * 2805 * Since: 2.4 2806 **/ 2807G_CONST_RETURN gchar * 2808g_strip_context (const gchar *msgid, 2809 const gchar *msgval) 2810{ 2811 if (msgval == msgid) 2812 { 2813 const char *c = strchr (msgid, '|'); 2814 if (c != NULL) 2815 return c + 1; 2816 } 2817 2818 return msgval; 2819} 2820 2821 2822/** 2823 * g_strv_length: 2824 * @str_array: a %NULL-terminated array of strings. 2825 * 2826 * Returns the length of the given %NULL-terminated 2827 * string array @str_array. 2828 * 2829 * Return value: length of @str_array. 2830 * 2831 * Since: 2.6 2832 **/ 2833guint 2834g_strv_length (gchar **str_array) 2835{ 2836 guint i = 0; 2837 2838 g_return_val_if_fail (str_array != NULL, 0); 2839 2840 while (str_array[i]) 2841 ++i; 2842 2843 return i; 2844} 2845 2846#define __G_STRFUNCS_C__ 2847#include "galiasdef.c" 2848#endif 2849