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)
90 length = strlen (str) + 1;
91 new_str = g_new (char, length);
92 memcpy (new_str, str, length);
101 g_memdup (gconstpointer mem,
108 new_mem = g_malloc (byte_size);
109 memcpy (new_mem, mem, byte_size);
118 g_strndup (const gchar *str,
125 new_str = g_new (gchar, n + 1);
126 strncpy (new_str, str, n);
136 g_strnfill (gsize length,
141 str = g_new (gchar, length + 1);
142 memset (str, (guchar)fill_char, length);
150 * @dest: destination buffer.
151 * @src: source string.
153 * Copies a nul-terminated string into the dest buffer, include the
154 * trailing nul, and return a pointer to the trailing nul byte.
155 * This is useful for concatenating multiple strings together
156 * without having to repeatedly scan for the end.
158 * Return value: a pointer to trailing nul byte.
161 g_stpcpy (gchar *dest,
165 g_return_val_if_fail (dest != NULL, NULL);
166 g_return_val_if_fail (src != NULL, NULL);
167 return stpcpy (dest, src);
169 register gchar *d = dest;
170 register const gchar *s = src;
172 g_return_val_if_fail (dest != NULL, NULL);
173 g_return_val_if_fail (src != NULL, NULL);
176 while (*s++ != '\0');
183 g_strdup_vprintf (const gchar *format,
187 #ifdef HAVE_VASPRINTF
189 len = _g_vasprintf (&buffer, format, args1);
192 else if (!g_mem_is_system_malloc ())
194 gchar *buffer1 = g_strndup (buffer, len);
201 G_VA_COPY (args2, args1);
203 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
205 _g_vsprintf (buffer, format, args2);
212 g_strdup_printf (const gchar *format,
218 va_start (args, format);
219 buffer = g_strdup_vprintf (format, args);
226 g_strconcat (const gchar *string1, ...)
234 g_return_val_if_fail (string1 != NULL, NULL);
236 l = 1 + strlen (string1);
237 va_start (args, string1);
238 s = va_arg (args, gchar*);
242 s = va_arg (args, gchar*);
246 concat = g_new (gchar, l);
249 ptr = g_stpcpy (ptr, string1);
250 va_start (args, string1);
251 s = va_arg (args, gchar*);
254 ptr = g_stpcpy (ptr, s);
255 s = va_arg (args, gchar*);
264 * @nptr: the string to convert to a numeric value.
265 * @endptr: if non-%NULL, it returns the character after
266 * the last character used in the conversion.
268 * Converts a string to a #gdouble value.
269 * It calls the standard strtod() function to handle the conversion, but
270 * if the string is not completely converted it attempts the conversion
271 * again with g_ascii_strtod(), and returns the best match.
273 * This function should seldomly be used. The normal situation when reading
274 * numbers not for human consumption is to use g_ascii_strtod(). Only when
275 * you know that you must expect both locale formatted and C formatted numbers
276 * should you use this. Make sure that you don't pass strings such as comma
277 * separated lists of values, since the commas may be interpreted as a decimal
278 * point in some locales, causing unexpected results.
280 * Return value: the #gdouble value.
283 g_strtod (const gchar *nptr,
291 g_return_val_if_fail (nptr != NULL, 0);
296 val_1 = strtod (nptr, &fail_pos_1);
298 if (fail_pos_1 && fail_pos_1[0] != 0)
299 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
301 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
304 *endptr = fail_pos_1;
310 *endptr = fail_pos_2;
317 * @nptr: the string to convert to a numeric value.
318 * @endptr: if non-%NULL, it returns the character after
319 * the last character used in the conversion.
321 * Converts a string to a #gdouble value.
322 * This function behaves like the standard strtod() function
323 * does in the C locale. It does this without actually
324 * changing the current locale, since that would not be
327 * This function is typically used when reading configuration
328 * files or other non-user input that should be locale independent.
329 * To handle input from the user you should normally use the
330 * locale-sensitive system strtod() function.
332 * To convert from a #gdouble to a string in a locale-insensitive
333 * way, use g_ascii_dtostr().
335 * If the correct value would cause overflow, plus or minus %HUGE_VAL
336 * is returned (according to the sign of the value), and %ERANGE is
337 * stored in %errno. If the correct value would cause underflow,
338 * zero is returned and %ERANGE is stored in %errno.
340 * This function resets %errno before calling strtod() so that
341 * you can reliably detect overflow and underflow.
343 * Return value: the #gdouble value.
346 g_ascii_strtod (const gchar *nptr,
351 struct lconv *locale_data;
352 const char *decimal_point;
353 int decimal_point_len;
354 const char *p, *decimal_point_pos;
355 const char *end = NULL; /* Silence gcc */
357 g_return_val_if_fail (nptr != NULL, 0);
361 locale_data = localeconv ();
362 decimal_point = locale_data->decimal_point;
363 decimal_point_len = strlen (decimal_point);
365 g_assert (decimal_point_len != 0);
367 decimal_point_pos = NULL;
368 if (decimal_point[0] != '.' ||
369 decimal_point[1] != 0)
372 /* Skip leading space */
373 while (g_ascii_isspace (*p))
376 /* Skip leading optional sign */
377 if (*p == '+' || *p == '-')
381 (p[1] == 'x' || p[1] == 'X'))
384 /* HEX - find the (optional) decimal point */
386 while (g_ascii_isxdigit (*p))
391 decimal_point_pos = p++;
393 while (g_ascii_isxdigit (*p))
396 if (*p == 'p' || *p == 'P')
398 if (*p == '+' || *p == '-')
400 while (g_ascii_isdigit (*p))
407 while (g_ascii_isdigit (*p))
412 decimal_point_pos = p++;
414 while (g_ascii_isdigit (*p))
417 if (*p == 'e' || *p == 'E')
419 if (*p == '+' || *p == '-')
421 while (g_ascii_isdigit (*p))
426 /* For the other cases, we need not convert the decimal point */
429 /* Set errno to zero, so that we can distinguish zero results
433 if (decimal_point_pos)
437 /* We need to convert the '.' to the locale specific decimal point */
438 copy = g_malloc (end - nptr + 1 + decimal_point_len);
441 memcpy (c, nptr, decimal_point_pos - nptr);
442 c += decimal_point_pos - nptr;
443 memcpy (c, decimal_point, decimal_point_len);
444 c += decimal_point_len;
445 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
446 c += end - (decimal_point_pos + 1);
449 val = strtod (copy, &fail_pos);
453 if (fail_pos > decimal_point_pos)
454 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
456 fail_pos = (char *)nptr + (fail_pos - copy);
463 val = strtod (nptr, &fail_pos);
474 * @buffer: A buffer to place the resulting string in
475 * @buf_len: The length of the buffer.
476 * @d: The #gdouble to convert
478 * Converts a #gdouble to a string, using the '.' as
481 * This functions generates enough precision that converting
482 * the string back using g_ascii_strtod() gives the same machine-number
483 * (on machines with IEEE compatible 64bit doubles). It is
484 * guaranteed that the size of the resulting string will never
485 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
487 * Return value: The pointer to the buffer with the converted string.
490 g_ascii_dtostr (gchar *buffer,
494 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
499 * @buffer: A buffer to place the resulting string in
500 * @buf_len: The length of the buffer.
501 * @format: The printf()-style format to use for the
502 * code to use for converting.
503 * @d: The #gdouble to convert
505 * Converts a #gdouble to a string, using the '.' as
506 * decimal point. To format the number you pass in
507 * a printf()-style format string. Allowed conversion
508 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
510 * If you just want to want to serialize the value into a
511 * string, use g_ascii_dtostr().
513 * Return value: The pointer to the buffer with the converted string.
516 g_ascii_formatd (gchar *buffer,
521 struct lconv *locale_data;
522 const char *decimal_point;
523 int decimal_point_len;
528 g_return_val_if_fail (buffer != NULL, NULL);
529 g_return_val_if_fail (format[0] == '%', NULL);
530 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
532 format_char = format[strlen (format) - 1];
534 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
535 format_char == 'f' || format_char == 'F' ||
536 format_char == 'g' || format_char == 'G',
539 if (format[0] != '%')
542 if (strpbrk (format + 1, "'l%"))
545 if (!(format_char == 'e' || format_char == 'E' ||
546 format_char == 'f' || format_char == 'F' ||
547 format_char == 'g' || format_char == 'G'))
551 _g_snprintf (buffer, buf_len, format, d);
553 locale_data = localeconv ();
554 decimal_point = locale_data->decimal_point;
555 decimal_point_len = strlen (decimal_point);
557 g_assert (decimal_point_len != 0);
559 if (decimal_point[0] != '.' ||
560 decimal_point[1] != 0)
564 if (*p == '+' || *p == '-')
567 while (isdigit ((guchar)*p))
570 if (strncmp (p, decimal_point, decimal_point_len) == 0)
574 if (decimal_point_len > 1) {
575 rest_len = strlen (p + (decimal_point_len-1));
576 memmove (p, p + (decimal_point_len-1),
589 * @nptr: the string to convert to a numeric value.
590 * @endptr: if non-%NULL, it returns the character after
591 * the last character used in the conversion.
592 * @base: to be used for the conversion, 2..36 or 0
594 * Converts a string to a #guint64 value.
595 * This function behaves like the standard strtoull() function
596 * does in the C locale. It does this without actually
597 * changing the current locale, since that would not be
600 * This function is typically used when reading configuration
601 * files or other non-user input that should be locale independent.
602 * To handle input from the user you should normally use the
603 * locale-sensitive system strtoull() function.
605 * If the correct value would cause overflow, %G_MAXUINT64
606 * is returned, and %ERANGE is stored in %errno.
608 * Return value: the #guint64 value.
613 g_ascii_strtoull (const gchar *nptr,
617 /* this code is based on on the strtol(3) code from GNU libc released under
618 * the GNU Lesser General Public License.
620 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
621 * Free Software Foundation, Inc.
623 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
624 (c) == '\r' || (c) == '\t' || (c) == '\v')
625 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
626 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
627 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
628 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
629 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
630 gboolean negative, overflow;
634 const gchar *s, *save;
637 g_return_val_if_fail (nptr != NULL, 0);
639 if (base == 1 || base > 36)
647 /* Skip white space. */
653 /* Check for a sign. */
663 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
666 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
677 /* Save the pointer so we can check later if anything happened. */
679 cutoff = G_MAXUINT64 / base;
680 cutlim = G_MAXUINT64 % base;
687 if (c >= '0' && c <= '9')
689 else if (ISALPHA (c))
690 c = TOUPPER (c) - 'A' + 10;
695 /* Check for overflow. */
696 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
705 /* Check if anything actually happened. */
709 /* Store in ENDPTR the address of one character
710 past the last character we converted. */
712 *endptr = (gchar*) s;
720 /* Return the result of the appropriate sign. */
721 return negative ? -ui64 : ui64;
724 /* We must handle a special case here: the base is 0 or 16 and the
725 first two characters are '0' and 'x', but the rest are no
726 hexadecimal digits. This is no error case. We return 0 and
727 ENDPTR points to the `x`. */
730 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
732 *endptr = (gchar*) &save[-1];
734 /* There was no number to convert. */
735 *endptr = (gchar*) nptr;
741 G_CONST_RETURN gchar*
742 g_strerror (gint errnum)
744 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
748 const char *msg_locale;
750 msg_locale = strerror (errnum);
751 if (g_get_charset (NULL))
755 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
758 /* Stick in the quark table so that we can return a static result
760 GQuark msg_quark = g_quark_from_string (msg_utf8);
763 return g_quark_to_string (msg_quark);
770 case E2BIG: return "argument list too long";
773 case EACCES: return "permission denied";
776 case EADDRINUSE: return "address already in use";
779 case EADDRNOTAVAIL: return "can't assign requested address";
782 case EADV: return "advertise error";
785 case EAFNOSUPPORT: return "address family not supported by protocol family";
788 case EAGAIN: return "try again";
791 case EALIGN: return "EALIGN";
794 case EALREADY: return "operation already in progress";
797 case EBADE: return "bad exchange descriptor";
800 case EBADF: return "bad file number";
803 case EBADFD: return "file descriptor in bad state";
806 case EBADMSG: return "not a data message";
809 case EBADR: return "bad request descriptor";
812 case EBADRPC: return "RPC structure is bad";
815 case EBADRQC: return "bad request code";
818 case EBADSLT: return "invalid slot";
821 case EBFONT: return "bad font file format";
824 case EBUSY: return "mount device busy";
827 case ECHILD: return "no children";
830 case ECHRNG: return "channel number out of range";
833 case ECOMM: return "communication error on send";
836 case ECONNABORTED: return "software caused connection abort";
839 case ECONNREFUSED: return "connection refused";
842 case ECONNRESET: return "connection reset by peer";
844 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
845 case EDEADLK: return "resource deadlock avoided";
848 case EDEADLOCK: return "resource deadlock avoided";
851 case EDESTADDRREQ: return "destination address required";
854 case EDIRTY: return "mounting a dirty fs w/o force";
857 case EDOM: return "math argument out of range";
860 case EDOTDOT: return "cross mount point";
863 case EDQUOT: return "disk quota exceeded";
866 case EDUPPKG: return "duplicate package name";
869 case EEXIST: return "file already exists";
872 case EFAULT: return "bad address in system call argument";
875 case EFBIG: return "file too large";
878 case EHOSTDOWN: return "host is down";
881 case EHOSTUNREACH: return "host is unreachable";
884 case EIDRM: return "identifier removed";
887 case EINIT: return "initialization error";
890 case EINPROGRESS: return "operation now in progress";
893 case EINTR: return "interrupted system call";
896 case EINVAL: return "invalid argument";
899 case EIO: return "I/O error";
902 case EISCONN: return "socket is already connected";
905 case EISDIR: return "is a directory";
908 case EISNAM: return "is a name file";
911 case ELBIN: return "ELBIN";
914 case EL2HLT: return "level 2 halted";
917 case EL2NSYNC: return "level 2 not synchronized";
920 case EL3HLT: return "level 3 halted";
923 case EL3RST: return "level 3 reset";
926 case ELIBACC: return "can not access a needed shared library";
929 case ELIBBAD: return "accessing a corrupted shared library";
932 case ELIBEXEC: return "can not exec a shared library directly";
935 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
938 case ELIBSCN: return ".lib section in a.out corrupted";
941 case ELNRNG: return "link number out of range";
944 case ELOOP: return "too many levels of symbolic links";
947 case EMFILE: return "too many open files";
950 case EMLINK: return "too many links";
953 case EMSGSIZE: return "message too long";
956 case EMULTIHOP: return "multihop attempted";
959 case ENAMETOOLONG: return "file name too long";
962 case ENAVAIL: return "not available";
965 case ENET: return "ENET";
968 case ENETDOWN: return "network is down";
971 case ENETRESET: return "network dropped connection on reset";
974 case ENETUNREACH: return "network is unreachable";
977 case ENFILE: return "file table overflow";
980 case ENOANO: return "anode table overflow";
982 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
983 case ENOBUFS: return "no buffer space available";
986 case ENOCSI: return "no CSI structure available";
989 case ENODATA: return "no data available";
992 case ENODEV: return "no such device";
995 case ENOENT: return "no such file or directory";
998 case ENOEXEC: return "exec format error";
1001 case ENOLCK: return "no locks available";
1004 case ENOLINK: return "link has be severed";
1007 case ENOMEM: return "not enough memory";
1010 case ENOMSG: return "no message of desired type";
1013 case ENONET: return "machine is not on the network";
1016 case ENOPKG: return "package not installed";
1019 case ENOPROTOOPT: return "bad proocol option";
1022 case ENOSPC: return "no space left on device";
1025 case ENOSR: return "out of stream resources";
1028 case ENOSTR: return "not a stream device";
1031 case ENOSYM: return "unresolved symbol name";
1034 case ENOSYS: return "function not implemented";
1037 case ENOTBLK: return "block device required";
1040 case ENOTCONN: return "socket is not connected";
1043 case ENOTDIR: return "not a directory";
1046 case ENOTEMPTY: return "directory not empty";
1049 case ENOTNAM: return "not a name file";
1052 case ENOTSOCK: return "socket operation on non-socket";
1055 case ENOTTY: return "inappropriate device for ioctl";
1058 case ENOTUNIQ: return "name not unique on network";
1061 case ENXIO: return "no such device or address";
1064 case EOPNOTSUPP: return "operation not supported on socket";
1067 case EPERM: return "not owner";
1070 case EPFNOSUPPORT: return "protocol family not supported";
1073 case EPIPE: return "broken pipe";
1076 case EPROCLIM: return "too many processes";
1079 case EPROCUNAVAIL: return "bad procedure for program";
1081 #ifdef EPROGMISMATCH
1082 case EPROGMISMATCH: return "program version wrong";
1085 case EPROGUNAVAIL: return "RPC program not available";
1088 case EPROTO: return "protocol error";
1090 #ifdef EPROTONOSUPPORT
1091 case EPROTONOSUPPORT: return "protocol not suppored";
1094 case EPROTOTYPE: return "protocol wrong type for socket";
1097 case ERANGE: return "math result unrepresentable";
1099 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1100 case EREFUSED: return "EREFUSED";
1103 case EREMCHG: return "remote address changed";
1106 case EREMDEV: return "remote device";
1109 case EREMOTE: return "pathname hit remote file system";
1112 case EREMOTEIO: return "remote i/o error";
1114 #ifdef EREMOTERELEASE
1115 case EREMOTERELEASE: return "EREMOTERELEASE";
1118 case EROFS: return "read-only file system";
1121 case ERPCMISMATCH: return "RPC version is wrong";
1124 case ERREMOTE: return "object is remote";
1127 case ESHUTDOWN: return "can't send afer socket shutdown";
1129 #ifdef ESOCKTNOSUPPORT
1130 case ESOCKTNOSUPPORT: return "socket type not supported";
1133 case ESPIPE: return "invalid seek";
1136 case ESRCH: return "no such process";
1139 case ESRMNT: return "srmount error";
1142 case ESTALE: return "stale remote file handle";
1145 case ESUCCESS: return "Error 0";
1148 case ETIME: return "timer expired";
1151 case ETIMEDOUT: return "connection timed out";
1154 case ETOOMANYREFS: return "too many references: can't splice";
1157 case ETXTBSY: return "text file or pseudo-device busy";
1160 case EUCLEAN: return "structure needs cleaning";
1163 case EUNATCH: return "protocol driver not attached";
1166 case EUSERS: return "too many users";
1169 case EVERSION: return "version mismatch";
1171 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1172 case EWOULDBLOCK: return "operation would block";
1175 case EXDEV: return "cross-domain link";
1178 case EXFULL: return "message tables full";
1181 #else /* NO_SYS_ERRLIST */
1182 extern int sys_nerr;
1183 extern char *sys_errlist[];
1185 if ((errnum > 0) && (errnum <= sys_nerr))
1186 return sys_errlist [errnum];
1187 #endif /* NO_SYS_ERRLIST */
1189 msg = g_static_private_get (&msg_private);
1192 msg = g_new (gchar, 64);
1193 g_static_private_set (&msg_private, msg, g_free);
1196 _g_sprintf (msg, "unknown error (%d)", errnum);
1201 G_CONST_RETURN gchar*
1202 g_strsignal (gint signum)
1204 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1207 #ifdef HAVE_STRSIGNAL
1208 const char *msg_locale;
1210 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1211 extern const char *strsignal(int);
1213 /* this is declared differently (const) in string.h on BeOS */
1214 extern char *strsignal (int sig);
1215 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1216 msg_locale = strsignal (signum);
1217 if (g_get_charset (NULL))
1221 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1224 /* Stick in the quark table so that we can return a static result
1226 GQuark msg_quark = g_quark_from_string (msg_utf8);
1229 return g_quark_to_string (msg_quark);
1232 #elif NO_SYS_SIGLIST
1236 case SIGHUP: return "Hangup";
1239 case SIGINT: return "Interrupt";
1242 case SIGQUIT: return "Quit";
1245 case SIGILL: return "Illegal instruction";
1248 case SIGTRAP: return "Trace/breakpoint trap";
1251 case SIGABRT: return "IOT trap/Abort";
1254 case SIGBUS: return "Bus error";
1257 case SIGFPE: return "Floating point exception";
1260 case SIGKILL: return "Killed";
1263 case SIGUSR1: return "User defined signal 1";
1266 case SIGSEGV: return "Segmentation fault";
1269 case SIGUSR2: return "User defined signal 2";
1272 case SIGPIPE: return "Broken pipe";
1275 case SIGALRM: return "Alarm clock";
1278 case SIGTERM: return "Terminated";
1281 case SIGSTKFLT: return "Stack fault";
1284 case SIGCHLD: return "Child exited";
1287 case SIGCONT: return "Continued";
1290 case SIGSTOP: return "Stopped (signal)";
1293 case SIGTSTP: return "Stopped";
1296 case SIGTTIN: return "Stopped (tty input)";
1299 case SIGTTOU: return "Stopped (tty output)";
1302 case SIGURG: return "Urgent condition";
1305 case SIGXCPU: return "CPU time limit exceeded";
1308 case SIGXFSZ: return "File size limit exceeded";
1311 case SIGVTALRM: return "Virtual time alarm";
1314 case SIGPROF: return "Profile signal";
1317 case SIGWINCH: return "Window size changed";
1320 case SIGIO: return "Possible I/O";
1323 case SIGPWR: return "Power failure";
1326 case SIGUNUSED: return "Unused signal";
1329 #else /* NO_SYS_SIGLIST */
1331 #ifdef NO_SYS_SIGLIST_DECL
1332 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1335 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1336 #endif /* NO_SYS_SIGLIST */
1338 msg = g_static_private_get (&msg_private);
1341 msg = g_new (gchar, 64);
1342 g_static_private_set (&msg_private, msg, g_free);
1345 _g_sprintf (msg, "unknown signal (%d)", signum);
1350 /* Functions g_strlcpy and g_strlcat were originally developed by
1351 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1352 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1353 * for more information.
1357 /* Use the native ones, if available; they might be implemented in assembly */
1359 g_strlcpy (gchar *dest,
1363 g_return_val_if_fail (dest != NULL, 0);
1364 g_return_val_if_fail (src != NULL, 0);
1366 return strlcpy (dest, src, dest_size);
1370 g_strlcat (gchar *dest,
1374 g_return_val_if_fail (dest != NULL, 0);
1375 g_return_val_if_fail (src != NULL, 0);
1377 return strlcat (dest, src, dest_size);
1380 #else /* ! HAVE_STRLCPY */
1383 * Copy string src to buffer dest (of buffer size dest_size). At most
1384 * dest_size-1 characters will be copied. Always NUL terminates
1385 * (unless dest_size == 0). This function does NOT allocate memory.
1386 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1387 * Returns size of attempted result, strlen(src),
1388 * so if retval >= dest_size, truncation occurred.
1391 g_strlcpy (gchar *dest,
1395 register gchar *d = dest;
1396 register const gchar *s = src;
1397 register gsize n = dest_size;
1399 g_return_val_if_fail (dest != NULL, 0);
1400 g_return_val_if_fail (src != NULL, 0);
1402 /* Copy as many bytes as will fit */
1403 if (n != 0 && --n != 0)
1406 register gchar c = *s++;
1414 /* If not enough room in dest, add NUL and traverse rest of src */
1423 return s - src - 1; /* count does not include NUL */
1428 * Appends string src to buffer dest (of buffer size dest_size).
1429 * At most dest_size-1 characters will be copied.
1430 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1431 * This function does NOT allocate memory.
1432 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1433 * in the dest_size characters of dest to start with).
1434 * Returns size of attempted result, which is
1435 * MIN (dest_size, strlen (original dest)) + strlen (src),
1436 * so if retval >= dest_size, truncation occurred.
1439 g_strlcat (gchar *dest,
1443 register gchar *d = dest;
1444 register const gchar *s = src;
1445 register gsize bytes_left = dest_size;
1446 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1448 g_return_val_if_fail (dest != NULL, 0);
1449 g_return_val_if_fail (src != NULL, 0);
1451 /* Find the end of dst and adjust bytes left but don't go past end */
1452 while (*d != 0 && bytes_left-- != 0)
1455 bytes_left = dest_size - dlength;
1457 if (bytes_left == 0)
1458 return dlength + strlen (s);
1462 if (bytes_left != 1)
1471 return dlength + (s - src); /* count does not include NUL */
1473 #endif /* ! HAVE_STRLCPY */
1478 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1480 * Converts all upper case ASCII letters to lower case ASCII letters.
1482 * Return value: a newly-allocated string, with all the upper case
1483 * characters in @str converted to lower case, with
1484 * semantics that exactly match g_ascii_tolower(). (Note
1485 * that this is unlike the old g_strdown(), which modified
1486 * the string in place.)
1489 g_ascii_strdown (const gchar *str,
1494 g_return_val_if_fail (str != NULL, NULL);
1499 result = g_strndup (str, len);
1500 for (s = result; *s; s++)
1501 *s = g_ascii_tolower (*s);
1509 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1511 * Converts all lower case ASCII letters to upper case ASCII letters.
1513 * Return value: a newly allocated string, with all the lower case
1514 * characters in @str converted to upper case, with
1515 * semantics that exactly match g_ascii_toupper(). (Note
1516 * that this is unlike the old g_strup(), which modified
1517 * the string in place.)
1520 g_ascii_strup (const gchar *str,
1525 g_return_val_if_fail (str != NULL, NULL);
1530 result = g_strndup (str, len);
1531 for (s = result; *s; s++)
1532 *s = g_ascii_toupper (*s);
1539 * @string: the string to convert.
1541 * Converts a string to lower case.
1543 * Return value: the string
1545 * Deprecated: This function is totally broken for the reasons discussed in
1546 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1550 g_strdown (gchar *string)
1554 g_return_val_if_fail (string != NULL, NULL);
1556 s = (guchar *) string;
1565 return (gchar *) string;
1570 * @string: the string to convert.
1572 * Converts a string to upper case.
1574 * Return value: the string
1576 * Deprecated: This function is totally broken for the reasons discussed in
1577 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1580 g_strup (gchar *string)
1584 g_return_val_if_fail (string != NULL, NULL);
1586 s = (guchar *) string;
1595 return (gchar *) string;
1599 g_strreverse (gchar *string)
1601 g_return_val_if_fail (string != NULL, NULL);
1605 register gchar *h, *t;
1608 t = string + strlen (string) - 1;
1627 * @c: any character.
1629 * Convert a character to ASCII lower case.
1631 * Unlike the standard C library tolower() function, this only
1632 * recognizes standard ASCII letters and ignores the locale, returning
1633 * all non-ASCII characters unchanged, even if they are lower case
1634 * letters in a particular character set. Also unlike the standard
1635 * library function, this takes and returns a char, not an int, so
1636 * don't call it on %EOF but no need to worry about casting to #guchar
1637 * before passing a possibly non-ASCII character in.
1639 * Return value: the result of converting @c to lower case.
1640 * If @c is not an ASCII upper case letter,
1641 * @c is returned unchanged.
1644 g_ascii_tolower (gchar c)
1646 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1651 * @c: any character.
1653 * Convert a character to ASCII upper case.
1655 * Unlike the standard C library toupper() function, this only
1656 * recognizes standard ASCII letters and ignores the locale, returning
1657 * all non-ASCII characters unchanged, even if they are upper case
1658 * letters in a particular character set. Also unlike the standard
1659 * library function, this takes and returns a char, not an int, so
1660 * don't call it on %EOF but no need to worry about casting to #guchar
1661 * before passing a possibly non-ASCII character in.
1663 * Return value: the result of converting @c to upper case.
1664 * If @c is not an ASCII lower case letter,
1665 * @c is returned unchanged.
1668 g_ascii_toupper (gchar c)
1670 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1674 * g_ascii_digit_value:
1675 * @c: an ASCII character.
1677 * Determines the numeric value of a character as a decimal
1678 * digit. Differs from g_unichar_digit_value() because it takes
1679 * a char, so there's no worry about sign extension if characters
1682 * Return value: If @c is a decimal digit (according to
1683 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1686 g_ascii_digit_value (gchar c)
1688 if (g_ascii_isdigit (c))
1694 * g_ascii_xdigit_value:
1695 * @c: an ASCII character.
1697 * Determines the numeric value of a character as a hexidecimal
1698 * digit. Differs from g_unichar_xdigit_value() because it takes
1699 * a char, so there's no worry about sign extension if characters
1702 * Return value: If @c is a hex digit (according to
1703 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1706 g_ascii_xdigit_value (gchar c)
1708 if (c >= 'A' && c <= 'F')
1709 return c - 'A' + 10;
1710 if (c >= 'a' && c <= 'f')
1711 return c - 'a' + 10;
1712 return g_ascii_digit_value (c);
1716 * g_ascii_strcasecmp:
1717 * @s1: string to compare with @s2.
1718 * @s2: string to compare with @s1.
1720 * Compare two strings, ignoring the case of ASCII characters.
1722 * Unlike the BSD strcasecmp() function, this only recognizes standard
1723 * ASCII letters and ignores the locale, treating all non-ASCII
1724 * characters as if they are not letters.
1726 * Return value: an integer less than, equal to, or greater than
1727 * zero if @s1 is found, respectively, to be less than,
1728 * to match, or to be greater than @s2.
1731 g_ascii_strcasecmp (const gchar *s1,
1736 g_return_val_if_fail (s1 != NULL, 0);
1737 g_return_val_if_fail (s2 != NULL, 0);
1741 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1742 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1748 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1752 * g_ascii_strncasecmp:
1753 * @s1: string to compare with @s2.
1754 * @s2: string to compare with @s1.
1755 * @n: number of characters to compare.
1757 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1758 * characters after the first @n in each string.
1760 * Unlike the BSD strcasecmp() function, this only recognizes standard
1761 * ASCII letters and ignores the locale, treating all non-ASCII
1762 * characters as if they are not letters.
1764 * Return value: an integer less than, equal to, or greater than zero
1765 * if the first @n bytes of @s1 is found, respectively,
1766 * to be less than, to match, or to be greater than the
1767 * first @n bytes of @s2.
1770 g_ascii_strncasecmp (const gchar *s1,
1776 g_return_val_if_fail (s1 != NULL, 0);
1777 g_return_val_if_fail (s2 != NULL, 0);
1779 while (n && *s1 && *s2)
1782 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1783 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1790 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1798 * @s2: a string to compare with @s1.
1800 * A case-insensitive string comparison, corresponding to the standard
1801 * strcasecmp() function on platforms which support it.
1803 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1804 * or a positive value if @s1 > @s2.
1806 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1807 * deprecated and how to replace it.
1810 g_strcasecmp (const gchar *s1,
1813 #ifdef HAVE_STRCASECMP
1814 g_return_val_if_fail (s1 != NULL, 0);
1815 g_return_val_if_fail (s2 != NULL, 0);
1817 return strcasecmp (s1, s2);
1821 g_return_val_if_fail (s1 != NULL, 0);
1822 g_return_val_if_fail (s2 != NULL, 0);
1826 /* According to A. Cox, some platforms have islower's that
1827 * don't work right on non-uppercase
1829 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1830 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1836 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1843 * @s2: a string to compare with @s1.
1844 * @n: the maximum number of characters to compare.
1846 * A case-insensitive string comparison, corresponding to the standard
1847 * strncasecmp() function on platforms which support it.
1848 * It is similar to g_strcasecmp() except it only compares the first @n
1849 * characters of the strings.
1851 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1852 * or a positive value if @s1 > @s2.
1854 * Deprecated: The problem with g_strncasecmp() is that it does the
1855 * comparison by calling toupper()/tolower(). These functions are
1856 * locale-specific and operate on single bytes. However, it is impossible
1857 * to handle things correctly from an I18N standpoint by operating on
1858 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1859 * broken if your string is guaranteed to be ASCII, since it's
1860 * locale-sensitive, and it's broken if your string is localized, since
1861 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1864 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1865 * which only works on ASCII and is not locale-sensitive, and
1866 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1869 g_strncasecmp (const gchar *s1,
1873 #ifdef HAVE_STRNCASECMP
1874 return strncasecmp (s1, s2, n);
1878 g_return_val_if_fail (s1 != NULL, 0);
1879 g_return_val_if_fail (s2 != NULL, 0);
1881 while (n && *s1 && *s2)
1884 /* According to A. Cox, some platforms have islower's that
1885 * don't work right on non-uppercase
1887 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1888 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1895 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1902 g_strdelimit (gchar *string,
1903 const gchar *delimiters,
1908 g_return_val_if_fail (string != NULL, NULL);
1911 delimiters = G_STR_DELIMITERS;
1913 for (c = string; *c; c++)
1915 if (strchr (delimiters, *c))
1923 g_strcanon (gchar *string,
1924 const gchar *valid_chars,
1929 g_return_val_if_fail (string != NULL, NULL);
1930 g_return_val_if_fail (valid_chars != NULL, NULL);
1932 for (c = string; *c; c++)
1934 if (!strchr (valid_chars, *c))
1942 g_strcompress (const gchar *source)
1944 const gchar *p = source, *octal;
1945 gchar *dest = g_malloc (strlen (source) + 1);
1955 case '0': case '1': case '2': case '3': case '4':
1956 case '5': case '6': case '7':
1959 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1961 *q = (*q * 8) + (*p - '0');
1982 default: /* Also handles \" and \\ */
1997 g_strescape (const gchar *source,
1998 const gchar *exceptions)
2005 g_return_val_if_fail (source != NULL, NULL);
2007 p = (guchar *) source;
2008 /* Each source byte needs maximally four destination chars (\777) */
2009 q = dest = g_malloc (strlen (source) * 4 + 1);
2011 memset (excmap, 0, 256);
2014 guchar *e = (guchar *) exceptions;
2060 if ((*p < ' ') || (*p >= 0177))
2063 *q++ = '0' + (((*p) >> 6) & 07);
2064 *q++ = '0' + (((*p) >> 3) & 07);
2065 *q++ = '0' + ((*p) & 07);
2079 g_strchug (gchar *string)
2083 g_return_val_if_fail (string != NULL, NULL);
2085 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2088 g_memmove (string, start, strlen ((gchar *) start) + 1);
2094 g_strchomp (gchar *string)
2098 g_return_val_if_fail (string != NULL, NULL);
2100 len = strlen (string);
2103 if (g_ascii_isspace ((guchar) string[len]))
2114 * @string: a string to split.
2115 * @delimiter: a string which specifies the places at which to split the string.
2116 * The delimiter is not included in any of the resulting strings, unless
2117 * @max_tokens is reached.
2118 * @max_tokens: the maximum number of pieces to split @string into. If this is
2119 * less than 1, the string is split completely.
2121 * Splits a string into a maximum of @max_tokens pieces, using the given
2122 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2123 * to the last token.
2125 * As a special case, the result of splitting the empty string "" is an empty
2126 * vector, not a vector containing a single string. The reason for this
2127 * special case is that being able to represent a empty vector is typically
2128 * more useful than consistent handling of empty elements. If you do need
2129 * to represent empty elements, you'll need to check for the empty string
2130 * before calling g_strsplit().
2132 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2133 * g_strfreev() to free it.
2136 g_strsplit (const gchar *string,
2137 const gchar *delimiter,
2140 GSList *string_list = NULL, *slist;
2141 gchar **str_array, *s;
2143 const gchar *remainder;
2145 g_return_val_if_fail (string != NULL, NULL);
2146 g_return_val_if_fail (delimiter != NULL, NULL);
2147 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2150 max_tokens = G_MAXINT;
2153 s = strstr (remainder, delimiter);
2156 gsize delimiter_len = strlen (delimiter);
2158 while (--max_tokens && s)
2163 len = s - remainder;
2164 new_string = g_new (gchar, len + 1);
2165 strncpy (new_string, remainder, len);
2166 new_string[len] = 0;
2167 string_list = g_slist_prepend (string_list, new_string);
2169 remainder = s + delimiter_len;
2170 s = strstr (remainder, delimiter);
2176 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2179 str_array = g_new (gchar*, n + 1);
2181 str_array[n--] = NULL;
2182 for (slist = string_list; slist; slist = slist->next)
2183 str_array[n--] = slist->data;
2185 g_slist_free (string_list);
2191 g_strfreev (gchar **str_array)
2197 for(i = 0; str_array[i] != NULL; i++)
2198 g_free(str_array[i]);
2206 * @str_array: %NULL-terminated array of strings.
2208 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2209 * the new array should be freed by first freeing each string, then
2210 * the array itself. g_strfreev() does this for you. If called
2211 * on a %NULL value, g_strdupv() simply returns %NULL.
2213 * Return value: a new %NULL-terminated array of strings.
2216 g_strdupv (gchar **str_array)
2224 while (str_array[i])
2227 retval = g_new (gchar*, i + 1);
2230 while (str_array[i])
2232 retval[i] = g_strdup (str_array[i]);
2244 g_strjoinv (const gchar *separator,
2250 g_return_val_if_fail (str_array != NULL, NULL);
2252 if (separator == NULL)
2259 gsize separator_len;
2261 separator_len = strlen (separator);
2262 /* First part, getting length */
2263 len = 1 + strlen (str_array[0]);
2264 for (i = 1; str_array[i] != NULL; i++)
2265 len += strlen (str_array[i]);
2266 len += separator_len * (i - 1);
2268 /* Second part, building string */
2269 string = g_new (gchar, len);
2270 ptr = g_stpcpy (string, *str_array);
2271 for (i = 1; str_array[i] != NULL; i++)
2273 ptr = g_stpcpy (ptr, separator);
2274 ptr = g_stpcpy (ptr, str_array[i]);
2278 string = g_strdup ("");
2284 g_strjoin (const gchar *separator,
2290 gsize separator_len;
2293 if (separator == NULL)
2296 separator_len = strlen (separator);
2298 va_start (args, separator);
2300 s = va_arg (args, gchar*);
2304 /* First part, getting length */
2305 len = 1 + strlen (s);
2307 s = va_arg (args, gchar*);
2310 len += separator_len + strlen (s);
2311 s = va_arg (args, gchar*);
2315 /* Second part, building string */
2316 string = g_new (gchar, len);
2318 va_start (args, separator);
2320 s = va_arg (args, gchar*);
2321 ptr = g_stpcpy (string, s);
2323 s = va_arg (args, gchar*);
2326 ptr = g_stpcpy (ptr, separator);
2327 ptr = g_stpcpy (ptr, s);
2328 s = va_arg (args, gchar*);
2332 string = g_strdup ("");
2342 * @haystack: a string.
2343 * @haystack_len: the maximum length of @haystack.
2344 * @needle: the string to search for.
2346 * Searches the string @haystack for the first occurrence
2347 * of the string @needle, limiting the length of the search
2350 * Return value: a pointer to the found occurrence, or
2351 * %NULL if not found.
2354 g_strstr_len (const gchar *haystack,
2355 gssize haystack_len,
2356 const gchar *needle)
2358 g_return_val_if_fail (haystack != NULL, NULL);
2359 g_return_val_if_fail (needle != NULL, NULL);
2361 if (haystack_len < 0)
2362 return strstr (haystack, needle);
2365 const gchar *p = haystack;
2366 gsize needle_len = strlen (needle);
2370 if (needle_len == 0)
2371 return (gchar *)haystack;
2373 if (haystack_len < needle_len)
2376 end = haystack + haystack_len - needle_len;
2378 while (*p && p <= end)
2380 for (i = 0; i < needle_len; i++)
2381 if (p[i] != needle[i])
2396 * @haystack: a nul-terminated string.
2397 * @needle: the nul-terminated string to search for.
2399 * Searches the string @haystack for the last occurrence
2400 * of the string @needle.
2402 * Return value: a pointer to the found occurrence, or
2403 * %NULL if not found.
2406 g_strrstr (const gchar *haystack,
2407 const gchar *needle)
2414 g_return_val_if_fail (haystack != NULL, NULL);
2415 g_return_val_if_fail (needle != NULL, NULL);
2417 needle_len = strlen (needle);
2418 haystack_len = strlen (haystack);
2420 if (needle_len == 0)
2421 return (gchar *)haystack;
2423 if (haystack_len < needle_len)
2426 p = haystack + haystack_len - needle_len;
2428 while (p >= haystack)
2430 for (i = 0; i < needle_len; i++)
2431 if (p[i] != needle[i])
2445 * @haystack: a nul-terminated string.
2446 * @haystack_len: the maximum length of @haystack.
2447 * @needle: the nul-terminated string to search for.
2449 * Searches the string @haystack for the last occurrence
2450 * of the string @needle, limiting the length of the search
2453 * Return value: a pointer to the found occurrence, or
2454 * %NULL if not found.
2457 g_strrstr_len (const gchar *haystack,
2458 gssize haystack_len,
2459 const gchar *needle)
2461 g_return_val_if_fail (haystack != NULL, NULL);
2462 g_return_val_if_fail (needle != NULL, NULL);
2464 if (haystack_len < 0)
2465 return g_strrstr (haystack, needle);
2468 gsize needle_len = strlen (needle);
2469 const gchar *haystack_max = haystack + haystack_len;
2470 const gchar *p = haystack;
2473 while (p < haystack_max && *p)
2476 if (p < haystack + needle_len)
2481 while (p >= haystack)
2483 for (i = 0; i < needle_len; i++)
2484 if (p[i] != needle[i])
2500 * @str: a nul-terminated string.
2501 * @suffix: the nul-terminated suffix to look for.
2503 * Looks whether the string @str ends with @suffix.
2505 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2510 g_str_has_suffix (const gchar *str,
2511 const gchar *suffix)
2516 g_return_val_if_fail (str != NULL, FALSE);
2517 g_return_val_if_fail (suffix != NULL, FALSE);
2519 str_len = strlen (str);
2520 suffix_len = strlen (suffix);
2522 if (str_len < suffix_len)
2525 return strcmp (str + str_len - suffix_len, suffix) == 0;
2530 * @str: a nul-terminated string.
2531 * @prefix: the nul-terminated prefix to look for.
2533 * Looks whether the string @str begins with @prefix.
2535 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2540 g_str_has_prefix (const gchar *str,
2541 const gchar *prefix)
2546 g_return_val_if_fail (str != NULL, FALSE);
2547 g_return_val_if_fail (prefix != NULL, FALSE);
2549 str_len = strlen (str);
2550 prefix_len = strlen (prefix);
2552 if (str_len < prefix_len)
2555 return strncmp (str, prefix, prefix_len) == 0;