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