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);
654 if (base == 1 || base > 36)
664 /* Skip white space. */
668 if (G_UNLIKELY (!*s))
671 /* Check for a sign. */
680 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
683 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
694 /* Save the pointer so we can check later if anything happened. */
696 cutoff = G_MAXUINT64 / base;
697 cutlim = G_MAXUINT64 % base;
704 if (c >= '0' && c <= '9')
706 else if (ISALPHA (c))
707 c = TOUPPER (c) - 'A' + 10;
712 /* Check for overflow. */
713 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
722 /* Check if anything actually happened. */
726 /* Store in ENDPTR the address of one character
727 past the last character we converted. */
729 *endptr = (gchar*) s;
731 if (G_UNLIKELY (overflow))
740 /* We must handle a special case here: the base is 0 or 16 and the
741 first two characters are '0' and 'x', but the rest are no
742 hexadecimal digits. This is no error case. We return 0 and
743 ENDPTR points to the `x`. */
746 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
748 *endptr = (gchar*) &save[-1];
750 /* There was no number to convert. */
751 *endptr = (gchar*) nptr;
758 * @nptr: the string to convert to a numeric value.
759 * @endptr: if non-%NULL, it returns the character after
760 * the last character used in the conversion.
761 * @base: to be used for the conversion, 2..36 or 0
763 * Converts a string to a #guint64 value.
764 * This function behaves like the standard strtoull() function
765 * does in the C locale. It does this without actually
766 * changing the current locale, since that would not be
769 * This function is typically used when reading configuration
770 * files or other non-user input that should be locale independent.
771 * To handle input from the user you should normally use the
772 * locale-sensitive system strtoull() function.
774 * If the correct value would cause overflow, %G_MAXUINT64
775 * is returned, and %ERANGE is stored in %errno. If the base is
776 * outside the valid range, zero is returned, and %EINVAL is stored
777 * in %errno. If the string conversion fails, zero is returned, and
778 * @endptr returns @nptr (if @endptr is non-%NULL).
780 * Return value: the #guint64 value or zero on error.
785 g_ascii_strtoull (const gchar *nptr,
792 result = g_parse_long_long (nptr, endptr, base, &negative);
794 /* Return the result of the appropriate sign. */
795 return negative ? -result : result;
800 * @nptr: the string to convert to a numeric value.
801 * @endptr: if non-%NULL, it returns the character after
802 * the last character used in the conversion.
803 * @base: to be used for the conversion, 2..36 or 0
805 * Converts a string to a #gint64 value.
806 * This function behaves like the standard strtoll() function
807 * does in the C locale. It does this without actually
808 * changing the current locale, since that would not be
811 * This function is typically used when reading configuration
812 * files or other non-user input that should be locale independent.
813 * To handle input from the user you should normally use the
814 * locale-sensitive system strtoll() function.
816 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
817 * is returned, and %ERANGE is stored in %errno. If the base is
818 * outside the valid range, zero is returned, and %EINVAL is stored
819 * in %errno. If the string conversion fails, zero is returned, and
820 * @endptr returns @nptr (if @endptr is non-%NULL).
822 * Return value: the #gint64 value or zero on error.
827 g_ascii_strtoll (const gchar *nptr,
834 result = g_parse_long_long (nptr, endptr, base, &negative);
836 if (negative && result > (guint64) G_MININT64)
841 else if (!negative && result > (guint64) G_MAXINT64)
847 return - (gint64) result;
849 return (gint64) result;
852 G_CONST_RETURN gchar*
853 g_strerror (gint errnum)
855 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
857 int saved_errno = errno;
860 const char *msg_locale;
862 msg_locale = strerror (errnum);
863 if (g_get_charset (NULL))
870 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
873 /* Stick in the quark table so that we can return a static result
875 GQuark msg_quark = g_quark_from_string (msg_utf8);
878 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
887 case E2BIG: return "argument list too long";
890 case EACCES: return "permission denied";
893 case EADDRINUSE: return "address already in use";
896 case EADDRNOTAVAIL: return "can't assign requested address";
899 case EADV: return "advertise error";
902 case EAFNOSUPPORT: return "address family not supported by protocol family";
905 case EAGAIN: return "try again";
908 case EALIGN: return "EALIGN";
911 case EALREADY: return "operation already in progress";
914 case EBADE: return "bad exchange descriptor";
917 case EBADF: return "bad file number";
920 case EBADFD: return "file descriptor in bad state";
923 case EBADMSG: return "not a data message";
926 case EBADR: return "bad request descriptor";
929 case EBADRPC: return "RPC structure is bad";
932 case EBADRQC: return "bad request code";
935 case EBADSLT: return "invalid slot";
938 case EBFONT: return "bad font file format";
941 case EBUSY: return "mount device busy";
944 case ECHILD: return "no children";
947 case ECHRNG: return "channel number out of range";
950 case ECOMM: return "communication error on send";
953 case ECONNABORTED: return "software caused connection abort";
956 case ECONNREFUSED: return "connection refused";
959 case ECONNRESET: return "connection reset by peer";
961 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
962 case EDEADLK: return "resource deadlock avoided";
965 case EDEADLOCK: return "resource deadlock avoided";
968 case EDESTADDRREQ: return "destination address required";
971 case EDIRTY: return "mounting a dirty fs w/o force";
974 case EDOM: return "math argument out of range";
977 case EDOTDOT: return "cross mount point";
980 case EDQUOT: return "disk quota exceeded";
983 case EDUPPKG: return "duplicate package name";
986 case EEXIST: return "file already exists";
989 case EFAULT: return "bad address in system call argument";
992 case EFBIG: return "file too large";
995 case EHOSTDOWN: return "host is down";
998 case EHOSTUNREACH: return "host is unreachable";
1001 case EIDRM: return "identifier removed";
1004 case EINIT: return "initialization error";
1007 case EINPROGRESS: return "operation now in progress";
1010 case EINTR: return "interrupted system call";
1013 case EINVAL: return "invalid argument";
1016 case EIO: return "I/O error";
1019 case EISCONN: return "socket is already connected";
1022 case EISDIR: return "is a directory";
1025 case EISNAM: return "is a name file";
1028 case ELBIN: return "ELBIN";
1031 case EL2HLT: return "level 2 halted";
1034 case EL2NSYNC: return "level 2 not synchronized";
1037 case EL3HLT: return "level 3 halted";
1040 case EL3RST: return "level 3 reset";
1043 case ELIBACC: return "can not access a needed shared library";
1046 case ELIBBAD: return "accessing a corrupted shared library";
1049 case ELIBEXEC: return "can not exec a shared library directly";
1052 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
1055 case ELIBSCN: return ".lib section in a.out corrupted";
1058 case ELNRNG: return "link number out of range";
1061 case ELOOP: return "too many levels of symbolic links";
1064 case EMFILE: return "too many open files";
1067 case EMLINK: return "too many links";
1070 case EMSGSIZE: return "message too long";
1073 case EMULTIHOP: return "multihop attempted";
1076 case ENAMETOOLONG: return "file name too long";
1079 case ENAVAIL: return "not available";
1082 case ENET: return "ENET";
1085 case ENETDOWN: return "network is down";
1088 case ENETRESET: return "network dropped connection on reset";
1091 case ENETUNREACH: return "network is unreachable";
1094 case ENFILE: return "file table overflow";
1097 case ENOANO: return "anode table overflow";
1099 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
1100 case ENOBUFS: return "no buffer space available";
1103 case ENOCSI: return "no CSI structure available";
1106 case ENODATA: return "no data available";
1109 case ENODEV: return "no such device";
1112 case ENOENT: return "no such file or directory";
1115 case ENOEXEC: return "exec format error";
1118 case ENOLCK: return "no locks available";
1121 case ENOLINK: return "link has be severed";
1124 case ENOMEM: return "not enough memory";
1127 case ENOMSG: return "no message of desired type";
1130 case ENONET: return "machine is not on the network";
1133 case ENOPKG: return "package not installed";
1136 case ENOPROTOOPT: return "bad proocol option";
1139 case ENOSPC: return "no space left on device";
1142 case ENOSR: return "out of stream resources";
1145 case ENOSTR: return "not a stream device";
1148 case ENOSYM: return "unresolved symbol name";
1151 case ENOSYS: return "function not implemented";
1154 case ENOTBLK: return "block device required";
1157 case ENOTCONN: return "socket is not connected";
1160 case ENOTDIR: return "not a directory";
1163 case ENOTEMPTY: return "directory not empty";
1166 case ENOTNAM: return "not a name file";
1169 case ENOTSOCK: return "socket operation on non-socket";
1172 case ENOTTY: return "inappropriate device for ioctl";
1175 case ENOTUNIQ: return "name not unique on network";
1178 case ENXIO: return "no such device or address";
1181 case EOPNOTSUPP: return "operation not supported on socket";
1184 case EPERM: return "not owner";
1187 case EPFNOSUPPORT: return "protocol family not supported";
1190 case EPIPE: return "broken pipe";
1193 case EPROCLIM: return "too many processes";
1196 case EPROCUNAVAIL: return "bad procedure for program";
1198 #ifdef EPROGMISMATCH
1199 case EPROGMISMATCH: return "program version wrong";
1202 case EPROGUNAVAIL: return "RPC program not available";
1205 case EPROTO: return "protocol error";
1207 #ifdef EPROTONOSUPPORT
1208 case EPROTONOSUPPORT: return "protocol not suppored";
1211 case EPROTOTYPE: return "protocol wrong type for socket";
1214 case ERANGE: return "math result unrepresentable";
1216 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1217 case EREFUSED: return "EREFUSED";
1220 case EREMCHG: return "remote address changed";
1223 case EREMDEV: return "remote device";
1226 case EREMOTE: return "pathname hit remote file system";
1229 case EREMOTEIO: return "remote i/o error";
1231 #ifdef EREMOTERELEASE
1232 case EREMOTERELEASE: return "EREMOTERELEASE";
1235 case EROFS: return "read-only file system";
1238 case ERPCMISMATCH: return "RPC version is wrong";
1241 case ERREMOTE: return "object is remote";
1244 case ESHUTDOWN: return "can't send afer socket shutdown";
1246 #ifdef ESOCKTNOSUPPORT
1247 case ESOCKTNOSUPPORT: return "socket type not supported";
1250 case ESPIPE: return "invalid seek";
1253 case ESRCH: return "no such process";
1256 case ESRMNT: return "srmount error";
1259 case ESTALE: return "stale remote file handle";
1262 case ESUCCESS: return "Error 0";
1265 case ETIME: return "timer expired";
1268 case ETIMEDOUT: return "connection timed out";
1271 case ETOOMANYREFS: return "too many references: can't splice";
1274 case ETXTBSY: return "text file or pseudo-device busy";
1277 case EUCLEAN: return "structure needs cleaning";
1280 case EUNATCH: return "protocol driver not attached";
1283 case EUSERS: return "too many users";
1286 case EVERSION: return "version mismatch";
1288 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1289 case EWOULDBLOCK: return "operation would block";
1292 case EXDEV: return "cross-domain link";
1295 case EXFULL: return "message tables full";
1298 #else /* NO_SYS_ERRLIST */
1299 extern int sys_nerr;
1300 extern char *sys_errlist[];
1302 if ((errnum > 0) && (errnum <= sys_nerr))
1303 return sys_errlist [errnum];
1304 #endif /* NO_SYS_ERRLIST */
1306 msg = g_static_private_get (&msg_private);
1309 msg = g_new (gchar, 64);
1310 g_static_private_set (&msg_private, msg, g_free);
1313 _g_sprintf (msg, "unknown error (%d)", errnum);
1315 errno = saved_errno;
1319 G_CONST_RETURN gchar*
1320 g_strsignal (gint signum)
1322 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1325 #ifdef HAVE_STRSIGNAL
1326 const char *msg_locale;
1328 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1329 extern const char *strsignal(int);
1331 /* this is declared differently (const) in string.h on BeOS */
1332 extern char *strsignal (int sig);
1333 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1334 msg_locale = strsignal (signum);
1335 if (g_get_charset (NULL))
1339 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1342 /* Stick in the quark table so that we can return a static result
1344 GQuark msg_quark = g_quark_from_string (msg_utf8);
1347 return g_quark_to_string (msg_quark);
1350 #elif NO_SYS_SIGLIST
1354 case SIGHUP: return "Hangup";
1357 case SIGINT: return "Interrupt";
1360 case SIGQUIT: return "Quit";
1363 case SIGILL: return "Illegal instruction";
1366 case SIGTRAP: return "Trace/breakpoint trap";
1369 case SIGABRT: return "IOT trap/Abort";
1372 case SIGBUS: return "Bus error";
1375 case SIGFPE: return "Floating point exception";
1378 case SIGKILL: return "Killed";
1381 case SIGUSR1: return "User defined signal 1";
1384 case SIGSEGV: return "Segmentation fault";
1387 case SIGUSR2: return "User defined signal 2";
1390 case SIGPIPE: return "Broken pipe";
1393 case SIGALRM: return "Alarm clock";
1396 case SIGTERM: return "Terminated";
1399 case SIGSTKFLT: return "Stack fault";
1402 case SIGCHLD: return "Child exited";
1405 case SIGCONT: return "Continued";
1408 case SIGSTOP: return "Stopped (signal)";
1411 case SIGTSTP: return "Stopped";
1414 case SIGTTIN: return "Stopped (tty input)";
1417 case SIGTTOU: return "Stopped (tty output)";
1420 case SIGURG: return "Urgent condition";
1423 case SIGXCPU: return "CPU time limit exceeded";
1426 case SIGXFSZ: return "File size limit exceeded";
1429 case SIGVTALRM: return "Virtual time alarm";
1432 case SIGPROF: return "Profile signal";
1435 case SIGWINCH: return "Window size changed";
1438 case SIGIO: return "Possible I/O";
1441 case SIGPWR: return "Power failure";
1444 case SIGUNUSED: return "Unused signal";
1447 #else /* NO_SYS_SIGLIST */
1449 #ifdef NO_SYS_SIGLIST_DECL
1450 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1453 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1454 #endif /* NO_SYS_SIGLIST */
1456 msg = g_static_private_get (&msg_private);
1459 msg = g_new (gchar, 64);
1460 g_static_private_set (&msg_private, msg, g_free);
1463 _g_sprintf (msg, "unknown signal (%d)", signum);
1468 /* Functions g_strlcpy and g_strlcat were originally developed by
1469 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1470 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1471 * for more information.
1475 /* Use the native ones, if available; they might be implemented in assembly */
1477 g_strlcpy (gchar *dest,
1481 g_return_val_if_fail (dest != NULL, 0);
1482 g_return_val_if_fail (src != NULL, 0);
1484 return strlcpy (dest, src, dest_size);
1488 g_strlcat (gchar *dest,
1492 g_return_val_if_fail (dest != NULL, 0);
1493 g_return_val_if_fail (src != NULL, 0);
1495 return strlcat (dest, src, dest_size);
1498 #else /* ! HAVE_STRLCPY */
1501 * Copy string src to buffer dest (of buffer size dest_size). At most
1502 * dest_size-1 characters will be copied. Always NUL terminates
1503 * (unless dest_size == 0). This function does NOT allocate memory.
1504 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1505 * Returns size of attempted result, strlen(src),
1506 * so if retval >= dest_size, truncation occurred.
1509 g_strlcpy (gchar *dest,
1513 register gchar *d = dest;
1514 register const gchar *s = src;
1515 register gsize n = dest_size;
1517 g_return_val_if_fail (dest != NULL, 0);
1518 g_return_val_if_fail (src != NULL, 0);
1520 /* Copy as many bytes as will fit */
1521 if (n != 0 && --n != 0)
1524 register gchar c = *s++;
1532 /* If not enough room in dest, add NUL and traverse rest of src */
1541 return s - src - 1; /* count does not include NUL */
1546 * Appends string src to buffer dest (of buffer size dest_size).
1547 * At most dest_size-1 characters will be copied.
1548 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1549 * This function does NOT allocate memory.
1550 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1551 * in the dest_size characters of dest to start with).
1552 * Returns size of attempted result, which is
1553 * MIN (dest_size, strlen (original dest)) + strlen (src),
1554 * so if retval >= dest_size, truncation occurred.
1557 g_strlcat (gchar *dest,
1561 register gchar *d = dest;
1562 register const gchar *s = src;
1563 register gsize bytes_left = dest_size;
1564 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1566 g_return_val_if_fail (dest != NULL, 0);
1567 g_return_val_if_fail (src != NULL, 0);
1569 /* Find the end of dst and adjust bytes left but don't go past end */
1570 while (*d != 0 && bytes_left-- != 0)
1573 bytes_left = dest_size - dlength;
1575 if (bytes_left == 0)
1576 return dlength + strlen (s);
1580 if (bytes_left != 1)
1589 return dlength + (s - src); /* count does not include NUL */
1591 #endif /* ! HAVE_STRLCPY */
1596 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1598 * Converts all upper case ASCII letters to lower case ASCII letters.
1600 * Return value: a newly-allocated string, with all the upper case
1601 * characters in @str converted to lower case, with
1602 * semantics that exactly match g_ascii_tolower(). (Note
1603 * that this is unlike the old g_strdown(), which modified
1604 * the string in place.)
1607 g_ascii_strdown (const gchar *str,
1612 g_return_val_if_fail (str != NULL, NULL);
1617 result = g_strndup (str, len);
1618 for (s = result; *s; s++)
1619 *s = g_ascii_tolower (*s);
1627 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1629 * Converts all lower case ASCII letters to upper case ASCII letters.
1631 * Return value: a newly allocated string, with all the lower case
1632 * characters in @str converted to upper case, with
1633 * semantics that exactly match g_ascii_toupper(). (Note
1634 * that this is unlike the old g_strup(), which modified
1635 * the string in place.)
1638 g_ascii_strup (const gchar *str,
1643 g_return_val_if_fail (str != NULL, NULL);
1648 result = g_strndup (str, len);
1649 for (s = result; *s; s++)
1650 *s = g_ascii_toupper (*s);
1657 * @string: the string to convert.
1659 * Converts a string to lower case.
1661 * Return value: the string
1663 * Deprecated:2.2: This function is totally broken for the reasons discussed
1664 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1668 g_strdown (gchar *string)
1672 g_return_val_if_fail (string != NULL, NULL);
1674 s = (guchar *) string;
1683 return (gchar *) string;
1688 * @string: the string to convert.
1690 * Converts a string to upper case.
1692 * Return value: the string
1694 * Deprecated:2.2: This function is totally broken for the reasons discussed
1695 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1698 g_strup (gchar *string)
1702 g_return_val_if_fail (string != NULL, NULL);
1704 s = (guchar *) string;
1713 return (gchar *) string;
1717 g_strreverse (gchar *string)
1719 g_return_val_if_fail (string != NULL, NULL);
1723 register gchar *h, *t;
1726 t = string + strlen (string) - 1;
1745 * @c: any character.
1747 * Convert a character to ASCII lower case.
1749 * Unlike the standard C library tolower() function, this only
1750 * recognizes standard ASCII letters and ignores the locale, returning
1751 * all non-ASCII characters unchanged, even if they are lower case
1752 * letters in a particular character set. Also unlike the standard
1753 * library function, this takes and returns a char, not an int, so
1754 * don't call it on %EOF but no need to worry about casting to #guchar
1755 * before passing a possibly non-ASCII character in.
1757 * Return value: the result of converting @c to lower case.
1758 * If @c is not an ASCII upper case letter,
1759 * @c is returned unchanged.
1762 g_ascii_tolower (gchar c)
1764 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1769 * @c: any character.
1771 * Convert a character to ASCII upper case.
1773 * Unlike the standard C library toupper() function, this only
1774 * recognizes standard ASCII letters and ignores the locale, returning
1775 * all non-ASCII characters unchanged, even if they are upper case
1776 * letters in a particular character set. Also unlike the standard
1777 * library function, this takes and returns a char, not an int, so
1778 * don't call it on %EOF but no need to worry about casting to #guchar
1779 * before passing a possibly non-ASCII character in.
1781 * Return value: the result of converting @c to upper case.
1782 * If @c is not an ASCII lower case letter,
1783 * @c is returned unchanged.
1786 g_ascii_toupper (gchar c)
1788 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1792 * g_ascii_digit_value:
1793 * @c: an ASCII character.
1795 * Determines the numeric value of a character as a decimal
1796 * digit. Differs from g_unichar_digit_value() because it takes
1797 * a char, so there's no worry about sign extension if characters
1800 * Return value: If @c is a decimal digit (according to
1801 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1804 g_ascii_digit_value (gchar c)
1806 if (g_ascii_isdigit (c))
1812 * g_ascii_xdigit_value:
1813 * @c: an ASCII character.
1815 * Determines the numeric value of a character as a hexidecimal
1816 * digit. Differs from g_unichar_xdigit_value() because it takes
1817 * a char, so there's no worry about sign extension if characters
1820 * Return value: If @c is a hex digit (according to
1821 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1824 g_ascii_xdigit_value (gchar c)
1826 if (c >= 'A' && c <= 'F')
1827 return c - 'A' + 10;
1828 if (c >= 'a' && c <= 'f')
1829 return c - 'a' + 10;
1830 return g_ascii_digit_value (c);
1834 * g_ascii_strcasecmp:
1835 * @s1: string to compare with @s2.
1836 * @s2: string to compare with @s1.
1838 * Compare two strings, ignoring the case of ASCII characters.
1840 * Unlike the BSD strcasecmp() function, this only recognizes standard
1841 * ASCII letters and ignores the locale, treating all non-ASCII
1842 * bytes as if they are not letters.
1844 * This function should be used only on strings that are known to be
1845 * in encodings where the bytes corresponding to ASCII letters always
1846 * represent themselves. This includes UTF-8 and the ISO-8859-*
1847 * charsets, but not for instance double-byte encodings like the
1848 * Windows Codepage 932, where the trailing bytes of double-byte
1849 * characters include all ASCII letters. If you compare two CP932
1850 * strings using this function, you will get false matches.
1852 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1853 * or a positive value if @s1 > @s2.
1856 g_ascii_strcasecmp (const gchar *s1,
1861 g_return_val_if_fail (s1 != NULL, 0);
1862 g_return_val_if_fail (s2 != NULL, 0);
1866 c1 = (gint)(guchar) TOLOWER (*s1);
1867 c2 = (gint)(guchar) TOLOWER (*s2);
1873 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1877 * g_ascii_strncasecmp:
1878 * @s1: string to compare with @s2.
1879 * @s2: string to compare with @s1.
1880 * @n: number of characters to compare.
1882 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1883 * characters after the first @n in each string.
1885 * Unlike the BSD strcasecmp() function, this only recognizes standard
1886 * ASCII letters and ignores the locale, treating all non-ASCII
1887 * characters as if they are not letters.
1889 * The same warning as in g_ascii_strcasecmp() applies: Use this
1890 * function only on strings known to be in encodings where bytes
1891 * corresponding to ASCII letters always represent themselves.
1893 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1894 * or a positive value if @s1 > @s2.
1897 g_ascii_strncasecmp (const gchar *s1,
1903 g_return_val_if_fail (s1 != NULL, 0);
1904 g_return_val_if_fail (s2 != NULL, 0);
1906 while (n && *s1 && *s2)
1909 c1 = (gint)(guchar) TOLOWER (*s1);
1910 c2 = (gint)(guchar) TOLOWER (*s2);
1917 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1925 * @s2: a string to compare with @s1.
1927 * A case-insensitive string comparison, corresponding to the standard
1928 * strcasecmp() function on platforms which support it.
1930 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1931 * or a positive value if @s1 > @s2.
1933 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1934 * is deprecated and how to replace it.
1937 g_strcasecmp (const gchar *s1,
1940 #ifdef HAVE_STRCASECMP
1941 g_return_val_if_fail (s1 != NULL, 0);
1942 g_return_val_if_fail (s2 != NULL, 0);
1944 return strcasecmp (s1, s2);
1948 g_return_val_if_fail (s1 != NULL, 0);
1949 g_return_val_if_fail (s2 != NULL, 0);
1953 /* According to A. Cox, some platforms have islower's that
1954 * don't work right on non-uppercase
1956 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1957 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1963 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1970 * @s2: a string to compare with @s1.
1971 * @n: the maximum number of characters to compare.
1973 * A case-insensitive string comparison, corresponding to the standard
1974 * strncasecmp() function on platforms which support it.
1975 * It is similar to g_strcasecmp() except it only compares the first @n
1976 * characters of the strings.
1978 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1979 * or a positive value if @s1 > @s2.
1981 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1982 * comparison by calling toupper()/tolower(). These functions are
1983 * locale-specific and operate on single bytes. However, it is impossible
1984 * to handle things correctly from an I18N standpoint by operating on
1985 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1986 * broken if your string is guaranteed to be ASCII, since it's
1987 * locale-sensitive, and it's broken if your string is localized, since
1988 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1991 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1992 * which only works on ASCII and is not locale-sensitive, and
1993 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1996 g_strncasecmp (const gchar *s1,
2000 #ifdef HAVE_STRNCASECMP
2001 return strncasecmp (s1, s2, n);
2005 g_return_val_if_fail (s1 != NULL, 0);
2006 g_return_val_if_fail (s2 != NULL, 0);
2008 while (n && *s1 && *s2)
2011 /* According to A. Cox, some platforms have islower's that
2012 * don't work right on non-uppercase
2014 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
2015 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
2022 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
2029 g_strdelimit (gchar *string,
2030 const gchar *delimiters,
2035 g_return_val_if_fail (string != NULL, NULL);
2038 delimiters = G_STR_DELIMITERS;
2040 for (c = string; *c; c++)
2042 if (strchr (delimiters, *c))
2050 g_strcanon (gchar *string,
2051 const gchar *valid_chars,
2056 g_return_val_if_fail (string != NULL, NULL);
2057 g_return_val_if_fail (valid_chars != NULL, NULL);
2059 for (c = string; *c; c++)
2061 if (!strchr (valid_chars, *c))
2069 g_strcompress (const gchar *source)
2071 const gchar *p = source, *octal;
2072 gchar *dest = g_malloc (strlen (source) + 1);
2083 g_warning ("g_strcompress: trailing \\");
2085 case '0': case '1': case '2': case '3': case '4':
2086 case '5': case '6': case '7':
2089 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2091 *q = (*q * 8) + (*p - '0');
2112 default: /* Also handles \" and \\ */
2128 g_strescape (const gchar *source,
2129 const gchar *exceptions)
2136 g_return_val_if_fail (source != NULL, NULL);
2138 p = (guchar *) source;
2139 /* Each source byte needs maximally four destination chars (\777) */
2140 q = dest = g_malloc (strlen (source) * 4 + 1);
2142 memset (excmap, 0, 256);
2145 guchar *e = (guchar *) exceptions;
2191 if ((*p < ' ') || (*p >= 0177))
2194 *q++ = '0' + (((*p) >> 6) & 07);
2195 *q++ = '0' + (((*p) >> 3) & 07);
2196 *q++ = '0' + ((*p) & 07);
2210 g_strchug (gchar *string)
2214 g_return_val_if_fail (string != NULL, NULL);
2216 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2219 g_memmove (string, start, strlen ((gchar *) start) + 1);
2225 g_strchomp (gchar *string)
2229 g_return_val_if_fail (string != NULL, NULL);
2231 len = strlen (string);
2234 if (g_ascii_isspace ((guchar) string[len]))
2245 * @string: a string to split.
2246 * @delimiter: a string which specifies the places at which to split the string.
2247 * The delimiter is not included in any of the resulting strings, unless
2248 * @max_tokens is reached.
2249 * @max_tokens: the maximum number of pieces to split @string into. If this is
2250 * less than 1, the string is split completely.
2252 * Splits a string into a maximum of @max_tokens pieces, using the given
2253 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2254 * to the last token.
2256 * As a special case, the result of splitting the empty string "" is an empty
2257 * vector, not a vector containing a single string. The reason for this
2258 * special case is that being able to represent a empty vector is typically
2259 * more useful than consistent handling of empty elements. If you do need
2260 * to represent empty elements, you'll need to check for the empty string
2261 * before calling g_strsplit().
2263 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2264 * g_strfreev() to free it.
2267 g_strsplit (const gchar *string,
2268 const gchar *delimiter,
2271 GSList *string_list = NULL, *slist;
2272 gchar **str_array, *s;
2274 const gchar *remainder;
2276 g_return_val_if_fail (string != NULL, NULL);
2277 g_return_val_if_fail (delimiter != NULL, NULL);
2278 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2281 max_tokens = G_MAXINT;
2284 s = strstr (remainder, delimiter);
2287 gsize delimiter_len = strlen (delimiter);
2289 while (--max_tokens && s)
2293 len = s - remainder;
2294 string_list = g_slist_prepend (string_list,
2295 g_strndup (remainder, len));
2297 remainder = s + delimiter_len;
2298 s = strstr (remainder, delimiter);
2304 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2307 str_array = g_new (gchar*, n + 1);
2309 str_array[n--] = NULL;
2310 for (slist = string_list; slist; slist = slist->next)
2311 str_array[n--] = slist->data;
2313 g_slist_free (string_list);
2320 * @string: The string to be tokenized
2321 * @delimiters: A nul-terminated string containing bytes that are used
2322 * to split the string.
2323 * @max_tokens: The maximum number of tokens to split @string into.
2324 * If this is less than 1, the string is split completely
2326 * Splits @string into a number of tokens not containing any of the characters
2327 * in @delimiter. A token is the (possibly empty) longest string that does not
2328 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2329 * remainder is appended to the last token.
2331 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2332 * %NULL-terminated vector containing the three strings "abc", "def",
2335 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2336 * vector containing the four strings "", "def", "ghi", and "".
2338 * As a special case, the result of splitting the empty string "" is an empty
2339 * vector, not a vector containing a single string. The reason for this
2340 * special case is that being able to represent a empty vector is typically
2341 * more useful than consistent handling of empty elements. If you do need
2342 * to represent empty elements, you'll need to check for the empty string
2343 * before calling g_strsplit_set().
2345 * Note that this function works on bytes not characters, so it can't be used
2346 * to delimit UTF-8 strings for anything but ASCII characters.
2348 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2349 * g_strfreev() to free it.
2354 g_strsplit_set (const gchar *string,
2355 const gchar *delimiters,
2358 gboolean delim_table[256];
2359 GSList *tokens, *list;
2362 const gchar *current;
2366 g_return_val_if_fail (string != NULL, NULL);
2367 g_return_val_if_fail (delimiters != NULL, NULL);
2370 max_tokens = G_MAXINT;
2372 if (*string == '\0')
2374 result = g_new (char *, 1);
2379 memset (delim_table, FALSE, sizeof (delim_table));
2380 for (s = delimiters; *s != '\0'; ++s)
2381 delim_table[*(guchar *)s] = TRUE;
2386 s = current = string;
2389 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2393 token = g_strndup (current, s - current);
2394 tokens = g_slist_prepend (tokens, token);
2403 token = g_strndup (current, s - current);
2404 tokens = g_slist_prepend (tokens, token);
2407 result = g_new (gchar *, n_tokens + 1);
2409 result[n_tokens] = NULL;
2410 for (list = tokens; list != NULL; list = list->next)
2411 result[--n_tokens] = list->data;
2413 g_slist_free (tokens);
2420 * @str_array: a %NULL-terminated array of strings to free.
2422 * Frees a %NULL-terminated array of strings, and the array itself.
2423 * If called on a %NULL value, g_strfreev() simply returns.
2426 g_strfreev (gchar **str_array)
2432 for (i = 0; str_array[i] != NULL; i++)
2433 g_free (str_array[i]);
2441 * @str_array: %NULL-terminated array of strings.
2443 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2444 * the new array should be freed by first freeing each string, then
2445 * the array itself. g_strfreev() does this for you. If called
2446 * on a %NULL value, g_strdupv() simply returns %NULL.
2448 * Return value: a new %NULL-terminated array of strings.
2451 g_strdupv (gchar **str_array)
2459 while (str_array[i])
2462 retval = g_new (gchar*, i + 1);
2465 while (str_array[i])
2467 retval[i] = g_strdup (str_array[i]);
2479 g_strjoinv (const gchar *separator,
2485 g_return_val_if_fail (str_array != NULL, NULL);
2487 if (separator == NULL)
2494 gsize separator_len;
2496 separator_len = strlen (separator);
2497 /* First part, getting length */
2498 len = 1 + strlen (str_array[0]);
2499 for (i = 1; str_array[i] != NULL; i++)
2500 len += strlen (str_array[i]);
2501 len += separator_len * (i - 1);
2503 /* Second part, building string */
2504 string = g_new (gchar, len);
2505 ptr = g_stpcpy (string, *str_array);
2506 for (i = 1; str_array[i] != NULL; i++)
2508 ptr = g_stpcpy (ptr, separator);
2509 ptr = g_stpcpy (ptr, str_array[i]);
2513 string = g_strdup ("");
2519 g_strjoin (const gchar *separator,
2525 gsize separator_len;
2528 if (separator == NULL)
2531 separator_len = strlen (separator);
2533 va_start (args, separator);
2535 s = va_arg (args, gchar*);
2539 /* First part, getting length */
2540 len = 1 + strlen (s);
2542 s = va_arg (args, gchar*);
2545 len += separator_len + strlen (s);
2546 s = va_arg (args, gchar*);
2550 /* Second part, building string */
2551 string = g_new (gchar, len);
2553 va_start (args, separator);
2555 s = va_arg (args, gchar*);
2556 ptr = g_stpcpy (string, s);
2558 s = va_arg (args, gchar*);
2561 ptr = g_stpcpy (ptr, separator);
2562 ptr = g_stpcpy (ptr, s);
2563 s = va_arg (args, gchar*);
2567 string = g_strdup ("");
2577 * @haystack: a string.
2578 * @haystack_len: the maximum length of @haystack.
2579 * @needle: the string to search for.
2581 * Searches the string @haystack for the first occurrence
2582 * of the string @needle, limiting the length of the search
2585 * Return value: a pointer to the found occurrence, or
2586 * %NULL if not found.
2589 g_strstr_len (const gchar *haystack,
2590 gssize haystack_len,
2591 const gchar *needle)
2593 g_return_val_if_fail (haystack != NULL, NULL);
2594 g_return_val_if_fail (needle != NULL, NULL);
2596 if (haystack_len < 0)
2597 return strstr (haystack, needle);
2600 const gchar *p = haystack;
2601 gsize needle_len = strlen (needle);
2605 if (needle_len == 0)
2606 return (gchar *)haystack;
2608 if (haystack_len < needle_len)
2611 end = haystack + haystack_len - needle_len;
2613 while (*p && p <= end)
2615 for (i = 0; i < needle_len; i++)
2616 if (p[i] != needle[i])
2631 * @haystack: a nul-terminated string.
2632 * @needle: the nul-terminated string to search for.
2634 * Searches the string @haystack for the last occurrence
2635 * of the string @needle.
2637 * Return value: a pointer to the found occurrence, or
2638 * %NULL if not found.
2641 g_strrstr (const gchar *haystack,
2642 const gchar *needle)
2649 g_return_val_if_fail (haystack != NULL, NULL);
2650 g_return_val_if_fail (needle != NULL, NULL);
2652 needle_len = strlen (needle);
2653 haystack_len = strlen (haystack);
2655 if (needle_len == 0)
2656 return (gchar *)haystack;
2658 if (haystack_len < needle_len)
2661 p = haystack + haystack_len - needle_len;
2663 while (p >= haystack)
2665 for (i = 0; i < needle_len; i++)
2666 if (p[i] != needle[i])
2680 * @haystack: a nul-terminated string.
2681 * @haystack_len: the maximum length of @haystack.
2682 * @needle: the nul-terminated string to search for.
2684 * Searches the string @haystack for the last occurrence
2685 * of the string @needle, limiting the length of the search
2688 * Return value: a pointer to the found occurrence, or
2689 * %NULL if not found.
2692 g_strrstr_len (const gchar *haystack,
2693 gssize haystack_len,
2694 const gchar *needle)
2696 g_return_val_if_fail (haystack != NULL, NULL);
2697 g_return_val_if_fail (needle != NULL, NULL);
2699 if (haystack_len < 0)
2700 return g_strrstr (haystack, needle);
2703 gsize needle_len = strlen (needle);
2704 const gchar *haystack_max = haystack + haystack_len;
2705 const gchar *p = haystack;
2708 while (p < haystack_max && *p)
2711 if (p < haystack + needle_len)
2716 while (p >= haystack)
2718 for (i = 0; i < needle_len; i++)
2719 if (p[i] != needle[i])
2735 * @str: a nul-terminated string.
2736 * @suffix: the nul-terminated suffix to look for.
2738 * Looks whether the string @str ends with @suffix.
2740 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2745 g_str_has_suffix (const gchar *str,
2746 const gchar *suffix)
2751 g_return_val_if_fail (str != NULL, FALSE);
2752 g_return_val_if_fail (suffix != NULL, FALSE);
2754 str_len = strlen (str);
2755 suffix_len = strlen (suffix);
2757 if (str_len < suffix_len)
2760 return strcmp (str + str_len - suffix_len, suffix) == 0;
2765 * @str: a nul-terminated string.
2766 * @prefix: the nul-terminated prefix to look for.
2768 * Looks whether the string @str begins with @prefix.
2770 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2775 g_str_has_prefix (const gchar *str,
2776 const gchar *prefix)
2781 g_return_val_if_fail (str != NULL, FALSE);
2782 g_return_val_if_fail (prefix != NULL, FALSE);
2784 str_len = strlen (str);
2785 prefix_len = strlen (prefix);
2787 if (str_len < prefix_len)
2790 return strncmp (str, prefix, prefix_len) == 0;
2797 * @msgval: another string
2799 * An auxiliary function for gettext() support (see Q_()).
2801 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2802 * a '|' character, in which case a pointer to the substring of msgid after
2803 * the first '|' character is returned.
2807 G_CONST_RETURN gchar *
2808 g_strip_context (const gchar *msgid,
2809 const gchar *msgval)
2811 if (msgval == msgid)
2813 const char *c = strchr (msgid, '|');
2824 * @str_array: a %NULL-terminated array of strings.
2826 * Returns the length of the given %NULL-terminated
2827 * string array @str_array.
2829 * Return value: length of @str_array.
2834 g_strv_length (gchar **str_array)
2838 g_return_val_if_fail (str_array != NULL, 0);
2840 while (str_array[i])
2846 #define __G_STRFUNCS_C__
2847 #include "galiasdef.c"