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)
49 #include "gprintfint.h"
55 /* do not include <unistd.h> in this place since it
56 * interferes with g_strsignal() on some OSes
59 static const guint16 ascii_table_data[256] = {
60 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
62 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
64 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
67 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
68 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
69 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
70 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
71 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
72 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
73 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
74 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
75 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
76 /* the upper 128 are all zeroes */
79 const guint16 * const g_ascii_table = ascii_table_data;
82 g_strdup (const gchar *str)
89 length = strlen (str) + 1;
90 new_str = g_new (char, length);
91 memcpy (new_str, str, length);
100 g_memdup (gconstpointer mem,
107 new_mem = g_malloc (byte_size);
108 memcpy (new_mem, mem, byte_size);
117 g_strndup (const gchar *str,
124 new_str = g_new (gchar, n + 1);
125 strncpy (new_str, str, n);
135 g_strnfill (gsize length,
140 str = g_new (gchar, length + 1);
141 memset (str, (guchar)fill_char, length);
149 * @dest: destination buffer.
150 * @src: source string.
152 * Copies a nul-terminated string into the dest buffer, include the
153 * trailing nul, and return a pointer to the trailing nul byte.
154 * This is useful for concatenating multiple strings together
155 * without having to repeatedly scan for the end.
157 * Return value: a pointer to trailing nul byte.
160 g_stpcpy (gchar *dest,
164 g_return_val_if_fail (dest != NULL, NULL);
165 g_return_val_if_fail (src != NULL, NULL);
166 return stpcpy (dest, src);
168 register gchar *d = dest;
169 register const gchar *s = src;
171 g_return_val_if_fail (dest != NULL, NULL);
172 g_return_val_if_fail (src != NULL, NULL);
175 while (*s++ != '\0');
182 g_strdup_vprintf (const gchar *format,
185 gchar *string = NULL;
187 g_vasprintf (&string, format, args);
193 g_strdup_printf (const gchar *format,
199 va_start (args, format);
200 buffer = g_strdup_vprintf (format, args);
207 g_strconcat (const gchar *string1, ...)
218 l = 1 + strlen (string1);
219 va_start (args, string1);
220 s = va_arg (args, gchar*);
224 s = va_arg (args, gchar*);
228 concat = g_new (gchar, l);
231 ptr = g_stpcpy (ptr, string1);
232 va_start (args, string1);
233 s = va_arg (args, gchar*);
236 ptr = g_stpcpy (ptr, s);
237 s = va_arg (args, gchar*);
246 * @nptr: the string to convert to a numeric value.
247 * @endptr: if non-%NULL, it returns the character after
248 * the last character used in the conversion.
250 * Converts a string to a #gdouble value.
251 * It calls the standard strtod() function to handle the conversion, but
252 * if the string is not completely converted it attempts the conversion
253 * again with g_ascii_strtod(), and returns the best match.
255 * This function should seldomly be used. The normal situation when reading
256 * numbers not for human consumption is to use g_ascii_strtod(). Only when
257 * you know that you must expect both locale formatted and C formatted numbers
258 * should you use this. Make sure that you don't pass strings such as comma
259 * separated lists of values, since the commas may be interpreted as a decimal
260 * point in some locales, causing unexpected results.
262 * Return value: the #gdouble value.
265 g_strtod (const gchar *nptr,
273 g_return_val_if_fail (nptr != NULL, 0);
278 val_1 = strtod (nptr, &fail_pos_1);
280 if (fail_pos_1 && fail_pos_1[0] != 0)
281 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
283 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
286 *endptr = fail_pos_1;
292 *endptr = fail_pos_2;
299 * @nptr: the string to convert to a numeric value.
300 * @endptr: if non-%NULL, it returns the character after
301 * the last character used in the conversion.
303 * Converts a string to a #gdouble value.
304 * This function behaves like the standard strtod() function
305 * does in the C locale. It does this without actually
306 * changing the current locale, since that would not be
309 * This function is typically used when reading configuration
310 * files or other non-user input that should be locale independent.
311 * To handle input from the user you should normally use the
312 * locale-sensitive system strtod() function.
314 * To convert from a #gdouble to a string in a locale-insensitive
315 * way, use g_ascii_dtostr().
317 * If the correct value would cause overflow, plus or minus %HUGE_VAL
318 * is returned (according to the sign of the value), and %ERANGE is
319 * stored in %errno. If the correct value would cause underflow,
320 * zero is returned and %ERANGE is stored in %errno.
322 * This function resets %errno before calling strtod() so that
323 * you can reliably detect overflow and underflow.
325 * Return value: the #gdouble value.
328 g_ascii_strtod (const gchar *nptr,
333 struct lconv *locale_data;
334 const char *decimal_point;
335 int decimal_point_len;
336 const char *p, *decimal_point_pos;
337 const char *end = NULL; /* Silence gcc */
339 g_return_val_if_fail (nptr != NULL, 0);
343 locale_data = localeconv ();
344 decimal_point = locale_data->decimal_point;
345 decimal_point_len = strlen (decimal_point);
347 g_assert (decimal_point_len != 0);
349 decimal_point_pos = NULL;
350 if (decimal_point[0] != '.' ||
351 decimal_point[1] != 0)
354 /* Skip leading space */
355 while (g_ascii_isspace (*p))
358 /* Skip leading optional sign */
359 if (*p == '+' || *p == '-')
363 (p[1] == 'x' || p[1] == 'X'))
366 /* HEX - find the (optional) decimal point */
368 while (g_ascii_isxdigit (*p))
373 decimal_point_pos = p++;
375 while (g_ascii_isxdigit (*p))
378 if (*p == 'p' || *p == 'P')
380 if (*p == '+' || *p == '-')
382 while (g_ascii_isdigit (*p))
388 while (g_ascii_isdigit (*p))
393 decimal_point_pos = p++;
395 while (g_ascii_isdigit (*p))
398 if (*p == 'e' || *p == 'E')
400 if (*p == '+' || *p == '-')
402 while (g_ascii_isdigit (*p))
406 /* For the other cases, we need not convert the decimal point */
410 /* Set errno to zero, so that we can distinguish zero results
414 if (decimal_point_pos)
418 /* We need to convert the '.' to the locale specific decimal point */
419 copy = g_malloc (end - nptr + 1 + decimal_point_len);
422 memcpy (c, nptr, decimal_point_pos - nptr);
423 c += decimal_point_pos - nptr;
424 memcpy (c, decimal_point, decimal_point_len);
425 c += decimal_point_len;
426 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
427 c += end - (decimal_point_pos + 1);
430 val = strtod (copy, &fail_pos);
434 if (fail_pos - copy > decimal_point_pos - nptr)
435 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
437 fail_pos = (char *)nptr + (fail_pos - copy);
443 else if (decimal_point[0] != '.' ||
444 decimal_point[1] != 0)
448 copy = g_malloc (end - (char *)nptr + 1);
449 memcpy (copy, nptr, end - nptr);
450 *(copy + (end - (char *)nptr)) = 0;
452 val = strtod (copy, &fail_pos);
456 fail_pos = (char *)nptr + (fail_pos - copy);
463 val = strtod (nptr, &fail_pos);
475 * @buffer: A buffer to place the resulting string in
476 * @buf_len: The length of the buffer.
477 * @d: The #gdouble to convert
479 * Converts a #gdouble to a string, using the '.' as
482 * This functions generates enough precision that converting
483 * the string back using g_ascii_strtod() gives the same machine-number
484 * (on machines with IEEE compatible 64bit doubles). It is
485 * guaranteed that the size of the resulting string will never
486 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
488 * Return value: The pointer to the buffer with the converted string.
491 g_ascii_dtostr (gchar *buffer,
495 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
500 * @buffer: A buffer to place the resulting string in
501 * @buf_len: The length of the buffer.
502 * @format: The printf()-style format to use for the
503 * code to use for converting.
504 * @d: The #gdouble to convert
506 * Converts a #gdouble to a string, using the '.' as
507 * decimal point. To format the number you pass in
508 * a printf()-style format string. Allowed conversion
509 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
511 * If you just want to want to serialize the value into a
512 * string, use g_ascii_dtostr().
514 * Return value: The pointer to the buffer with the converted string.
517 g_ascii_formatd (gchar *buffer,
522 struct lconv *locale_data;
523 const char *decimal_point;
524 int decimal_point_len;
529 g_return_val_if_fail (buffer != NULL, NULL);
530 g_return_val_if_fail (format[0] == '%', NULL);
531 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
533 format_char = format[strlen (format) - 1];
535 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
536 format_char == 'f' || format_char == 'F' ||
537 format_char == 'g' || format_char == 'G',
540 if (format[0] != '%')
543 if (strpbrk (format + 1, "'l%"))
546 if (!(format_char == 'e' || format_char == 'E' ||
547 format_char == 'f' || format_char == 'F' ||
548 format_char == 'g' || format_char == 'G'))
552 _g_snprintf (buffer, buf_len, format, d);
554 locale_data = localeconv ();
555 decimal_point = locale_data->decimal_point;
556 decimal_point_len = strlen (decimal_point);
558 g_assert (decimal_point_len != 0);
560 if (decimal_point[0] != '.' ||
561 decimal_point[1] != 0)
565 if (*p == '+' || *p == '-')
568 while (isdigit ((guchar)*p))
571 if (strncmp (p, decimal_point, decimal_point_len) == 0)
575 if (decimal_point_len > 1) {
576 rest_len = strlen (p + (decimal_point_len-1));
577 memmove (p, p + (decimal_point_len-1),
590 * @nptr: the string to convert to a numeric value.
591 * @endptr: if non-%NULL, it returns the character after
592 * the last character used in the conversion.
593 * @base: to be used for the conversion, 2..36 or 0
595 * Converts a string to a #guint64 value.
596 * This function behaves like the standard strtoull() function
597 * does in the C locale. It does this without actually
598 * changing the current locale, since that would not be
601 * This function is typically used when reading configuration
602 * files or other non-user input that should be locale independent.
603 * To handle input from the user you should normally use the
604 * locale-sensitive system strtoull() function.
606 * If the correct value would cause overflow, %G_MAXUINT64
607 * is returned, and %ERANGE is stored in %errno.
609 * Return value: the #guint64 value.
614 g_ascii_strtoull (const gchar *nptr,
618 /* this code is based on on the strtol(3) code from GNU libc released under
619 * the GNU Lesser General Public License.
621 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
622 * Free Software Foundation, Inc.
624 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
625 (c) == '\r' || (c) == '\t' || (c) == '\v')
626 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
627 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
628 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
629 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
630 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
631 gboolean negative, overflow;
635 const gchar *s, *save;
638 g_return_val_if_fail (nptr != NULL, 0);
640 if (base == 1 || base > 36)
648 /* Skip white space. */
654 /* Check for a sign. */
664 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
667 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
678 /* Save the pointer so we can check later if anything happened. */
680 cutoff = G_MAXUINT64 / base;
681 cutlim = G_MAXUINT64 % base;
688 if (c >= '0' && c <= '9')
690 else if (ISALPHA (c))
691 c = TOUPPER (c) - 'A' + 10;
696 /* Check for overflow. */
697 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
706 /* Check if anything actually happened. */
710 /* Store in ENDPTR the address of one character
711 past the last character we converted. */
713 *endptr = (gchar*) s;
721 /* Return the result of the appropriate sign. */
722 return negative ? -ui64 : ui64;
725 /* We must handle a special case here: the base is 0 or 16 and the
726 first two characters are '0' and 'x', but the rest are no
727 hexadecimal digits. This is no error case. We return 0 and
728 ENDPTR points to the `x`. */
731 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
733 *endptr = (gchar*) &save[-1];
735 /* There was no number to convert. */
736 *endptr = (gchar*) nptr;
742 G_CONST_RETURN gchar*
743 g_strerror (gint errnum)
745 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
747 int saved_errno = errno;
750 const char *msg_locale;
752 msg_locale = strerror (errnum);
753 if (g_get_charset (NULL))
760 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
763 /* Stick in the quark table so that we can return a static result
765 GQuark msg_quark = g_quark_from_string (msg_utf8);
768 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
777 case E2BIG: return "argument list too long";
780 case EACCES: return "permission denied";
783 case EADDRINUSE: return "address already in use";
786 case EADDRNOTAVAIL: return "can't assign requested address";
789 case EADV: return "advertise error";
792 case EAFNOSUPPORT: return "address family not supported by protocol family";
795 case EAGAIN: return "try again";
798 case EALIGN: return "EALIGN";
801 case EALREADY: return "operation already in progress";
804 case EBADE: return "bad exchange descriptor";
807 case EBADF: return "bad file number";
810 case EBADFD: return "file descriptor in bad state";
813 case EBADMSG: return "not a data message";
816 case EBADR: return "bad request descriptor";
819 case EBADRPC: return "RPC structure is bad";
822 case EBADRQC: return "bad request code";
825 case EBADSLT: return "invalid slot";
828 case EBFONT: return "bad font file format";
831 case EBUSY: return "mount device busy";
834 case ECHILD: return "no children";
837 case ECHRNG: return "channel number out of range";
840 case ECOMM: return "communication error on send";
843 case ECONNABORTED: return "software caused connection abort";
846 case ECONNREFUSED: return "connection refused";
849 case ECONNRESET: return "connection reset by peer";
851 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
852 case EDEADLK: return "resource deadlock avoided";
855 case EDEADLOCK: return "resource deadlock avoided";
858 case EDESTADDRREQ: return "destination address required";
861 case EDIRTY: return "mounting a dirty fs w/o force";
864 case EDOM: return "math argument out of range";
867 case EDOTDOT: return "cross mount point";
870 case EDQUOT: return "disk quota exceeded";
873 case EDUPPKG: return "duplicate package name";
876 case EEXIST: return "file already exists";
879 case EFAULT: return "bad address in system call argument";
882 case EFBIG: return "file too large";
885 case EHOSTDOWN: return "host is down";
888 case EHOSTUNREACH: return "host is unreachable";
891 case EIDRM: return "identifier removed";
894 case EINIT: return "initialization error";
897 case EINPROGRESS: return "operation now in progress";
900 case EINTR: return "interrupted system call";
903 case EINVAL: return "invalid argument";
906 case EIO: return "I/O error";
909 case EISCONN: return "socket is already connected";
912 case EISDIR: return "is a directory";
915 case EISNAM: return "is a name file";
918 case ELBIN: return "ELBIN";
921 case EL2HLT: return "level 2 halted";
924 case EL2NSYNC: return "level 2 not synchronized";
927 case EL3HLT: return "level 3 halted";
930 case EL3RST: return "level 3 reset";
933 case ELIBACC: return "can not access a needed shared library";
936 case ELIBBAD: return "accessing a corrupted shared library";
939 case ELIBEXEC: return "can not exec a shared library directly";
942 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
945 case ELIBSCN: return ".lib section in a.out corrupted";
948 case ELNRNG: return "link number out of range";
951 case ELOOP: return "too many levels of symbolic links";
954 case EMFILE: return "too many open files";
957 case EMLINK: return "too many links";
960 case EMSGSIZE: return "message too long";
963 case EMULTIHOP: return "multihop attempted";
966 case ENAMETOOLONG: return "file name too long";
969 case ENAVAIL: return "not available";
972 case ENET: return "ENET";
975 case ENETDOWN: return "network is down";
978 case ENETRESET: return "network dropped connection on reset";
981 case ENETUNREACH: return "network is unreachable";
984 case ENFILE: return "file table overflow";
987 case ENOANO: return "anode table overflow";
989 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
990 case ENOBUFS: return "no buffer space available";
993 case ENOCSI: return "no CSI structure available";
996 case ENODATA: return "no data available";
999 case ENODEV: return "no such device";
1002 case ENOENT: return "no such file or directory";
1005 case ENOEXEC: return "exec format error";
1008 case ENOLCK: return "no locks available";
1011 case ENOLINK: return "link has be severed";
1014 case ENOMEM: return "not enough memory";
1017 case ENOMSG: return "no message of desired type";
1020 case ENONET: return "machine is not on the network";
1023 case ENOPKG: return "package not installed";
1026 case ENOPROTOOPT: return "bad proocol option";
1029 case ENOSPC: return "no space left on device";
1032 case ENOSR: return "out of stream resources";
1035 case ENOSTR: return "not a stream device";
1038 case ENOSYM: return "unresolved symbol name";
1041 case ENOSYS: return "function not implemented";
1044 case ENOTBLK: return "block device required";
1047 case ENOTCONN: return "socket is not connected";
1050 case ENOTDIR: return "not a directory";
1053 case ENOTEMPTY: return "directory not empty";
1056 case ENOTNAM: return "not a name file";
1059 case ENOTSOCK: return "socket operation on non-socket";
1062 case ENOTTY: return "inappropriate device for ioctl";
1065 case ENOTUNIQ: return "name not unique on network";
1068 case ENXIO: return "no such device or address";
1071 case EOPNOTSUPP: return "operation not supported on socket";
1074 case EPERM: return "not owner";
1077 case EPFNOSUPPORT: return "protocol family not supported";
1080 case EPIPE: return "broken pipe";
1083 case EPROCLIM: return "too many processes";
1086 case EPROCUNAVAIL: return "bad procedure for program";
1088 #ifdef EPROGMISMATCH
1089 case EPROGMISMATCH: return "program version wrong";
1092 case EPROGUNAVAIL: return "RPC program not available";
1095 case EPROTO: return "protocol error";
1097 #ifdef EPROTONOSUPPORT
1098 case EPROTONOSUPPORT: return "protocol not suppored";
1101 case EPROTOTYPE: return "protocol wrong type for socket";
1104 case ERANGE: return "math result unrepresentable";
1106 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1107 case EREFUSED: return "EREFUSED";
1110 case EREMCHG: return "remote address changed";
1113 case EREMDEV: return "remote device";
1116 case EREMOTE: return "pathname hit remote file system";
1119 case EREMOTEIO: return "remote i/o error";
1121 #ifdef EREMOTERELEASE
1122 case EREMOTERELEASE: return "EREMOTERELEASE";
1125 case EROFS: return "read-only file system";
1128 case ERPCMISMATCH: return "RPC version is wrong";
1131 case ERREMOTE: return "object is remote";
1134 case ESHUTDOWN: return "can't send afer socket shutdown";
1136 #ifdef ESOCKTNOSUPPORT
1137 case ESOCKTNOSUPPORT: return "socket type not supported";
1140 case ESPIPE: return "invalid seek";
1143 case ESRCH: return "no such process";
1146 case ESRMNT: return "srmount error";
1149 case ESTALE: return "stale remote file handle";
1152 case ESUCCESS: return "Error 0";
1155 case ETIME: return "timer expired";
1158 case ETIMEDOUT: return "connection timed out";
1161 case ETOOMANYREFS: return "too many references: can't splice";
1164 case ETXTBSY: return "text file or pseudo-device busy";
1167 case EUCLEAN: return "structure needs cleaning";
1170 case EUNATCH: return "protocol driver not attached";
1173 case EUSERS: return "too many users";
1176 case EVERSION: return "version mismatch";
1178 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1179 case EWOULDBLOCK: return "operation would block";
1182 case EXDEV: return "cross-domain link";
1185 case EXFULL: return "message tables full";
1188 #else /* NO_SYS_ERRLIST */
1189 extern int sys_nerr;
1190 extern char *sys_errlist[];
1192 if ((errnum > 0) && (errnum <= sys_nerr))
1193 return sys_errlist [errnum];
1194 #endif /* NO_SYS_ERRLIST */
1196 msg = g_static_private_get (&msg_private);
1199 msg = g_new (gchar, 64);
1200 g_static_private_set (&msg_private, msg, g_free);
1203 _g_sprintf (msg, "unknown error (%d)", errnum);
1205 errno = saved_errno;
1209 G_CONST_RETURN gchar*
1210 g_strsignal (gint signum)
1212 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1215 #ifdef HAVE_STRSIGNAL
1216 const char *msg_locale;
1218 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1219 extern const char *strsignal(int);
1221 /* this is declared differently (const) in string.h on BeOS */
1222 extern char *strsignal (int sig);
1223 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1224 msg_locale = strsignal (signum);
1225 if (g_get_charset (NULL))
1229 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1232 /* Stick in the quark table so that we can return a static result
1234 GQuark msg_quark = g_quark_from_string (msg_utf8);
1237 return g_quark_to_string (msg_quark);
1240 #elif NO_SYS_SIGLIST
1244 case SIGHUP: return "Hangup";
1247 case SIGINT: return "Interrupt";
1250 case SIGQUIT: return "Quit";
1253 case SIGILL: return "Illegal instruction";
1256 case SIGTRAP: return "Trace/breakpoint trap";
1259 case SIGABRT: return "IOT trap/Abort";
1262 case SIGBUS: return "Bus error";
1265 case SIGFPE: return "Floating point exception";
1268 case SIGKILL: return "Killed";
1271 case SIGUSR1: return "User defined signal 1";
1274 case SIGSEGV: return "Segmentation fault";
1277 case SIGUSR2: return "User defined signal 2";
1280 case SIGPIPE: return "Broken pipe";
1283 case SIGALRM: return "Alarm clock";
1286 case SIGTERM: return "Terminated";
1289 case SIGSTKFLT: return "Stack fault";
1292 case SIGCHLD: return "Child exited";
1295 case SIGCONT: return "Continued";
1298 case SIGSTOP: return "Stopped (signal)";
1301 case SIGTSTP: return "Stopped";
1304 case SIGTTIN: return "Stopped (tty input)";
1307 case SIGTTOU: return "Stopped (tty output)";
1310 case SIGURG: return "Urgent condition";
1313 case SIGXCPU: return "CPU time limit exceeded";
1316 case SIGXFSZ: return "File size limit exceeded";
1319 case SIGVTALRM: return "Virtual time alarm";
1322 case SIGPROF: return "Profile signal";
1325 case SIGWINCH: return "Window size changed";
1328 case SIGIO: return "Possible I/O";
1331 case SIGPWR: return "Power failure";
1334 case SIGUNUSED: return "Unused signal";
1337 #else /* NO_SYS_SIGLIST */
1339 #ifdef NO_SYS_SIGLIST_DECL
1340 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1343 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1344 #endif /* NO_SYS_SIGLIST */
1346 msg = g_static_private_get (&msg_private);
1349 msg = g_new (gchar, 64);
1350 g_static_private_set (&msg_private, msg, g_free);
1353 _g_sprintf (msg, "unknown signal (%d)", signum);
1358 /* Functions g_strlcpy and g_strlcat were originally developed by
1359 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1360 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1361 * for more information.
1365 /* Use the native ones, if available; they might be implemented in assembly */
1367 g_strlcpy (gchar *dest,
1371 g_return_val_if_fail (dest != NULL, 0);
1372 g_return_val_if_fail (src != NULL, 0);
1374 return strlcpy (dest, src, dest_size);
1378 g_strlcat (gchar *dest,
1382 g_return_val_if_fail (dest != NULL, 0);
1383 g_return_val_if_fail (src != NULL, 0);
1385 return strlcat (dest, src, dest_size);
1388 #else /* ! HAVE_STRLCPY */
1391 * Copy string src to buffer dest (of buffer size dest_size). At most
1392 * dest_size-1 characters will be copied. Always NUL terminates
1393 * (unless dest_size == 0). This function does NOT allocate memory.
1394 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1395 * Returns size of attempted result, strlen(src),
1396 * so if retval >= dest_size, truncation occurred.
1399 g_strlcpy (gchar *dest,
1403 register gchar *d = dest;
1404 register const gchar *s = src;
1405 register gsize n = dest_size;
1407 g_return_val_if_fail (dest != NULL, 0);
1408 g_return_val_if_fail (src != NULL, 0);
1410 /* Copy as many bytes as will fit */
1411 if (n != 0 && --n != 0)
1414 register gchar c = *s++;
1422 /* If not enough room in dest, add NUL and traverse rest of src */
1431 return s - src - 1; /* count does not include NUL */
1436 * Appends string src to buffer dest (of buffer size dest_size).
1437 * At most dest_size-1 characters will be copied.
1438 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1439 * This function does NOT allocate memory.
1440 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1441 * in the dest_size characters of dest to start with).
1442 * Returns size of attempted result, which is
1443 * MIN (dest_size, strlen (original dest)) + strlen (src),
1444 * so if retval >= dest_size, truncation occurred.
1447 g_strlcat (gchar *dest,
1451 register gchar *d = dest;
1452 register const gchar *s = src;
1453 register gsize bytes_left = dest_size;
1454 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1456 g_return_val_if_fail (dest != NULL, 0);
1457 g_return_val_if_fail (src != NULL, 0);
1459 /* Find the end of dst and adjust bytes left but don't go past end */
1460 while (*d != 0 && bytes_left-- != 0)
1463 bytes_left = dest_size - dlength;
1465 if (bytes_left == 0)
1466 return dlength + strlen (s);
1470 if (bytes_left != 1)
1479 return dlength + (s - src); /* count does not include NUL */
1481 #endif /* ! HAVE_STRLCPY */
1486 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1488 * Converts all upper case ASCII letters to lower case ASCII letters.
1490 * Return value: a newly-allocated string, with all the upper case
1491 * characters in @str converted to lower case, with
1492 * semantics that exactly match g_ascii_tolower(). (Note
1493 * that this is unlike the old g_strdown(), which modified
1494 * the string in place.)
1497 g_ascii_strdown (const gchar *str,
1502 g_return_val_if_fail (str != NULL, NULL);
1507 result = g_strndup (str, len);
1508 for (s = result; *s; s++)
1509 *s = g_ascii_tolower (*s);
1517 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1519 * Converts all lower case ASCII letters to upper case ASCII letters.
1521 * Return value: a newly allocated string, with all the lower case
1522 * characters in @str converted to upper case, with
1523 * semantics that exactly match g_ascii_toupper(). (Note
1524 * that this is unlike the old g_strup(), which modified
1525 * the string in place.)
1528 g_ascii_strup (const gchar *str,
1533 g_return_val_if_fail (str != NULL, NULL);
1538 result = g_strndup (str, len);
1539 for (s = result; *s; s++)
1540 *s = g_ascii_toupper (*s);
1547 * @string: the string to convert.
1549 * Converts a string to lower case.
1551 * Return value: the string
1553 * Deprecated: This function is totally broken for the reasons discussed in
1554 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1558 g_strdown (gchar *string)
1562 g_return_val_if_fail (string != NULL, NULL);
1564 s = (guchar *) string;
1573 return (gchar *) string;
1578 * @string: the string to convert.
1580 * Converts a string to upper case.
1582 * Return value: the string
1584 * Deprecated: This function is totally broken for the reasons discussed in
1585 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1588 g_strup (gchar *string)
1592 g_return_val_if_fail (string != NULL, NULL);
1594 s = (guchar *) string;
1603 return (gchar *) string;
1607 g_strreverse (gchar *string)
1609 g_return_val_if_fail (string != NULL, NULL);
1613 register gchar *h, *t;
1616 t = string + strlen (string) - 1;
1635 * @c: any character.
1637 * Convert a character to ASCII lower case.
1639 * Unlike the standard C library tolower() function, this only
1640 * recognizes standard ASCII letters and ignores the locale, returning
1641 * all non-ASCII characters unchanged, even if they are lower case
1642 * letters in a particular character set. Also unlike the standard
1643 * library function, this takes and returns a char, not an int, so
1644 * don't call it on %EOF but no need to worry about casting to #guchar
1645 * before passing a possibly non-ASCII character in.
1647 * Return value: the result of converting @c to lower case.
1648 * If @c is not an ASCII upper case letter,
1649 * @c is returned unchanged.
1652 g_ascii_tolower (gchar c)
1654 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1659 * @c: any character.
1661 * Convert a character to ASCII upper case.
1663 * Unlike the standard C library toupper() function, this only
1664 * recognizes standard ASCII letters and ignores the locale, returning
1665 * all non-ASCII characters unchanged, even if they are upper case
1666 * letters in a particular character set. Also unlike the standard
1667 * library function, this takes and returns a char, not an int, so
1668 * don't call it on %EOF but no need to worry about casting to #guchar
1669 * before passing a possibly non-ASCII character in.
1671 * Return value: the result of converting @c to upper case.
1672 * If @c is not an ASCII lower case letter,
1673 * @c is returned unchanged.
1676 g_ascii_toupper (gchar c)
1678 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1682 * g_ascii_digit_value:
1683 * @c: an ASCII character.
1685 * Determines the numeric value of a character as a decimal
1686 * digit. Differs from g_unichar_digit_value() because it takes
1687 * a char, so there's no worry about sign extension if characters
1690 * Return value: If @c is a decimal digit (according to
1691 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1694 g_ascii_digit_value (gchar c)
1696 if (g_ascii_isdigit (c))
1702 * g_ascii_xdigit_value:
1703 * @c: an ASCII character.
1705 * Determines the numeric value of a character as a hexidecimal
1706 * digit. Differs from g_unichar_xdigit_value() because it takes
1707 * a char, so there's no worry about sign extension if characters
1710 * Return value: If @c is a hex digit (according to
1711 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1714 g_ascii_xdigit_value (gchar c)
1716 if (c >= 'A' && c <= 'F')
1717 return c - 'A' + 10;
1718 if (c >= 'a' && c <= 'f')
1719 return c - 'a' + 10;
1720 return g_ascii_digit_value (c);
1724 * g_ascii_strcasecmp:
1725 * @s1: string to compare with @s2.
1726 * @s2: string to compare with @s1.
1728 * Compare two strings, ignoring the case of ASCII characters.
1730 * Unlike the BSD strcasecmp() function, this only recognizes standard
1731 * ASCII letters and ignores the locale, treating all non-ASCII
1732 * characters as if they are not letters.
1734 * Return value: an integer less than, equal to, or greater than
1735 * zero if @s1 is found, respectively, to be less than,
1736 * to match, or to be greater than @s2.
1739 g_ascii_strcasecmp (const gchar *s1,
1744 g_return_val_if_fail (s1 != NULL, 0);
1745 g_return_val_if_fail (s2 != NULL, 0);
1749 c1 = (gint)(guchar) TOLOWER (*s1);
1750 c2 = (gint)(guchar) TOLOWER (*s2);
1756 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1760 * g_ascii_strncasecmp:
1761 * @s1: string to compare with @s2.
1762 * @s2: string to compare with @s1.
1763 * @n: number of characters to compare.
1765 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1766 * characters after the first @n in each string.
1768 * Unlike the BSD strcasecmp() function, this only recognizes standard
1769 * ASCII letters and ignores the locale, treating all non-ASCII
1770 * characters as if they are not letters.
1772 * Return value: an integer less than, equal to, or greater than zero
1773 * if the first @n bytes of @s1 is found, respectively,
1774 * to be less than, to match, or to be greater than the
1775 * first @n bytes of @s2.
1778 g_ascii_strncasecmp (const gchar *s1,
1784 g_return_val_if_fail (s1 != NULL, 0);
1785 g_return_val_if_fail (s2 != NULL, 0);
1787 while (n && *s1 && *s2)
1790 c1 = (gint)(guchar) TOLOWER (*s1);
1791 c2 = (gint)(guchar) TOLOWER (*s2);
1798 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1806 * @s2: a string to compare with @s1.
1808 * A case-insensitive string comparison, corresponding to the standard
1809 * strcasecmp() function on platforms which support it.
1811 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1812 * or a positive value if @s1 > @s2.
1814 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1815 * deprecated and how to replace it.
1818 g_strcasecmp (const gchar *s1,
1821 #ifdef HAVE_STRCASECMP
1822 g_return_val_if_fail (s1 != NULL, 0);
1823 g_return_val_if_fail (s2 != NULL, 0);
1825 return strcasecmp (s1, s2);
1829 g_return_val_if_fail (s1 != NULL, 0);
1830 g_return_val_if_fail (s2 != NULL, 0);
1834 /* According to A. Cox, some platforms have islower's that
1835 * don't work right on non-uppercase
1837 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1838 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1844 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1851 * @s2: a string to compare with @s1.
1852 * @n: the maximum number of characters to compare.
1854 * A case-insensitive string comparison, corresponding to the standard
1855 * strncasecmp() function on platforms which support it.
1856 * It is similar to g_strcasecmp() except it only compares the first @n
1857 * characters of the strings.
1859 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1860 * or a positive value if @s1 > @s2.
1862 * Deprecated: The problem with g_strncasecmp() is that it does the
1863 * comparison by calling toupper()/tolower(). These functions are
1864 * locale-specific and operate on single bytes. However, it is impossible
1865 * to handle things correctly from an I18N standpoint by operating on
1866 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1867 * broken if your string is guaranteed to be ASCII, since it's
1868 * locale-sensitive, and it's broken if your string is localized, since
1869 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1872 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1873 * which only works on ASCII and is not locale-sensitive, and
1874 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1877 g_strncasecmp (const gchar *s1,
1881 #ifdef HAVE_STRNCASECMP
1882 return strncasecmp (s1, s2, n);
1886 g_return_val_if_fail (s1 != NULL, 0);
1887 g_return_val_if_fail (s2 != NULL, 0);
1889 while (n && *s1 && *s2)
1892 /* According to A. Cox, some platforms have islower's that
1893 * don't work right on non-uppercase
1895 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1896 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1903 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1910 g_strdelimit (gchar *string,
1911 const gchar *delimiters,
1916 g_return_val_if_fail (string != NULL, NULL);
1919 delimiters = G_STR_DELIMITERS;
1921 for (c = string; *c; c++)
1923 if (strchr (delimiters, *c))
1931 g_strcanon (gchar *string,
1932 const gchar *valid_chars,
1937 g_return_val_if_fail (string != NULL, NULL);
1938 g_return_val_if_fail (valid_chars != NULL, NULL);
1940 for (c = string; *c; c++)
1942 if (!strchr (valid_chars, *c))
1950 g_strcompress (const gchar *source)
1952 const gchar *p = source, *octal;
1953 gchar *dest = g_malloc (strlen (source) + 1);
1963 case '0': case '1': case '2': case '3': case '4':
1964 case '5': case '6': case '7':
1967 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1969 *q = (*q * 8) + (*p - '0');
1990 default: /* Also handles \" and \\ */
2005 g_strescape (const gchar *source,
2006 const gchar *exceptions)
2013 g_return_val_if_fail (source != NULL, NULL);
2015 p = (guchar *) source;
2016 /* Each source byte needs maximally four destination chars (\777) */
2017 q = dest = g_malloc (strlen (source) * 4 + 1);
2019 memset (excmap, 0, 256);
2022 guchar *e = (guchar *) exceptions;
2068 if ((*p < ' ') || (*p >= 0177))
2071 *q++ = '0' + (((*p) >> 6) & 07);
2072 *q++ = '0' + (((*p) >> 3) & 07);
2073 *q++ = '0' + ((*p) & 07);
2087 g_strchug (gchar *string)
2091 g_return_val_if_fail (string != NULL, NULL);
2093 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2096 g_memmove (string, start, strlen ((gchar *) start) + 1);
2102 g_strchomp (gchar *string)
2106 g_return_val_if_fail (string != NULL, NULL);
2108 len = strlen (string);
2111 if (g_ascii_isspace ((guchar) string[len]))
2122 * @string: a string to split.
2123 * @delimiter: a string which specifies the places at which to split the string.
2124 * The delimiter is not included in any of the resulting strings, unless
2125 * @max_tokens is reached.
2126 * @max_tokens: the maximum number of pieces to split @string into. If this is
2127 * less than 1, the string is split completely.
2129 * Splits a string into a maximum of @max_tokens pieces, using the given
2130 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2131 * to the last token.
2133 * As a special case, the result of splitting the empty string "" is an empty
2134 * vector, not a vector containing a single string. The reason for this
2135 * special case is that being able to represent a empty vector is typically
2136 * more useful than consistent handling of empty elements. If you do need
2137 * to represent empty elements, you'll need to check for the empty string
2138 * before calling g_strsplit().
2140 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2141 * g_strfreev() to free it.
2144 g_strsplit (const gchar *string,
2145 const gchar *delimiter,
2148 GSList *string_list = NULL, *slist;
2149 gchar **str_array, *s;
2151 const gchar *remainder;
2153 g_return_val_if_fail (string != NULL, NULL);
2154 g_return_val_if_fail (delimiter != NULL, NULL);
2155 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2158 max_tokens = G_MAXINT;
2161 s = strstr (remainder, delimiter);
2164 gsize delimiter_len = strlen (delimiter);
2166 while (--max_tokens && s)
2171 len = s - remainder;
2172 new_string = g_new (gchar, len + 1);
2173 strncpy (new_string, remainder, len);
2174 new_string[len] = 0;
2175 string_list = g_slist_prepend (string_list, new_string);
2177 remainder = s + delimiter_len;
2178 s = strstr (remainder, delimiter);
2184 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2187 str_array = g_new (gchar*, n + 1);
2189 str_array[n--] = NULL;
2190 for (slist = string_list; slist; slist = slist->next)
2191 str_array[n--] = slist->data;
2193 g_slist_free (string_list);
2200 * @string: The string to be tokenized
2201 * @delimiters: A nul-terminated string containing bytes that are used
2202 * to split the string.
2203 * @max_tokens: The maximum number of tokens to split @string into.
2204 * If this is less than 1, the string is split completely
2206 * Splits @string into a number of tokens not containing any of the characters
2207 * in @delimiter. A token is the (possibly empty) longest string that does not
2208 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2209 * remainder is appended to the last token.
2211 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2212 * %NULL-terminated vector containing the three strings "abc", "def",
2215 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2216 * vector containing the four strings "", "def", "ghi", and "".
2218 * As a special case, the result of splitting the empty string "" is an empty
2219 * vector, not a vector containing a single string. The reason for this
2220 * special case is that being able to represent a empty vector is typically
2221 * more useful than consistent handling of empty elements. If you do need
2222 * to represent empty elements, you'll need to check for the empty string
2223 * before calling g_strsplit_set().
2225 * Note that this function works on bytes not characters, so it can't be used
2226 * to delimit UTF-8 strings for anything but ASCII characters.
2228 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2229 * g_strfreev() to free it.
2234 g_strsplit_set (const gchar *string,
2235 const gchar *delimiters,
2238 gboolean delim_table[256];
2239 GSList *tokens, *list;
2242 const gchar *current;
2246 g_return_val_if_fail (string != NULL, NULL);
2247 g_return_val_if_fail (delimiters != NULL, NULL);
2250 max_tokens = G_MAXINT;
2252 if (*string == '\0')
2254 result = g_new (char *, 1);
2259 memset (delim_table, FALSE, sizeof (delim_table));
2260 for (s = delimiters; *s != '\0'; ++s)
2261 delim_table[*(guchar *)s] = TRUE;
2266 s = current = string;
2269 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2273 token = g_strndup (current, s - current);
2274 tokens = g_slist_prepend (tokens, token);
2283 token = g_strndup (current, s - current);
2284 tokens = g_slist_prepend (tokens, token);
2287 result = g_new (gchar *, n_tokens + 1);
2289 result[n_tokens] = NULL;
2290 for (list = tokens; list != NULL; list = list->next)
2291 result[--n_tokens] = list->data;
2293 g_slist_free (tokens);
2300 * @str_array: a %NULL-terminated array of strings to free.
2302 * Frees a %NULL-terminated array of strings, and the array itself.
2303 * If called on a %NULL value, g_strfreev() simply returns.
2306 g_strfreev (gchar **str_array)
2312 for(i = 0; str_array[i] != NULL; i++)
2313 g_free(str_array[i]);
2321 * @str_array: %NULL-terminated array of strings.
2323 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2324 * the new array should be freed by first freeing each string, then
2325 * the array itself. g_strfreev() does this for you. If called
2326 * on a %NULL value, g_strdupv() simply returns %NULL.
2328 * Return value: a new %NULL-terminated array of strings.
2331 g_strdupv (gchar **str_array)
2339 while (str_array[i])
2342 retval = g_new (gchar*, i + 1);
2345 while (str_array[i])
2347 retval[i] = g_strdup (str_array[i]);
2359 g_strjoinv (const gchar *separator,
2365 g_return_val_if_fail (str_array != NULL, NULL);
2367 if (separator == NULL)
2374 gsize separator_len;
2376 separator_len = strlen (separator);
2377 /* First part, getting length */
2378 len = 1 + strlen (str_array[0]);
2379 for (i = 1; str_array[i] != NULL; i++)
2380 len += strlen (str_array[i]);
2381 len += separator_len * (i - 1);
2383 /* Second part, building string */
2384 string = g_new (gchar, len);
2385 ptr = g_stpcpy (string, *str_array);
2386 for (i = 1; str_array[i] != NULL; i++)
2388 ptr = g_stpcpy (ptr, separator);
2389 ptr = g_stpcpy (ptr, str_array[i]);
2393 string = g_strdup ("");
2399 g_strjoin (const gchar *separator,
2405 gsize separator_len;
2408 if (separator == NULL)
2411 separator_len = strlen (separator);
2413 va_start (args, separator);
2415 s = va_arg (args, gchar*);
2419 /* First part, getting length */
2420 len = 1 + strlen (s);
2422 s = va_arg (args, gchar*);
2425 len += separator_len + strlen (s);
2426 s = va_arg (args, gchar*);
2430 /* Second part, building string */
2431 string = g_new (gchar, len);
2433 va_start (args, separator);
2435 s = va_arg (args, gchar*);
2436 ptr = g_stpcpy (string, s);
2438 s = va_arg (args, gchar*);
2441 ptr = g_stpcpy (ptr, separator);
2442 ptr = g_stpcpy (ptr, s);
2443 s = va_arg (args, gchar*);
2447 string = g_strdup ("");
2457 * @haystack: a string.
2458 * @haystack_len: the maximum length of @haystack.
2459 * @needle: the string to search for.
2461 * Searches the string @haystack for the first occurrence
2462 * of the string @needle, limiting the length of the search
2465 * Return value: a pointer to the found occurrence, or
2466 * %NULL if not found.
2469 g_strstr_len (const gchar *haystack,
2470 gssize haystack_len,
2471 const gchar *needle)
2473 g_return_val_if_fail (haystack != NULL, NULL);
2474 g_return_val_if_fail (needle != NULL, NULL);
2476 if (haystack_len < 0)
2477 return strstr (haystack, needle);
2480 const gchar *p = haystack;
2481 gsize needle_len = strlen (needle);
2485 if (needle_len == 0)
2486 return (gchar *)haystack;
2488 if (haystack_len < needle_len)
2491 end = haystack + haystack_len - needle_len;
2493 while (*p && p <= end)
2495 for (i = 0; i < needle_len; i++)
2496 if (p[i] != needle[i])
2511 * @haystack: a nul-terminated string.
2512 * @needle: the nul-terminated string to search for.
2514 * Searches the string @haystack for the last occurrence
2515 * of the string @needle.
2517 * Return value: a pointer to the found occurrence, or
2518 * %NULL if not found.
2521 g_strrstr (const gchar *haystack,
2522 const gchar *needle)
2529 g_return_val_if_fail (haystack != NULL, NULL);
2530 g_return_val_if_fail (needle != NULL, NULL);
2532 needle_len = strlen (needle);
2533 haystack_len = strlen (haystack);
2535 if (needle_len == 0)
2536 return (gchar *)haystack;
2538 if (haystack_len < needle_len)
2541 p = haystack + haystack_len - needle_len;
2543 while (p >= haystack)
2545 for (i = 0; i < needle_len; i++)
2546 if (p[i] != needle[i])
2560 * @haystack: a nul-terminated string.
2561 * @haystack_len: the maximum length of @haystack.
2562 * @needle: the nul-terminated string to search for.
2564 * Searches the string @haystack for the last occurrence
2565 * of the string @needle, limiting the length of the search
2568 * Return value: a pointer to the found occurrence, or
2569 * %NULL if not found.
2572 g_strrstr_len (const gchar *haystack,
2573 gssize haystack_len,
2574 const gchar *needle)
2576 g_return_val_if_fail (haystack != NULL, NULL);
2577 g_return_val_if_fail (needle != NULL, NULL);
2579 if (haystack_len < 0)
2580 return g_strrstr (haystack, needle);
2583 gsize needle_len = strlen (needle);
2584 const gchar *haystack_max = haystack + haystack_len;
2585 const gchar *p = haystack;
2588 while (p < haystack_max && *p)
2591 if (p < haystack + needle_len)
2596 while (p >= haystack)
2598 for (i = 0; i < needle_len; i++)
2599 if (p[i] != needle[i])
2615 * @str: a nul-terminated string.
2616 * @suffix: the nul-terminated suffix to look for.
2618 * Looks whether the string @str ends with @suffix.
2620 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2625 g_str_has_suffix (const gchar *str,
2626 const gchar *suffix)
2631 g_return_val_if_fail (str != NULL, FALSE);
2632 g_return_val_if_fail (suffix != NULL, FALSE);
2634 str_len = strlen (str);
2635 suffix_len = strlen (suffix);
2637 if (str_len < suffix_len)
2640 return strcmp (str + str_len - suffix_len, suffix) == 0;
2645 * @str: a nul-terminated string.
2646 * @prefix: the nul-terminated prefix to look for.
2648 * Looks whether the string @str begins with @prefix.
2650 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2655 g_str_has_prefix (const gchar *str,
2656 const gchar *prefix)
2661 g_return_val_if_fail (str != NULL, FALSE);
2662 g_return_val_if_fail (prefix != NULL, FALSE);
2664 str_len = strlen (str);
2665 prefix_len = strlen (prefix);
2667 if (str_len < prefix_len)
2670 return strncmp (str, prefix, prefix_len) == 0;
2677 * @msgval: another string
2679 * An auxiliary function for gettext() support (see Q_()).
2681 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2682 * a '|' character, in which case a pointer to the substring of msgid after
2683 * the first '|' character is returned.
2687 G_CONST_RETURN gchar *
2688 g_strip_context (const gchar *msgid,
2689 const gchar *msgval)
2691 if (msgval == msgid)
2693 const char *c = strchr (msgid, '|');
2704 * @str_array: a %NULL-terminated array of strings.
2706 * Returns the length of the given %NULL-terminated
2707 * string array @str_array.
2709 * Return value: length of @str_array.
2714 g_strv_length (gchar **str_array)
2718 g_return_val_if_fail (str_array != NULL, 0);
2720 while (str_array[i])