1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
33 #define _GNU_SOURCE /* For stpcpy */
41 #include <ctype.h> /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
48 #include "gprintfint.h"
57 /* do not include <unistd.h> in this place since it
58 * interferes with g_strsignal() on some OSes
61 static const guint16 ascii_table_data[256] = {
62 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
64 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
65 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
66 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
68 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
69 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
70 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
71 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
72 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
73 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
74 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
75 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
76 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
77 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
78 /* the upper 128 are all zeroes */
81 const guint16 * const g_ascii_table = ascii_table_data;
84 g_strdup (const gchar *str)
91 length = strlen (str) + 1;
92 new_str = g_new (char, length);
93 memcpy (new_str, str, length);
102 g_memdup (gconstpointer mem,
109 new_mem = g_malloc (byte_size);
110 memcpy (new_mem, mem, byte_size);
120 * @str: the string to duplicate
121 * @n: the maximum number of bytes to copy from @str
123 * Duplicates the first @n bytes of a string, returning a newly-allocated
124 * buffer @n + 1 bytes long which will always be nul-terminated.
125 * If @str is less than @n bytes long the buffer is padded with nuls.
126 * If @str is %NULL it returns %NULL.
127 * The returned value should be freed when no longer needed.
130 * To copy a number of characters from a UTF-8 encoded string, use
131 * g_utf8_strncpy() instead.
134 * Returns: a newly-allocated buffer containing the first @n bytes
135 * of @str, nul-terminated
138 g_strndup (const gchar *str,
145 new_str = g_new (gchar, n + 1);
146 strncpy (new_str, str, n);
157 * @length: the length of the new string
158 * @fill_char: the byte to fill the string with
160 * Creates a new string @length bytes long filled with @fill_char.
161 * The returned string should be freed when no longer needed.
163 * Returns: a newly-allocated string filled the @fill_char
166 g_strnfill (gsize length,
171 str = g_new (gchar, length + 1);
172 memset (str, (guchar)fill_char, length);
180 * @dest: destination buffer.
181 * @src: source string.
183 * Copies a nul-terminated string into the dest buffer, include the
184 * trailing nul, and return a pointer to the trailing nul byte.
185 * This is useful for concatenating multiple strings together
186 * without having to repeatedly scan for the end.
188 * Return value: a pointer to trailing nul byte.
191 g_stpcpy (gchar *dest,
195 g_return_val_if_fail (dest != NULL, NULL);
196 g_return_val_if_fail (src != NULL, NULL);
197 return stpcpy (dest, src);
199 register gchar *d = dest;
200 register const gchar *s = src;
202 g_return_val_if_fail (dest != NULL, NULL);
203 g_return_val_if_fail (src != NULL, NULL);
206 while (*s++ != '\0');
213 g_strdup_vprintf (const gchar *format,
216 gchar *string = NULL;
218 g_vasprintf (&string, format, args);
224 g_strdup_printf (const gchar *format,
230 va_start (args, format);
231 buffer = g_strdup_vprintf (format, args);
238 g_strconcat (const gchar *string1, ...)
249 l = 1 + strlen (string1);
250 va_start (args, string1);
251 s = va_arg (args, gchar*);
255 s = va_arg (args, gchar*);
259 concat = g_new (gchar, l);
262 ptr = g_stpcpy (ptr, string1);
263 va_start (args, string1);
264 s = va_arg (args, gchar*);
267 ptr = g_stpcpy (ptr, s);
268 s = va_arg (args, gchar*);
277 * @nptr: the string to convert to a numeric value.
278 * @endptr: if non-%NULL, it returns the character after
279 * the last character used in the conversion.
281 * Converts a string to a #gdouble value.
282 * It calls the standard strtod() function to handle the conversion, but
283 * if the string is not completely converted it attempts the conversion
284 * again with g_ascii_strtod(), and returns the best match.
286 * This function should seldomly be used. The normal situation when reading
287 * numbers not for human consumption is to use g_ascii_strtod(). Only when
288 * you know that you must expect both locale formatted and C formatted numbers
289 * should you use this. Make sure that you don't pass strings such as comma
290 * separated lists of values, since the commas may be interpreted as a decimal
291 * point in some locales, causing unexpected results.
293 * Return value: the #gdouble value.
296 g_strtod (const gchar *nptr,
304 g_return_val_if_fail (nptr != NULL, 0);
309 val_1 = strtod (nptr, &fail_pos_1);
311 if (fail_pos_1 && fail_pos_1[0] != 0)
312 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
314 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
317 *endptr = fail_pos_1;
323 *endptr = fail_pos_2;
330 * @nptr: the string to convert to a numeric value.
331 * @endptr: if non-%NULL, it returns the character after
332 * the last character used in the conversion.
334 * Converts a string to a #gdouble value.
335 * This function behaves like the standard strtod() function
336 * does in the C locale. It does this without actually
337 * changing the current locale, since that would not be
340 * This function is typically used when reading configuration
341 * files or other non-user input that should be locale independent.
342 * To handle input from the user you should normally use the
343 * locale-sensitive system strtod() function.
345 * To convert from a #gdouble to a string in a locale-insensitive
346 * way, use g_ascii_dtostr().
348 * If the correct value would cause overflow, plus or minus %HUGE_VAL
349 * is returned (according to the sign of the value), and %ERANGE is
350 * stored in %errno. If the correct value would cause underflow,
351 * zero is returned and %ERANGE is stored in %errno.
353 * This function resets %errno before calling strtod() so that
354 * you can reliably detect overflow and underflow.
356 * Return value: the #gdouble value.
359 g_ascii_strtod (const gchar *nptr,
364 struct lconv *locale_data;
365 const char *decimal_point;
366 int decimal_point_len;
367 const char *p, *decimal_point_pos;
368 const char *end = NULL; /* Silence gcc */
371 g_return_val_if_fail (nptr != NULL, 0);
375 locale_data = localeconv ();
376 decimal_point = locale_data->decimal_point;
377 decimal_point_len = strlen (decimal_point);
379 g_assert (decimal_point_len != 0);
381 decimal_point_pos = NULL;
384 if (decimal_point[0] != '.' ||
385 decimal_point[1] != 0)
388 /* Skip leading space */
389 while (g_ascii_isspace (*p))
392 /* Skip leading optional sign */
393 if (*p == '+' || *p == '-')
397 (p[1] == 'x' || p[1] == 'X'))
400 /* HEX - find the (optional) decimal point */
402 while (g_ascii_isxdigit (*p))
406 decimal_point_pos = p++;
408 while (g_ascii_isxdigit (*p))
411 if (*p == 'p' || *p == 'P')
413 if (*p == '+' || *p == '-')
415 while (g_ascii_isdigit (*p))
420 else if (g_ascii_isdigit (*p) || *p == '.')
422 while (g_ascii_isdigit (*p))
426 decimal_point_pos = p++;
428 while (g_ascii_isdigit (*p))
431 if (*p == 'e' || *p == 'E')
433 if (*p == '+' || *p == '-')
435 while (g_ascii_isdigit (*p))
440 /* For the other cases, we need not convert the decimal point */
443 if (decimal_point_pos)
447 /* We need to convert the '.' to the locale specific decimal point */
448 copy = g_malloc (end - nptr + 1 + decimal_point_len);
451 memcpy (c, nptr, decimal_point_pos - nptr);
452 c += decimal_point_pos - nptr;
453 memcpy (c, decimal_point, decimal_point_len);
454 c += decimal_point_len;
455 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
456 c += end - (decimal_point_pos + 1);
460 val = strtod (copy, &fail_pos);
461 strtod_errno = errno;
465 if (fail_pos - copy > decimal_point_pos - nptr)
466 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
468 fail_pos = (char *)nptr + (fail_pos - copy);
478 copy = g_malloc (end - (char *)nptr + 1);
479 memcpy (copy, nptr, end - nptr);
480 *(copy + (end - (char *)nptr)) = 0;
483 val = strtod (copy, &fail_pos);
484 strtod_errno = errno;
488 fail_pos = (char *)nptr + (fail_pos - copy);
496 val = strtod (nptr, &fail_pos);
497 strtod_errno = errno;
503 errno = strtod_errno;
511 * @buffer: A buffer to place the resulting string in
512 * @buf_len: The length of the buffer.
513 * @d: The #gdouble to convert
515 * Converts a #gdouble to a string, using the '.' as
518 * This functions generates enough precision that converting
519 * the string back using g_ascii_strtod() gives the same machine-number
520 * (on machines with IEEE compatible 64bit doubles). It is
521 * guaranteed that the size of the resulting string will never
522 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
524 * Return value: The pointer to the buffer with the converted string.
527 g_ascii_dtostr (gchar *buffer,
531 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
536 * @buffer: A buffer to place the resulting string in
537 * @buf_len: The length of the buffer.
538 * @format: The printf()-style format to use for the
539 * code to use for converting.
540 * @d: The #gdouble to convert
542 * Converts a #gdouble to a string, using the '.' as
543 * decimal point. To format the number you pass in
544 * a printf()-style format string. Allowed conversion
545 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
547 * If you just want to want to serialize the value into a
548 * string, use g_ascii_dtostr().
550 * Return value: The pointer to the buffer with the converted string.
553 g_ascii_formatd (gchar *buffer,
558 struct lconv *locale_data;
559 const char *decimal_point;
560 int decimal_point_len;
565 g_return_val_if_fail (buffer != NULL, NULL);
566 g_return_val_if_fail (format[0] == '%', NULL);
567 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
569 format_char = format[strlen (format) - 1];
571 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
572 format_char == 'f' || format_char == 'F' ||
573 format_char == 'g' || format_char == 'G',
576 if (format[0] != '%')
579 if (strpbrk (format + 1, "'l%"))
582 if (!(format_char == 'e' || format_char == 'E' ||
583 format_char == 'f' || format_char == 'F' ||
584 format_char == 'g' || format_char == 'G'))
588 _g_snprintf (buffer, buf_len, format, d);
590 locale_data = localeconv ();
591 decimal_point = locale_data->decimal_point;
592 decimal_point_len = strlen (decimal_point);
594 g_assert (decimal_point_len != 0);
596 if (decimal_point[0] != '.' ||
597 decimal_point[1] != 0)
601 while (g_ascii_isspace (*p))
604 if (*p == '+' || *p == '-')
607 while (isdigit ((guchar)*p))
610 if (strncmp (p, decimal_point, decimal_point_len) == 0)
614 if (decimal_point_len > 1)
616 rest_len = strlen (p + (decimal_point_len-1));
617 memmove (p, p + (decimal_point_len-1), rest_len);
627 g_parse_long_long (const gchar *nptr,
632 /* this code is based on on the strtol(3) code from GNU libc released under
633 * the GNU Lesser General Public License.
635 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
636 * Free Software Foundation, Inc.
638 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
639 (c) == '\r' || (c) == '\t' || (c) == '\v')
640 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
641 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
642 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
643 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
644 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
649 const gchar *s, *save;
652 g_return_val_if_fail (nptr != NULL, 0);
655 if (base == 1 || base > 36)
665 /* Skip white space. */
669 if (G_UNLIKELY (!*s))
672 /* Check for a sign. */
681 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
684 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
695 /* Save the pointer so we can check later if anything happened. */
697 cutoff = G_MAXUINT64 / base;
698 cutlim = G_MAXUINT64 % base;
705 if (c >= '0' && c <= '9')
707 else if (ISALPHA (c))
708 c = TOUPPER (c) - 'A' + 10;
713 /* Check for overflow. */
714 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
723 /* Check if anything actually happened. */
727 /* Store in ENDPTR the address of one character
728 past the last character we converted. */
730 *endptr = (gchar*) s;
732 if (G_UNLIKELY (overflow))
741 /* We must handle a special case here: the base is 0 or 16 and the
742 first two characters are '0' and 'x', but the rest are no
743 hexadecimal digits. This is no error case. We return 0 and
744 ENDPTR points to the `x`. */
747 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
749 *endptr = (gchar*) &save[-1];
751 /* There was no number to convert. */
752 *endptr = (gchar*) nptr;
759 * @nptr: the string to convert to a numeric value.
760 * @endptr: if non-%NULL, it returns the character after
761 * the last character used in the conversion.
762 * @base: to be used for the conversion, 2..36 or 0
764 * Converts a string to a #guint64 value.
765 * This function behaves like the standard strtoull() function
766 * does in the C locale. It does this without actually
767 * changing the current locale, since that would not be
770 * This function is typically used when reading configuration
771 * files or other non-user input that should be locale independent.
772 * To handle input from the user you should normally use the
773 * locale-sensitive system strtoull() function.
775 * If the correct value would cause overflow, %G_MAXUINT64
776 * is returned, and %ERANGE is stored in %errno. If the base is
777 * outside the valid range, zero is returned, and %EINVAL is stored
778 * in %errno. If the string conversion fails, zero is returned, and
779 * @endptr returns @nptr (if @endptr is non-%NULL).
781 * Return value: the #guint64 value or zero on error.
786 g_ascii_strtoull (const gchar *nptr,
793 result = g_parse_long_long (nptr, endptr, base, &negative);
795 /* Return the result of the appropriate sign. */
796 return negative ? -result : result;
801 * @nptr: the string to convert to a numeric value.
802 * @endptr: if non-%NULL, it returns the character after
803 * the last character used in the conversion.
804 * @base: to be used for the conversion, 2..36 or 0
806 * Converts a string to a #gint64 value.
807 * This function behaves like the standard strtoll() function
808 * does in the C locale. It does this without actually
809 * changing the current locale, since that would not be
812 * This function is typically used when reading configuration
813 * files or other non-user input that should be locale independent.
814 * To handle input from the user you should normally use the
815 * locale-sensitive system strtoll() function.
817 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
818 * is returned, and %ERANGE is stored in %errno. If the base is
819 * outside the valid range, zero is returned, and %EINVAL is stored
820 * in %errno. If the string conversion fails, zero is returned, and
821 * @endptr returns @nptr (if @endptr is non-%NULL).
823 * Return value: the #gint64 value or zero on error.
828 g_ascii_strtoll (const gchar *nptr,
835 result = g_parse_long_long (nptr, endptr, base, &negative);
837 if (negative && result > (guint64) G_MININT64)
842 else if (!negative && result > (guint64) G_MAXINT64)
848 return - (gint64) result;
850 return (gint64) result;
853 G_CONST_RETURN gchar*
854 g_strerror (gint errnum)
856 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
858 int saved_errno = errno;
861 const char *msg_locale;
863 msg_locale = strerror (errnum);
864 if (g_get_charset (NULL))
871 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
874 /* Stick in the quark table so that we can return a static result
876 GQuark msg_quark = g_quark_from_string (msg_utf8);
879 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
888 case E2BIG: return "argument list too long";
891 case EACCES: return "permission denied";
894 case EADDRINUSE: return "address already in use";
897 case EADDRNOTAVAIL: return "can't assign requested address";
900 case EADV: return "advertise error";
903 case EAFNOSUPPORT: return "address family not supported by protocol family";
906 case EAGAIN: return "try again";
909 case EALIGN: return "EALIGN";
912 case EALREADY: return "operation already in progress";
915 case EBADE: return "bad exchange descriptor";
918 case EBADF: return "bad file number";
921 case EBADFD: return "file descriptor in bad state";
924 case EBADMSG: return "not a data message";
927 case EBADR: return "bad request descriptor";
930 case EBADRPC: return "RPC structure is bad";
933 case EBADRQC: return "bad request code";
936 case EBADSLT: return "invalid slot";
939 case EBFONT: return "bad font file format";
942 case EBUSY: return "mount device busy";
945 case ECHILD: return "no children";
948 case ECHRNG: return "channel number out of range";
951 case ECOMM: return "communication error on send";
954 case ECONNABORTED: return "software caused connection abort";
957 case ECONNREFUSED: return "connection refused";
960 case ECONNRESET: return "connection reset by peer";
962 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
963 case EDEADLK: return "resource deadlock avoided";
966 case EDEADLOCK: return "resource deadlock avoided";
969 case EDESTADDRREQ: return "destination address required";
972 case EDIRTY: return "mounting a dirty fs w/o force";
975 case EDOM: return "math argument out of range";
978 case EDOTDOT: return "cross mount point";
981 case EDQUOT: return "disk quota exceeded";
984 case EDUPPKG: return "duplicate package name";
987 case EEXIST: return "file already exists";
990 case EFAULT: return "bad address in system call argument";
993 case EFBIG: return "file too large";
996 case EHOSTDOWN: return "host is down";
999 case EHOSTUNREACH: return "host is unreachable";
1002 case EIDRM: return "identifier removed";
1005 case EINIT: return "initialization error";
1008 case EINPROGRESS: return "operation now in progress";
1011 case EINTR: return "interrupted system call";
1014 case EINVAL: return "invalid argument";
1017 case EIO: return "I/O error";
1020 case EISCONN: return "socket is already connected";
1023 case EISDIR: return "is a directory";
1026 case EISNAM: return "is a name file";
1029 case ELBIN: return "ELBIN";
1032 case EL2HLT: return "level 2 halted";
1035 case EL2NSYNC: return "level 2 not synchronized";
1038 case EL3HLT: return "level 3 halted";
1041 case EL3RST: return "level 3 reset";
1044 case ELIBACC: return "can not access a needed shared library";
1047 case ELIBBAD: return "accessing a corrupted shared library";
1050 case ELIBEXEC: return "can not exec a shared library directly";
1053 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
1056 case ELIBSCN: return ".lib section in a.out corrupted";
1059 case ELNRNG: return "link number out of range";
1062 case ELOOP: return "too many levels of symbolic links";
1065 case EMFILE: return "too many open files";
1068 case EMLINK: return "too many links";
1071 case EMSGSIZE: return "message too long";
1074 case EMULTIHOP: return "multihop attempted";
1077 case ENAMETOOLONG: return "file name too long";
1080 case ENAVAIL: return "not available";
1083 case ENET: return "ENET";
1086 case ENETDOWN: return "network is down";
1089 case ENETRESET: return "network dropped connection on reset";
1092 case ENETUNREACH: return "network is unreachable";
1095 case ENFILE: return "file table overflow";
1098 case ENOANO: return "anode table overflow";
1100 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
1101 case ENOBUFS: return "no buffer space available";
1104 case ENOCSI: return "no CSI structure available";
1107 case ENODATA: return "no data available";
1110 case ENODEV: return "no such device";
1113 case ENOENT: return "no such file or directory";
1116 case ENOEXEC: return "exec format error";
1119 case ENOLCK: return "no locks available";
1122 case ENOLINK: return "link has be severed";
1125 case ENOMEM: return "not enough memory";
1128 case ENOMSG: return "no message of desired type";
1131 case ENONET: return "machine is not on the network";
1134 case ENOPKG: return "package not installed";
1137 case ENOPROTOOPT: return "bad proocol option";
1140 case ENOSPC: return "no space left on device";
1143 case ENOSR: return "out of stream resources";
1146 case ENOSTR: return "not a stream device";
1149 case ENOSYM: return "unresolved symbol name";
1152 case ENOSYS: return "function not implemented";
1155 case ENOTBLK: return "block device required";
1158 case ENOTCONN: return "socket is not connected";
1161 case ENOTDIR: return "not a directory";
1164 case ENOTEMPTY: return "directory not empty";
1167 case ENOTNAM: return "not a name file";
1170 case ENOTSOCK: return "socket operation on non-socket";
1173 case ENOTTY: return "inappropriate device for ioctl";
1176 case ENOTUNIQ: return "name not unique on network";
1179 case ENXIO: return "no such device or address";
1182 case EOPNOTSUPP: return "operation not supported on socket";
1185 case EPERM: return "not owner";
1188 case EPFNOSUPPORT: return "protocol family not supported";
1191 case EPIPE: return "broken pipe";
1194 case EPROCLIM: return "too many processes";
1197 case EPROCUNAVAIL: return "bad procedure for program";
1199 #ifdef EPROGMISMATCH
1200 case EPROGMISMATCH: return "program version wrong";
1203 case EPROGUNAVAIL: return "RPC program not available";
1206 case EPROTO: return "protocol error";
1208 #ifdef EPROTONOSUPPORT
1209 case EPROTONOSUPPORT: return "protocol not suppored";
1212 case EPROTOTYPE: return "protocol wrong type for socket";
1215 case ERANGE: return "math result unrepresentable";
1217 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1218 case EREFUSED: return "EREFUSED";
1221 case EREMCHG: return "remote address changed";
1224 case EREMDEV: return "remote device";
1227 case EREMOTE: return "pathname hit remote file system";
1230 case EREMOTEIO: return "remote i/o error";
1232 #ifdef EREMOTERELEASE
1233 case EREMOTERELEASE: return "EREMOTERELEASE";
1236 case EROFS: return "read-only file system";
1239 case ERPCMISMATCH: return "RPC version is wrong";
1242 case ERREMOTE: return "object is remote";
1245 case ESHUTDOWN: return "can't send afer socket shutdown";
1247 #ifdef ESOCKTNOSUPPORT
1248 case ESOCKTNOSUPPORT: return "socket type not supported";
1251 case ESPIPE: return "invalid seek";
1254 case ESRCH: return "no such process";
1257 case ESRMNT: return "srmount error";
1260 case ESTALE: return "stale remote file handle";
1263 case ESUCCESS: return "Error 0";
1266 case ETIME: return "timer expired";
1269 case ETIMEDOUT: return "connection timed out";
1272 case ETOOMANYREFS: return "too many references: can't splice";
1275 case ETXTBSY: return "text file or pseudo-device busy";
1278 case EUCLEAN: return "structure needs cleaning";
1281 case EUNATCH: return "protocol driver not attached";
1284 case EUSERS: return "too many users";
1287 case EVERSION: return "version mismatch";
1289 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1290 case EWOULDBLOCK: return "operation would block";
1293 case EXDEV: return "cross-domain link";
1296 case EXFULL: return "message tables full";
1299 #else /* NO_SYS_ERRLIST */
1300 extern int sys_nerr;
1301 extern char *sys_errlist[];
1303 if ((errnum > 0) && (errnum <= sys_nerr))
1304 return sys_errlist [errnum];
1305 #endif /* NO_SYS_ERRLIST */
1307 msg = g_static_private_get (&msg_private);
1310 msg = g_new (gchar, 64);
1311 g_static_private_set (&msg_private, msg, g_free);
1314 _g_sprintf (msg, "unknown error (%d)", errnum);
1316 errno = saved_errno;
1320 G_CONST_RETURN gchar*
1321 g_strsignal (gint signum)
1323 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1326 #ifdef HAVE_STRSIGNAL
1327 const char *msg_locale;
1329 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1330 extern const char *strsignal(int);
1332 /* this is declared differently (const) in string.h on BeOS */
1333 extern char *strsignal (int sig);
1334 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1335 msg_locale = strsignal (signum);
1336 if (g_get_charset (NULL))
1340 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1343 /* Stick in the quark table so that we can return a static result
1345 GQuark msg_quark = g_quark_from_string (msg_utf8);
1348 return g_quark_to_string (msg_quark);
1351 #elif NO_SYS_SIGLIST
1355 case SIGHUP: return "Hangup";
1358 case SIGINT: return "Interrupt";
1361 case SIGQUIT: return "Quit";
1364 case SIGILL: return "Illegal instruction";
1367 case SIGTRAP: return "Trace/breakpoint trap";
1370 case SIGABRT: return "IOT trap/Abort";
1373 case SIGBUS: return "Bus error";
1376 case SIGFPE: return "Floating point exception";
1379 case SIGKILL: return "Killed";
1382 case SIGUSR1: return "User defined signal 1";
1385 case SIGSEGV: return "Segmentation fault";
1388 case SIGUSR2: return "User defined signal 2";
1391 case SIGPIPE: return "Broken pipe";
1394 case SIGALRM: return "Alarm clock";
1397 case SIGTERM: return "Terminated";
1400 case SIGSTKFLT: return "Stack fault";
1403 case SIGCHLD: return "Child exited";
1406 case SIGCONT: return "Continued";
1409 case SIGSTOP: return "Stopped (signal)";
1412 case SIGTSTP: return "Stopped";
1415 case SIGTTIN: return "Stopped (tty input)";
1418 case SIGTTOU: return "Stopped (tty output)";
1421 case SIGURG: return "Urgent condition";
1424 case SIGXCPU: return "CPU time limit exceeded";
1427 case SIGXFSZ: return "File size limit exceeded";
1430 case SIGVTALRM: return "Virtual time alarm";
1433 case SIGPROF: return "Profile signal";
1436 case SIGWINCH: return "Window size changed";
1439 case SIGIO: return "Possible I/O";
1442 case SIGPWR: return "Power failure";
1445 case SIGUNUSED: return "Unused signal";
1448 #else /* NO_SYS_SIGLIST */
1450 #ifdef NO_SYS_SIGLIST_DECL
1451 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1454 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1455 #endif /* NO_SYS_SIGLIST */
1457 msg = g_static_private_get (&msg_private);
1460 msg = g_new (gchar, 64);
1461 g_static_private_set (&msg_private, msg, g_free);
1464 _g_sprintf (msg, "unknown signal (%d)", signum);
1469 /* Functions g_strlcpy and g_strlcat were originally developed by
1470 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1471 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1472 * for more information.
1476 /* Use the native ones, if available; they might be implemented in assembly */
1478 g_strlcpy (gchar *dest,
1482 g_return_val_if_fail (dest != NULL, 0);
1483 g_return_val_if_fail (src != NULL, 0);
1485 return strlcpy (dest, src, dest_size);
1489 g_strlcat (gchar *dest,
1493 g_return_val_if_fail (dest != NULL, 0);
1494 g_return_val_if_fail (src != NULL, 0);
1496 return strlcat (dest, src, dest_size);
1499 #else /* ! HAVE_STRLCPY */
1502 * Copy string src to buffer dest (of buffer size dest_size). At most
1503 * dest_size-1 characters will be copied. Always NUL terminates
1504 * (unless dest_size == 0). This function does NOT allocate memory.
1505 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1506 * Returns size of attempted result, strlen(src),
1507 * so if retval >= dest_size, truncation occurred.
1510 g_strlcpy (gchar *dest,
1514 register gchar *d = dest;
1515 register const gchar *s = src;
1516 register gsize n = dest_size;
1518 g_return_val_if_fail (dest != NULL, 0);
1519 g_return_val_if_fail (src != NULL, 0);
1521 /* Copy as many bytes as will fit */
1522 if (n != 0 && --n != 0)
1525 register gchar c = *s++;
1533 /* If not enough room in dest, add NUL and traverse rest of src */
1542 return s - src - 1; /* count does not include NUL */
1547 * Appends string src to buffer dest (of buffer size dest_size).
1548 * At most dest_size-1 characters will be copied.
1549 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1550 * This function does NOT allocate memory.
1551 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1552 * in the dest_size characters of dest to start with).
1553 * Returns size of attempted result, which is
1554 * MIN (dest_size, strlen (original dest)) + strlen (src),
1555 * so if retval >= dest_size, truncation occurred.
1558 g_strlcat (gchar *dest,
1562 register gchar *d = dest;
1563 register const gchar *s = src;
1564 register gsize bytes_left = dest_size;
1565 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1567 g_return_val_if_fail (dest != NULL, 0);
1568 g_return_val_if_fail (src != NULL, 0);
1570 /* Find the end of dst and adjust bytes left but don't go past end */
1571 while (*d != 0 && bytes_left-- != 0)
1574 bytes_left = dest_size - dlength;
1576 if (bytes_left == 0)
1577 return dlength + strlen (s);
1581 if (bytes_left != 1)
1590 return dlength + (s - src); /* count does not include NUL */
1592 #endif /* ! HAVE_STRLCPY */
1597 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1599 * Converts all upper case ASCII letters to lower case ASCII letters.
1601 * Return value: a newly-allocated string, with all the upper case
1602 * characters in @str converted to lower case, with
1603 * semantics that exactly match g_ascii_tolower(). (Note
1604 * that this is unlike the old g_strdown(), which modified
1605 * the string in place.)
1608 g_ascii_strdown (const gchar *str,
1613 g_return_val_if_fail (str != NULL, NULL);
1618 result = g_strndup (str, len);
1619 for (s = result; *s; s++)
1620 *s = g_ascii_tolower (*s);
1628 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1630 * Converts all lower case ASCII letters to upper case ASCII letters.
1632 * Return value: a newly allocated string, with all the lower case
1633 * characters in @str converted to upper case, with
1634 * semantics that exactly match g_ascii_toupper(). (Note
1635 * that this is unlike the old g_strup(), which modified
1636 * the string in place.)
1639 g_ascii_strup (const gchar *str,
1644 g_return_val_if_fail (str != NULL, NULL);
1649 result = g_strndup (str, len);
1650 for (s = result; *s; s++)
1651 *s = g_ascii_toupper (*s);
1658 * @string: the string to convert.
1660 * Converts a string to lower case.
1662 * Return value: the string
1664 * Deprecated:2.2: This function is totally broken for the reasons discussed
1665 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1669 g_strdown (gchar *string)
1673 g_return_val_if_fail (string != NULL, NULL);
1675 s = (guchar *) string;
1684 return (gchar *) string;
1689 * @string: the string to convert.
1691 * Converts a string to upper case.
1693 * Return value: the string
1695 * Deprecated:2.2: This function is totally broken for the reasons discussed
1696 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1699 g_strup (gchar *string)
1703 g_return_val_if_fail (string != NULL, NULL);
1705 s = (guchar *) string;
1714 return (gchar *) string;
1718 g_strreverse (gchar *string)
1720 g_return_val_if_fail (string != NULL, NULL);
1724 register gchar *h, *t;
1727 t = string + strlen (string) - 1;
1746 * @c: any character.
1748 * Convert a character to ASCII lower case.
1750 * Unlike the standard C library tolower() function, this only
1751 * recognizes standard ASCII letters and ignores the locale, returning
1752 * all non-ASCII characters unchanged, even if they are lower case
1753 * letters in a particular character set. Also unlike the standard
1754 * library function, this takes and returns a char, not an int, so
1755 * don't call it on %EOF but no need to worry about casting to #guchar
1756 * before passing a possibly non-ASCII character in.
1758 * Return value: the result of converting @c to lower case.
1759 * If @c is not an ASCII upper case letter,
1760 * @c is returned unchanged.
1763 g_ascii_tolower (gchar c)
1765 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1770 * @c: any character.
1772 * Convert a character to ASCII upper case.
1774 * Unlike the standard C library toupper() function, this only
1775 * recognizes standard ASCII letters and ignores the locale, returning
1776 * all non-ASCII characters unchanged, even if they are upper case
1777 * letters in a particular character set. Also unlike the standard
1778 * library function, this takes and returns a char, not an int, so
1779 * don't call it on %EOF but no need to worry about casting to #guchar
1780 * before passing a possibly non-ASCII character in.
1782 * Return value: the result of converting @c to upper case.
1783 * If @c is not an ASCII lower case letter,
1784 * @c is returned unchanged.
1787 g_ascii_toupper (gchar c)
1789 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1793 * g_ascii_digit_value:
1794 * @c: an ASCII character.
1796 * Determines the numeric value of a character as a decimal
1797 * digit. Differs from g_unichar_digit_value() because it takes
1798 * a char, so there's no worry about sign extension if characters
1801 * Return value: If @c is a decimal digit (according to
1802 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1805 g_ascii_digit_value (gchar c)
1807 if (g_ascii_isdigit (c))
1813 * g_ascii_xdigit_value:
1814 * @c: an ASCII character.
1816 * Determines the numeric value of a character as a hexidecimal
1817 * digit. Differs from g_unichar_xdigit_value() because it takes
1818 * a char, so there's no worry about sign extension if characters
1821 * Return value: If @c is a hex digit (according to
1822 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1825 g_ascii_xdigit_value (gchar c)
1827 if (c >= 'A' && c <= 'F')
1828 return c - 'A' + 10;
1829 if (c >= 'a' && c <= 'f')
1830 return c - 'a' + 10;
1831 return g_ascii_digit_value (c);
1835 * g_ascii_strcasecmp:
1836 * @s1: string to compare with @s2.
1837 * @s2: string to compare with @s1.
1839 * Compare two strings, ignoring the case of ASCII characters.
1841 * Unlike the BSD strcasecmp() function, this only recognizes standard
1842 * ASCII letters and ignores the locale, treating all non-ASCII
1843 * bytes as if they are not letters.
1845 * This function should be used only on strings that are known to be
1846 * in encodings where the bytes corresponding to ASCII letters always
1847 * represent themselves. This includes UTF-8 and the ISO-8859-*
1848 * charsets, but not for instance double-byte encodings like the
1849 * Windows Codepage 932, where the trailing bytes of double-byte
1850 * characters include all ASCII letters. If you compare two CP932
1851 * strings using this function, you will get false matches.
1853 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1854 * or a positive value if @s1 > @s2.
1857 g_ascii_strcasecmp (const gchar *s1,
1862 g_return_val_if_fail (s1 != NULL, 0);
1863 g_return_val_if_fail (s2 != NULL, 0);
1867 c1 = (gint)(guchar) TOLOWER (*s1);
1868 c2 = (gint)(guchar) TOLOWER (*s2);
1874 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1878 * g_ascii_strncasecmp:
1879 * @s1: string to compare with @s2.
1880 * @s2: string to compare with @s1.
1881 * @n: number of characters to compare.
1883 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1884 * characters after the first @n in each string.
1886 * Unlike the BSD strcasecmp() function, this only recognizes standard
1887 * ASCII letters and ignores the locale, treating all non-ASCII
1888 * characters as if they are not letters.
1890 * The same warning as in g_ascii_strcasecmp() applies: Use this
1891 * function only on strings known to be in encodings where bytes
1892 * corresponding to ASCII letters always represent themselves.
1894 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1895 * or a positive value if @s1 > @s2.
1898 g_ascii_strncasecmp (const gchar *s1,
1904 g_return_val_if_fail (s1 != NULL, 0);
1905 g_return_val_if_fail (s2 != NULL, 0);
1907 while (n && *s1 && *s2)
1910 c1 = (gint)(guchar) TOLOWER (*s1);
1911 c2 = (gint)(guchar) TOLOWER (*s2);
1918 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1926 * @s2: a string to compare with @s1.
1928 * A case-insensitive string comparison, corresponding to the standard
1929 * strcasecmp() function on platforms which support it.
1931 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1932 * or a positive value if @s1 > @s2.
1934 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1935 * is deprecated and how to replace it.
1938 g_strcasecmp (const gchar *s1,
1941 #ifdef HAVE_STRCASECMP
1942 g_return_val_if_fail (s1 != NULL, 0);
1943 g_return_val_if_fail (s2 != NULL, 0);
1945 return strcasecmp (s1, s2);
1949 g_return_val_if_fail (s1 != NULL, 0);
1950 g_return_val_if_fail (s2 != NULL, 0);
1954 /* According to A. Cox, some platforms have islower's that
1955 * don't work right on non-uppercase
1957 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1958 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1964 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1971 * @s2: a string to compare with @s1.
1972 * @n: the maximum number of characters to compare.
1974 * A case-insensitive string comparison, corresponding to the standard
1975 * strncasecmp() function on platforms which support it.
1976 * It is similar to g_strcasecmp() except it only compares the first @n
1977 * characters of the strings.
1979 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1980 * or a positive value if @s1 > @s2.
1982 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1983 * comparison by calling toupper()/tolower(). These functions are
1984 * locale-specific and operate on single bytes. However, it is impossible
1985 * to handle things correctly from an I18N standpoint by operating on
1986 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1987 * broken if your string is guaranteed to be ASCII, since it's
1988 * locale-sensitive, and it's broken if your string is localized, since
1989 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1992 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1993 * which only works on ASCII and is not locale-sensitive, and
1994 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1997 g_strncasecmp (const gchar *s1,
2001 #ifdef HAVE_STRNCASECMP
2002 return strncasecmp (s1, s2, n);
2006 g_return_val_if_fail (s1 != NULL, 0);
2007 g_return_val_if_fail (s2 != NULL, 0);
2009 while (n && *s1 && *s2)
2012 /* According to A. Cox, some platforms have islower's that
2013 * don't work right on non-uppercase
2015 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
2016 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
2023 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
2030 g_strdelimit (gchar *string,
2031 const gchar *delimiters,
2036 g_return_val_if_fail (string != NULL, NULL);
2039 delimiters = G_STR_DELIMITERS;
2041 for (c = string; *c; c++)
2043 if (strchr (delimiters, *c))
2051 g_strcanon (gchar *string,
2052 const gchar *valid_chars,
2057 g_return_val_if_fail (string != NULL, NULL);
2058 g_return_val_if_fail (valid_chars != NULL, NULL);
2060 for (c = string; *c; c++)
2062 if (!strchr (valid_chars, *c))
2070 g_strcompress (const gchar *source)
2072 const gchar *p = source, *octal;
2073 gchar *dest = g_malloc (strlen (source) + 1);
2084 g_warning ("g_strcompress: trailing \\");
2086 case '0': case '1': case '2': case '3': case '4':
2087 case '5': case '6': case '7':
2090 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2092 *q = (*q * 8) + (*p - '0');
2113 default: /* Also handles \" and \\ */
2129 g_strescape (const gchar *source,
2130 const gchar *exceptions)
2137 g_return_val_if_fail (source != NULL, NULL);
2139 p = (guchar *) source;
2140 /* Each source byte needs maximally four destination chars (\777) */
2141 q = dest = g_malloc (strlen (source) * 4 + 1);
2143 memset (excmap, 0, 256);
2146 guchar *e = (guchar *) exceptions;
2192 if ((*p < ' ') || (*p >= 0177))
2195 *q++ = '0' + (((*p) >> 6) & 07);
2196 *q++ = '0' + (((*p) >> 3) & 07);
2197 *q++ = '0' + ((*p) & 07);
2211 g_strchug (gchar *string)
2215 g_return_val_if_fail (string != NULL, NULL);
2217 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2220 g_memmove (string, start, strlen ((gchar *) start) + 1);
2226 g_strchomp (gchar *string)
2230 g_return_val_if_fail (string != NULL, NULL);
2232 len = strlen (string);
2235 if (g_ascii_isspace ((guchar) string[len]))
2246 * @string: a string to split.
2247 * @delimiter: a string which specifies the places at which to split the string.
2248 * The delimiter is not included in any of the resulting strings, unless
2249 * @max_tokens is reached.
2250 * @max_tokens: the maximum number of pieces to split @string into. If this is
2251 * less than 1, the string is split completely.
2253 * Splits a string into a maximum of @max_tokens pieces, using the given
2254 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2255 * to the last token.
2257 * As a special case, the result of splitting the empty string "" is an empty
2258 * vector, not a vector containing a single string. The reason for this
2259 * special case is that being able to represent a empty vector is typically
2260 * more useful than consistent handling of empty elements. If you do need
2261 * to represent empty elements, you'll need to check for the empty string
2262 * before calling g_strsplit().
2264 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2265 * g_strfreev() to free it.
2268 g_strsplit (const gchar *string,
2269 const gchar *delimiter,
2272 GSList *string_list = NULL, *slist;
2273 gchar **str_array, *s;
2275 const gchar *remainder;
2277 g_return_val_if_fail (string != NULL, NULL);
2278 g_return_val_if_fail (delimiter != NULL, NULL);
2279 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2282 max_tokens = G_MAXINT;
2285 s = strstr (remainder, delimiter);
2288 gsize delimiter_len = strlen (delimiter);
2290 while (--max_tokens && s)
2294 len = s - remainder;
2295 string_list = g_slist_prepend (string_list,
2296 g_strndup (remainder, len));
2298 remainder = s + delimiter_len;
2299 s = strstr (remainder, delimiter);
2305 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2308 str_array = g_new (gchar*, n + 1);
2310 str_array[n--] = NULL;
2311 for (slist = string_list; slist; slist = slist->next)
2312 str_array[n--] = slist->data;
2314 g_slist_free (string_list);
2321 * @string: The string to be tokenized
2322 * @delimiters: A nul-terminated string containing bytes that are used
2323 * to split the string.
2324 * @max_tokens: The maximum number of tokens to split @string into.
2325 * If this is less than 1, the string is split completely
2327 * Splits @string into a number of tokens not containing any of the characters
2328 * in @delimiter. A token is the (possibly empty) longest string that does not
2329 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2330 * remainder is appended to the last token.
2332 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2333 * %NULL-terminated vector containing the three strings "abc", "def",
2336 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2337 * vector containing the four strings "", "def", "ghi", and "".
2339 * As a special case, the result of splitting the empty string "" is an empty
2340 * vector, not a vector containing a single string. The reason for this
2341 * special case is that being able to represent a empty vector is typically
2342 * more useful than consistent handling of empty elements. If you do need
2343 * to represent empty elements, you'll need to check for the empty string
2344 * before calling g_strsplit_set().
2346 * Note that this function works on bytes not characters, so it can't be used
2347 * to delimit UTF-8 strings for anything but ASCII characters.
2349 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2350 * g_strfreev() to free it.
2355 g_strsplit_set (const gchar *string,
2356 const gchar *delimiters,
2359 gboolean delim_table[256];
2360 GSList *tokens, *list;
2363 const gchar *current;
2367 g_return_val_if_fail (string != NULL, NULL);
2368 g_return_val_if_fail (delimiters != NULL, NULL);
2371 max_tokens = G_MAXINT;
2373 if (*string == '\0')
2375 result = g_new (char *, 1);
2380 memset (delim_table, FALSE, sizeof (delim_table));
2381 for (s = delimiters; *s != '\0'; ++s)
2382 delim_table[*(guchar *)s] = TRUE;
2387 s = current = string;
2390 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2394 token = g_strndup (current, s - current);
2395 tokens = g_slist_prepend (tokens, token);
2404 token = g_strndup (current, s - current);
2405 tokens = g_slist_prepend (tokens, token);
2408 result = g_new (gchar *, n_tokens + 1);
2410 result[n_tokens] = NULL;
2411 for (list = tokens; list != NULL; list = list->next)
2412 result[--n_tokens] = list->data;
2414 g_slist_free (tokens);
2421 * @str_array: a %NULL-terminated array of strings to free.
2423 * Frees a %NULL-terminated array of strings, and the array itself.
2424 * If called on a %NULL value, g_strfreev() simply returns.
2427 g_strfreev (gchar **str_array)
2433 for (i = 0; str_array[i] != NULL; i++)
2434 g_free (str_array[i]);
2442 * @str_array: %NULL-terminated array of strings.
2444 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2445 * the new array should be freed by first freeing each string, then
2446 * the array itself. g_strfreev() does this for you. If called
2447 * on a %NULL value, g_strdupv() simply returns %NULL.
2449 * Return value: a new %NULL-terminated array of strings.
2452 g_strdupv (gchar **str_array)
2460 while (str_array[i])
2463 retval = g_new (gchar*, i + 1);
2466 while (str_array[i])
2468 retval[i] = g_strdup (str_array[i]);
2480 g_strjoinv (const gchar *separator,
2486 g_return_val_if_fail (str_array != NULL, NULL);
2488 if (separator == NULL)
2495 gsize separator_len;
2497 separator_len = strlen (separator);
2498 /* First part, getting length */
2499 len = 1 + strlen (str_array[0]);
2500 for (i = 1; str_array[i] != NULL; i++)
2501 len += strlen (str_array[i]);
2502 len += separator_len * (i - 1);
2504 /* Second part, building string */
2505 string = g_new (gchar, len);
2506 ptr = g_stpcpy (string, *str_array);
2507 for (i = 1; str_array[i] != NULL; i++)
2509 ptr = g_stpcpy (ptr, separator);
2510 ptr = g_stpcpy (ptr, str_array[i]);
2514 string = g_strdup ("");
2520 g_strjoin (const gchar *separator,
2526 gsize separator_len;
2529 if (separator == NULL)
2532 separator_len = strlen (separator);
2534 va_start (args, separator);
2536 s = va_arg (args, gchar*);
2540 /* First part, getting length */
2541 len = 1 + strlen (s);
2543 s = va_arg (args, gchar*);
2546 len += separator_len + strlen (s);
2547 s = va_arg (args, gchar*);
2551 /* Second part, building string */
2552 string = g_new (gchar, len);
2554 va_start (args, separator);
2556 s = va_arg (args, gchar*);
2557 ptr = g_stpcpy (string, s);
2559 s = va_arg (args, gchar*);
2562 ptr = g_stpcpy (ptr, separator);
2563 ptr = g_stpcpy (ptr, s);
2564 s = va_arg (args, gchar*);
2568 string = g_strdup ("");
2578 * @haystack: a string.
2579 * @haystack_len: the maximum length of @haystack.
2580 * @needle: the string to search for.
2582 * Searches the string @haystack for the first occurrence
2583 * of the string @needle, limiting the length of the search
2586 * Return value: a pointer to the found occurrence, or
2587 * %NULL if not found.
2590 g_strstr_len (const gchar *haystack,
2591 gssize haystack_len,
2592 const gchar *needle)
2594 g_return_val_if_fail (haystack != NULL, NULL);
2595 g_return_val_if_fail (needle != NULL, NULL);
2597 if (haystack_len < 0)
2598 return strstr (haystack, needle);
2601 const gchar *p = haystack;
2602 gsize needle_len = strlen (needle);
2606 if (needle_len == 0)
2607 return (gchar *)haystack;
2609 if (haystack_len < needle_len)
2612 end = haystack + haystack_len - needle_len;
2614 while (*p && p <= end)
2616 for (i = 0; i < needle_len; i++)
2617 if (p[i] != needle[i])
2632 * @haystack: a nul-terminated string.
2633 * @needle: the nul-terminated string to search for.
2635 * Searches the string @haystack for the last occurrence
2636 * of the string @needle.
2638 * Return value: a pointer to the found occurrence, or
2639 * %NULL if not found.
2642 g_strrstr (const gchar *haystack,
2643 const gchar *needle)
2650 g_return_val_if_fail (haystack != NULL, NULL);
2651 g_return_val_if_fail (needle != NULL, NULL);
2653 needle_len = strlen (needle);
2654 haystack_len = strlen (haystack);
2656 if (needle_len == 0)
2657 return (gchar *)haystack;
2659 if (haystack_len < needle_len)
2662 p = haystack + haystack_len - needle_len;
2664 while (p >= haystack)
2666 for (i = 0; i < needle_len; i++)
2667 if (p[i] != needle[i])
2681 * @haystack: a nul-terminated string.
2682 * @haystack_len: the maximum length of @haystack.
2683 * @needle: the nul-terminated string to search for.
2685 * Searches the string @haystack for the last occurrence
2686 * of the string @needle, limiting the length of the search
2689 * Return value: a pointer to the found occurrence, or
2690 * %NULL if not found.
2693 g_strrstr_len (const gchar *haystack,
2694 gssize haystack_len,
2695 const gchar *needle)
2697 g_return_val_if_fail (haystack != NULL, NULL);
2698 g_return_val_if_fail (needle != NULL, NULL);
2700 if (haystack_len < 0)
2701 return g_strrstr (haystack, needle);
2704 gsize needle_len = strlen (needle);
2705 const gchar *haystack_max = haystack + haystack_len;
2706 const gchar *p = haystack;
2709 while (p < haystack_max && *p)
2712 if (p < haystack + needle_len)
2717 while (p >= haystack)
2719 for (i = 0; i < needle_len; i++)
2720 if (p[i] != needle[i])
2736 * @str: a nul-terminated string.
2737 * @suffix: the nul-terminated suffix to look for.
2739 * Looks whether the string @str ends with @suffix.
2741 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2746 g_str_has_suffix (const gchar *str,
2747 const gchar *suffix)
2752 g_return_val_if_fail (str != NULL, FALSE);
2753 g_return_val_if_fail (suffix != NULL, FALSE);
2755 str_len = strlen (str);
2756 suffix_len = strlen (suffix);
2758 if (str_len < suffix_len)
2761 return strcmp (str + str_len - suffix_len, suffix) == 0;
2766 * @str: a nul-terminated string.
2767 * @prefix: the nul-terminated prefix to look for.
2769 * Looks whether the string @str begins with @prefix.
2771 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2776 g_str_has_prefix (const gchar *str,
2777 const gchar *prefix)
2782 g_return_val_if_fail (str != NULL, FALSE);
2783 g_return_val_if_fail (prefix != NULL, FALSE);
2785 str_len = strlen (str);
2786 prefix_len = strlen (prefix);
2788 if (str_len < prefix_len)
2791 return strncmp (str, prefix, prefix_len) == 0;
2798 * @msgval: another string
2800 * An auxiliary function for gettext() support (see Q_()).
2802 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2803 * a '|' character, in which case a pointer to the substring of msgid after
2804 * the first '|' character is returned.
2808 G_CONST_RETURN gchar *
2809 g_strip_context (const gchar *msgid,
2810 const gchar *msgval)
2812 if (msgval == msgid)
2814 const char *c = strchr (msgid, '|');
2825 * @str_array: a %NULL-terminated array of strings.
2827 * Returns the length of the given %NULL-terminated
2828 * string array @str_array.
2830 * Return value: length of @str_array.
2835 g_strv_length (gchar **str_array)
2839 g_return_val_if_fail (str_array != NULL, 0);
2841 while (str_array[i])
2847 #define __G_STRFUNCS_C__
2848 #include "galiasdef.c"