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"
56 /* do not include <unistd.h> in this place since it
57 * interferes with g_strsignal() on some OSes
60 static const guint16 ascii_table_data[256] = {
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
63 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
64 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
65 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
68 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
69 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
70 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
71 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
72 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
73 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
74 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
75 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
76 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
77 /* the upper 128 are all zeroes */
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);
119 * @str: the string to duplicate
120 * @n: the maximum number of bytes to copy from @str
122 * Duplicates the first @n bytes of a string, returning a newly-allocated
123 * buffer @n + 1 bytes long which will always be nul-terminated.
124 * If @str is less than @n bytes long the buffer is padded with nuls.
125 * If @str is %NULL it returns %NULL.
126 * The returned value should be freed when no longer needed.
129 * To copy a number of characters from a UTF-8 encoded string, use
130 * g_utf8_strncpy() instead.
133 * Returns: a newly-allocated buffer containing the first @n bytes
134 * of @str, nul-terminated
137 g_strndup (const gchar *str,
144 new_str = g_new (gchar, n + 1);
145 strncpy (new_str, str, n);
156 * @length: the length of the new string
157 * @fill_char: the byte to fill the string with
159 * Creates a new string @length bytes long filled with @fill_char.
160 * The returned string should be freed when no longer needed.
162 * Returns: a newly-allocated string filled the @fill_char
165 g_strnfill (gsize length,
170 str = g_new (gchar, length + 1);
171 memset (str, (guchar)fill_char, length);
179 * @dest: destination buffer.
180 * @src: source string.
182 * Copies a nul-terminated string into the dest buffer, include the
183 * trailing nul, and return a pointer to the trailing nul byte.
184 * This is useful for concatenating multiple strings together
185 * without having to repeatedly scan for the end.
187 * Return value: a pointer to trailing nul byte.
190 g_stpcpy (gchar *dest,
194 g_return_val_if_fail (dest != NULL, NULL);
195 g_return_val_if_fail (src != NULL, NULL);
196 return stpcpy (dest, src);
198 register gchar *d = dest;
199 register const gchar *s = src;
201 g_return_val_if_fail (dest != NULL, NULL);
202 g_return_val_if_fail (src != NULL, NULL);
205 while (*s++ != '\0');
212 g_strdup_vprintf (const gchar *format,
215 gchar *string = NULL;
217 g_vasprintf (&string, format, args);
223 g_strdup_printf (const gchar *format,
229 va_start (args, format);
230 buffer = g_strdup_vprintf (format, args);
237 g_strconcat (const gchar *string1, ...)
248 l = 1 + strlen (string1);
249 va_start (args, string1);
250 s = va_arg (args, gchar*);
254 s = va_arg (args, gchar*);
258 concat = g_new (gchar, l);
261 ptr = g_stpcpy (ptr, string1);
262 va_start (args, string1);
263 s = va_arg (args, gchar*);
266 ptr = g_stpcpy (ptr, s);
267 s = va_arg (args, gchar*);
276 * @nptr: the string to convert to a numeric value.
277 * @endptr: if non-%NULL, it returns the character after
278 * the last character used in the conversion.
280 * Converts a string to a #gdouble value.
281 * It calls the standard strtod() function to handle the conversion, but
282 * if the string is not completely converted it attempts the conversion
283 * again with g_ascii_strtod(), and returns the best match.
285 * This function should seldomly be used. The normal situation when reading
286 * numbers not for human consumption is to use g_ascii_strtod(). Only when
287 * you know that you must expect both locale formatted and C formatted numbers
288 * should you use this. Make sure that you don't pass strings such as comma
289 * separated lists of values, since the commas may be interpreted as a decimal
290 * point in some locales, causing unexpected results.
292 * Return value: the #gdouble value.
295 g_strtod (const gchar *nptr,
303 g_return_val_if_fail (nptr != NULL, 0);
308 val_1 = strtod (nptr, &fail_pos_1);
310 if (fail_pos_1 && fail_pos_1[0] != 0)
311 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
313 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
316 *endptr = fail_pos_1;
322 *endptr = fail_pos_2;
329 * @nptr: the string to convert to a numeric value.
330 * @endptr: if non-%NULL, it returns the character after
331 * the last character used in the conversion.
333 * Converts a string to a #gdouble value.
334 * This function behaves like the standard strtod() function
335 * does in the C locale. It does this without actually
336 * changing the current locale, since that would not be
339 * This function is typically used when reading configuration
340 * files or other non-user input that should be locale independent.
341 * To handle input from the user you should normally use the
342 * locale-sensitive system strtod() function.
344 * To convert from a #gdouble to a string in a locale-insensitive
345 * way, use g_ascii_dtostr().
347 * If the correct value would cause overflow, plus or minus %HUGE_VAL
348 * is returned (according to the sign of the value), and %ERANGE is
349 * stored in %errno. If the correct value would cause underflow,
350 * zero is returned and %ERANGE is stored in %errno.
352 * This function resets %errno before calling strtod() so that
353 * you can reliably detect overflow and underflow.
355 * Return value: the #gdouble value.
358 g_ascii_strtod (const gchar *nptr,
363 struct lconv *locale_data;
364 const char *decimal_point;
365 int decimal_point_len;
366 const char *p, *decimal_point_pos;
367 const char *end = NULL; /* Silence gcc */
370 g_return_val_if_fail (nptr != NULL, 0);
374 locale_data = localeconv ();
375 decimal_point = locale_data->decimal_point;
376 decimal_point_len = strlen (decimal_point);
378 g_assert (decimal_point_len != 0);
380 decimal_point_pos = NULL;
383 if (decimal_point[0] != '.' ||
384 decimal_point[1] != 0)
387 /* Skip leading space */
388 while (g_ascii_isspace (*p))
391 /* Skip leading optional sign */
392 if (*p == '+' || *p == '-')
396 (p[1] == 'x' || p[1] == 'X'))
399 /* HEX - find the (optional) decimal point */
401 while (g_ascii_isxdigit (*p))
405 decimal_point_pos = p++;
407 while (g_ascii_isxdigit (*p))
410 if (*p == 'p' || *p == 'P')
412 if (*p == '+' || *p == '-')
414 while (g_ascii_isdigit (*p))
419 else if (g_ascii_isdigit (*p) || *p == '.')
421 while (g_ascii_isdigit (*p))
425 decimal_point_pos = p++;
427 while (g_ascii_isdigit (*p))
430 if (*p == 'e' || *p == 'E')
432 if (*p == '+' || *p == '-')
434 while (g_ascii_isdigit (*p))
439 /* For the other cases, we need not convert the decimal point */
442 if (decimal_point_pos)
446 /* We need to convert the '.' to the locale specific decimal point */
447 copy = g_malloc (end - nptr + 1 + decimal_point_len);
450 memcpy (c, nptr, decimal_point_pos - nptr);
451 c += decimal_point_pos - nptr;
452 memcpy (c, decimal_point, decimal_point_len);
453 c += decimal_point_len;
454 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
455 c += end - (decimal_point_pos + 1);
459 val = strtod (copy, &fail_pos);
460 strtod_errno = errno;
464 if (fail_pos - copy > decimal_point_pos - nptr)
465 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
467 fail_pos = (char *)nptr + (fail_pos - copy);
477 copy = g_malloc (end - (char *)nptr + 1);
478 memcpy (copy, nptr, end - nptr);
479 *(copy + (end - (char *)nptr)) = 0;
482 val = strtod (copy, &fail_pos);
483 strtod_errno = errno;
487 fail_pos = (char *)nptr + (fail_pos - copy);
495 val = strtod (nptr, &fail_pos);
496 strtod_errno = errno;
502 errno = strtod_errno;
510 * @buffer: A buffer to place the resulting string in
511 * @buf_len: The length of the buffer.
512 * @d: The #gdouble to convert
514 * Converts a #gdouble to a string, using the '.' as
517 * This functions generates enough precision that converting
518 * the string back using g_ascii_strtod() gives the same machine-number
519 * (on machines with IEEE compatible 64bit doubles). It is
520 * guaranteed that the size of the resulting string will never
521 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
523 * Return value: The pointer to the buffer with the converted string.
526 g_ascii_dtostr (gchar *buffer,
530 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
535 * @buffer: A buffer to place the resulting string in
536 * @buf_len: The length of the buffer.
537 * @format: The printf()-style format to use for the
538 * code to use for converting.
539 * @d: The #gdouble to convert
541 * Converts a #gdouble to a string, using the '.' as
542 * decimal point. To format the number you pass in
543 * a printf()-style format string. Allowed conversion
544 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
546 * If you just want to want to serialize the value into a
547 * string, use g_ascii_dtostr().
549 * Return value: The pointer to the buffer with the converted string.
552 g_ascii_formatd (gchar *buffer,
557 struct lconv *locale_data;
558 const char *decimal_point;
559 int decimal_point_len;
564 g_return_val_if_fail (buffer != NULL, NULL);
565 g_return_val_if_fail (format[0] == '%', NULL);
566 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
568 format_char = format[strlen (format) - 1];
570 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
571 format_char == 'f' || format_char == 'F' ||
572 format_char == 'g' || format_char == 'G',
575 if (format[0] != '%')
578 if (strpbrk (format + 1, "'l%"))
581 if (!(format_char == 'e' || format_char == 'E' ||
582 format_char == 'f' || format_char == 'F' ||
583 format_char == 'g' || format_char == 'G'))
587 _g_snprintf (buffer, buf_len, format, d);
589 locale_data = localeconv ();
590 decimal_point = locale_data->decimal_point;
591 decimal_point_len = strlen (decimal_point);
593 g_assert (decimal_point_len != 0);
595 if (decimal_point[0] != '.' ||
596 decimal_point[1] != 0)
600 while (g_ascii_isspace (*p))
603 if (*p == '+' || *p == '-')
606 while (isdigit ((guchar)*p))
609 if (strncmp (p, decimal_point, decimal_point_len) == 0)
613 if (decimal_point_len > 1)
615 rest_len = strlen (p + (decimal_point_len-1));
616 memmove (p, p + (decimal_point_len-1), rest_len);
626 g_parse_long_long (const gchar *nptr,
631 /* this code is based on on the strtol(3) code from GNU libc released under
632 * the GNU Lesser General Public License.
634 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
635 * Free Software Foundation, Inc.
637 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
638 (c) == '\r' || (c) == '\t' || (c) == '\v')
639 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
640 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
641 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
642 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
643 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
648 const gchar *s, *save;
651 g_return_val_if_fail (nptr != NULL, 0);
653 if (base == 1 || base > 36)
661 /* Skip white space. */
665 if (G_UNLIKELY (!*s))
668 /* Check for a sign. */
678 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
681 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
692 /* Save the pointer so we can check later if anything happened. */
694 cutoff = G_MAXUINT64 / base;
695 cutlim = G_MAXUINT64 % base;
702 if (c >= '0' && c <= '9')
704 else if (ISALPHA (c))
705 c = TOUPPER (c) - 'A' + 10;
710 /* Check for overflow. */
711 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
720 /* Check if anything actually happened. */
724 /* Store in ENDPTR the address of one character
725 past the last character we converted. */
727 *endptr = (gchar*) s;
729 if (G_UNLIKELY (overflow))
738 /* We must handle a special case here: the base is 0 or 16 and the
739 first two characters are '0' and 'x', but the rest are no
740 hexadecimal digits. This is no error case. We return 0 and
741 ENDPTR points to the `x`. */
744 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
746 *endptr = (gchar*) &save[-1];
748 /* There was no number to convert. */
749 *endptr = (gchar*) nptr;
756 * @nptr: the string to convert to a numeric value.
757 * @endptr: if non-%NULL, it returns the character after
758 * the last character used in the conversion.
759 * @base: to be used for the conversion, 2..36 or 0
761 * Converts a string to a #guint64 value.
762 * This function behaves like the standard strtoull() function
763 * does in the C locale. It does this without actually
764 * changing the current locale, since that would not be
767 * This function is typically used when reading configuration
768 * files or other non-user input that should be locale independent.
769 * To handle input from the user you should normally use the
770 * locale-sensitive system strtoull() function.
772 * If the correct value would cause overflow, %G_MAXUINT64
773 * is returned, and %ERANGE is stored in %errno. If the base is
774 * outside the valid range, zero is returned, and %EINVAL is stored
775 * in %errno. If the string conversion fails, zero is returned, and
776 * @endptr returns @nptr (if @endptr is non-%NULL).
778 * Return value: the #guint64 value or zero on error.
783 g_ascii_strtoull (const gchar *nptr,
790 result = g_parse_long_long (nptr, endptr, base, &negative);
792 /* Return the result of the appropriate sign. */
793 return negative ? -result : result;
798 * @nptr: the string to convert to a numeric value.
799 * @endptr: if non-%NULL, it returns the character after
800 * the last character used in the conversion.
801 * @base: to be used for the conversion, 2..36 or 0
803 * Converts a string to a #gint64 value.
804 * This function behaves like the standard strtoll() function
805 * does in the C locale. It does this without actually
806 * changing the current locale, since that would not be
809 * This function is typically used when reading configuration
810 * files or other non-user input that should be locale independent.
811 * To handle input from the user you should normally use the
812 * locale-sensitive system strtoll() function.
814 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
815 * is returned, and %ERANGE is stored in %errno. If the base is
816 * outside the valid range, zero is returned, and %EINVAL is stored
817 * in %errno. If the string conversion fails, zero is returned, and
818 * @endptr returns @nptr (if @endptr is non-%NULL).
820 * Return value: the #gint64 value or zero on error.
825 g_ascii_strtoll (const gchar *nptr,
832 result = g_parse_long_long (nptr, endptr, base, &negative);
834 if (negative && result > (guint64) G_MININT64)
839 else if (!negative && result > (guint64) G_MAXINT64)
845 return - (gint64) result;
847 return (gint64) result;
850 G_CONST_RETURN gchar*
851 g_strerror (gint errnum)
853 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
855 int saved_errno = errno;
858 const char *msg_locale;
860 msg_locale = strerror (errnum);
861 if (g_get_charset (NULL))
868 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
871 /* Stick in the quark table so that we can return a static result
873 GQuark msg_quark = g_quark_from_string (msg_utf8);
876 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
885 case E2BIG: return "argument list too long";
888 case EACCES: return "permission denied";
891 case EADDRINUSE: return "address already in use";
894 case EADDRNOTAVAIL: return "can't assign requested address";
897 case EADV: return "advertise error";
900 case EAFNOSUPPORT: return "address family not supported by protocol family";
903 case EAGAIN: return "try again";
906 case EALIGN: return "EALIGN";
909 case EALREADY: return "operation already in progress";
912 case EBADE: return "bad exchange descriptor";
915 case EBADF: return "bad file number";
918 case EBADFD: return "file descriptor in bad state";
921 case EBADMSG: return "not a data message";
924 case EBADR: return "bad request descriptor";
927 case EBADRPC: return "RPC structure is bad";
930 case EBADRQC: return "bad request code";
933 case EBADSLT: return "invalid slot";
936 case EBFONT: return "bad font file format";
939 case EBUSY: return "mount device busy";
942 case ECHILD: return "no children";
945 case ECHRNG: return "channel number out of range";
948 case ECOMM: return "communication error on send";
951 case ECONNABORTED: return "software caused connection abort";
954 case ECONNREFUSED: return "connection refused";
957 case ECONNRESET: return "connection reset by peer";
959 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
960 case EDEADLK: return "resource deadlock avoided";
963 case EDEADLOCK: return "resource deadlock avoided";
966 case EDESTADDRREQ: return "destination address required";
969 case EDIRTY: return "mounting a dirty fs w/o force";
972 case EDOM: return "math argument out of range";
975 case EDOTDOT: return "cross mount point";
978 case EDQUOT: return "disk quota exceeded";
981 case EDUPPKG: return "duplicate package name";
984 case EEXIST: return "file already exists";
987 case EFAULT: return "bad address in system call argument";
990 case EFBIG: return "file too large";
993 case EHOSTDOWN: return "host is down";
996 case EHOSTUNREACH: return "host is unreachable";
999 case EIDRM: return "identifier removed";
1002 case EINIT: return "initialization error";
1005 case EINPROGRESS: return "operation now in progress";
1008 case EINTR: return "interrupted system call";
1011 case EINVAL: return "invalid argument";
1014 case EIO: return "I/O error";
1017 case EISCONN: return "socket is already connected";
1020 case EISDIR: return "is a directory";
1023 case EISNAM: return "is a name file";
1026 case ELBIN: return "ELBIN";
1029 case EL2HLT: return "level 2 halted";
1032 case EL2NSYNC: return "level 2 not synchronized";
1035 case EL3HLT: return "level 3 halted";
1038 case EL3RST: return "level 3 reset";
1041 case ELIBACC: return "can not access a needed shared library";
1044 case ELIBBAD: return "accessing a corrupted shared library";
1047 case ELIBEXEC: return "can not exec a shared library directly";
1050 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
1053 case ELIBSCN: return ".lib section in a.out corrupted";
1056 case ELNRNG: return "link number out of range";
1059 case ELOOP: return "too many levels of symbolic links";
1062 case EMFILE: return "too many open files";
1065 case EMLINK: return "too many links";
1068 case EMSGSIZE: return "message too long";
1071 case EMULTIHOP: return "multihop attempted";
1074 case ENAMETOOLONG: return "file name too long";
1077 case ENAVAIL: return "not available";
1080 case ENET: return "ENET";
1083 case ENETDOWN: return "network is down";
1086 case ENETRESET: return "network dropped connection on reset";
1089 case ENETUNREACH: return "network is unreachable";
1092 case ENFILE: return "file table overflow";
1095 case ENOANO: return "anode table overflow";
1097 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
1098 case ENOBUFS: return "no buffer space available";
1101 case ENOCSI: return "no CSI structure available";
1104 case ENODATA: return "no data available";
1107 case ENODEV: return "no such device";
1110 case ENOENT: return "no such file or directory";
1113 case ENOEXEC: return "exec format error";
1116 case ENOLCK: return "no locks available";
1119 case ENOLINK: return "link has be severed";
1122 case ENOMEM: return "not enough memory";
1125 case ENOMSG: return "no message of desired type";
1128 case ENONET: return "machine is not on the network";
1131 case ENOPKG: return "package not installed";
1134 case ENOPROTOOPT: return "bad proocol option";
1137 case ENOSPC: return "no space left on device";
1140 case ENOSR: return "out of stream resources";
1143 case ENOSTR: return "not a stream device";
1146 case ENOSYM: return "unresolved symbol name";
1149 case ENOSYS: return "function not implemented";
1152 case ENOTBLK: return "block device required";
1155 case ENOTCONN: return "socket is not connected";
1158 case ENOTDIR: return "not a directory";
1161 case ENOTEMPTY: return "directory not empty";
1164 case ENOTNAM: return "not a name file";
1167 case ENOTSOCK: return "socket operation on non-socket";
1170 case ENOTTY: return "inappropriate device for ioctl";
1173 case ENOTUNIQ: return "name not unique on network";
1176 case ENXIO: return "no such device or address";
1179 case EOPNOTSUPP: return "operation not supported on socket";
1182 case EPERM: return "not owner";
1185 case EPFNOSUPPORT: return "protocol family not supported";
1188 case EPIPE: return "broken pipe";
1191 case EPROCLIM: return "too many processes";
1194 case EPROCUNAVAIL: return "bad procedure for program";
1196 #ifdef EPROGMISMATCH
1197 case EPROGMISMATCH: return "program version wrong";
1200 case EPROGUNAVAIL: return "RPC program not available";
1203 case EPROTO: return "protocol error";
1205 #ifdef EPROTONOSUPPORT
1206 case EPROTONOSUPPORT: return "protocol not suppored";
1209 case EPROTOTYPE: return "protocol wrong type for socket";
1212 case ERANGE: return "math result unrepresentable";
1214 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1215 case EREFUSED: return "EREFUSED";
1218 case EREMCHG: return "remote address changed";
1221 case EREMDEV: return "remote device";
1224 case EREMOTE: return "pathname hit remote file system";
1227 case EREMOTEIO: return "remote i/o error";
1229 #ifdef EREMOTERELEASE
1230 case EREMOTERELEASE: return "EREMOTERELEASE";
1233 case EROFS: return "read-only file system";
1236 case ERPCMISMATCH: return "RPC version is wrong";
1239 case ERREMOTE: return "object is remote";
1242 case ESHUTDOWN: return "can't send afer socket shutdown";
1244 #ifdef ESOCKTNOSUPPORT
1245 case ESOCKTNOSUPPORT: return "socket type not supported";
1248 case ESPIPE: return "invalid seek";
1251 case ESRCH: return "no such process";
1254 case ESRMNT: return "srmount error";
1257 case ESTALE: return "stale remote file handle";
1260 case ESUCCESS: return "Error 0";
1263 case ETIME: return "timer expired";
1266 case ETIMEDOUT: return "connection timed out";
1269 case ETOOMANYREFS: return "too many references: can't splice";
1272 case ETXTBSY: return "text file or pseudo-device busy";
1275 case EUCLEAN: return "structure needs cleaning";
1278 case EUNATCH: return "protocol driver not attached";
1281 case EUSERS: return "too many users";
1284 case EVERSION: return "version mismatch";
1286 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1287 case EWOULDBLOCK: return "operation would block";
1290 case EXDEV: return "cross-domain link";
1293 case EXFULL: return "message tables full";
1296 #else /* NO_SYS_ERRLIST */
1297 extern int sys_nerr;
1298 extern char *sys_errlist[];
1300 if ((errnum > 0) && (errnum <= sys_nerr))
1301 return sys_errlist [errnum];
1302 #endif /* NO_SYS_ERRLIST */
1304 msg = g_static_private_get (&msg_private);
1307 msg = g_new (gchar, 64);
1308 g_static_private_set (&msg_private, msg, g_free);
1311 _g_sprintf (msg, "unknown error (%d)", errnum);
1313 errno = saved_errno;
1317 G_CONST_RETURN gchar*
1318 g_strsignal (gint signum)
1320 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1323 #ifdef HAVE_STRSIGNAL
1324 const char *msg_locale;
1326 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1327 extern const char *strsignal(int);
1329 /* this is declared differently (const) in string.h on BeOS */
1330 extern char *strsignal (int sig);
1331 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1332 msg_locale = strsignal (signum);
1333 if (g_get_charset (NULL))
1337 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1340 /* Stick in the quark table so that we can return a static result
1342 GQuark msg_quark = g_quark_from_string (msg_utf8);
1345 return g_quark_to_string (msg_quark);
1348 #elif NO_SYS_SIGLIST
1352 case SIGHUP: return "Hangup";
1355 case SIGINT: return "Interrupt";
1358 case SIGQUIT: return "Quit";
1361 case SIGILL: return "Illegal instruction";
1364 case SIGTRAP: return "Trace/breakpoint trap";
1367 case SIGABRT: return "IOT trap/Abort";
1370 case SIGBUS: return "Bus error";
1373 case SIGFPE: return "Floating point exception";
1376 case SIGKILL: return "Killed";
1379 case SIGUSR1: return "User defined signal 1";
1382 case SIGSEGV: return "Segmentation fault";
1385 case SIGUSR2: return "User defined signal 2";
1388 case SIGPIPE: return "Broken pipe";
1391 case SIGALRM: return "Alarm clock";
1394 case SIGTERM: return "Terminated";
1397 case SIGSTKFLT: return "Stack fault";
1400 case SIGCHLD: return "Child exited";
1403 case SIGCONT: return "Continued";
1406 case SIGSTOP: return "Stopped (signal)";
1409 case SIGTSTP: return "Stopped";
1412 case SIGTTIN: return "Stopped (tty input)";
1415 case SIGTTOU: return "Stopped (tty output)";
1418 case SIGURG: return "Urgent condition";
1421 case SIGXCPU: return "CPU time limit exceeded";
1424 case SIGXFSZ: return "File size limit exceeded";
1427 case SIGVTALRM: return "Virtual time alarm";
1430 case SIGPROF: return "Profile signal";
1433 case SIGWINCH: return "Window size changed";
1436 case SIGIO: return "Possible I/O";
1439 case SIGPWR: return "Power failure";
1442 case SIGUNUSED: return "Unused signal";
1445 #else /* NO_SYS_SIGLIST */
1447 #ifdef NO_SYS_SIGLIST_DECL
1448 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1451 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1452 #endif /* NO_SYS_SIGLIST */
1454 msg = g_static_private_get (&msg_private);
1457 msg = g_new (gchar, 64);
1458 g_static_private_set (&msg_private, msg, g_free);
1461 _g_sprintf (msg, "unknown signal (%d)", signum);
1466 /* Functions g_strlcpy and g_strlcat were originally developed by
1467 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1468 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1469 * for more information.
1473 /* Use the native ones, if available; they might be implemented in assembly */
1475 g_strlcpy (gchar *dest,
1479 g_return_val_if_fail (dest != NULL, 0);
1480 g_return_val_if_fail (src != NULL, 0);
1482 return strlcpy (dest, src, dest_size);
1486 g_strlcat (gchar *dest,
1490 g_return_val_if_fail (dest != NULL, 0);
1491 g_return_val_if_fail (src != NULL, 0);
1493 return strlcat (dest, src, dest_size);
1496 #else /* ! HAVE_STRLCPY */
1499 * Copy string src to buffer dest (of buffer size dest_size). At most
1500 * dest_size-1 characters will be copied. Always NUL terminates
1501 * (unless dest_size == 0). This function does NOT allocate memory.
1502 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1503 * Returns size of attempted result, strlen(src),
1504 * so if retval >= dest_size, truncation occurred.
1507 g_strlcpy (gchar *dest,
1511 register gchar *d = dest;
1512 register const gchar *s = src;
1513 register gsize n = dest_size;
1515 g_return_val_if_fail (dest != NULL, 0);
1516 g_return_val_if_fail (src != NULL, 0);
1518 /* Copy as many bytes as will fit */
1519 if (n != 0 && --n != 0)
1522 register gchar c = *s++;
1530 /* If not enough room in dest, add NUL and traverse rest of src */
1539 return s - src - 1; /* count does not include NUL */
1544 * Appends string src to buffer dest (of buffer size dest_size).
1545 * At most dest_size-1 characters will be copied.
1546 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1547 * This function does NOT allocate memory.
1548 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1549 * in the dest_size characters of dest to start with).
1550 * Returns size of attempted result, which is
1551 * MIN (dest_size, strlen (original dest)) + strlen (src),
1552 * so if retval >= dest_size, truncation occurred.
1555 g_strlcat (gchar *dest,
1559 register gchar *d = dest;
1560 register const gchar *s = src;
1561 register gsize bytes_left = dest_size;
1562 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1564 g_return_val_if_fail (dest != NULL, 0);
1565 g_return_val_if_fail (src != NULL, 0);
1567 /* Find the end of dst and adjust bytes left but don't go past end */
1568 while (*d != 0 && bytes_left-- != 0)
1571 bytes_left = dest_size - dlength;
1573 if (bytes_left == 0)
1574 return dlength + strlen (s);
1578 if (bytes_left != 1)
1587 return dlength + (s - src); /* count does not include NUL */
1589 #endif /* ! HAVE_STRLCPY */
1594 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1596 * Converts all upper case ASCII letters to lower case ASCII letters.
1598 * Return value: a newly-allocated string, with all the upper case
1599 * characters in @str converted to lower case, with
1600 * semantics that exactly match g_ascii_tolower(). (Note
1601 * that this is unlike the old g_strdown(), which modified
1602 * the string in place.)
1605 g_ascii_strdown (const gchar *str,
1610 g_return_val_if_fail (str != NULL, NULL);
1615 result = g_strndup (str, len);
1616 for (s = result; *s; s++)
1617 *s = g_ascii_tolower (*s);
1625 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1627 * Converts all lower case ASCII letters to upper case ASCII letters.
1629 * Return value: a newly allocated string, with all the lower case
1630 * characters in @str converted to upper case, with
1631 * semantics that exactly match g_ascii_toupper(). (Note
1632 * that this is unlike the old g_strup(), which modified
1633 * the string in place.)
1636 g_ascii_strup (const gchar *str,
1641 g_return_val_if_fail (str != NULL, NULL);
1646 result = g_strndup (str, len);
1647 for (s = result; *s; s++)
1648 *s = g_ascii_toupper (*s);
1655 * @string: the string to convert.
1657 * Converts a string to lower case.
1659 * Return value: the string
1661 * Deprecated:2.2: This function is totally broken for the reasons discussed
1662 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1666 g_strdown (gchar *string)
1670 g_return_val_if_fail (string != NULL, NULL);
1672 s = (guchar *) string;
1681 return (gchar *) string;
1686 * @string: the string to convert.
1688 * Converts a string to upper case.
1690 * Return value: the string
1692 * Deprecated:2.2: This function is totally broken for the reasons discussed
1693 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1696 g_strup (gchar *string)
1700 g_return_val_if_fail (string != NULL, NULL);
1702 s = (guchar *) string;
1711 return (gchar *) string;
1715 g_strreverse (gchar *string)
1717 g_return_val_if_fail (string != NULL, NULL);
1721 register gchar *h, *t;
1724 t = string + strlen (string) - 1;
1743 * @c: any character.
1745 * Convert a character to ASCII lower case.
1747 * Unlike the standard C library tolower() function, this only
1748 * recognizes standard ASCII letters and ignores the locale, returning
1749 * all non-ASCII characters unchanged, even if they are lower case
1750 * letters in a particular character set. Also unlike the standard
1751 * library function, this takes and returns a char, not an int, so
1752 * don't call it on %EOF but no need to worry about casting to #guchar
1753 * before passing a possibly non-ASCII character in.
1755 * Return value: the result of converting @c to lower case.
1756 * If @c is not an ASCII upper case letter,
1757 * @c is returned unchanged.
1760 g_ascii_tolower (gchar c)
1762 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1767 * @c: any character.
1769 * Convert a character to ASCII upper case.
1771 * Unlike the standard C library toupper() function, this only
1772 * recognizes standard ASCII letters and ignores the locale, returning
1773 * all non-ASCII characters unchanged, even if they are upper case
1774 * letters in a particular character set. Also unlike the standard
1775 * library function, this takes and returns a char, not an int, so
1776 * don't call it on %EOF but no need to worry about casting to #guchar
1777 * before passing a possibly non-ASCII character in.
1779 * Return value: the result of converting @c to upper case.
1780 * If @c is not an ASCII lower case letter,
1781 * @c is returned unchanged.
1784 g_ascii_toupper (gchar c)
1786 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1790 * g_ascii_digit_value:
1791 * @c: an ASCII character.
1793 * Determines the numeric value of a character as a decimal
1794 * digit. Differs from g_unichar_digit_value() because it takes
1795 * a char, so there's no worry about sign extension if characters
1798 * Return value: If @c is a decimal digit (according to
1799 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1802 g_ascii_digit_value (gchar c)
1804 if (g_ascii_isdigit (c))
1810 * g_ascii_xdigit_value:
1811 * @c: an ASCII character.
1813 * Determines the numeric value of a character as a hexidecimal
1814 * digit. Differs from g_unichar_xdigit_value() because it takes
1815 * a char, so there's no worry about sign extension if characters
1818 * Return value: If @c is a hex digit (according to
1819 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1822 g_ascii_xdigit_value (gchar c)
1824 if (c >= 'A' && c <= 'F')
1825 return c - 'A' + 10;
1826 if (c >= 'a' && c <= 'f')
1827 return c - 'a' + 10;
1828 return g_ascii_digit_value (c);
1832 * g_ascii_strcasecmp:
1833 * @s1: string to compare with @s2.
1834 * @s2: string to compare with @s1.
1836 * Compare two strings, ignoring the case of ASCII characters.
1838 * Unlike the BSD strcasecmp() function, this only recognizes standard
1839 * ASCII letters and ignores the locale, treating all non-ASCII
1840 * bytes as if they are not letters.
1842 * This function should be used only on strings that are known to be
1843 * in encodings where the bytes corresponding to ASCII letters always
1844 * represent themselves. This includes UTF-8 and the ISO-8859-*
1845 * charsets, but not for instance double-byte encodings like the
1846 * Windows Codepage 932, where the trailing bytes of double-byte
1847 * characters include all ASCII letters. If you compare two CP932
1848 * strings using this function, you will get false matches.
1850 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1851 * or a positive value if @s1 > @s2.
1854 g_ascii_strcasecmp (const gchar *s1,
1859 g_return_val_if_fail (s1 != NULL, 0);
1860 g_return_val_if_fail (s2 != NULL, 0);
1864 c1 = (gint)(guchar) TOLOWER (*s1);
1865 c2 = (gint)(guchar) TOLOWER (*s2);
1871 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1875 * g_ascii_strncasecmp:
1876 * @s1: string to compare with @s2.
1877 * @s2: string to compare with @s1.
1878 * @n: number of characters to compare.
1880 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1881 * characters after the first @n in each string.
1883 * Unlike the BSD strcasecmp() function, this only recognizes standard
1884 * ASCII letters and ignores the locale, treating all non-ASCII
1885 * characters as if they are not letters.
1887 * The same warning as in g_ascii_strcasecmp() applies: Use this
1888 * function only on strings known to be in encodings where bytes
1889 * corresponding to ASCII letters always represent themselves.
1891 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1892 * or a positive value if @s1 > @s2.
1895 g_ascii_strncasecmp (const gchar *s1,
1901 g_return_val_if_fail (s1 != NULL, 0);
1902 g_return_val_if_fail (s2 != NULL, 0);
1904 while (n && *s1 && *s2)
1907 c1 = (gint)(guchar) TOLOWER (*s1);
1908 c2 = (gint)(guchar) TOLOWER (*s2);
1915 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1923 * @s2: a string to compare with @s1.
1925 * A case-insensitive string comparison, corresponding to the standard
1926 * strcasecmp() function on platforms which support it.
1928 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1929 * or a positive value if @s1 > @s2.
1931 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1932 * is deprecated and how to replace it.
1935 g_strcasecmp (const gchar *s1,
1938 #ifdef HAVE_STRCASECMP
1939 g_return_val_if_fail (s1 != NULL, 0);
1940 g_return_val_if_fail (s2 != NULL, 0);
1942 return strcasecmp (s1, s2);
1946 g_return_val_if_fail (s1 != NULL, 0);
1947 g_return_val_if_fail (s2 != NULL, 0);
1951 /* According to A. Cox, some platforms have islower's that
1952 * don't work right on non-uppercase
1954 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1955 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1961 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1968 * @s2: a string to compare with @s1.
1969 * @n: the maximum number of characters to compare.
1971 * A case-insensitive string comparison, corresponding to the standard
1972 * strncasecmp() function on platforms which support it.
1973 * It is similar to g_strcasecmp() except it only compares the first @n
1974 * characters of the strings.
1976 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1977 * or a positive value if @s1 > @s2.
1979 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1980 * comparison by calling toupper()/tolower(). These functions are
1981 * locale-specific and operate on single bytes. However, it is impossible
1982 * to handle things correctly from an I18N standpoint by operating on
1983 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1984 * broken if your string is guaranteed to be ASCII, since it's
1985 * locale-sensitive, and it's broken if your string is localized, since
1986 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1989 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1990 * which only works on ASCII and is not locale-sensitive, and
1991 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1994 g_strncasecmp (const gchar *s1,
1998 #ifdef HAVE_STRNCASECMP
1999 return strncasecmp (s1, s2, n);
2003 g_return_val_if_fail (s1 != NULL, 0);
2004 g_return_val_if_fail (s2 != NULL, 0);
2006 while (n && *s1 && *s2)
2009 /* According to A. Cox, some platforms have islower's that
2010 * don't work right on non-uppercase
2012 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
2013 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
2020 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
2027 g_strdelimit (gchar *string,
2028 const gchar *delimiters,
2033 g_return_val_if_fail (string != NULL, NULL);
2036 delimiters = G_STR_DELIMITERS;
2038 for (c = string; *c; c++)
2040 if (strchr (delimiters, *c))
2048 g_strcanon (gchar *string,
2049 const gchar *valid_chars,
2054 g_return_val_if_fail (string != NULL, NULL);
2055 g_return_val_if_fail (valid_chars != NULL, NULL);
2057 for (c = string; *c; c++)
2059 if (!strchr (valid_chars, *c))
2067 g_strcompress (const gchar *source)
2069 const gchar *p = source, *octal;
2070 gchar *dest = g_malloc (strlen (source) + 1);
2081 g_warning ("g_strcompress: trailing \\");
2083 case '0': case '1': case '2': case '3': case '4':
2084 case '5': case '6': case '7':
2087 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2089 *q = (*q * 8) + (*p - '0');
2110 default: /* Also handles \" and \\ */
2126 g_strescape (const gchar *source,
2127 const gchar *exceptions)
2134 g_return_val_if_fail (source != NULL, NULL);
2136 p = (guchar *) source;
2137 /* Each source byte needs maximally four destination chars (\777) */
2138 q = dest = g_malloc (strlen (source) * 4 + 1);
2140 memset (excmap, 0, 256);
2143 guchar *e = (guchar *) exceptions;
2189 if ((*p < ' ') || (*p >= 0177))
2192 *q++ = '0' + (((*p) >> 6) & 07);
2193 *q++ = '0' + (((*p) >> 3) & 07);
2194 *q++ = '0' + ((*p) & 07);
2208 g_strchug (gchar *string)
2212 g_return_val_if_fail (string != NULL, NULL);
2214 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2217 g_memmove (string, start, strlen ((gchar *) start) + 1);
2223 g_strchomp (gchar *string)
2227 g_return_val_if_fail (string != NULL, NULL);
2229 len = strlen (string);
2232 if (g_ascii_isspace ((guchar) string[len]))
2243 * @string: a string to split.
2244 * @delimiter: a string which specifies the places at which to split the string.
2245 * The delimiter is not included in any of the resulting strings, unless
2246 * @max_tokens is reached.
2247 * @max_tokens: the maximum number of pieces to split @string into. If this is
2248 * less than 1, the string is split completely.
2250 * Splits a string into a maximum of @max_tokens pieces, using the given
2251 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2252 * to the last token.
2254 * As a special case, the result of splitting the empty string "" is an empty
2255 * vector, not a vector containing a single string. The reason for this
2256 * special case is that being able to represent a empty vector is typically
2257 * more useful than consistent handling of empty elements. If you do need
2258 * to represent empty elements, you'll need to check for the empty string
2259 * before calling g_strsplit().
2261 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2262 * g_strfreev() to free it.
2265 g_strsplit (const gchar *string,
2266 const gchar *delimiter,
2269 GSList *string_list = NULL, *slist;
2270 gchar **str_array, *s;
2272 const gchar *remainder;
2274 g_return_val_if_fail (string != NULL, NULL);
2275 g_return_val_if_fail (delimiter != NULL, NULL);
2276 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2279 max_tokens = G_MAXINT;
2282 s = strstr (remainder, delimiter);
2285 gsize delimiter_len = strlen (delimiter);
2287 while (--max_tokens && s)
2291 len = s - remainder;
2292 string_list = g_slist_prepend (string_list,
2293 g_strndup (remainder, len));
2295 remainder = s + delimiter_len;
2296 s = strstr (remainder, delimiter);
2302 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2305 str_array = g_new (gchar*, n + 1);
2307 str_array[n--] = NULL;
2308 for (slist = string_list; slist; slist = slist->next)
2309 str_array[n--] = slist->data;
2311 g_slist_free (string_list);
2318 * @string: The string to be tokenized
2319 * @delimiters: A nul-terminated string containing bytes that are used
2320 * to split the string.
2321 * @max_tokens: The maximum number of tokens to split @string into.
2322 * If this is less than 1, the string is split completely
2324 * Splits @string into a number of tokens not containing any of the characters
2325 * in @delimiter. A token is the (possibly empty) longest string that does not
2326 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2327 * remainder is appended to the last token.
2329 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2330 * %NULL-terminated vector containing the three strings "abc", "def",
2333 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2334 * vector containing the four strings "", "def", "ghi", and "".
2336 * As a special case, the result of splitting the empty string "" is an empty
2337 * vector, not a vector containing a single string. The reason for this
2338 * special case is that being able to represent a empty vector is typically
2339 * more useful than consistent handling of empty elements. If you do need
2340 * to represent empty elements, you'll need to check for the empty string
2341 * before calling g_strsplit_set().
2343 * Note that this function works on bytes not characters, so it can't be used
2344 * to delimit UTF-8 strings for anything but ASCII characters.
2346 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2347 * g_strfreev() to free it.
2352 g_strsplit_set (const gchar *string,
2353 const gchar *delimiters,
2356 gboolean delim_table[256];
2357 GSList *tokens, *list;
2360 const gchar *current;
2364 g_return_val_if_fail (string != NULL, NULL);
2365 g_return_val_if_fail (delimiters != NULL, NULL);
2368 max_tokens = G_MAXINT;
2370 if (*string == '\0')
2372 result = g_new (char *, 1);
2377 memset (delim_table, FALSE, sizeof (delim_table));
2378 for (s = delimiters; *s != '\0'; ++s)
2379 delim_table[*(guchar *)s] = TRUE;
2384 s = current = string;
2387 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2391 token = g_strndup (current, s - current);
2392 tokens = g_slist_prepend (tokens, token);
2401 token = g_strndup (current, s - current);
2402 tokens = g_slist_prepend (tokens, token);
2405 result = g_new (gchar *, n_tokens + 1);
2407 result[n_tokens] = NULL;
2408 for (list = tokens; list != NULL; list = list->next)
2409 result[--n_tokens] = list->data;
2411 g_slist_free (tokens);
2418 * @str_array: a %NULL-terminated array of strings to free.
2420 * Frees a %NULL-terminated array of strings, and the array itself.
2421 * If called on a %NULL value, g_strfreev() simply returns.
2424 g_strfreev (gchar **str_array)
2430 for (i = 0; str_array[i] != NULL; i++)
2431 g_free (str_array[i]);
2439 * @str_array: %NULL-terminated array of strings.
2441 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2442 * the new array should be freed by first freeing each string, then
2443 * the array itself. g_strfreev() does this for you. If called
2444 * on a %NULL value, g_strdupv() simply returns %NULL.
2446 * Return value: a new %NULL-terminated array of strings.
2449 g_strdupv (gchar **str_array)
2457 while (str_array[i])
2460 retval = g_new (gchar*, i + 1);
2463 while (str_array[i])
2465 retval[i] = g_strdup (str_array[i]);
2477 g_strjoinv (const gchar *separator,
2483 g_return_val_if_fail (str_array != NULL, NULL);
2485 if (separator == NULL)
2492 gsize separator_len;
2494 separator_len = strlen (separator);
2495 /* First part, getting length */
2496 len = 1 + strlen (str_array[0]);
2497 for (i = 1; str_array[i] != NULL; i++)
2498 len += strlen (str_array[i]);
2499 len += separator_len * (i - 1);
2501 /* Second part, building string */
2502 string = g_new (gchar, len);
2503 ptr = g_stpcpy (string, *str_array);
2504 for (i = 1; str_array[i] != NULL; i++)
2506 ptr = g_stpcpy (ptr, separator);
2507 ptr = g_stpcpy (ptr, str_array[i]);
2511 string = g_strdup ("");
2517 g_strjoin (const gchar *separator,
2523 gsize separator_len;
2526 if (separator == NULL)
2529 separator_len = strlen (separator);
2531 va_start (args, separator);
2533 s = va_arg (args, gchar*);
2537 /* First part, getting length */
2538 len = 1 + strlen (s);
2540 s = va_arg (args, gchar*);
2543 len += separator_len + strlen (s);
2544 s = va_arg (args, gchar*);
2548 /* Second part, building string */
2549 string = g_new (gchar, len);
2551 va_start (args, separator);
2553 s = va_arg (args, gchar*);
2554 ptr = g_stpcpy (string, s);
2556 s = va_arg (args, gchar*);
2559 ptr = g_stpcpy (ptr, separator);
2560 ptr = g_stpcpy (ptr, s);
2561 s = va_arg (args, gchar*);
2565 string = g_strdup ("");
2575 * @haystack: a string.
2576 * @haystack_len: the maximum length of @haystack.
2577 * @needle: the string to search for.
2579 * Searches the string @haystack for the first occurrence
2580 * of the string @needle, limiting the length of the search
2583 * Return value: a pointer to the found occurrence, or
2584 * %NULL if not found.
2587 g_strstr_len (const gchar *haystack,
2588 gssize haystack_len,
2589 const gchar *needle)
2591 g_return_val_if_fail (haystack != NULL, NULL);
2592 g_return_val_if_fail (needle != NULL, NULL);
2594 if (haystack_len < 0)
2595 return strstr (haystack, needle);
2598 const gchar *p = haystack;
2599 gsize needle_len = strlen (needle);
2603 if (needle_len == 0)
2604 return (gchar *)haystack;
2606 if (haystack_len < needle_len)
2609 end = haystack + haystack_len - needle_len;
2611 while (*p && p <= end)
2613 for (i = 0; i < needle_len; i++)
2614 if (p[i] != needle[i])
2629 * @haystack: a nul-terminated string.
2630 * @needle: the nul-terminated string to search for.
2632 * Searches the string @haystack for the last occurrence
2633 * of the string @needle.
2635 * Return value: a pointer to the found occurrence, or
2636 * %NULL if not found.
2639 g_strrstr (const gchar *haystack,
2640 const gchar *needle)
2647 g_return_val_if_fail (haystack != NULL, NULL);
2648 g_return_val_if_fail (needle != NULL, NULL);
2650 needle_len = strlen (needle);
2651 haystack_len = strlen (haystack);
2653 if (needle_len == 0)
2654 return (gchar *)haystack;
2656 if (haystack_len < needle_len)
2659 p = haystack + haystack_len - needle_len;
2661 while (p >= haystack)
2663 for (i = 0; i < needle_len; i++)
2664 if (p[i] != needle[i])
2678 * @haystack: a nul-terminated string.
2679 * @haystack_len: the maximum length of @haystack.
2680 * @needle: the nul-terminated string to search for.
2682 * Searches the string @haystack for the last occurrence
2683 * of the string @needle, limiting the length of the search
2686 * Return value: a pointer to the found occurrence, or
2687 * %NULL if not found.
2690 g_strrstr_len (const gchar *haystack,
2691 gssize haystack_len,
2692 const gchar *needle)
2694 g_return_val_if_fail (haystack != NULL, NULL);
2695 g_return_val_if_fail (needle != NULL, NULL);
2697 if (haystack_len < 0)
2698 return g_strrstr (haystack, needle);
2701 gsize needle_len = strlen (needle);
2702 const gchar *haystack_max = haystack + haystack_len;
2703 const gchar *p = haystack;
2706 while (p < haystack_max && *p)
2709 if (p < haystack + needle_len)
2714 while (p >= haystack)
2716 for (i = 0; i < needle_len; i++)
2717 if (p[i] != needle[i])
2733 * @str: a nul-terminated string.
2734 * @suffix: the nul-terminated suffix to look for.
2736 * Looks whether the string @str ends with @suffix.
2738 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2743 g_str_has_suffix (const gchar *str,
2744 const gchar *suffix)
2749 g_return_val_if_fail (str != NULL, FALSE);
2750 g_return_val_if_fail (suffix != NULL, FALSE);
2752 str_len = strlen (str);
2753 suffix_len = strlen (suffix);
2755 if (str_len < suffix_len)
2758 return strcmp (str + str_len - suffix_len, suffix) == 0;
2763 * @str: a nul-terminated string.
2764 * @prefix: the nul-terminated prefix to look for.
2766 * Looks whether the string @str begins with @prefix.
2768 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2773 g_str_has_prefix (const gchar *str,
2774 const gchar *prefix)
2779 g_return_val_if_fail (str != NULL, FALSE);
2780 g_return_val_if_fail (prefix != NULL, FALSE);
2782 str_len = strlen (str);
2783 prefix_len = strlen (prefix);
2785 if (str_len < prefix_len)
2788 return strncmp (str, prefix, prefix_len) == 0;
2795 * @msgval: another string
2797 * An auxiliary function for gettext() support (see Q_()).
2799 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2800 * a '|' character, in which case a pointer to the substring of msgid after
2801 * the first '|' character is returned.
2805 G_CONST_RETURN gchar *
2806 g_strip_context (const gchar *msgid,
2807 const gchar *msgval)
2809 if (msgval == msgid)
2811 const char *c = strchr (msgid, '|');
2822 * @str_array: a %NULL-terminated array of strings.
2824 * Returns the length of the given %NULL-terminated
2825 * string array @str_array.
2827 * Return value: length of @str_array.
2832 g_strv_length (gchar **str_array)
2836 g_return_val_if_fail (str_array != NULL, 0);
2838 while (str_array[i])
2844 #define __G_STRFUNCS_C__
2845 #include "galiasdef.c"