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);
118 g_strndup (const gchar *str,
125 new_str = g_new (gchar, n + 1);
126 strncpy (new_str, str, n);
136 g_strnfill (gsize length,
141 str = g_new (gchar, length + 1);
142 memset (str, (guchar)fill_char, length);
150 * @dest: destination buffer.
151 * @src: source string.
153 * Copies a nul-terminated string into the dest buffer, include the
154 * trailing nul, and return a pointer to the trailing nul byte.
155 * This is useful for concatenating multiple strings together
156 * without having to repeatedly scan for the end.
158 * Return value: a pointer to trailing nul byte.
161 g_stpcpy (gchar *dest,
165 g_return_val_if_fail (dest != NULL, NULL);
166 g_return_val_if_fail (src != NULL, NULL);
167 return stpcpy (dest, src);
169 register gchar *d = dest;
170 register const gchar *s = src;
172 g_return_val_if_fail (dest != NULL, NULL);
173 g_return_val_if_fail (src != NULL, NULL);
176 while (*s++ != '\0');
183 g_strdup_vprintf (const gchar *format,
186 gchar *string = NULL;
188 g_vasprintf (&string, format, args);
194 g_strdup_printf (const gchar *format,
200 va_start (args, format);
201 buffer = g_strdup_vprintf (format, args);
208 g_strconcat (const gchar *string1, ...)
219 l = 1 + strlen (string1);
220 va_start (args, string1);
221 s = va_arg (args, gchar*);
225 s = va_arg (args, gchar*);
229 concat = g_new (gchar, l);
232 ptr = g_stpcpy (ptr, string1);
233 va_start (args, string1);
234 s = va_arg (args, gchar*);
237 ptr = g_stpcpy (ptr, s);
238 s = va_arg (args, gchar*);
247 * @nptr: the string to convert to a numeric value.
248 * @endptr: if non-%NULL, it returns the character after
249 * the last character used in the conversion.
251 * Converts a string to a #gdouble value.
252 * It calls the standard strtod() function to handle the conversion, but
253 * if the string is not completely converted it attempts the conversion
254 * again with g_ascii_strtod(), and returns the best match.
256 * This function should seldomly be used. The normal situation when reading
257 * numbers not for human consumption is to use g_ascii_strtod(). Only when
258 * you know that you must expect both locale formatted and C formatted numbers
259 * should you use this. Make sure that you don't pass strings such as comma
260 * separated lists of values, since the commas may be interpreted as a decimal
261 * point in some locales, causing unexpected results.
263 * Return value: the #gdouble value.
266 g_strtod (const gchar *nptr,
274 g_return_val_if_fail (nptr != NULL, 0);
279 val_1 = strtod (nptr, &fail_pos_1);
281 if (fail_pos_1 && fail_pos_1[0] != 0)
282 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
284 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
287 *endptr = fail_pos_1;
293 *endptr = fail_pos_2;
300 * @nptr: the string to convert to a numeric value.
301 * @endptr: if non-%NULL, it returns the character after
302 * the last character used in the conversion.
304 * Converts a string to a #gdouble value.
305 * This function behaves like the standard strtod() function
306 * does in the C locale. It does this without actually
307 * changing the current locale, since that would not be
310 * This function is typically used when reading configuration
311 * files or other non-user input that should be locale independent.
312 * To handle input from the user you should normally use the
313 * locale-sensitive system strtod() function.
315 * To convert from a #gdouble to a string in a locale-insensitive
316 * way, use g_ascii_dtostr().
318 * If the correct value would cause overflow, plus or minus %HUGE_VAL
319 * is returned (according to the sign of the value), and %ERANGE is
320 * stored in %errno. If the correct value would cause underflow,
321 * zero is returned and %ERANGE is stored in %errno.
323 * This function resets %errno before calling strtod() so that
324 * you can reliably detect overflow and underflow.
326 * Return value: the #gdouble value.
329 g_ascii_strtod (const gchar *nptr,
334 struct lconv *locale_data;
335 const char *decimal_point;
336 int decimal_point_len;
337 const char *p, *decimal_point_pos;
338 const char *end = NULL; /* Silence gcc */
341 g_return_val_if_fail (nptr != NULL, 0);
345 locale_data = localeconv ();
346 decimal_point = locale_data->decimal_point;
347 decimal_point_len = strlen (decimal_point);
349 g_assert (decimal_point_len != 0);
351 decimal_point_pos = NULL;
354 if (decimal_point[0] != '.' ||
355 decimal_point[1] != 0)
358 /* Skip leading space */
359 while (g_ascii_isspace (*p))
362 /* Skip leading optional sign */
363 if (*p == '+' || *p == '-')
367 (p[1] == 'x' || p[1] == 'X'))
370 /* HEX - find the (optional) decimal point */
372 while (g_ascii_isxdigit (*p))
376 decimal_point_pos = p++;
378 while (g_ascii_isxdigit (*p))
381 if (*p == 'p' || *p == 'P')
383 if (*p == '+' || *p == '-')
385 while (g_ascii_isdigit (*p))
390 else if (g_ascii_isdigit (*p) || *p == '.')
392 while (g_ascii_isdigit (*p))
396 decimal_point_pos = p++;
398 while (g_ascii_isdigit (*p))
401 if (*p == 'e' || *p == 'E')
403 if (*p == '+' || *p == '-')
405 while (g_ascii_isdigit (*p))
410 /* For the other cases, we need not convert the decimal point */
413 if (decimal_point_pos)
417 /* We need to convert the '.' to the locale specific decimal point */
418 copy = g_malloc (end - nptr + 1 + decimal_point_len);
421 memcpy (c, nptr, decimal_point_pos - nptr);
422 c += decimal_point_pos - nptr;
423 memcpy (c, decimal_point, decimal_point_len);
424 c += decimal_point_len;
425 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
426 c += end - (decimal_point_pos + 1);
430 val = strtod (copy, &fail_pos);
431 strtod_errno = errno;
435 if (fail_pos - copy > decimal_point_pos - nptr)
436 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
438 fail_pos = (char *)nptr + (fail_pos - copy);
448 copy = g_malloc (end - (char *)nptr + 1);
449 memcpy (copy, nptr, end - nptr);
450 *(copy + (end - (char *)nptr)) = 0;
453 val = strtod (copy, &fail_pos);
454 strtod_errno = errno;
458 fail_pos = (char *)nptr + (fail_pos - copy);
466 val = strtod (nptr, &fail_pos);
467 strtod_errno = errno;
473 errno = strtod_errno;
481 * @buffer: A buffer to place the resulting string in
482 * @buf_len: The length of the buffer.
483 * @d: The #gdouble to convert
485 * Converts a #gdouble to a string, using the '.' as
488 * This functions generates enough precision that converting
489 * the string back using g_ascii_strtod() gives the same machine-number
490 * (on machines with IEEE compatible 64bit doubles). It is
491 * guaranteed that the size of the resulting string will never
492 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
494 * Return value: The pointer to the buffer with the converted string.
497 g_ascii_dtostr (gchar *buffer,
501 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
506 * @buffer: A buffer to place the resulting string in
507 * @buf_len: The length of the buffer.
508 * @format: The printf()-style format to use for the
509 * code to use for converting.
510 * @d: The #gdouble to convert
512 * Converts a #gdouble to a string, using the '.' as
513 * decimal point. To format the number you pass in
514 * a printf()-style format string. Allowed conversion
515 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
517 * If you just want to want to serialize the value into a
518 * string, use g_ascii_dtostr().
520 * Return value: The pointer to the buffer with the converted string.
523 g_ascii_formatd (gchar *buffer,
528 struct lconv *locale_data;
529 const char *decimal_point;
530 int decimal_point_len;
535 g_return_val_if_fail (buffer != NULL, NULL);
536 g_return_val_if_fail (format[0] == '%', NULL);
537 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
539 format_char = format[strlen (format) - 1];
541 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
542 format_char == 'f' || format_char == 'F' ||
543 format_char == 'g' || format_char == 'G',
546 if (format[0] != '%')
549 if (strpbrk (format + 1, "'l%"))
552 if (!(format_char == 'e' || format_char == 'E' ||
553 format_char == 'f' || format_char == 'F' ||
554 format_char == 'g' || format_char == 'G'))
558 _g_snprintf (buffer, buf_len, format, d);
560 locale_data = localeconv ();
561 decimal_point = locale_data->decimal_point;
562 decimal_point_len = strlen (decimal_point);
564 g_assert (decimal_point_len != 0);
566 if (decimal_point[0] != '.' ||
567 decimal_point[1] != 0)
571 if (*p == '+' || *p == '-')
574 while (isdigit ((guchar)*p))
577 if (strncmp (p, decimal_point, decimal_point_len) == 0)
581 if (decimal_point_len > 1) {
582 rest_len = strlen (p + (decimal_point_len-1));
583 memmove (p, p + (decimal_point_len-1),
596 * @nptr: the string to convert to a numeric value.
597 * @endptr: if non-%NULL, it returns the character after
598 * the last character used in the conversion.
599 * @base: to be used for the conversion, 2..36 or 0
601 * Converts a string to a #guint64 value.
602 * This function behaves like the standard strtoull() function
603 * does in the C locale. It does this without actually
604 * changing the current locale, since that would not be
607 * This function is typically used when reading configuration
608 * files or other non-user input that should be locale independent.
609 * To handle input from the user you should normally use the
610 * locale-sensitive system strtoull() function.
612 * If the correct value would cause overflow, %G_MAXUINT64
613 * is returned, and %ERANGE is stored in %errno. If the base is
614 * outside the valid range, zero is returned, and %EINVAL is stored
615 * in %errno. If the string conversion fails, zero is returned, and
616 * @endptr returns @nptr (if @endptr is non-%NULL).
618 * Return value: the #guint64 value or zero on error.
623 g_ascii_strtoull (const gchar *nptr,
627 /* this code is based on on the strtol(3) code from GNU libc released under
628 * the GNU Lesser General Public License.
630 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
631 * Free Software Foundation, Inc.
633 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
634 (c) == '\r' || (c) == '\t' || (c) == '\v')
635 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
636 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
637 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
638 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
639 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
640 gboolean negative, overflow;
644 const gchar *s, *save;
647 g_return_val_if_fail (nptr != NULL, 0);
649 if (base == 1 || base > 36)
657 /* Skip white space. */
663 /* Check for a sign. */
673 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
676 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
687 /* Save the pointer so we can check later if anything happened. */
689 cutoff = G_MAXUINT64 / base;
690 cutlim = G_MAXUINT64 % base;
697 if (c >= '0' && c <= '9')
699 else if (ISALPHA (c))
700 c = TOUPPER (c) - 'A' + 10;
705 /* Check for overflow. */
706 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
715 /* Check if anything actually happened. */
719 /* Store in ENDPTR the address of one character
720 past the last character we converted. */
722 *endptr = (gchar*) s;
730 /* Return the result of the appropriate sign. */
731 return negative ? -ui64 : ui64;
734 /* We must handle a special case here: the base is 0 or 16 and the
735 first two characters are '0' and 'x', but the rest are no
736 hexadecimal digits. This is no error case. We return 0 and
737 ENDPTR points to the `x`. */
740 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
742 *endptr = (gchar*) &save[-1];
744 /* There was no number to convert. */
745 *endptr = (gchar*) nptr;
751 G_CONST_RETURN gchar*
752 g_strerror (gint errnum)
754 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
756 int saved_errno = errno;
759 const char *msg_locale;
761 msg_locale = strerror (errnum);
762 if (g_get_charset (NULL))
769 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
772 /* Stick in the quark table so that we can return a static result
774 GQuark msg_quark = g_quark_from_string (msg_utf8);
777 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
786 case E2BIG: return "argument list too long";
789 case EACCES: return "permission denied";
792 case EADDRINUSE: return "address already in use";
795 case EADDRNOTAVAIL: return "can't assign requested address";
798 case EADV: return "advertise error";
801 case EAFNOSUPPORT: return "address family not supported by protocol family";
804 case EAGAIN: return "try again";
807 case EALIGN: return "EALIGN";
810 case EALREADY: return "operation already in progress";
813 case EBADE: return "bad exchange descriptor";
816 case EBADF: return "bad file number";
819 case EBADFD: return "file descriptor in bad state";
822 case EBADMSG: return "not a data message";
825 case EBADR: return "bad request descriptor";
828 case EBADRPC: return "RPC structure is bad";
831 case EBADRQC: return "bad request code";
834 case EBADSLT: return "invalid slot";
837 case EBFONT: return "bad font file format";
840 case EBUSY: return "mount device busy";
843 case ECHILD: return "no children";
846 case ECHRNG: return "channel number out of range";
849 case ECOMM: return "communication error on send";
852 case ECONNABORTED: return "software caused connection abort";
855 case ECONNREFUSED: return "connection refused";
858 case ECONNRESET: return "connection reset by peer";
860 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
861 case EDEADLK: return "resource deadlock avoided";
864 case EDEADLOCK: return "resource deadlock avoided";
867 case EDESTADDRREQ: return "destination address required";
870 case EDIRTY: return "mounting a dirty fs w/o force";
873 case EDOM: return "math argument out of range";
876 case EDOTDOT: return "cross mount point";
879 case EDQUOT: return "disk quota exceeded";
882 case EDUPPKG: return "duplicate package name";
885 case EEXIST: return "file already exists";
888 case EFAULT: return "bad address in system call argument";
891 case EFBIG: return "file too large";
894 case EHOSTDOWN: return "host is down";
897 case EHOSTUNREACH: return "host is unreachable";
900 case EIDRM: return "identifier removed";
903 case EINIT: return "initialization error";
906 case EINPROGRESS: return "operation now in progress";
909 case EINTR: return "interrupted system call";
912 case EINVAL: return "invalid argument";
915 case EIO: return "I/O error";
918 case EISCONN: return "socket is already connected";
921 case EISDIR: return "is a directory";
924 case EISNAM: return "is a name file";
927 case ELBIN: return "ELBIN";
930 case EL2HLT: return "level 2 halted";
933 case EL2NSYNC: return "level 2 not synchronized";
936 case EL3HLT: return "level 3 halted";
939 case EL3RST: return "level 3 reset";
942 case ELIBACC: return "can not access a needed shared library";
945 case ELIBBAD: return "accessing a corrupted shared library";
948 case ELIBEXEC: return "can not exec a shared library directly";
951 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
954 case ELIBSCN: return ".lib section in a.out corrupted";
957 case ELNRNG: return "link number out of range";
960 case ELOOP: return "too many levels of symbolic links";
963 case EMFILE: return "too many open files";
966 case EMLINK: return "too many links";
969 case EMSGSIZE: return "message too long";
972 case EMULTIHOP: return "multihop attempted";
975 case ENAMETOOLONG: return "file name too long";
978 case ENAVAIL: return "not available";
981 case ENET: return "ENET";
984 case ENETDOWN: return "network is down";
987 case ENETRESET: return "network dropped connection on reset";
990 case ENETUNREACH: return "network is unreachable";
993 case ENFILE: return "file table overflow";
996 case ENOANO: return "anode table overflow";
998 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
999 case ENOBUFS: return "no buffer space available";
1002 case ENOCSI: return "no CSI structure available";
1005 case ENODATA: return "no data available";
1008 case ENODEV: return "no such device";
1011 case ENOENT: return "no such file or directory";
1014 case ENOEXEC: return "exec format error";
1017 case ENOLCK: return "no locks available";
1020 case ENOLINK: return "link has be severed";
1023 case ENOMEM: return "not enough memory";
1026 case ENOMSG: return "no message of desired type";
1029 case ENONET: return "machine is not on the network";
1032 case ENOPKG: return "package not installed";
1035 case ENOPROTOOPT: return "bad proocol option";
1038 case ENOSPC: return "no space left on device";
1041 case ENOSR: return "out of stream resources";
1044 case ENOSTR: return "not a stream device";
1047 case ENOSYM: return "unresolved symbol name";
1050 case ENOSYS: return "function not implemented";
1053 case ENOTBLK: return "block device required";
1056 case ENOTCONN: return "socket is not connected";
1059 case ENOTDIR: return "not a directory";
1062 case ENOTEMPTY: return "directory not empty";
1065 case ENOTNAM: return "not a name file";
1068 case ENOTSOCK: return "socket operation on non-socket";
1071 case ENOTTY: return "inappropriate device for ioctl";
1074 case ENOTUNIQ: return "name not unique on network";
1077 case ENXIO: return "no such device or address";
1080 case EOPNOTSUPP: return "operation not supported on socket";
1083 case EPERM: return "not owner";
1086 case EPFNOSUPPORT: return "protocol family not supported";
1089 case EPIPE: return "broken pipe";
1092 case EPROCLIM: return "too many processes";
1095 case EPROCUNAVAIL: return "bad procedure for program";
1097 #ifdef EPROGMISMATCH
1098 case EPROGMISMATCH: return "program version wrong";
1101 case EPROGUNAVAIL: return "RPC program not available";
1104 case EPROTO: return "protocol error";
1106 #ifdef EPROTONOSUPPORT
1107 case EPROTONOSUPPORT: return "protocol not suppored";
1110 case EPROTOTYPE: return "protocol wrong type for socket";
1113 case ERANGE: return "math result unrepresentable";
1115 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1116 case EREFUSED: return "EREFUSED";
1119 case EREMCHG: return "remote address changed";
1122 case EREMDEV: return "remote device";
1125 case EREMOTE: return "pathname hit remote file system";
1128 case EREMOTEIO: return "remote i/o error";
1130 #ifdef EREMOTERELEASE
1131 case EREMOTERELEASE: return "EREMOTERELEASE";
1134 case EROFS: return "read-only file system";
1137 case ERPCMISMATCH: return "RPC version is wrong";
1140 case ERREMOTE: return "object is remote";
1143 case ESHUTDOWN: return "can't send afer socket shutdown";
1145 #ifdef ESOCKTNOSUPPORT
1146 case ESOCKTNOSUPPORT: return "socket type not supported";
1149 case ESPIPE: return "invalid seek";
1152 case ESRCH: return "no such process";
1155 case ESRMNT: return "srmount error";
1158 case ESTALE: return "stale remote file handle";
1161 case ESUCCESS: return "Error 0";
1164 case ETIME: return "timer expired";
1167 case ETIMEDOUT: return "connection timed out";
1170 case ETOOMANYREFS: return "too many references: can't splice";
1173 case ETXTBSY: return "text file or pseudo-device busy";
1176 case EUCLEAN: return "structure needs cleaning";
1179 case EUNATCH: return "protocol driver not attached";
1182 case EUSERS: return "too many users";
1185 case EVERSION: return "version mismatch";
1187 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1188 case EWOULDBLOCK: return "operation would block";
1191 case EXDEV: return "cross-domain link";
1194 case EXFULL: return "message tables full";
1197 #else /* NO_SYS_ERRLIST */
1198 extern int sys_nerr;
1199 extern char *sys_errlist[];
1201 if ((errnum > 0) && (errnum <= sys_nerr))
1202 return sys_errlist [errnum];
1203 #endif /* NO_SYS_ERRLIST */
1205 msg = g_static_private_get (&msg_private);
1208 msg = g_new (gchar, 64);
1209 g_static_private_set (&msg_private, msg, g_free);
1212 _g_sprintf (msg, "unknown error (%d)", errnum);
1214 errno = saved_errno;
1218 G_CONST_RETURN gchar*
1219 g_strsignal (gint signum)
1221 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1224 #ifdef HAVE_STRSIGNAL
1225 const char *msg_locale;
1227 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1228 extern const char *strsignal(int);
1230 /* this is declared differently (const) in string.h on BeOS */
1231 extern char *strsignal (int sig);
1232 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1233 msg_locale = strsignal (signum);
1234 if (g_get_charset (NULL))
1238 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1241 /* Stick in the quark table so that we can return a static result
1243 GQuark msg_quark = g_quark_from_string (msg_utf8);
1246 return g_quark_to_string (msg_quark);
1249 #elif NO_SYS_SIGLIST
1253 case SIGHUP: return "Hangup";
1256 case SIGINT: return "Interrupt";
1259 case SIGQUIT: return "Quit";
1262 case SIGILL: return "Illegal instruction";
1265 case SIGTRAP: return "Trace/breakpoint trap";
1268 case SIGABRT: return "IOT trap/Abort";
1271 case SIGBUS: return "Bus error";
1274 case SIGFPE: return "Floating point exception";
1277 case SIGKILL: return "Killed";
1280 case SIGUSR1: return "User defined signal 1";
1283 case SIGSEGV: return "Segmentation fault";
1286 case SIGUSR2: return "User defined signal 2";
1289 case SIGPIPE: return "Broken pipe";
1292 case SIGALRM: return "Alarm clock";
1295 case SIGTERM: return "Terminated";
1298 case SIGSTKFLT: return "Stack fault";
1301 case SIGCHLD: return "Child exited";
1304 case SIGCONT: return "Continued";
1307 case SIGSTOP: return "Stopped (signal)";
1310 case SIGTSTP: return "Stopped";
1313 case SIGTTIN: return "Stopped (tty input)";
1316 case SIGTTOU: return "Stopped (tty output)";
1319 case SIGURG: return "Urgent condition";
1322 case SIGXCPU: return "CPU time limit exceeded";
1325 case SIGXFSZ: return "File size limit exceeded";
1328 case SIGVTALRM: return "Virtual time alarm";
1331 case SIGPROF: return "Profile signal";
1334 case SIGWINCH: return "Window size changed";
1337 case SIGIO: return "Possible I/O";
1340 case SIGPWR: return "Power failure";
1343 case SIGUNUSED: return "Unused signal";
1346 #else /* NO_SYS_SIGLIST */
1348 #ifdef NO_SYS_SIGLIST_DECL
1349 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1352 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1353 #endif /* NO_SYS_SIGLIST */
1355 msg = g_static_private_get (&msg_private);
1358 msg = g_new (gchar, 64);
1359 g_static_private_set (&msg_private, msg, g_free);
1362 _g_sprintf (msg, "unknown signal (%d)", signum);
1367 /* Functions g_strlcpy and g_strlcat were originally developed by
1368 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1369 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1370 * for more information.
1374 /* Use the native ones, if available; they might be implemented in assembly */
1376 g_strlcpy (gchar *dest,
1380 g_return_val_if_fail (dest != NULL, 0);
1381 g_return_val_if_fail (src != NULL, 0);
1383 return strlcpy (dest, src, dest_size);
1387 g_strlcat (gchar *dest,
1391 g_return_val_if_fail (dest != NULL, 0);
1392 g_return_val_if_fail (src != NULL, 0);
1394 return strlcat (dest, src, dest_size);
1397 #else /* ! HAVE_STRLCPY */
1400 * Copy string src to buffer dest (of buffer size dest_size). At most
1401 * dest_size-1 characters will be copied. Always NUL terminates
1402 * (unless dest_size == 0). This function does NOT allocate memory.
1403 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1404 * Returns size of attempted result, strlen(src),
1405 * so if retval >= dest_size, truncation occurred.
1408 g_strlcpy (gchar *dest,
1412 register gchar *d = dest;
1413 register const gchar *s = src;
1414 register gsize n = dest_size;
1416 g_return_val_if_fail (dest != NULL, 0);
1417 g_return_val_if_fail (src != NULL, 0);
1419 /* Copy as many bytes as will fit */
1420 if (n != 0 && --n != 0)
1423 register gchar c = *s++;
1431 /* If not enough room in dest, add NUL and traverse rest of src */
1440 return s - src - 1; /* count does not include NUL */
1445 * Appends string src to buffer dest (of buffer size dest_size).
1446 * At most dest_size-1 characters will be copied.
1447 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1448 * This function does NOT allocate memory.
1449 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1450 * in the dest_size characters of dest to start with).
1451 * Returns size of attempted result, which is
1452 * MIN (dest_size, strlen (original dest)) + strlen (src),
1453 * so if retval >= dest_size, truncation occurred.
1456 g_strlcat (gchar *dest,
1460 register gchar *d = dest;
1461 register const gchar *s = src;
1462 register gsize bytes_left = dest_size;
1463 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1465 g_return_val_if_fail (dest != NULL, 0);
1466 g_return_val_if_fail (src != NULL, 0);
1468 /* Find the end of dst and adjust bytes left but don't go past end */
1469 while (*d != 0 && bytes_left-- != 0)
1472 bytes_left = dest_size - dlength;
1474 if (bytes_left == 0)
1475 return dlength + strlen (s);
1479 if (bytes_left != 1)
1488 return dlength + (s - src); /* count does not include NUL */
1490 #endif /* ! HAVE_STRLCPY */
1495 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1497 * Converts all upper case ASCII letters to lower case ASCII letters.
1499 * Return value: a newly-allocated string, with all the upper case
1500 * characters in @str converted to lower case, with
1501 * semantics that exactly match g_ascii_tolower(). (Note
1502 * that this is unlike the old g_strdown(), which modified
1503 * the string in place.)
1506 g_ascii_strdown (const gchar *str,
1511 g_return_val_if_fail (str != NULL, NULL);
1516 result = g_strndup (str, len);
1517 for (s = result; *s; s++)
1518 *s = g_ascii_tolower (*s);
1526 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1528 * Converts all lower case ASCII letters to upper case ASCII letters.
1530 * Return value: a newly allocated string, with all the lower case
1531 * characters in @str converted to upper case, with
1532 * semantics that exactly match g_ascii_toupper(). (Note
1533 * that this is unlike the old g_strup(), which modified
1534 * the string in place.)
1537 g_ascii_strup (const gchar *str,
1542 g_return_val_if_fail (str != NULL, NULL);
1547 result = g_strndup (str, len);
1548 for (s = result; *s; s++)
1549 *s = g_ascii_toupper (*s);
1556 * @string: the string to convert.
1558 * Converts a string to lower case.
1560 * Return value: the string
1562 * Deprecated: This function is totally broken for the reasons discussed in
1563 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1567 g_strdown (gchar *string)
1571 g_return_val_if_fail (string != NULL, NULL);
1573 s = (guchar *) string;
1582 return (gchar *) string;
1587 * @string: the string to convert.
1589 * Converts a string to upper case.
1591 * Return value: the string
1593 * Deprecated: This function is totally broken for the reasons discussed in
1594 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1597 g_strup (gchar *string)
1601 g_return_val_if_fail (string != NULL, NULL);
1603 s = (guchar *) string;
1612 return (gchar *) string;
1616 g_strreverse (gchar *string)
1618 g_return_val_if_fail (string != NULL, NULL);
1622 register gchar *h, *t;
1625 t = string + strlen (string) - 1;
1644 * @c: any character.
1646 * Convert a character to ASCII lower case.
1648 * Unlike the standard C library tolower() function, this only
1649 * recognizes standard ASCII letters and ignores the locale, returning
1650 * all non-ASCII characters unchanged, even if they are lower case
1651 * letters in a particular character set. Also unlike the standard
1652 * library function, this takes and returns a char, not an int, so
1653 * don't call it on %EOF but no need to worry about casting to #guchar
1654 * before passing a possibly non-ASCII character in.
1656 * Return value: the result of converting @c to lower case.
1657 * If @c is not an ASCII upper case letter,
1658 * @c is returned unchanged.
1661 g_ascii_tolower (gchar c)
1663 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1668 * @c: any character.
1670 * Convert a character to ASCII upper case.
1672 * Unlike the standard C library toupper() function, this only
1673 * recognizes standard ASCII letters and ignores the locale, returning
1674 * all non-ASCII characters unchanged, even if they are upper case
1675 * letters in a particular character set. Also unlike the standard
1676 * library function, this takes and returns a char, not an int, so
1677 * don't call it on %EOF but no need to worry about casting to #guchar
1678 * before passing a possibly non-ASCII character in.
1680 * Return value: the result of converting @c to upper case.
1681 * If @c is not an ASCII lower case letter,
1682 * @c is returned unchanged.
1685 g_ascii_toupper (gchar c)
1687 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1691 * g_ascii_digit_value:
1692 * @c: an ASCII character.
1694 * Determines the numeric value of a character as a decimal
1695 * digit. Differs from g_unichar_digit_value() because it takes
1696 * a char, so there's no worry about sign extension if characters
1699 * Return value: If @c is a decimal digit (according to
1700 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1703 g_ascii_digit_value (gchar c)
1705 if (g_ascii_isdigit (c))
1711 * g_ascii_xdigit_value:
1712 * @c: an ASCII character.
1714 * Determines the numeric value of a character as a hexidecimal
1715 * digit. Differs from g_unichar_xdigit_value() because it takes
1716 * a char, so there's no worry about sign extension if characters
1719 * Return value: If @c is a hex digit (according to
1720 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1723 g_ascii_xdigit_value (gchar c)
1725 if (c >= 'A' && c <= 'F')
1726 return c - 'A' + 10;
1727 if (c >= 'a' && c <= 'f')
1728 return c - 'a' + 10;
1729 return g_ascii_digit_value (c);
1733 * g_ascii_strcasecmp:
1734 * @s1: string to compare with @s2.
1735 * @s2: string to compare with @s1.
1737 * Compare two strings, ignoring the case of ASCII characters.
1739 * Unlike the BSD strcasecmp() function, this only recognizes standard
1740 * ASCII letters and ignores the locale, treating all non-ASCII
1741 * bytes as if they are not letters.
1743 * This function should be used only on strings that are known to be
1744 * in encodings where the bytes corresponding to ASCII letters always
1745 * represent themselves. This includes UTF-8 and the ISO-8859-*
1746 * charsets, but not for instance double-byte encodings like the
1747 * Windows Codepage 932, where the trailing bytes of double-byte
1748 * characters include all ASCII letters. If you compare two CP932
1749 * strings using this function, you will get false matches.
1751 * Return value: an integer less than, equal to, or greater than
1752 * zero if @s1 is found, respectively, to be less than,
1753 * to match, or to be greater than @s2.
1756 g_ascii_strcasecmp (const gchar *s1,
1761 g_return_val_if_fail (s1 != NULL, 0);
1762 g_return_val_if_fail (s2 != NULL, 0);
1766 c1 = (gint)(guchar) TOLOWER (*s1);
1767 c2 = (gint)(guchar) TOLOWER (*s2);
1773 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1777 * g_ascii_strncasecmp:
1778 * @s1: string to compare with @s2.
1779 * @s2: string to compare with @s1.
1780 * @n: number of characters to compare.
1782 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1783 * characters after the first @n in each string.
1785 * Unlike the BSD strcasecmp() function, this only recognizes standard
1786 * ASCII letters and ignores the locale, treating all non-ASCII
1787 * characters as if they are not letters.
1789 * The same warning as in g_ascii_strcasecmp() applies: Use this
1790 * function only on strings known to be in encodings where bytes
1791 * corresponding to ASCII letters always represent themselves.
1793 * Return value: an integer less than, equal to, or greater than zero
1794 * if the first @n bytes of @s1 is found, respectively,
1795 * to be less than, to match, or to be greater than the
1796 * first @n bytes of @s2.
1799 g_ascii_strncasecmp (const gchar *s1,
1805 g_return_val_if_fail (s1 != NULL, 0);
1806 g_return_val_if_fail (s2 != NULL, 0);
1808 while (n && *s1 && *s2)
1811 c1 = (gint)(guchar) TOLOWER (*s1);
1812 c2 = (gint)(guchar) TOLOWER (*s2);
1819 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1827 * @s2: a string to compare with @s1.
1829 * A case-insensitive string comparison, corresponding to the standard
1830 * strcasecmp() function on platforms which support it.
1832 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1833 * or a positive value if @s1 > @s2.
1835 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1836 * deprecated and how to replace it.
1839 g_strcasecmp (const gchar *s1,
1842 #ifdef HAVE_STRCASECMP
1843 g_return_val_if_fail (s1 != NULL, 0);
1844 g_return_val_if_fail (s2 != NULL, 0);
1846 return strcasecmp (s1, s2);
1850 g_return_val_if_fail (s1 != NULL, 0);
1851 g_return_val_if_fail (s2 != NULL, 0);
1855 /* According to A. Cox, some platforms have islower's that
1856 * don't work right on non-uppercase
1858 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1859 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1865 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1872 * @s2: a string to compare with @s1.
1873 * @n: the maximum number of characters to compare.
1875 * A case-insensitive string comparison, corresponding to the standard
1876 * strncasecmp() function on platforms which support it.
1877 * It is similar to g_strcasecmp() except it only compares the first @n
1878 * characters of the strings.
1880 * Return value: 0 if the strings match, a negative value if @s1 < @s2,
1881 * or a positive value if @s1 > @s2.
1883 * Deprecated: The problem with g_strncasecmp() is that it does the
1884 * comparison by calling toupper()/tolower(). These functions are
1885 * locale-specific and operate on single bytes. However, it is impossible
1886 * to handle things correctly from an I18N standpoint by operating on
1887 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1888 * broken if your string is guaranteed to be ASCII, since it's
1889 * locale-sensitive, and it's broken if your string is localized, since
1890 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1893 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1894 * which only works on ASCII and is not locale-sensitive, and
1895 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1898 g_strncasecmp (const gchar *s1,
1902 #ifdef HAVE_STRNCASECMP
1903 return strncasecmp (s1, s2, n);
1907 g_return_val_if_fail (s1 != NULL, 0);
1908 g_return_val_if_fail (s2 != NULL, 0);
1910 while (n && *s1 && *s2)
1913 /* According to A. Cox, some platforms have islower's that
1914 * don't work right on non-uppercase
1916 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1917 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1924 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1931 g_strdelimit (gchar *string,
1932 const gchar *delimiters,
1937 g_return_val_if_fail (string != NULL, NULL);
1940 delimiters = G_STR_DELIMITERS;
1942 for (c = string; *c; c++)
1944 if (strchr (delimiters, *c))
1952 g_strcanon (gchar *string,
1953 const gchar *valid_chars,
1958 g_return_val_if_fail (string != NULL, NULL);
1959 g_return_val_if_fail (valid_chars != NULL, NULL);
1961 for (c = string; *c; c++)
1963 if (!strchr (valid_chars, *c))
1971 g_strcompress (const gchar *source)
1973 const gchar *p = source, *octal;
1974 gchar *dest = g_malloc (strlen (source) + 1);
1985 g_warning ("g_strcompress: trailing \\");
1987 case '0': case '1': case '2': case '3': case '4':
1988 case '5': case '6': case '7':
1991 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1993 *q = (*q * 8) + (*p - '0');
2014 default: /* Also handles \" and \\ */
2030 g_strescape (const gchar *source,
2031 const gchar *exceptions)
2038 g_return_val_if_fail (source != NULL, NULL);
2040 p = (guchar *) source;
2041 /* Each source byte needs maximally four destination chars (\777) */
2042 q = dest = g_malloc (strlen (source) * 4 + 1);
2044 memset (excmap, 0, 256);
2047 guchar *e = (guchar *) exceptions;
2093 if ((*p < ' ') || (*p >= 0177))
2096 *q++ = '0' + (((*p) >> 6) & 07);
2097 *q++ = '0' + (((*p) >> 3) & 07);
2098 *q++ = '0' + ((*p) & 07);
2112 g_strchug (gchar *string)
2116 g_return_val_if_fail (string != NULL, NULL);
2118 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2121 g_memmove (string, start, strlen ((gchar *) start) + 1);
2127 g_strchomp (gchar *string)
2131 g_return_val_if_fail (string != NULL, NULL);
2133 len = strlen (string);
2136 if (g_ascii_isspace ((guchar) string[len]))
2147 * @string: a string to split.
2148 * @delimiter: a string which specifies the places at which to split the string.
2149 * The delimiter is not included in any of the resulting strings, unless
2150 * @max_tokens is reached.
2151 * @max_tokens: the maximum number of pieces to split @string into. If this is
2152 * less than 1, the string is split completely.
2154 * Splits a string into a maximum of @max_tokens pieces, using the given
2155 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2156 * to the last token.
2158 * As a special case, the result of splitting the empty string "" is an empty
2159 * vector, not a vector containing a single string. The reason for this
2160 * special case is that being able to represent a empty vector is typically
2161 * more useful than consistent handling of empty elements. If you do need
2162 * to represent empty elements, you'll need to check for the empty string
2163 * before calling g_strsplit().
2165 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2166 * g_strfreev() to free it.
2169 g_strsplit (const gchar *string,
2170 const gchar *delimiter,
2173 GSList *string_list = NULL, *slist;
2174 gchar **str_array, *s;
2176 const gchar *remainder;
2178 g_return_val_if_fail (string != NULL, NULL);
2179 g_return_val_if_fail (delimiter != NULL, NULL);
2180 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2183 max_tokens = G_MAXINT;
2186 s = strstr (remainder, delimiter);
2189 gsize delimiter_len = strlen (delimiter);
2191 while (--max_tokens && s)
2196 len = s - remainder;
2197 new_string = g_new (gchar, len + 1);
2198 strncpy (new_string, remainder, len);
2199 new_string[len] = 0;
2200 string_list = g_slist_prepend (string_list, new_string);
2202 remainder = s + delimiter_len;
2203 s = strstr (remainder, delimiter);
2209 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2212 str_array = g_new (gchar*, n + 1);
2214 str_array[n--] = NULL;
2215 for (slist = string_list; slist; slist = slist->next)
2216 str_array[n--] = slist->data;
2218 g_slist_free (string_list);
2225 * @string: The string to be tokenized
2226 * @delimiters: A nul-terminated string containing bytes that are used
2227 * to split the string.
2228 * @max_tokens: The maximum number of tokens to split @string into.
2229 * If this is less than 1, the string is split completely
2231 * Splits @string into a number of tokens not containing any of the characters
2232 * in @delimiter. A token is the (possibly empty) longest string that does not
2233 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2234 * remainder is appended to the last token.
2236 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2237 * %NULL-terminated vector containing the three strings "abc", "def",
2240 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2241 * vector containing the four strings "", "def", "ghi", and "".
2243 * As a special case, the result of splitting the empty string "" is an empty
2244 * vector, not a vector containing a single string. The reason for this
2245 * special case is that being able to represent a empty vector is typically
2246 * more useful than consistent handling of empty elements. If you do need
2247 * to represent empty elements, you'll need to check for the empty string
2248 * before calling g_strsplit_set().
2250 * Note that this function works on bytes not characters, so it can't be used
2251 * to delimit UTF-8 strings for anything but ASCII characters.
2253 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2254 * g_strfreev() to free it.
2259 g_strsplit_set (const gchar *string,
2260 const gchar *delimiters,
2263 gboolean delim_table[256];
2264 GSList *tokens, *list;
2267 const gchar *current;
2271 g_return_val_if_fail (string != NULL, NULL);
2272 g_return_val_if_fail (delimiters != NULL, NULL);
2275 max_tokens = G_MAXINT;
2277 if (*string == '\0')
2279 result = g_new (char *, 1);
2284 memset (delim_table, FALSE, sizeof (delim_table));
2285 for (s = delimiters; *s != '\0'; ++s)
2286 delim_table[*(guchar *)s] = TRUE;
2291 s = current = string;
2294 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2298 token = g_strndup (current, s - current);
2299 tokens = g_slist_prepend (tokens, token);
2308 token = g_strndup (current, s - current);
2309 tokens = g_slist_prepend (tokens, token);
2312 result = g_new (gchar *, n_tokens + 1);
2314 result[n_tokens] = NULL;
2315 for (list = tokens; list != NULL; list = list->next)
2316 result[--n_tokens] = list->data;
2318 g_slist_free (tokens);
2325 * @str_array: a %NULL-terminated array of strings to free.
2327 * Frees a %NULL-terminated array of strings, and the array itself.
2328 * If called on a %NULL value, g_strfreev() simply returns.
2331 g_strfreev (gchar **str_array)
2337 for(i = 0; str_array[i] != NULL; i++)
2338 g_free(str_array[i]);
2346 * @str_array: %NULL-terminated array of strings.
2348 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2349 * the new array should be freed by first freeing each string, then
2350 * the array itself. g_strfreev() does this for you. If called
2351 * on a %NULL value, g_strdupv() simply returns %NULL.
2353 * Return value: a new %NULL-terminated array of strings.
2356 g_strdupv (gchar **str_array)
2364 while (str_array[i])
2367 retval = g_new (gchar*, i + 1);
2370 while (str_array[i])
2372 retval[i] = g_strdup (str_array[i]);
2384 g_strjoinv (const gchar *separator,
2390 g_return_val_if_fail (str_array != NULL, NULL);
2392 if (separator == NULL)
2399 gsize separator_len;
2401 separator_len = strlen (separator);
2402 /* First part, getting length */
2403 len = 1 + strlen (str_array[0]);
2404 for (i = 1; str_array[i] != NULL; i++)
2405 len += strlen (str_array[i]);
2406 len += separator_len * (i - 1);
2408 /* Second part, building string */
2409 string = g_new (gchar, len);
2410 ptr = g_stpcpy (string, *str_array);
2411 for (i = 1; str_array[i] != NULL; i++)
2413 ptr = g_stpcpy (ptr, separator);
2414 ptr = g_stpcpy (ptr, str_array[i]);
2418 string = g_strdup ("");
2424 g_strjoin (const gchar *separator,
2430 gsize separator_len;
2433 if (separator == NULL)
2436 separator_len = strlen (separator);
2438 va_start (args, separator);
2440 s = va_arg (args, gchar*);
2444 /* First part, getting length */
2445 len = 1 + strlen (s);
2447 s = va_arg (args, gchar*);
2450 len += separator_len + strlen (s);
2451 s = va_arg (args, gchar*);
2455 /* Second part, building string */
2456 string = g_new (gchar, len);
2458 va_start (args, separator);
2460 s = va_arg (args, gchar*);
2461 ptr = g_stpcpy (string, s);
2463 s = va_arg (args, gchar*);
2466 ptr = g_stpcpy (ptr, separator);
2467 ptr = g_stpcpy (ptr, s);
2468 s = va_arg (args, gchar*);
2472 string = g_strdup ("");
2482 * @haystack: a string.
2483 * @haystack_len: the maximum length of @haystack.
2484 * @needle: the string to search for.
2486 * Searches the string @haystack for the first occurrence
2487 * of the string @needle, limiting the length of the search
2490 * Return value: a pointer to the found occurrence, or
2491 * %NULL if not found.
2494 g_strstr_len (const gchar *haystack,
2495 gssize haystack_len,
2496 const gchar *needle)
2498 g_return_val_if_fail (haystack != NULL, NULL);
2499 g_return_val_if_fail (needle != NULL, NULL);
2501 if (haystack_len < 0)
2502 return strstr (haystack, needle);
2505 const gchar *p = haystack;
2506 gsize needle_len = strlen (needle);
2510 if (needle_len == 0)
2511 return (gchar *)haystack;
2513 if (haystack_len < needle_len)
2516 end = haystack + haystack_len - needle_len;
2518 while (*p && p <= end)
2520 for (i = 0; i < needle_len; i++)
2521 if (p[i] != needle[i])
2536 * @haystack: a nul-terminated string.
2537 * @needle: the nul-terminated string to search for.
2539 * Searches the string @haystack for the last occurrence
2540 * of the string @needle.
2542 * Return value: a pointer to the found occurrence, or
2543 * %NULL if not found.
2546 g_strrstr (const gchar *haystack,
2547 const gchar *needle)
2554 g_return_val_if_fail (haystack != NULL, NULL);
2555 g_return_val_if_fail (needle != NULL, NULL);
2557 needle_len = strlen (needle);
2558 haystack_len = strlen (haystack);
2560 if (needle_len == 0)
2561 return (gchar *)haystack;
2563 if (haystack_len < needle_len)
2566 p = haystack + haystack_len - needle_len;
2568 while (p >= haystack)
2570 for (i = 0; i < needle_len; i++)
2571 if (p[i] != needle[i])
2585 * @haystack: a nul-terminated string.
2586 * @haystack_len: the maximum length of @haystack.
2587 * @needle: the nul-terminated string to search for.
2589 * Searches the string @haystack for the last occurrence
2590 * of the string @needle, limiting the length of the search
2593 * Return value: a pointer to the found occurrence, or
2594 * %NULL if not found.
2597 g_strrstr_len (const gchar *haystack,
2598 gssize haystack_len,
2599 const gchar *needle)
2601 g_return_val_if_fail (haystack != NULL, NULL);
2602 g_return_val_if_fail (needle != NULL, NULL);
2604 if (haystack_len < 0)
2605 return g_strrstr (haystack, needle);
2608 gsize needle_len = strlen (needle);
2609 const gchar *haystack_max = haystack + haystack_len;
2610 const gchar *p = haystack;
2613 while (p < haystack_max && *p)
2616 if (p < haystack + needle_len)
2621 while (p >= haystack)
2623 for (i = 0; i < needle_len; i++)
2624 if (p[i] != needle[i])
2640 * @str: a nul-terminated string.
2641 * @suffix: the nul-terminated suffix to look for.
2643 * Looks whether the string @str ends with @suffix.
2645 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2650 g_str_has_suffix (const gchar *str,
2651 const gchar *suffix)
2656 g_return_val_if_fail (str != NULL, FALSE);
2657 g_return_val_if_fail (suffix != NULL, FALSE);
2659 str_len = strlen (str);
2660 suffix_len = strlen (suffix);
2662 if (str_len < suffix_len)
2665 return strcmp (str + str_len - suffix_len, suffix) == 0;
2670 * @str: a nul-terminated string.
2671 * @prefix: the nul-terminated prefix to look for.
2673 * Looks whether the string @str begins with @prefix.
2675 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2680 g_str_has_prefix (const gchar *str,
2681 const gchar *prefix)
2686 g_return_val_if_fail (str != NULL, FALSE);
2687 g_return_val_if_fail (prefix != NULL, FALSE);
2689 str_len = strlen (str);
2690 prefix_len = strlen (prefix);
2692 if (str_len < prefix_len)
2695 return strncmp (str, prefix, prefix_len) == 0;
2702 * @msgval: another string
2704 * An auxiliary function for gettext() support (see Q_()).
2706 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2707 * a '|' character, in which case a pointer to the substring of msgid after
2708 * the first '|' character is returned.
2712 G_CONST_RETURN gchar *
2713 g_strip_context (const gchar *msgid,
2714 const gchar *msgval)
2716 if (msgval == msgid)
2718 const char *c = strchr (msgid, '|');
2729 * @str_array: a %NULL-terminated array of strings.
2731 * Returns the length of the given %NULL-terminated
2732 * string array @str_array.
2734 * Return value: length of @str_array.
2739 g_strv_length (gchar **str_array)
2743 g_return_val_if_fail (str_array != NULL, 0);
2745 while (str_array[i])
2751 #define __G_STRFUNCS_C__
2752 #include "galiasdef.c"