1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
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.
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.
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.
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/.
33 #define _GNU_SOURCE /* For stpcpy */
41 #include <ctype.h> /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
48 #include "gprintfint.h"
54 /* do not include <unistd.h> in this place since it
55 * interferes with g_strsignal() on some OSes
58 static const guint16 ascii_table_data[256] = {
59 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
60 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
64 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
66 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
68 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
69 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
70 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
71 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
72 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
73 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
74 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
75 /* the upper 128 are all zeroes */
78 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
81 const guint16 * const g_ascii_table = ascii_table_data;
84 g_strdup (const gchar *str)
91 length = strlen (str) + 1;
92 new_str = g_new (char, length);
93 memcpy (new_str, str, length);
102 g_memdup (gconstpointer mem,
109 new_mem = g_malloc (byte_size);
110 memcpy (new_mem, mem, byte_size);
119 g_strndup (const gchar *str,
126 new_str = g_new (gchar, n + 1);
127 strncpy (new_str, str, n);
137 g_strnfill (gsize length,
142 str = g_new (gchar, length + 1);
143 memset (str, (guchar)fill_char, length);
151 * @dest: destination buffer.
152 * @src: source string.
154 * Copies a nul-terminated string into the dest buffer, include the
155 * trailing nul, and return a pointer to the trailing nul byte.
156 * This is useful for concatenating multiple strings together
157 * without having to repeatedly scan for the end.
159 * Return value: a pointer to trailing nul byte.
162 g_stpcpy (gchar *dest,
166 g_return_val_if_fail (dest != NULL, NULL);
167 g_return_val_if_fail (src != NULL, NULL);
168 return stpcpy (dest, src);
170 register gchar *d = dest;
171 register const gchar *s = src;
173 g_return_val_if_fail (dest != NULL, NULL);
174 g_return_val_if_fail (src != NULL, NULL);
177 while (*s++ != '\0');
184 g_strdup_vprintf (const gchar *format,
187 gchar *string = NULL;
189 g_vasprintf (&string, format, args);
195 g_strdup_printf (const gchar *format,
201 va_start (args, format);
202 buffer = g_strdup_vprintf (format, args);
209 g_strconcat (const gchar *string1, ...)
220 l = 1 + strlen (string1);
221 va_start (args, string1);
222 s = va_arg (args, gchar*);
226 s = va_arg (args, gchar*);
230 concat = g_new (gchar, l);
233 ptr = g_stpcpy (ptr, string1);
234 va_start (args, string1);
235 s = va_arg (args, gchar*);
238 ptr = g_stpcpy (ptr, s);
239 s = va_arg (args, gchar*);
248 * @nptr: the string to convert to a numeric value.
249 * @endptr: if non-%NULL, it returns the character after
250 * the last character used in the conversion.
252 * Converts a string to a #gdouble value.
253 * It calls the standard strtod() function to handle the conversion, but
254 * if the string is not completely converted it attempts the conversion
255 * again with g_ascii_strtod(), and returns the best match.
257 * This function should seldomly be used. The normal situation when reading
258 * numbers not for human consumption is to use g_ascii_strtod(). Only when
259 * you know that you must expect both locale formatted and C formatted numbers
260 * should you use this. Make sure that you don't pass strings such as comma
261 * separated lists of values, since the commas may be interpreted as a decimal
262 * point in some locales, causing unexpected results.
264 * Return value: the #gdouble value.
267 g_strtod (const gchar *nptr,
275 g_return_val_if_fail (nptr != NULL, 0);
280 val_1 = strtod (nptr, &fail_pos_1);
282 if (fail_pos_1 && fail_pos_1[0] != 0)
283 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
285 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
288 *endptr = fail_pos_1;
294 *endptr = fail_pos_2;
301 * @nptr: the string to convert to a numeric value.
302 * @endptr: if non-%NULL, it returns the character after
303 * the last character used in the conversion.
305 * Converts a string to a #gdouble value.
306 * This function behaves like the standard strtod() function
307 * does in the C locale. It does this without actually
308 * changing the current locale, since that would not be
311 * This function is typically used when reading configuration
312 * files or other non-user input that should be locale independent.
313 * To handle input from the user you should normally use the
314 * locale-sensitive system strtod() function.
316 * To convert from a #gdouble to a string in a locale-insensitive
317 * way, use g_ascii_dtostr().
319 * If the correct value would cause overflow, plus or minus %HUGE_VAL
320 * is returned (according to the sign of the value), and %ERANGE is
321 * stored in %errno. If the correct value would cause underflow,
322 * zero is returned and %ERANGE is stored in %errno.
324 * This function resets %errno before calling strtod() so that
325 * you can reliably detect overflow and underflow.
327 * Return value: the #gdouble value.
330 g_ascii_strtod (const gchar *nptr,
335 struct lconv *locale_data;
336 const char *decimal_point;
337 int decimal_point_len;
338 const char *p, *decimal_point_pos;
339 const char *end = NULL; /* Silence gcc */
341 g_return_val_if_fail (nptr != NULL, 0);
345 locale_data = localeconv ();
346 decimal_point = locale_data->decimal_point;
347 decimal_point_len = strlen (decimal_point);
349 g_assert (decimal_point_len != 0);
351 decimal_point_pos = NULL;
352 if (decimal_point[0] != '.' ||
353 decimal_point[1] != 0)
356 /* Skip leading space */
357 while (g_ascii_isspace (*p))
360 /* Skip leading optional sign */
361 if (*p == '+' || *p == '-')
365 (p[1] == 'x' || p[1] == 'X'))
368 /* HEX - find the (optional) decimal point */
370 while (g_ascii_isxdigit (*p))
375 decimal_point_pos = p++;
377 while (g_ascii_isxdigit (*p))
380 if (*p == 'p' || *p == 'P')
382 if (*p == '+' || *p == '-')
384 while (g_ascii_isdigit (*p))
391 while (g_ascii_isdigit (*p))
396 decimal_point_pos = p++;
398 while (g_ascii_isdigit (*p))
401 if (*p == 'e' || *p == 'E')
403 if (*p == '+' || *p == '-')
405 while (g_ascii_isdigit (*p))
410 /* For the other cases, we need not convert the decimal point */
413 /* Set errno to zero, so that we can distinguish zero results
417 if (decimal_point_pos)
421 /* We need to convert the '.' to the locale specific decimal point */
422 copy = g_malloc (end - nptr + 1 + decimal_point_len);
425 memcpy (c, nptr, decimal_point_pos - nptr);
426 c += decimal_point_pos - nptr;
427 memcpy (c, decimal_point, decimal_point_len);
428 c += decimal_point_len;
429 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
430 c += end - (decimal_point_pos + 1);
433 val = strtod (copy, &fail_pos);
437 if (fail_pos - copy > decimal_point_pos - nptr)
438 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
440 fail_pos = (char *)nptr + (fail_pos - copy);
447 val = strtod (nptr, &fail_pos);
458 * @buffer: A buffer to place the resulting string in
459 * @buf_len: The length of the buffer.
460 * @d: The #gdouble to convert
462 * Converts a #gdouble to a string, using the '.' as
465 * This functions generates enough precision that converting
466 * the string back using g_ascii_strtod() gives the same machine-number
467 * (on machines with IEEE compatible 64bit doubles). It is
468 * guaranteed that the size of the resulting string will never
469 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
471 * Return value: The pointer to the buffer with the converted string.
474 g_ascii_dtostr (gchar *buffer,
478 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
483 * @buffer: A buffer to place the resulting string in
484 * @buf_len: The length of the buffer.
485 * @format: The printf()-style format to use for the
486 * code to use for converting.
487 * @d: The #gdouble to convert
489 * Converts a #gdouble to a string, using the '.' as
490 * decimal point. To format the number you pass in
491 * a printf()-style format string. Allowed conversion
492 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
494 * If you just want to want to serialize the value into a
495 * string, use g_ascii_dtostr().
497 * Return value: The pointer to the buffer with the converted string.
500 g_ascii_formatd (gchar *buffer,
505 struct lconv *locale_data;
506 const char *decimal_point;
507 int decimal_point_len;
512 g_return_val_if_fail (buffer != NULL, NULL);
513 g_return_val_if_fail (format[0] == '%', NULL);
514 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
516 format_char = format[strlen (format) - 1];
518 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
519 format_char == 'f' || format_char == 'F' ||
520 format_char == 'g' || format_char == 'G',
523 if (format[0] != '%')
526 if (strpbrk (format + 1, "'l%"))
529 if (!(format_char == 'e' || format_char == 'E' ||
530 format_char == 'f' || format_char == 'F' ||
531 format_char == 'g' || format_char == 'G'))
535 _g_snprintf (buffer, buf_len, format, d);
537 locale_data = localeconv ();
538 decimal_point = locale_data->decimal_point;
539 decimal_point_len = strlen (decimal_point);
541 g_assert (decimal_point_len != 0);
543 if (decimal_point[0] != '.' ||
544 decimal_point[1] != 0)
548 if (*p == '+' || *p == '-')
551 while (isdigit ((guchar)*p))
554 if (strncmp (p, decimal_point, decimal_point_len) == 0)
558 if (decimal_point_len > 1) {
559 rest_len = strlen (p + (decimal_point_len-1));
560 memmove (p, p + (decimal_point_len-1),
573 * @nptr: the string to convert to a numeric value.
574 * @endptr: if non-%NULL, it returns the character after
575 * the last character used in the conversion.
576 * @base: to be used for the conversion, 2..36 or 0
578 * Converts a string to a #guint64 value.
579 * This function behaves like the standard strtoull() function
580 * does in the C locale. It does this without actually
581 * changing the current locale, since that would not be
584 * This function is typically used when reading configuration
585 * files or other non-user input that should be locale independent.
586 * To handle input from the user you should normally use the
587 * locale-sensitive system strtoull() function.
589 * If the correct value would cause overflow, %G_MAXUINT64
590 * is returned, and %ERANGE is stored in %errno.
592 * Return value: the #guint64 value.
597 g_ascii_strtoull (const gchar *nptr,
601 /* this code is based on on the strtol(3) code from GNU libc released under
602 * the GNU Lesser General Public License.
604 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
605 * Free Software Foundation, Inc.
607 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
608 (c) == '\r' || (c) == '\t' || (c) == '\v')
609 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
610 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
611 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
612 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
613 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
614 gboolean negative, overflow;
618 const gchar *s, *save;
621 g_return_val_if_fail (nptr != NULL, 0);
623 if (base == 1 || base > 36)
631 /* Skip white space. */
637 /* Check for a sign. */
647 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
650 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
661 /* Save the pointer so we can check later if anything happened. */
663 cutoff = G_MAXUINT64 / base;
664 cutlim = G_MAXUINT64 % base;
671 if (c >= '0' && c <= '9')
673 else if (ISALPHA (c))
674 c = TOUPPER (c) - 'A' + 10;
679 /* Check for overflow. */
680 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
689 /* Check if anything actually happened. */
693 /* Store in ENDPTR the address of one character
694 past the last character we converted. */
696 *endptr = (gchar*) s;
704 /* Return the result of the appropriate sign. */
705 return negative ? -ui64 : ui64;
708 /* We must handle a special case here: the base is 0 or 16 and the
709 first two characters are '0' and 'x', but the rest are no
710 hexadecimal digits. This is no error case. We return 0 and
711 ENDPTR points to the `x`. */
714 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
716 *endptr = (gchar*) &save[-1];
718 /* There was no number to convert. */
719 *endptr = (gchar*) nptr;
725 G_CONST_RETURN gchar*
726 g_strerror (gint errnum)
728 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
730 int saved_errno = errno;
733 const char *msg_locale;
735 msg_locale = strerror (errnum);
736 if (g_get_charset (NULL))
743 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
746 /* Stick in the quark table so that we can return a static result
748 GQuark msg_quark = g_quark_from_string (msg_utf8);
751 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
760 case E2BIG: return "argument list too long";
763 case EACCES: return "permission denied";
766 case EADDRINUSE: return "address already in use";
769 case EADDRNOTAVAIL: return "can't assign requested address";
772 case EADV: return "advertise error";
775 case EAFNOSUPPORT: return "address family not supported by protocol family";
778 case EAGAIN: return "try again";
781 case EALIGN: return "EALIGN";
784 case EALREADY: return "operation already in progress";
787 case EBADE: return "bad exchange descriptor";
790 case EBADF: return "bad file number";
793 case EBADFD: return "file descriptor in bad state";
796 case EBADMSG: return "not a data message";
799 case EBADR: return "bad request descriptor";
802 case EBADRPC: return "RPC structure is bad";
805 case EBADRQC: return "bad request code";
808 case EBADSLT: return "invalid slot";
811 case EBFONT: return "bad font file format";
814 case EBUSY: return "mount device busy";
817 case ECHILD: return "no children";
820 case ECHRNG: return "channel number out of range";
823 case ECOMM: return "communication error on send";
826 case ECONNABORTED: return "software caused connection abort";
829 case ECONNREFUSED: return "connection refused";
832 case ECONNRESET: return "connection reset by peer";
834 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
835 case EDEADLK: return "resource deadlock avoided";
838 case EDEADLOCK: return "resource deadlock avoided";
841 case EDESTADDRREQ: return "destination address required";
844 case EDIRTY: return "mounting a dirty fs w/o force";
847 case EDOM: return "math argument out of range";
850 case EDOTDOT: return "cross mount point";
853 case EDQUOT: return "disk quota exceeded";
856 case EDUPPKG: return "duplicate package name";
859 case EEXIST: return "file already exists";
862 case EFAULT: return "bad address in system call argument";
865 case EFBIG: return "file too large";
868 case EHOSTDOWN: return "host is down";
871 case EHOSTUNREACH: return "host is unreachable";
874 case EIDRM: return "identifier removed";
877 case EINIT: return "initialization error";
880 case EINPROGRESS: return "operation now in progress";
883 case EINTR: return "interrupted system call";
886 case EINVAL: return "invalid argument";
889 case EIO: return "I/O error";
892 case EISCONN: return "socket is already connected";
895 case EISDIR: return "is a directory";
898 case EISNAM: return "is a name file";
901 case ELBIN: return "ELBIN";
904 case EL2HLT: return "level 2 halted";
907 case EL2NSYNC: return "level 2 not synchronized";
910 case EL3HLT: return "level 3 halted";
913 case EL3RST: return "level 3 reset";
916 case ELIBACC: return "can not access a needed shared library";
919 case ELIBBAD: return "accessing a corrupted shared library";
922 case ELIBEXEC: return "can not exec a shared library directly";
925 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
928 case ELIBSCN: return ".lib section in a.out corrupted";
931 case ELNRNG: return "link number out of range";
934 case ELOOP: return "too many levels of symbolic links";
937 case EMFILE: return "too many open files";
940 case EMLINK: return "too many links";
943 case EMSGSIZE: return "message too long";
946 case EMULTIHOP: return "multihop attempted";
949 case ENAMETOOLONG: return "file name too long";
952 case ENAVAIL: return "not available";
955 case ENET: return "ENET";
958 case ENETDOWN: return "network is down";
961 case ENETRESET: return "network dropped connection on reset";
964 case ENETUNREACH: return "network is unreachable";
967 case ENFILE: return "file table overflow";
970 case ENOANO: return "anode table overflow";
972 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
973 case ENOBUFS: return "no buffer space available";
976 case ENOCSI: return "no CSI structure available";
979 case ENODATA: return "no data available";
982 case ENODEV: return "no such device";
985 case ENOENT: return "no such file or directory";
988 case ENOEXEC: return "exec format error";
991 case ENOLCK: return "no locks available";
994 case ENOLINK: return "link has be severed";
997 case ENOMEM: return "not enough memory";
1000 case ENOMSG: return "no message of desired type";
1003 case ENONET: return "machine is not on the network";
1006 case ENOPKG: return "package not installed";
1009 case ENOPROTOOPT: return "bad proocol option";
1012 case ENOSPC: return "no space left on device";
1015 case ENOSR: return "out of stream resources";
1018 case ENOSTR: return "not a stream device";
1021 case ENOSYM: return "unresolved symbol name";
1024 case ENOSYS: return "function not implemented";
1027 case ENOTBLK: return "block device required";
1030 case ENOTCONN: return "socket is not connected";
1033 case ENOTDIR: return "not a directory";
1036 case ENOTEMPTY: return "directory not empty";
1039 case ENOTNAM: return "not a name file";
1042 case ENOTSOCK: return "socket operation on non-socket";
1045 case ENOTTY: return "inappropriate device for ioctl";
1048 case ENOTUNIQ: return "name not unique on network";
1051 case ENXIO: return "no such device or address";
1054 case EOPNOTSUPP: return "operation not supported on socket";
1057 case EPERM: return "not owner";
1060 case EPFNOSUPPORT: return "protocol family not supported";
1063 case EPIPE: return "broken pipe";
1066 case EPROCLIM: return "too many processes";
1069 case EPROCUNAVAIL: return "bad procedure for program";
1071 #ifdef EPROGMISMATCH
1072 case EPROGMISMATCH: return "program version wrong";
1075 case EPROGUNAVAIL: return "RPC program not available";
1078 case EPROTO: return "protocol error";
1080 #ifdef EPROTONOSUPPORT
1081 case EPROTONOSUPPORT: return "protocol not suppored";
1084 case EPROTOTYPE: return "protocol wrong type for socket";
1087 case ERANGE: return "math result unrepresentable";
1089 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1090 case EREFUSED: return "EREFUSED";
1093 case EREMCHG: return "remote address changed";
1096 case EREMDEV: return "remote device";
1099 case EREMOTE: return "pathname hit remote file system";
1102 case EREMOTEIO: return "remote i/o error";
1104 #ifdef EREMOTERELEASE
1105 case EREMOTERELEASE: return "EREMOTERELEASE";
1108 case EROFS: return "read-only file system";
1111 case ERPCMISMATCH: return "RPC version is wrong";
1114 case ERREMOTE: return "object is remote";
1117 case ESHUTDOWN: return "can't send afer socket shutdown";
1119 #ifdef ESOCKTNOSUPPORT
1120 case ESOCKTNOSUPPORT: return "socket type not supported";
1123 case ESPIPE: return "invalid seek";
1126 case ESRCH: return "no such process";
1129 case ESRMNT: return "srmount error";
1132 case ESTALE: return "stale remote file handle";
1135 case ESUCCESS: return "Error 0";
1138 case ETIME: return "timer expired";
1141 case ETIMEDOUT: return "connection timed out";
1144 case ETOOMANYREFS: return "too many references: can't splice";
1147 case ETXTBSY: return "text file or pseudo-device busy";
1150 case EUCLEAN: return "structure needs cleaning";
1153 case EUNATCH: return "protocol driver not attached";
1156 case EUSERS: return "too many users";
1159 case EVERSION: return "version mismatch";
1161 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1162 case EWOULDBLOCK: return "operation would block";
1165 case EXDEV: return "cross-domain link";
1168 case EXFULL: return "message tables full";
1171 #else /* NO_SYS_ERRLIST */
1172 extern int sys_nerr;
1173 extern char *sys_errlist[];
1175 if ((errnum > 0) && (errnum <= sys_nerr))
1176 return sys_errlist [errnum];
1177 #endif /* NO_SYS_ERRLIST */
1179 msg = g_static_private_get (&msg_private);
1182 msg = g_new (gchar, 64);
1183 g_static_private_set (&msg_private, msg, g_free);
1186 _g_sprintf (msg, "unknown error (%d)", errnum);
1188 errno = saved_errno;
1192 G_CONST_RETURN gchar*
1193 g_strsignal (gint signum)
1195 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1198 #ifdef HAVE_STRSIGNAL
1199 const char *msg_locale;
1201 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1202 extern const char *strsignal(int);
1204 /* this is declared differently (const) in string.h on BeOS */
1205 extern char *strsignal (int sig);
1206 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1207 msg_locale = strsignal (signum);
1208 if (g_get_charset (NULL))
1212 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1215 /* Stick in the quark table so that we can return a static result
1217 GQuark msg_quark = g_quark_from_string (msg_utf8);
1220 return g_quark_to_string (msg_quark);
1223 #elif NO_SYS_SIGLIST
1227 case SIGHUP: return "Hangup";
1230 case SIGINT: return "Interrupt";
1233 case SIGQUIT: return "Quit";
1236 case SIGILL: return "Illegal instruction";
1239 case SIGTRAP: return "Trace/breakpoint trap";
1242 case SIGABRT: return "IOT trap/Abort";
1245 case SIGBUS: return "Bus error";
1248 case SIGFPE: return "Floating point exception";
1251 case SIGKILL: return "Killed";
1254 case SIGUSR1: return "User defined signal 1";
1257 case SIGSEGV: return "Segmentation fault";
1260 case SIGUSR2: return "User defined signal 2";
1263 case SIGPIPE: return "Broken pipe";
1266 case SIGALRM: return "Alarm clock";
1269 case SIGTERM: return "Terminated";
1272 case SIGSTKFLT: return "Stack fault";
1275 case SIGCHLD: return "Child exited";
1278 case SIGCONT: return "Continued";
1281 case SIGSTOP: return "Stopped (signal)";
1284 case SIGTSTP: return "Stopped";
1287 case SIGTTIN: return "Stopped (tty input)";
1290 case SIGTTOU: return "Stopped (tty output)";
1293 case SIGURG: return "Urgent condition";
1296 case SIGXCPU: return "CPU time limit exceeded";
1299 case SIGXFSZ: return "File size limit exceeded";
1302 case SIGVTALRM: return "Virtual time alarm";
1305 case SIGPROF: return "Profile signal";
1308 case SIGWINCH: return "Window size changed";
1311 case SIGIO: return "Possible I/O";
1314 case SIGPWR: return "Power failure";
1317 case SIGUNUSED: return "Unused signal";
1320 #else /* NO_SYS_SIGLIST */
1322 #ifdef NO_SYS_SIGLIST_DECL
1323 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1326 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1327 #endif /* NO_SYS_SIGLIST */
1329 msg = g_static_private_get (&msg_private);
1332 msg = g_new (gchar, 64);
1333 g_static_private_set (&msg_private, msg, g_free);
1336 _g_sprintf (msg, "unknown signal (%d)", signum);
1341 /* Functions g_strlcpy and g_strlcat were originally developed by
1342 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1343 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1344 * for more information.
1348 /* Use the native ones, if available; they might be implemented in assembly */
1350 g_strlcpy (gchar *dest,
1354 g_return_val_if_fail (dest != NULL, 0);
1355 g_return_val_if_fail (src != NULL, 0);
1357 return strlcpy (dest, src, dest_size);
1361 g_strlcat (gchar *dest,
1365 g_return_val_if_fail (dest != NULL, 0);
1366 g_return_val_if_fail (src != NULL, 0);
1368 return strlcat (dest, src, dest_size);
1371 #else /* ! HAVE_STRLCPY */
1374 * Copy string src to buffer dest (of buffer size dest_size). At most
1375 * dest_size-1 characters will be copied. Always NUL terminates
1376 * (unless dest_size == 0). This function does NOT allocate memory.
1377 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1378 * Returns size of attempted result, strlen(src),
1379 * so if retval >= dest_size, truncation occurred.
1382 g_strlcpy (gchar *dest,
1386 register gchar *d = dest;
1387 register const gchar *s = src;
1388 register gsize n = dest_size;
1390 g_return_val_if_fail (dest != NULL, 0);
1391 g_return_val_if_fail (src != NULL, 0);
1393 /* Copy as many bytes as will fit */
1394 if (n != 0 && --n != 0)
1397 register gchar c = *s++;
1405 /* If not enough room in dest, add NUL and traverse rest of src */
1414 return s - src - 1; /* count does not include NUL */
1419 * Appends string src to buffer dest (of buffer size dest_size).
1420 * At most dest_size-1 characters will be copied.
1421 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1422 * This function does NOT allocate memory.
1423 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1424 * in the dest_size characters of dest to start with).
1425 * Returns size of attempted result, which is
1426 * MIN (dest_size, strlen (original dest)) + strlen (src),
1427 * so if retval >= dest_size, truncation occurred.
1430 g_strlcat (gchar *dest,
1434 register gchar *d = dest;
1435 register const gchar *s = src;
1436 register gsize bytes_left = dest_size;
1437 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1439 g_return_val_if_fail (dest != NULL, 0);
1440 g_return_val_if_fail (src != NULL, 0);
1442 /* Find the end of dst and adjust bytes left but don't go past end */
1443 while (*d != 0 && bytes_left-- != 0)
1446 bytes_left = dest_size - dlength;
1448 if (bytes_left == 0)
1449 return dlength + strlen (s);
1453 if (bytes_left != 1)
1462 return dlength + (s - src); /* count does not include NUL */
1464 #endif /* ! HAVE_STRLCPY */
1469 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1471 * Converts all upper case ASCII letters to lower case ASCII letters.
1473 * Return value: a newly-allocated string, with all the upper case
1474 * characters in @str converted to lower case, with
1475 * semantics that exactly match g_ascii_tolower(). (Note
1476 * that this is unlike the old g_strdown(), which modified
1477 * the string in place.)
1480 g_ascii_strdown (const gchar *str,
1485 g_return_val_if_fail (str != NULL, NULL);
1490 result = g_strndup (str, len);
1491 for (s = result; *s; s++)
1492 *s = g_ascii_tolower (*s);
1500 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1502 * Converts all lower case ASCII letters to upper case ASCII letters.
1504 * Return value: a newly allocated string, with all the lower case
1505 * characters in @str converted to upper case, with
1506 * semantics that exactly match g_ascii_toupper(). (Note
1507 * that this is unlike the old g_strup(), which modified
1508 * the string in place.)
1511 g_ascii_strup (const gchar *str,
1516 g_return_val_if_fail (str != NULL, NULL);
1521 result = g_strndup (str, len);
1522 for (s = result; *s; s++)
1523 *s = g_ascii_toupper (*s);
1530 * @string: the string to convert.
1532 * Converts a string to lower case.
1534 * Return value: the string
1536 * Deprecated: This function is totally broken for the reasons discussed in
1537 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1541 g_strdown (gchar *string)
1545 g_return_val_if_fail (string != NULL, NULL);
1547 s = (guchar *) string;
1556 return (gchar *) string;
1561 * @string: the string to convert.
1563 * Converts a string to upper case.
1565 * Return value: the string
1567 * Deprecated: This function is totally broken for the reasons discussed in
1568 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1571 g_strup (gchar *string)
1575 g_return_val_if_fail (string != NULL, NULL);
1577 s = (guchar *) string;
1586 return (gchar *) string;
1590 g_strreverse (gchar *string)
1592 g_return_val_if_fail (string != NULL, NULL);
1596 register gchar *h, *t;
1599 t = string + strlen (string) - 1;
1618 * @c: any character.
1620 * Convert a character to ASCII lower case.
1622 * Unlike the standard C library tolower() function, this only
1623 * recognizes standard ASCII letters and ignores the locale, returning
1624 * all non-ASCII characters unchanged, even if they are lower case
1625 * letters in a particular character set. Also unlike the standard
1626 * library function, this takes and returns a char, not an int, so
1627 * don't call it on %EOF but no need to worry about casting to #guchar
1628 * before passing a possibly non-ASCII character in.
1630 * Return value: the result of converting @c to lower case.
1631 * If @c is not an ASCII upper case letter,
1632 * @c is returned unchanged.
1635 g_ascii_tolower (gchar c)
1637 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1642 * @c: any character.
1644 * Convert a character to ASCII upper case.
1646 * Unlike the standard C library toupper() function, this only
1647 * recognizes standard ASCII letters and ignores the locale, returning
1648 * all non-ASCII characters unchanged, even if they are upper case
1649 * letters in a particular character set. Also unlike the standard
1650 * library function, this takes and returns a char, not an int, so
1651 * don't call it on %EOF but no need to worry about casting to #guchar
1652 * before passing a possibly non-ASCII character in.
1654 * Return value: the result of converting @c to upper case.
1655 * If @c is not an ASCII lower case letter,
1656 * @c is returned unchanged.
1659 g_ascii_toupper (gchar c)
1661 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1665 * g_ascii_digit_value:
1666 * @c: an ASCII character.
1668 * Determines the numeric value of a character as a decimal
1669 * digit. Differs from g_unichar_digit_value() because it takes
1670 * a char, so there's no worry about sign extension if characters
1673 * Return value: If @c is a decimal digit (according to
1674 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1677 g_ascii_digit_value (gchar c)
1679 if (g_ascii_isdigit (c))
1685 * g_ascii_xdigit_value:
1686 * @c: an ASCII character.
1688 * Determines the numeric value of a character as a hexidecimal
1689 * digit. Differs from g_unichar_xdigit_value() because it takes
1690 * a char, so there's no worry about sign extension if characters
1693 * Return value: If @c is a hex digit (according to
1694 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1697 g_ascii_xdigit_value (gchar c)
1699 if (c >= 'A' && c <= 'F')
1700 return c - 'A' + 10;
1701 if (c >= 'a' && c <= 'f')
1702 return c - 'a' + 10;
1703 return g_ascii_digit_value (c);
1707 * g_ascii_strcasecmp:
1708 * @s1: string to compare with @s2.
1709 * @s2: string to compare with @s1.
1711 * Compare two strings, ignoring the case of ASCII characters.
1713 * Unlike the BSD strcasecmp() function, this only recognizes standard
1714 * ASCII letters and ignores the locale, treating all non-ASCII
1715 * characters as if they are not letters.
1717 * Return value: an integer less than, equal to, or greater than
1718 * zero if @s1 is found, respectively, to be less than,
1719 * to match, or to be greater than @s2.
1722 g_ascii_strcasecmp (const gchar *s1,
1727 g_return_val_if_fail (s1 != NULL, 0);
1728 g_return_val_if_fail (s2 != NULL, 0);
1732 c1 = (gint)(guchar) TOLOWER (*s1);
1733 c2 = (gint)(guchar) TOLOWER (*s2);
1739 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1743 * g_ascii_strncasecmp:
1744 * @s1: string to compare with @s2.
1745 * @s2: string to compare with @s1.
1746 * @n: number of characters to compare.
1748 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1749 * characters after the first @n in each string.
1751 * Unlike the BSD strcasecmp() function, this only recognizes standard
1752 * ASCII letters and ignores the locale, treating all non-ASCII
1753 * characters as if they are not letters.
1755 * Return value: an integer less than, equal to, or greater than zero
1756 * if the first @n bytes of @s1 is found, respectively,
1757 * to be less than, to match, or to be greater than the
1758 * first @n bytes of @s2.
1761 g_ascii_strncasecmp (const gchar *s1,
1767 g_return_val_if_fail (s1 != NULL, 0);
1768 g_return_val_if_fail (s2 != NULL, 0);
1770 while (n && *s1 && *s2)
1773 c1 = (gint)(guchar) TOLOWER (*s1);
1774 c2 = (gint)(guchar) TOLOWER (*s2);
1781 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1789 * @s2: a string to compare with @s1.
1791 * A case-insensitive string comparison, corresponding to the standard
1792 * strcasecmp() function on platforms which support it.
1794 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1795 * or a positive value if @s1 > @s2.
1797 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1798 * deprecated and how to replace it.
1801 g_strcasecmp (const gchar *s1,
1804 #ifdef HAVE_STRCASECMP
1805 g_return_val_if_fail (s1 != NULL, 0);
1806 g_return_val_if_fail (s2 != NULL, 0);
1808 return strcasecmp (s1, s2);
1812 g_return_val_if_fail (s1 != NULL, 0);
1813 g_return_val_if_fail (s2 != NULL, 0);
1817 /* According to A. Cox, some platforms have islower's that
1818 * don't work right on non-uppercase
1820 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1821 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1827 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1834 * @s2: a string to compare with @s1.
1835 * @n: the maximum number of characters to compare.
1837 * A case-insensitive string comparison, corresponding to the standard
1838 * strncasecmp() function on platforms which support it.
1839 * It is similar to g_strcasecmp() except it only compares the first @n
1840 * characters of the strings.
1842 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1843 * or a positive value if @s1 > @s2.
1845 * Deprecated: The problem with g_strncasecmp() is that it does the
1846 * comparison by calling toupper()/tolower(). These functions are
1847 * locale-specific and operate on single bytes. However, it is impossible
1848 * to handle things correctly from an I18N standpoint by operating on
1849 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1850 * broken if your string is guaranteed to be ASCII, since it's
1851 * locale-sensitive, and it's broken if your string is localized, since
1852 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1855 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1856 * which only works on ASCII and is not locale-sensitive, and
1857 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1860 g_strncasecmp (const gchar *s1,
1864 #ifdef HAVE_STRNCASECMP
1865 return strncasecmp (s1, s2, n);
1869 g_return_val_if_fail (s1 != NULL, 0);
1870 g_return_val_if_fail (s2 != NULL, 0);
1872 while (n && *s1 && *s2)
1875 /* According to A. Cox, some platforms have islower's that
1876 * don't work right on non-uppercase
1878 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1879 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1886 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1893 g_strdelimit (gchar *string,
1894 const gchar *delimiters,
1899 g_return_val_if_fail (string != NULL, NULL);
1902 delimiters = G_STR_DELIMITERS;
1904 for (c = string; *c; c++)
1906 if (strchr (delimiters, *c))
1914 g_strcanon (gchar *string,
1915 const gchar *valid_chars,
1920 g_return_val_if_fail (string != NULL, NULL);
1921 g_return_val_if_fail (valid_chars != NULL, NULL);
1923 for (c = string; *c; c++)
1925 if (!strchr (valid_chars, *c))
1933 g_strcompress (const gchar *source)
1935 const gchar *p = source, *octal;
1936 gchar *dest = g_malloc (strlen (source) + 1);
1946 case '0': case '1': case '2': case '3': case '4':
1947 case '5': case '6': case '7':
1950 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1952 *q = (*q * 8) + (*p - '0');
1973 default: /* Also handles \" and \\ */
1988 g_strescape (const gchar *source,
1989 const gchar *exceptions)
1996 g_return_val_if_fail (source != NULL, NULL);
1998 p = (guchar *) source;
1999 /* Each source byte needs maximally four destination chars (\777) */
2000 q = dest = g_malloc (strlen (source) * 4 + 1);
2002 memset (excmap, 0, 256);
2005 guchar *e = (guchar *) exceptions;
2051 if ((*p < ' ') || (*p >= 0177))
2054 *q++ = '0' + (((*p) >> 6) & 07);
2055 *q++ = '0' + (((*p) >> 3) & 07);
2056 *q++ = '0' + ((*p) & 07);
2070 g_strchug (gchar *string)
2074 g_return_val_if_fail (string != NULL, NULL);
2076 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2079 g_memmove (string, start, strlen ((gchar *) start) + 1);
2085 g_strchomp (gchar *string)
2089 g_return_val_if_fail (string != NULL, NULL);
2091 len = strlen (string);
2094 if (g_ascii_isspace ((guchar) string[len]))
2105 * @string: a string to split.
2106 * @delimiter: a string which specifies the places at which to split the string.
2107 * The delimiter is not included in any of the resulting strings, unless
2108 * @max_tokens is reached.
2109 * @max_tokens: the maximum number of pieces to split @string into. If this is
2110 * less than 1, the string is split completely.
2112 * Splits a string into a maximum of @max_tokens pieces, using the given
2113 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2114 * to the last token.
2116 * As a special case, the result of splitting the empty string "" is an empty
2117 * vector, not a vector containing a single string. The reason for this
2118 * special case is that being able to represent a empty vector is typically
2119 * more useful than consistent handling of empty elements. If you do need
2120 * to represent empty elements, you'll need to check for the empty string
2121 * before calling g_strsplit().
2123 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2124 * g_strfreev() to free it.
2127 g_strsplit (const gchar *string,
2128 const gchar *delimiter,
2131 GSList *string_list = NULL, *slist;
2132 gchar **str_array, *s;
2134 const gchar *remainder;
2136 g_return_val_if_fail (string != NULL, NULL);
2137 g_return_val_if_fail (delimiter != NULL, NULL);
2138 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2141 max_tokens = G_MAXINT;
2144 s = strstr (remainder, delimiter);
2147 gsize delimiter_len = strlen (delimiter);
2149 while (--max_tokens && s)
2154 len = s - remainder;
2155 new_string = g_new (gchar, len + 1);
2156 strncpy (new_string, remainder, len);
2157 new_string[len] = 0;
2158 string_list = g_slist_prepend (string_list, new_string);
2160 remainder = s + delimiter_len;
2161 s = strstr (remainder, delimiter);
2167 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2170 str_array = g_new (gchar*, n + 1);
2172 str_array[n--] = NULL;
2173 for (slist = string_list; slist; slist = slist->next)
2174 str_array[n--] = slist->data;
2176 g_slist_free (string_list);
2183 * @string: The string to be tokenized
2184 * @delimiters: A nul-terminated string containing bytes that are used
2185 * to split the string.
2186 * @max_tokens: The maximum number of tokens to split @string into.
2187 * If this is less than 1, the string is split completely
2189 * Splits @string into a number of tokens not containing any of the characters
2190 * in @delimiter. A token is the (possibly empty) longest string that does not
2191 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2192 * remainder is appended to the last token.
2194 * For example the result of g_strtokenize ("abc:def/ghi", ":/", -1) is a
2195 * %NULL-terminated vector containing the three strings "abc", "def",
2198 * The result if g_strtokenize (":def/ghi:", ":/", -1) is a %NULL-terminated
2199 * vector containing the four strings "", "def", "ghi", and "".
2201 * As a special case, the result of splitting the empty string "" is an empty
2202 * vector, not a vector containing a single string. The reason for this
2203 * special case is that being able to represent a empty vector is typically
2204 * more useful than consistent handling of empty elements. If you do need
2205 * to represent empty elements, you'll need to check for the empty string
2206 * before calling g_strsplit().
2208 * Note that this function works on bytes not characters, so it can't be used
2209 * to delimit UTF-8 strings for anything but ASCII characters.
2211 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2212 * g_strfreev() to free it.
2217 g_strsplit_set (const gchar *string,
2218 const gchar *delimiters,
2221 gboolean delim_table[256];
2222 GSList *tokens, *list;
2225 const gchar *current;
2229 g_return_val_if_fail (string != NULL, NULL);
2230 g_return_val_if_fail (delimiters != NULL, NULL);
2233 max_tokens = G_MAXINT;
2235 if (*string == '\0')
2237 result = g_new (char *, 1);
2242 memset (delim_table, FALSE, sizeof (delim_table));
2243 for (s = delimiters; *s != '\0'; ++s)
2244 delim_table[*(guchar *)s] = TRUE;
2249 s = current = string;
2252 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2256 token = g_strndup (current, s - current);
2257 tokens = g_slist_prepend (tokens, token);
2266 token = g_strndup (current, s - current);
2267 tokens = g_slist_prepend (tokens, token);
2270 result = g_new (gchar *, n_tokens + 1);
2272 result[n_tokens] = NULL;
2273 for (list = tokens; list != NULL; list = list->next)
2274 result[--n_tokens] = list->data;
2276 g_slist_free (tokens);
2283 * @str_array: a %NULL-terminated array of strings to free.
2285 * Frees a %NULL-terminated array of strings, and the array itself.
2286 * If called on a %NULL value, g_strfreev() simply returns.
2289 g_strfreev (gchar **str_array)
2295 for(i = 0; str_array[i] != NULL; i++)
2296 g_free(str_array[i]);
2304 * @str_array: %NULL-terminated array of strings.
2306 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2307 * the new array should be freed by first freeing each string, then
2308 * the array itself. g_strfreev() does this for you. If called
2309 * on a %NULL value, g_strdupv() simply returns %NULL.
2311 * Return value: a new %NULL-terminated array of strings.
2314 g_strdupv (gchar **str_array)
2322 while (str_array[i])
2325 retval = g_new (gchar*, i + 1);
2328 while (str_array[i])
2330 retval[i] = g_strdup (str_array[i]);
2342 g_strjoinv (const gchar *separator,
2348 g_return_val_if_fail (str_array != NULL, NULL);
2350 if (separator == NULL)
2357 gsize separator_len;
2359 separator_len = strlen (separator);
2360 /* First part, getting length */
2361 len = 1 + strlen (str_array[0]);
2362 for (i = 1; str_array[i] != NULL; i++)
2363 len += strlen (str_array[i]);
2364 len += separator_len * (i - 1);
2366 /* Second part, building string */
2367 string = g_new (gchar, len);
2368 ptr = g_stpcpy (string, *str_array);
2369 for (i = 1; str_array[i] != NULL; i++)
2371 ptr = g_stpcpy (ptr, separator);
2372 ptr = g_stpcpy (ptr, str_array[i]);
2376 string = g_strdup ("");
2382 g_strjoin (const gchar *separator,
2388 gsize separator_len;
2391 if (separator == NULL)
2394 separator_len = strlen (separator);
2396 va_start (args, separator);
2398 s = va_arg (args, gchar*);
2402 /* First part, getting length */
2403 len = 1 + strlen (s);
2405 s = va_arg (args, gchar*);
2408 len += separator_len + strlen (s);
2409 s = va_arg (args, gchar*);
2413 /* Second part, building string */
2414 string = g_new (gchar, len);
2416 va_start (args, separator);
2418 s = va_arg (args, gchar*);
2419 ptr = g_stpcpy (string, s);
2421 s = va_arg (args, gchar*);
2424 ptr = g_stpcpy (ptr, separator);
2425 ptr = g_stpcpy (ptr, s);
2426 s = va_arg (args, gchar*);
2430 string = g_strdup ("");
2440 * @haystack: a string.
2441 * @haystack_len: the maximum length of @haystack.
2442 * @needle: the string to search for.
2444 * Searches the string @haystack for the first occurrence
2445 * of the string @needle, limiting the length of the search
2448 * Return value: a pointer to the found occurrence, or
2449 * %NULL if not found.
2452 g_strstr_len (const gchar *haystack,
2453 gssize haystack_len,
2454 const gchar *needle)
2456 g_return_val_if_fail (haystack != NULL, NULL);
2457 g_return_val_if_fail (needle != NULL, NULL);
2459 if (haystack_len < 0)
2460 return strstr (haystack, needle);
2463 const gchar *p = haystack;
2464 gsize needle_len = strlen (needle);
2468 if (needle_len == 0)
2469 return (gchar *)haystack;
2471 if (haystack_len < needle_len)
2474 end = haystack + haystack_len - needle_len;
2476 while (*p && p <= end)
2478 for (i = 0; i < needle_len; i++)
2479 if (p[i] != needle[i])
2494 * @haystack: a nul-terminated string.
2495 * @needle: the nul-terminated string to search for.
2497 * Searches the string @haystack for the last occurrence
2498 * of the string @needle.
2500 * Return value: a pointer to the found occurrence, or
2501 * %NULL if not found.
2504 g_strrstr (const gchar *haystack,
2505 const gchar *needle)
2512 g_return_val_if_fail (haystack != NULL, NULL);
2513 g_return_val_if_fail (needle != NULL, NULL);
2515 needle_len = strlen (needle);
2516 haystack_len = strlen (haystack);
2518 if (needle_len == 0)
2519 return (gchar *)haystack;
2521 if (haystack_len < needle_len)
2524 p = haystack + haystack_len - needle_len;
2526 while (p >= haystack)
2528 for (i = 0; i < needle_len; i++)
2529 if (p[i] != needle[i])
2543 * @haystack: a nul-terminated string.
2544 * @haystack_len: the maximum length of @haystack.
2545 * @needle: the nul-terminated string to search for.
2547 * Searches the string @haystack for the last occurrence
2548 * of the string @needle, limiting the length of the search
2551 * Return value: a pointer to the found occurrence, or
2552 * %NULL if not found.
2555 g_strrstr_len (const gchar *haystack,
2556 gssize haystack_len,
2557 const gchar *needle)
2559 g_return_val_if_fail (haystack != NULL, NULL);
2560 g_return_val_if_fail (needle != NULL, NULL);
2562 if (haystack_len < 0)
2563 return g_strrstr (haystack, needle);
2566 gsize needle_len = strlen (needle);
2567 const gchar *haystack_max = haystack + haystack_len;
2568 const gchar *p = haystack;
2571 while (p < haystack_max && *p)
2574 if (p < haystack + needle_len)
2579 while (p >= haystack)
2581 for (i = 0; i < needle_len; i++)
2582 if (p[i] != needle[i])
2598 * @str: a nul-terminated string.
2599 * @suffix: the nul-terminated suffix to look for.
2601 * Looks whether the string @str ends with @suffix.
2603 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2608 g_str_has_suffix (const gchar *str,
2609 const gchar *suffix)
2614 g_return_val_if_fail (str != NULL, FALSE);
2615 g_return_val_if_fail (suffix != NULL, FALSE);
2617 str_len = strlen (str);
2618 suffix_len = strlen (suffix);
2620 if (str_len < suffix_len)
2623 return strcmp (str + str_len - suffix_len, suffix) == 0;
2628 * @str: a nul-terminated string.
2629 * @prefix: the nul-terminated prefix to look for.
2631 * Looks whether the string @str begins with @prefix.
2633 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2638 g_str_has_prefix (const gchar *str,
2639 const gchar *prefix)
2644 g_return_val_if_fail (str != NULL, FALSE);
2645 g_return_val_if_fail (prefix != NULL, FALSE);
2647 str_len = strlen (str);
2648 prefix_len = strlen (prefix);
2650 if (str_len < prefix_len)
2653 return strncmp (str, prefix, prefix_len) == 0;
2660 * @msgval: another string
2662 * An auxiliary function for gettext() support (see Q_()).
2664 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2665 * a '|' character, in which case a pointer to the substring of msgid after
2666 * the first '|' character is returned.
2670 G_CONST_RETURN gchar *
2671 g_strip_context (const gchar *msgid,
2672 const gchar *msgval)
2674 if (msgval == msgid)
2676 const char *c = strchr (msgid, '|');