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)
47 #include "gprintfint.h"
53 /* do not include <unistd.h> in this place since it
54 * interferes with g_strsignal() on some OSes
57 static const guint16 ascii_table_data[256] = {
58 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
59 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
60 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
63 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
64 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
65 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
67 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
68 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
69 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
70 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
71 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
72 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
73 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
74 /* the upper 128 are all zeroes */
77 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
80 const guint16 * const g_ascii_table = ascii_table_data;
83 g_strdup (const gchar *str)
89 new_str = g_new (char, strlen (str) + 1);
90 strcpy (new_str, str);
99 g_memdup (gconstpointer mem,
106 new_mem = g_malloc (byte_size);
107 memcpy (new_mem, mem, byte_size);
116 g_strndup (const gchar *str,
123 new_str = g_new (gchar, n + 1);
124 strncpy (new_str, str, n);
134 g_strnfill (gsize length,
139 str = g_new (gchar, length + 1);
140 memset (str, (guchar)fill_char, length);
148 * @dest: destination buffer.
149 * @src: source string.
151 * Copies a nul-terminated string into the dest buffer, include the
152 * trailing nul, and return a pointer to the trailing nul byte.
153 * This is useful for concatenating multiple strings together
154 * without having to repeatedly scan for the end.
156 * Return value: a pointer to trailing nul byte.
159 g_stpcpy (gchar *dest,
163 g_return_val_if_fail (dest != NULL, NULL);
164 g_return_val_if_fail (src != NULL, NULL);
165 return stpcpy (dest, src);
167 register gchar *d = dest;
168 register const gchar *s = src;
170 g_return_val_if_fail (dest != NULL, NULL);
171 g_return_val_if_fail (src != NULL, NULL);
174 while (*s++ != '\0');
181 g_strdup_vprintf (const gchar *format,
185 #ifdef HAVE_VASPRINTF
187 len = _g_vasprintf (&buffer, format, args1);
190 else if (!g_mem_is_system_malloc ())
192 gchar *buffer1 = g_strndup (buffer, len);
199 G_VA_COPY (args2, args1);
201 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
203 _g_vsprintf (buffer, format, args2);
210 g_strdup_printf (const gchar *format,
216 va_start (args, format);
217 buffer = g_strdup_vprintf (format, args);
224 g_strconcat (const gchar *string1, ...)
232 g_return_val_if_fail (string1 != NULL, NULL);
234 l = 1 + strlen (string1);
235 va_start (args, string1);
236 s = va_arg (args, gchar*);
240 s = va_arg (args, gchar*);
244 concat = g_new (gchar, l);
247 ptr = g_stpcpy (ptr, string1);
248 va_start (args, string1);
249 s = va_arg (args, gchar*);
252 ptr = g_stpcpy (ptr, s);
253 s = va_arg (args, gchar*);
262 * @nptr: the string to convert to a numeric value.
263 * @endptr: if non-%NULL, it returns the character after
264 * the last character used in the conversion.
266 * Converts a string to a #gdouble value.
267 * It calls the standard strtod() function to handle the conversion, but
268 * if the string is not completely converted it attempts the conversion
269 * again with g_ascii_strtod(), and returns the best match.
271 * This function should seldomly be used. The normal situation when reading
272 * numbers not for human consumption is to use g_ascii_strtod(). Only when
273 * you know that you must expect both locale formatted and C formatted numbers
274 * should you use this. Make sure that you don't pass strings such as comma
275 * separated lists of values, since the commas may be interpreted as a decimal
276 * point in some locales, causing unexpected results.
278 * Return value: the #gdouble value.
281 g_strtod (const gchar *nptr,
289 g_return_val_if_fail (nptr != NULL, 0);
294 val_1 = strtod (nptr, &fail_pos_1);
296 if (fail_pos_1 && fail_pos_1[0] != 0)
297 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
299 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
302 *endptr = fail_pos_1;
308 *endptr = fail_pos_2;
315 * @nptr: the string to convert to a numeric value.
316 * @endptr: if non-%NULL, it returns the character after
317 * the last character used in the conversion.
319 * Converts a string to a #gdouble value.
320 * This function behaves like the standard strtod() function
321 * does in the C locale. It does this without actually
322 * changing the current locale, since that would not be
325 * This function is typically used when reading configuration
326 * files or other non-user input that should be locale independent.
327 * To handle input from the user you should normally use the
328 * locale-sensitive system strtod() function.
330 * To convert from a #gdouble to a string in a locale-insensitive
331 * way, use g_ascii_dtostr().
333 * If the correct value would cause overflow, plus or minus %HUGE_VAL
334 * is returned (according to the sign of the value), and %ERANGE is
335 * stored in %errno. If the correct value would cause underflow,
336 * zero is returned and %ERANGE is stored in %errno.
338 * This function resets %errno before calling strtod() so that
339 * you can reliably detect overflow and underflow.
341 * Return value: the #gdouble value.
344 g_ascii_strtod (const gchar *nptr,
349 struct lconv *locale_data;
350 const char *decimal_point;
351 int decimal_point_len;
352 const char *p, *decimal_point_pos;
353 const char *end = NULL; /* Silence gcc */
355 g_return_val_if_fail (nptr != NULL, 0);
359 locale_data = localeconv ();
360 decimal_point = locale_data->decimal_point;
361 decimal_point_len = strlen (decimal_point);
363 g_assert (decimal_point_len != 0);
365 decimal_point_pos = NULL;
366 if (decimal_point[0] != '.' ||
367 decimal_point[1] != 0)
370 /* Skip leading space */
371 while (g_ascii_isspace (*p))
374 /* Skip leading optional sign */
375 if (*p == '+' || *p == '-')
379 (p[1] == 'x' || p[1] == 'X'))
382 /* HEX - find the (optional) decimal point */
384 while (g_ascii_isxdigit (*p))
389 decimal_point_pos = p++;
391 while (g_ascii_isxdigit (*p))
394 if (*p == 'p' || *p == 'P')
396 if (*p == '+' || *p == '-')
398 while (g_ascii_isdigit (*p))
405 while (g_ascii_isdigit (*p))
410 decimal_point_pos = p++;
412 while (g_ascii_isdigit (*p))
415 if (*p == 'e' || *p == 'E')
417 if (*p == '+' || *p == '-')
419 while (g_ascii_isdigit (*p))
424 /* For the other cases, we need not convert the decimal point */
427 /* Set errno to zero, so that we can distinguish zero results
431 if (decimal_point_pos)
435 /* We need to convert the '.' to the locale specific decimal point */
436 copy = g_malloc (end - nptr + 1 + decimal_point_len);
439 memcpy (c, nptr, decimal_point_pos - nptr);
440 c += decimal_point_pos - nptr;
441 memcpy (c, decimal_point, decimal_point_len);
442 c += decimal_point_len;
443 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
444 c += end - (decimal_point_pos + 1);
447 val = strtod (copy, &fail_pos);
451 if (fail_pos > decimal_point_pos)
452 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
454 fail_pos = (char *)nptr + (fail_pos - copy);
461 val = strtod (nptr, &fail_pos);
472 * @buffer: A buffer to place the resulting string in
473 * @buf_len: The length of the buffer.
474 * @d: The #gdouble to convert
476 * Converts a #gdouble to a string, using the '.' as
479 * This functions generates enough precision that converting
480 * the string back using g_ascii_strtod() gives the same machine-number
481 * (on machines with IEEE compatible 64bit doubles). It is
482 * guaranteed that the size of the resulting string will never
483 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
485 * Return value: The pointer to the buffer with the converted string.
488 g_ascii_dtostr (gchar *buffer,
492 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
497 * @buffer: A buffer to place the resulting string in
498 * @buf_len: The length of the buffer.
499 * @format: The printf()-style format to use for the
500 * code to use for converting.
501 * @d: The #gdouble to convert
503 * Converts a #gdouble to a string, using the '.' as
504 * decimal point. To format the number you pass in
505 * a printf()-style format string. Allowed conversion
506 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
508 * If you just want to want to serialize the value into a
509 * string, use g_ascii_dtostr().
511 * Return value: The pointer to the buffer with the converted string.
514 g_ascii_formatd (gchar *buffer,
519 struct lconv *locale_data;
520 const char *decimal_point;
521 int decimal_point_len;
526 g_return_val_if_fail (buffer != NULL, NULL);
527 g_return_val_if_fail (format[0] == '%', NULL);
528 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
530 format_char = format[strlen (format) - 1];
532 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
533 format_char == 'f' || format_char == 'F' ||
534 format_char == 'g' || format_char == 'G',
537 if (format[0] != '%')
540 if (strpbrk (format + 1, "'l%"))
543 if (!(format_char == 'e' || format_char == 'E' ||
544 format_char == 'f' || format_char == 'F' ||
545 format_char == 'g' || format_char == 'G'))
549 _g_snprintf (buffer, buf_len, format, d);
551 locale_data = localeconv ();
552 decimal_point = locale_data->decimal_point;
553 decimal_point_len = strlen (decimal_point);
555 g_assert (decimal_point_len != 0);
557 if (decimal_point[0] != '.' ||
558 decimal_point[1] != 0)
562 if (*p == '+' || *p == '-')
565 while (isdigit ((guchar)*p))
568 if (strncmp (p, decimal_point, decimal_point_len) == 0)
572 if (decimal_point_len > 1) {
573 rest_len = strlen (p + (decimal_point_len-1));
574 memmove (p, p + (decimal_point_len-1),
587 * @nptr: the string to convert to a numeric value.
588 * @endptr: if non-%NULL, it returns the character after
589 * the last character used in the conversion.
590 * @base: to be used for the conversion, 2..36 or 0
592 * Converts a string to a #guint64 value.
593 * This function behaves like the standard strtoull() function
594 * does in the C locale. It does this without actually
595 * changing the current locale, since that would not be
598 * This function is typically used when reading configuration
599 * files or other non-user input that should be locale independent.
600 * To handle input from the user you should normally use the
601 * locale-sensitive system strtoull() function.
603 * If the correct value would cause overflow, %G_MAXUINT64
604 * is returned, and %ERANGE is stored in %errno.
606 * Return value: the #guint64 value.
611 g_ascii_strtoull (const gchar *nptr,
615 /* this code is based on on the strtol(3) code from GNU libc released under
616 * the GNU Lesser General Public License.
618 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
619 * Free Software Foundation, Inc.
621 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
622 (c) == '\r' || (c) == '\t' || (c) == '\v')
623 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
624 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
625 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
626 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
627 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
628 gboolean negative, overflow;
632 const gchar *s, *save;
635 g_return_val_if_fail (nptr != NULL, 0);
637 if (base == 1 || base > 36)
645 /* Skip white space. */
651 /* Check for a sign. */
661 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
664 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
675 /* Save the pointer so we can check later if anything happened. */
677 cutoff = G_MAXUINT64 / base;
678 cutlim = G_MAXUINT64 % base;
685 if (c >= '0' && c <= '9')
687 else if (ISALPHA (c))
688 c = TOUPPER (c) - 'A' + 10;
693 /* Check for overflow. */
694 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
703 /* Check if anything actually happened. */
707 /* Store in ENDPTR the address of one character
708 past the last character we converted. */
710 *endptr = (gchar*) s;
718 /* Return the result of the appropriate sign. */
719 return negative ? -ui64 : ui64;
722 /* We must handle a special case here: the base is 0 or 16 and the
723 first two characters are '0' and 'x', but the rest are no
724 hexadecimal digits. This is no error case. We return 0 and
725 ENDPTR points to the `x`. */
728 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
730 *endptr = (gchar*) &save[-1];
732 /* There was no number to convert. */
733 *endptr = (gchar*) nptr;
739 G_CONST_RETURN gchar*
740 g_strerror (gint errnum)
742 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
746 const char *msg_locale;
748 msg_locale = strerror (errnum);
749 if (g_get_charset (NULL))
753 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
756 /* Stick in the quark table so that we can return a static result
758 GQuark msg_quark = g_quark_from_string (msg_utf8);
761 return g_quark_to_string (msg_quark);
768 case E2BIG: return "argument list too long";
771 case EACCES: return "permission denied";
774 case EADDRINUSE: return "address already in use";
777 case EADDRNOTAVAIL: return "can't assign requested address";
780 case EADV: return "advertise error";
783 case EAFNOSUPPORT: return "address family not supported by protocol family";
786 case EAGAIN: return "try again";
789 case EALIGN: return "EALIGN";
792 case EALREADY: return "operation already in progress";
795 case EBADE: return "bad exchange descriptor";
798 case EBADF: return "bad file number";
801 case EBADFD: return "file descriptor in bad state";
804 case EBADMSG: return "not a data message";
807 case EBADR: return "bad request descriptor";
810 case EBADRPC: return "RPC structure is bad";
813 case EBADRQC: return "bad request code";
816 case EBADSLT: return "invalid slot";
819 case EBFONT: return "bad font file format";
822 case EBUSY: return "mount device busy";
825 case ECHILD: return "no children";
828 case ECHRNG: return "channel number out of range";
831 case ECOMM: return "communication error on send";
834 case ECONNABORTED: return "software caused connection abort";
837 case ECONNREFUSED: return "connection refused";
840 case ECONNRESET: return "connection reset by peer";
842 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
843 case EDEADLK: return "resource deadlock avoided";
846 case EDEADLOCK: return "resource deadlock avoided";
849 case EDESTADDRREQ: return "destination address required";
852 case EDIRTY: return "mounting a dirty fs w/o force";
855 case EDOM: return "math argument out of range";
858 case EDOTDOT: return "cross mount point";
861 case EDQUOT: return "disk quota exceeded";
864 case EDUPPKG: return "duplicate package name";
867 case EEXIST: return "file already exists";
870 case EFAULT: return "bad address in system call argument";
873 case EFBIG: return "file too large";
876 case EHOSTDOWN: return "host is down";
879 case EHOSTUNREACH: return "host is unreachable";
882 case EIDRM: return "identifier removed";
885 case EINIT: return "initialization error";
888 case EINPROGRESS: return "operation now in progress";
891 case EINTR: return "interrupted system call";
894 case EINVAL: return "invalid argument";
897 case EIO: return "I/O error";
900 case EISCONN: return "socket is already connected";
903 case EISDIR: return "is a directory";
906 case EISNAM: return "is a name file";
909 case ELBIN: return "ELBIN";
912 case EL2HLT: return "level 2 halted";
915 case EL2NSYNC: return "level 2 not synchronized";
918 case EL3HLT: return "level 3 halted";
921 case EL3RST: return "level 3 reset";
924 case ELIBACC: return "can not access a needed shared library";
927 case ELIBBAD: return "accessing a corrupted shared library";
930 case ELIBEXEC: return "can not exec a shared library directly";
933 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
936 case ELIBSCN: return ".lib section in a.out corrupted";
939 case ELNRNG: return "link number out of range";
942 case ELOOP: return "too many levels of symbolic links";
945 case EMFILE: return "too many open files";
948 case EMLINK: return "too many links";
951 case EMSGSIZE: return "message too long";
954 case EMULTIHOP: return "multihop attempted";
957 case ENAMETOOLONG: return "file name too long";
960 case ENAVAIL: return "not available";
963 case ENET: return "ENET";
966 case ENETDOWN: return "network is down";
969 case ENETRESET: return "network dropped connection on reset";
972 case ENETUNREACH: return "network is unreachable";
975 case ENFILE: return "file table overflow";
978 case ENOANO: return "anode table overflow";
980 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
981 case ENOBUFS: return "no buffer space available";
984 case ENOCSI: return "no CSI structure available";
987 case ENODATA: return "no data available";
990 case ENODEV: return "no such device";
993 case ENOENT: return "no such file or directory";
996 case ENOEXEC: return "exec format error";
999 case ENOLCK: return "no locks available";
1002 case ENOLINK: return "link has be severed";
1005 case ENOMEM: return "not enough memory";
1008 case ENOMSG: return "no message of desired type";
1011 case ENONET: return "machine is not on the network";
1014 case ENOPKG: return "package not installed";
1017 case ENOPROTOOPT: return "bad proocol option";
1020 case ENOSPC: return "no space left on device";
1023 case ENOSR: return "out of stream resources";
1026 case ENOSTR: return "not a stream device";
1029 case ENOSYM: return "unresolved symbol name";
1032 case ENOSYS: return "function not implemented";
1035 case ENOTBLK: return "block device required";
1038 case ENOTCONN: return "socket is not connected";
1041 case ENOTDIR: return "not a directory";
1044 case ENOTEMPTY: return "directory not empty";
1047 case ENOTNAM: return "not a name file";
1050 case ENOTSOCK: return "socket operation on non-socket";
1053 case ENOTTY: return "inappropriate device for ioctl";
1056 case ENOTUNIQ: return "name not unique on network";
1059 case ENXIO: return "no such device or address";
1062 case EOPNOTSUPP: return "operation not supported on socket";
1065 case EPERM: return "not owner";
1068 case EPFNOSUPPORT: return "protocol family not supported";
1071 case EPIPE: return "broken pipe";
1074 case EPROCLIM: return "too many processes";
1077 case EPROCUNAVAIL: return "bad procedure for program";
1079 #ifdef EPROGMISMATCH
1080 case EPROGMISMATCH: return "program version wrong";
1083 case EPROGUNAVAIL: return "RPC program not available";
1086 case EPROTO: return "protocol error";
1088 #ifdef EPROTONOSUPPORT
1089 case EPROTONOSUPPORT: return "protocol not suppored";
1092 case EPROTOTYPE: return "protocol wrong type for socket";
1095 case ERANGE: return "math result unrepresentable";
1097 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1098 case EREFUSED: return "EREFUSED";
1101 case EREMCHG: return "remote address changed";
1104 case EREMDEV: return "remote device";
1107 case EREMOTE: return "pathname hit remote file system";
1110 case EREMOTEIO: return "remote i/o error";
1112 #ifdef EREMOTERELEASE
1113 case EREMOTERELEASE: return "EREMOTERELEASE";
1116 case EROFS: return "read-only file system";
1119 case ERPCMISMATCH: return "RPC version is wrong";
1122 case ERREMOTE: return "object is remote";
1125 case ESHUTDOWN: return "can't send afer socket shutdown";
1127 #ifdef ESOCKTNOSUPPORT
1128 case ESOCKTNOSUPPORT: return "socket type not supported";
1131 case ESPIPE: return "invalid seek";
1134 case ESRCH: return "no such process";
1137 case ESRMNT: return "srmount error";
1140 case ESTALE: return "stale remote file handle";
1143 case ESUCCESS: return "Error 0";
1146 case ETIME: return "timer expired";
1149 case ETIMEDOUT: return "connection timed out";
1152 case ETOOMANYREFS: return "too many references: can't splice";
1155 case ETXTBSY: return "text file or pseudo-device busy";
1158 case EUCLEAN: return "structure needs cleaning";
1161 case EUNATCH: return "protocol driver not attached";
1164 case EUSERS: return "too many users";
1167 case EVERSION: return "version mismatch";
1169 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1170 case EWOULDBLOCK: return "operation would block";
1173 case EXDEV: return "cross-domain link";
1176 case EXFULL: return "message tables full";
1179 #else /* NO_SYS_ERRLIST */
1180 extern int sys_nerr;
1181 extern char *sys_errlist[];
1183 if ((errnum > 0) && (errnum <= sys_nerr))
1184 return sys_errlist [errnum];
1185 #endif /* NO_SYS_ERRLIST */
1187 msg = g_static_private_get (&msg_private);
1190 msg = g_new (gchar, 64);
1191 g_static_private_set (&msg_private, msg, g_free);
1194 _g_sprintf (msg, "unknown error (%d)", errnum);
1199 G_CONST_RETURN gchar*
1200 g_strsignal (gint signum)
1202 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1205 #ifdef HAVE_STRSIGNAL
1206 const char *msg_locale;
1208 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1209 extern const char *strsignal(int);
1211 /* this is declared differently (const) in string.h on BeOS */
1212 extern char *strsignal (int sig);
1213 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1214 msg_locale = strsignal (signum);
1215 if (g_get_charset (NULL))
1219 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1222 /* Stick in the quark table so that we can return a static result
1224 GQuark msg_quark = g_quark_from_string (msg_utf8);
1227 return g_quark_to_string (msg_quark);
1230 #elif NO_SYS_SIGLIST
1234 case SIGHUP: return "Hangup";
1237 case SIGINT: return "Interrupt";
1240 case SIGQUIT: return "Quit";
1243 case SIGILL: return "Illegal instruction";
1246 case SIGTRAP: return "Trace/breakpoint trap";
1249 case SIGABRT: return "IOT trap/Abort";
1252 case SIGBUS: return "Bus error";
1255 case SIGFPE: return "Floating point exception";
1258 case SIGKILL: return "Killed";
1261 case SIGUSR1: return "User defined signal 1";
1264 case SIGSEGV: return "Segmentation fault";
1267 case SIGUSR2: return "User defined signal 2";
1270 case SIGPIPE: return "Broken pipe";
1273 case SIGALRM: return "Alarm clock";
1276 case SIGTERM: return "Terminated";
1279 case SIGSTKFLT: return "Stack fault";
1282 case SIGCHLD: return "Child exited";
1285 case SIGCONT: return "Continued";
1288 case SIGSTOP: return "Stopped (signal)";
1291 case SIGTSTP: return "Stopped";
1294 case SIGTTIN: return "Stopped (tty input)";
1297 case SIGTTOU: return "Stopped (tty output)";
1300 case SIGURG: return "Urgent condition";
1303 case SIGXCPU: return "CPU time limit exceeded";
1306 case SIGXFSZ: return "File size limit exceeded";
1309 case SIGVTALRM: return "Virtual time alarm";
1312 case SIGPROF: return "Profile signal";
1315 case SIGWINCH: return "Window size changed";
1318 case SIGIO: return "Possible I/O";
1321 case SIGPWR: return "Power failure";
1324 case SIGUNUSED: return "Unused signal";
1327 #else /* NO_SYS_SIGLIST */
1329 #ifdef NO_SYS_SIGLIST_DECL
1330 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1333 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1334 #endif /* NO_SYS_SIGLIST */
1336 msg = g_static_private_get (&msg_private);
1339 msg = g_new (gchar, 64);
1340 g_static_private_set (&msg_private, msg, g_free);
1343 _g_sprintf (msg, "unknown signal (%d)", signum);
1348 /* Functions g_strlcpy and g_strlcat were originally developed by
1349 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1350 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1351 * for more information.
1355 /* Use the native ones, if available; they might be implemented in assembly */
1357 g_strlcpy (gchar *dest,
1361 g_return_val_if_fail (dest != NULL, 0);
1362 g_return_val_if_fail (src != NULL, 0);
1364 return strlcpy (dest, src, dest_size);
1368 g_strlcat (gchar *dest,
1372 g_return_val_if_fail (dest != NULL, 0);
1373 g_return_val_if_fail (src != NULL, 0);
1375 return strlcat (dest, src, dest_size);
1378 #else /* ! HAVE_STRLCPY */
1381 * Copy string src to buffer dest (of buffer size dest_size). At most
1382 * dest_size-1 characters will be copied. Always NUL terminates
1383 * (unless dest_size == 0). This function does NOT allocate memory.
1384 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1385 * Returns size of attempted result, strlen(src),
1386 * so if retval >= dest_size, truncation occurred.
1389 g_strlcpy (gchar *dest,
1393 register gchar *d = dest;
1394 register const gchar *s = src;
1395 register gsize n = dest_size;
1397 g_return_val_if_fail (dest != NULL, 0);
1398 g_return_val_if_fail (src != NULL, 0);
1400 /* Copy as many bytes as will fit */
1401 if (n != 0 && --n != 0)
1404 register gchar c = *s++;
1412 /* If not enough room in dest, add NUL and traverse rest of src */
1421 return s - src - 1; /* count does not include NUL */
1426 * Appends string src to buffer dest (of buffer size dest_size).
1427 * At most dest_size-1 characters will be copied.
1428 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1429 * This function does NOT allocate memory.
1430 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1431 * in the dest_size characters of dest to start with).
1432 * Returns size of attempted result, which is
1433 * MIN (dest_size, strlen (original dest)) + strlen (src),
1434 * so if retval >= dest_size, truncation occurred.
1437 g_strlcat (gchar *dest,
1441 register gchar *d = dest;
1442 register const gchar *s = src;
1443 register gsize bytes_left = dest_size;
1444 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1446 g_return_val_if_fail (dest != NULL, 0);
1447 g_return_val_if_fail (src != NULL, 0);
1449 /* Find the end of dst and adjust bytes left but don't go past end */
1450 while (*d != 0 && bytes_left-- != 0)
1453 bytes_left = dest_size - dlength;
1455 if (bytes_left == 0)
1456 return dlength + strlen (s);
1460 if (bytes_left != 1)
1469 return dlength + (s - src); /* count does not include NUL */
1471 #endif /* ! HAVE_STRLCPY */
1476 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1478 * Converts all upper case ASCII letters to lower case ASCII letters.
1480 * Return value: a newly-allocated string, with all the upper case
1481 * characters in @str converted to lower case, with
1482 * semantics that exactly match g_ascii_tolower(). (Note
1483 * that this is unlike the old g_strdown(), which modified
1484 * the string in place.)
1487 g_ascii_strdown (const gchar *str,
1492 g_return_val_if_fail (str != NULL, NULL);
1497 result = g_strndup (str, len);
1498 for (s = result; *s; s++)
1499 *s = g_ascii_tolower (*s);
1507 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1509 * Converts all lower case ASCII letters to upper case ASCII letters.
1511 * Return value: a newly allocated string, with all the lower case
1512 * characters in @str converted to upper case, with
1513 * semantics that exactly match g_ascii_toupper(). (Note
1514 * that this is unlike the old g_strup(), which modified
1515 * the string in place.)
1518 g_ascii_strup (const gchar *str,
1523 g_return_val_if_fail (str != NULL, NULL);
1528 result = g_strndup (str, len);
1529 for (s = result; *s; s++)
1530 *s = g_ascii_toupper (*s);
1537 * @string: the string to convert.
1539 * Converts a string to lower case.
1541 * Return value: the string
1543 * Deprecated: This function is totally broken for the reasons discussed in
1544 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1548 g_strdown (gchar *string)
1552 g_return_val_if_fail (string != NULL, NULL);
1554 s = (guchar *) string;
1563 return (gchar *) string;
1568 * @string: the string to convert.
1570 * Converts a string to upper case.
1572 * Return value: the string
1574 * Deprecated: This function is totally broken for the reasons discussed in
1575 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1578 g_strup (gchar *string)
1582 g_return_val_if_fail (string != NULL, NULL);
1584 s = (guchar *) string;
1593 return (gchar *) string;
1597 g_strreverse (gchar *string)
1599 g_return_val_if_fail (string != NULL, NULL);
1603 register gchar *h, *t;
1606 t = string + strlen (string) - 1;
1625 * @c: any character.
1627 * Convert a character to ASCII lower case.
1629 * Unlike the standard C library tolower() function, this only
1630 * recognizes standard ASCII letters and ignores the locale, returning
1631 * all non-ASCII characters unchanged, even if they are lower case
1632 * letters in a particular character set. Also unlike the standard
1633 * library function, this takes and returns a char, not an int, so
1634 * don't call it on %EOF but no need to worry about casting to #guchar
1635 * before passing a possibly non-ASCII character in.
1637 * Return value: the result of converting @c to lower case.
1638 * If @c is not an ASCII upper case letter,
1639 * @c is returned unchanged.
1642 g_ascii_tolower (gchar c)
1644 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1649 * @c: any character.
1651 * Convert a character to ASCII upper case.
1653 * Unlike the standard C library toupper() function, this only
1654 * recognizes standard ASCII letters and ignores the locale, returning
1655 * all non-ASCII characters unchanged, even if they are upper case
1656 * letters in a particular character set. Also unlike the standard
1657 * library function, this takes and returns a char, not an int, so
1658 * don't call it on %EOF but no need to worry about casting to #guchar
1659 * before passing a possibly non-ASCII character in.
1661 * Return value: the result of converting @c to upper case.
1662 * If @c is not an ASCII lower case letter,
1663 * @c is returned unchanged.
1666 g_ascii_toupper (gchar c)
1668 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1672 * g_ascii_digit_value:
1673 * @c: an ASCII character.
1675 * Determines the numeric value of a character as a decimal
1676 * digit. Differs from g_unichar_digit_value() because it takes
1677 * a char, so there's no worry about sign extension if characters
1680 * Return value: If @c is a decimal digit (according to
1681 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1684 g_ascii_digit_value (gchar c)
1686 if (g_ascii_isdigit (c))
1692 * g_ascii_xdigit_value:
1693 * @c: an ASCII character.
1695 * Determines the numeric value of a character as a hexidecimal
1696 * digit. Differs from g_unichar_xdigit_value() because it takes
1697 * a char, so there's no worry about sign extension if characters
1700 * Return value: If @c is a hex digit (according to
1701 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1704 g_ascii_xdigit_value (gchar c)
1706 if (c >= 'A' && c <= 'F')
1707 return c - 'A' + 10;
1708 if (c >= 'a' && c <= 'f')
1709 return c - 'a' + 10;
1710 return g_ascii_digit_value (c);
1714 * g_ascii_strcasecmp:
1715 * @s1: string to compare with @s2.
1716 * @s2: string to compare with @s1.
1718 * Compare two strings, ignoring the case of ASCII characters.
1720 * Unlike the BSD strcasecmp() function, this only recognizes standard
1721 * ASCII letters and ignores the locale, treating all non-ASCII
1722 * characters as if they are not letters.
1724 * Return value: an integer less than, equal to, or greater than
1725 * zero if @s1 is found, respectively, to be less than,
1726 * to match, or to be greater than @s2.
1729 g_ascii_strcasecmp (const gchar *s1,
1734 g_return_val_if_fail (s1 != NULL, 0);
1735 g_return_val_if_fail (s2 != NULL, 0);
1739 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1740 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1746 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1750 * g_ascii_strncasecmp:
1751 * @s1: string to compare with @s2.
1752 * @s2: string to compare with @s1.
1753 * @n: number of characters to compare.
1755 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1756 * characters after the first @n in each string.
1758 * Unlike the BSD strcasecmp() function, this only recognizes standard
1759 * ASCII letters and ignores the locale, treating all non-ASCII
1760 * characters as if they are not letters.
1762 * Return value: an integer less than, equal to, or greater than zero
1763 * if the first @n bytes of @s1 is found, respectively,
1764 * to be less than, to match, or to be greater than the
1765 * first @n bytes of @s2.
1768 g_ascii_strncasecmp (const gchar *s1,
1774 g_return_val_if_fail (s1 != NULL, 0);
1775 g_return_val_if_fail (s2 != NULL, 0);
1777 while (n && *s1 && *s2)
1780 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1781 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1788 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1796 * @s2: a string to compare with @s1.
1798 * A case-insensitive string comparison, corresponding to the standard
1799 * strcasecmp() function on platforms which support it.
1801 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1802 * or a positive value if @s1 > @s2.
1804 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1805 * deprecated and how to replace it.
1808 g_strcasecmp (const gchar *s1,
1811 #ifdef HAVE_STRCASECMP
1812 g_return_val_if_fail (s1 != NULL, 0);
1813 g_return_val_if_fail (s2 != NULL, 0);
1815 return strcasecmp (s1, s2);
1819 g_return_val_if_fail (s1 != NULL, 0);
1820 g_return_val_if_fail (s2 != NULL, 0);
1824 /* According to A. Cox, some platforms have islower's that
1825 * don't work right on non-uppercase
1827 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1828 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1834 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1841 * @s2: a string to compare with @s1.
1842 * @n: the maximum number of characters to compare.
1844 * A case-insensitive string comparison, corresponding to the standard
1845 * strncasecmp() function on platforms which support it.
1846 * It is similar to g_strcasecmp() except it only compares the first @n
1847 * characters of the strings.
1849 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1850 * or a positive value if @s1 > @s2.
1852 * Deprecated: The problem with g_strncasecmp() is that it does the
1853 * comparison by calling toupper()/tolower(). These functions are
1854 * locale-specific and operate on single bytes. However, it is impossible
1855 * to handle things correctly from an I18N standpoint by operating on
1856 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1857 * broken if your string is guaranteed to be ASCII, since it's
1858 * locale-sensitive, and it's broken if your string is localized, since
1859 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1862 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1863 * which only works on ASCII and is not locale-sensitive, and
1864 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1867 g_strncasecmp (const gchar *s1,
1871 #ifdef HAVE_STRNCASECMP
1872 return strncasecmp (s1, s2, n);
1876 g_return_val_if_fail (s1 != NULL, 0);
1877 g_return_val_if_fail (s2 != NULL, 0);
1879 while (n && *s1 && *s2)
1882 /* According to A. Cox, some platforms have islower's that
1883 * don't work right on non-uppercase
1885 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1886 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1893 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1900 g_strdelimit (gchar *string,
1901 const gchar *delimiters,
1906 g_return_val_if_fail (string != NULL, NULL);
1909 delimiters = G_STR_DELIMITERS;
1911 for (c = string; *c; c++)
1913 if (strchr (delimiters, *c))
1921 g_strcanon (gchar *string,
1922 const gchar *valid_chars,
1927 g_return_val_if_fail (string != NULL, NULL);
1928 g_return_val_if_fail (valid_chars != NULL, NULL);
1930 for (c = string; *c; c++)
1932 if (!strchr (valid_chars, *c))
1940 g_strcompress (const gchar *source)
1942 const gchar *p = source, *octal;
1943 gchar *dest = g_malloc (strlen (source) + 1);
1953 case '0': case '1': case '2': case '3': case '4':
1954 case '5': case '6': case '7':
1957 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1959 *q = (*q * 8) + (*p - '0');
1980 default: /* Also handles \" and \\ */
1995 g_strescape (const gchar *source,
1996 const gchar *exceptions)
2003 g_return_val_if_fail (source != NULL, NULL);
2005 p = (guchar *) source;
2006 /* Each source byte needs maximally four destination chars (\777) */
2007 q = dest = g_malloc (strlen (source) * 4 + 1);
2009 memset (excmap, 0, 256);
2012 guchar *e = (guchar *) exceptions;
2058 if ((*p < ' ') || (*p >= 0177))
2061 *q++ = '0' + (((*p) >> 6) & 07);
2062 *q++ = '0' + (((*p) >> 3) & 07);
2063 *q++ = '0' + ((*p) & 07);
2077 g_strchug (gchar *string)
2081 g_return_val_if_fail (string != NULL, NULL);
2083 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2086 g_memmove (string, start, strlen ((gchar *) start) + 1);
2092 g_strchomp (gchar *string)
2096 g_return_val_if_fail (string != NULL, NULL);
2098 len = strlen (string);
2101 if (g_ascii_isspace ((guchar) string[len]))
2112 * @string: a string to split.
2113 * @delimiter: a string which specifies the places at which to split the string.
2114 * The delimiter is not included in any of the resulting strings, unless
2115 * @max_tokens is reached.
2116 * @max_tokens: the maximum number of pieces to split @string into. If this is
2117 * less than 1, the string is split completely.
2119 * Splits a string into a maximum of @max_tokens pieces, using the given
2120 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2121 * to the last token.
2123 * As a special case, the result of splitting the empty string "" is an empty
2124 * vector, not a vector containing a single string. The reason for this
2125 * special case is that being able to represent a empty vector is typically
2126 * more useful than consistent handling of empty elements. If you do need
2127 * to represent empty elements, you'll need to check for the empty string
2128 * before calling g_strsplit().
2130 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2131 * g_strfreev() to free it.
2134 g_strsplit (const gchar *string,
2135 const gchar *delimiter,
2138 GSList *string_list = NULL, *slist;
2139 gchar **str_array, *s;
2141 const gchar *remainder;
2143 g_return_val_if_fail (string != NULL, NULL);
2144 g_return_val_if_fail (delimiter != NULL, NULL);
2145 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2148 max_tokens = G_MAXINT;
2151 s = strstr (remainder, delimiter);
2154 gsize delimiter_len = strlen (delimiter);
2156 while (--max_tokens && s)
2161 len = s - remainder;
2162 new_string = g_new (gchar, len + 1);
2163 strncpy (new_string, remainder, len);
2164 new_string[len] = 0;
2165 string_list = g_slist_prepend (string_list, new_string);
2167 remainder = s + delimiter_len;
2168 s = strstr (remainder, delimiter);
2174 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2177 str_array = g_new (gchar*, n + 1);
2179 str_array[n--] = NULL;
2180 for (slist = string_list; slist; slist = slist->next)
2181 str_array[n--] = slist->data;
2183 g_slist_free (string_list);
2189 g_strfreev (gchar **str_array)
2195 for(i = 0; str_array[i] != NULL; i++)
2196 g_free(str_array[i]);
2204 * @str_array: %NULL-terminated array of strings.
2206 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2207 * the new array should be freed by first freeing each string, then
2208 * the array itself. g_strfreev() does this for you. If called
2209 * on a %NULL value, g_strdupv() simply returns %NULL.
2211 * Return value: a new %NULL-terminated array of strings.
2214 g_strdupv (gchar **str_array)
2222 while (str_array[i])
2225 retval = g_new (gchar*, i + 1);
2228 while (str_array[i])
2230 retval[i] = g_strdup (str_array[i]);
2242 g_strjoinv (const gchar *separator,
2248 g_return_val_if_fail (str_array != NULL, NULL);
2250 if (separator == NULL)
2257 gsize separator_len;
2259 separator_len = strlen (separator);
2260 /* First part, getting length */
2261 len = 1 + strlen (str_array[0]);
2262 for (i = 1; str_array[i] != NULL; i++)
2263 len += strlen (str_array[i]);
2264 len += separator_len * (i - 1);
2266 /* Second part, building string */
2267 string = g_new (gchar, len);
2268 ptr = g_stpcpy (string, *str_array);
2269 for (i = 1; str_array[i] != NULL; i++)
2271 ptr = g_stpcpy (ptr, separator);
2272 ptr = g_stpcpy (ptr, str_array[i]);
2276 string = g_strdup ("");
2282 g_strjoin (const gchar *separator,
2288 gsize separator_len;
2291 if (separator == NULL)
2294 separator_len = strlen (separator);
2296 va_start (args, separator);
2298 s = va_arg (args, gchar*);
2302 /* First part, getting length */
2303 len = 1 + strlen (s);
2305 s = va_arg (args, gchar*);
2308 len += separator_len + strlen (s);
2309 s = va_arg (args, gchar*);
2313 /* Second part, building string */
2314 string = g_new (gchar, len);
2316 va_start (args, separator);
2318 s = va_arg (args, gchar*);
2319 ptr = g_stpcpy (string, s);
2321 s = va_arg (args, gchar*);
2324 ptr = g_stpcpy (ptr, separator);
2325 ptr = g_stpcpy (ptr, s);
2326 s = va_arg (args, gchar*);
2330 string = g_strdup ("");
2340 * @haystack: a string.
2341 * @haystack_len: the maximum length of @haystack.
2342 * @needle: the string to search for.
2344 * Searches the string @haystack for the first occurrence
2345 * of the string @needle, limiting the length of the search
2348 * Return value: a pointer to the found occurrence, or
2349 * %NULL if not found.
2352 g_strstr_len (const gchar *haystack,
2353 gssize haystack_len,
2354 const gchar *needle)
2356 g_return_val_if_fail (haystack != NULL, NULL);
2357 g_return_val_if_fail (needle != NULL, NULL);
2359 if (haystack_len < 0)
2360 return strstr (haystack, needle);
2363 const gchar *p = haystack;
2364 gsize needle_len = strlen (needle);
2368 if (needle_len == 0)
2369 return (gchar *)haystack;
2371 if (haystack_len < needle_len)
2374 end = haystack + haystack_len - needle_len;
2376 while (*p && p <= end)
2378 for (i = 0; i < needle_len; i++)
2379 if (p[i] != needle[i])
2394 * @haystack: a nul-terminated string.
2395 * @needle: the nul-terminated string to search for.
2397 * Searches the string @haystack for the last occurrence
2398 * of the string @needle.
2400 * Return value: a pointer to the found occurrence, or
2401 * %NULL if not found.
2404 g_strrstr (const gchar *haystack,
2405 const gchar *needle)
2412 g_return_val_if_fail (haystack != NULL, NULL);
2413 g_return_val_if_fail (needle != NULL, NULL);
2415 needle_len = strlen (needle);
2416 haystack_len = strlen (haystack);
2418 if (needle_len == 0)
2419 return (gchar *)haystack;
2421 if (haystack_len < needle_len)
2424 p = haystack + haystack_len - needle_len;
2426 while (p >= haystack)
2428 for (i = 0; i < needle_len; i++)
2429 if (p[i] != needle[i])
2443 * @haystack: a nul-terminated string.
2444 * @haystack_len: the maximum length of @haystack.
2445 * @needle: the nul-terminated string to search for.
2447 * Searches the string @haystack for the last occurrence
2448 * of the string @needle, limiting the length of the search
2451 * Return value: a pointer to the found occurrence, or
2452 * %NULL if not found.
2455 g_strrstr_len (const gchar *haystack,
2456 gssize haystack_len,
2457 const gchar *needle)
2459 g_return_val_if_fail (haystack != NULL, NULL);
2460 g_return_val_if_fail (needle != NULL, NULL);
2462 if (haystack_len < 0)
2463 return g_strrstr (haystack, needle);
2466 gsize needle_len = strlen (needle);
2467 const gchar *haystack_max = haystack + haystack_len;
2468 const gchar *p = haystack;
2471 while (p < haystack_max && *p)
2474 if (p < haystack + needle_len)
2479 while (p >= haystack)
2481 for (i = 0; i < needle_len; i++)
2482 if (p[i] != needle[i])
2498 * @str: a nul-terminated string.
2499 * @suffix: the nul-terminated suffix to look for.
2501 * Looks whether the string @str ends with @suffix.
2503 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2508 g_str_has_suffix (const gchar *str,
2509 const gchar *suffix)
2514 g_return_val_if_fail (str != NULL, FALSE);
2515 g_return_val_if_fail (suffix != NULL, FALSE);
2517 str_len = strlen (str);
2518 suffix_len = strlen (suffix);
2520 if (str_len < suffix_len)
2523 return strcmp (str + str_len - suffix_len, suffix) == 0;
2528 * @str: a nul-terminated string.
2529 * @prefix: the nul-terminated prefix to look for.
2531 * Looks whether the string @str begins with @prefix.
2533 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2538 g_str_has_prefix (const gchar *str,
2539 const gchar *prefix)
2544 g_return_val_if_fail (str != NULL, FALSE);
2545 g_return_val_if_fail (prefix != NULL, FALSE);
2547 str_len = strlen (str);
2548 prefix_len = strlen (prefix);
2550 if (str_len < prefix_len)
2553 return strncmp (str, prefix, prefix_len) == 0;