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/.
35 #define _GNU_SOURCE /* For stpcpy */
42 #include <ctype.h> /* For tolower() */
43 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
52 /* do not include <unistd.h> in this place since it
53 * inteferes with g_strsignal() on some OSes
56 static const guint16 ascii_table_data[256] = {
57 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
58 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
59 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
60 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
62 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
63 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
64 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
66 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
67 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
68 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
69 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
70 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
71 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
72 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
73 /* the upper 128 are all zeroes */
76 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
79 const guint16 * const g_ascii_table = ascii_table_data;
82 g_strdup (const gchar *str)
88 new_str = g_new (char, strlen (str) + 1);
89 strcpy (new_str, str);
98 g_memdup (gconstpointer mem,
105 new_mem = g_malloc (byte_size);
106 memcpy (new_mem, mem, byte_size);
115 g_strndup (const gchar *str,
122 new_str = g_new (gchar, n + 1);
123 strncpy (new_str, str, n);
133 g_strnfill (gsize length,
136 register gchar *str, *s, *end;
138 str = g_new (gchar, length + 1);
150 * @dest: destination buffer
151 * @src: source string
153 * Copies a nul-terminated string into the dest buffer, include the
154 * trailing nul, and return a pointer to the trailing nul byte.
155 * This is useful for concatenating multiple strings together
156 * without having to repeatedly scan for the end.
158 * Return value: a pointer to trailing nul byte.
161 g_stpcpy (gchar *dest,
165 g_return_val_if_fail (dest != NULL, NULL);
166 g_return_val_if_fail (src != NULL, NULL);
167 return stpcpy (dest, src);
169 register gchar *d = dest;
170 register const gchar *s = src;
172 g_return_val_if_fail (dest != NULL, NULL);
173 g_return_val_if_fail (src != NULL, NULL);
176 while (*s++ != '\0');
183 g_strdup_vprintf (const gchar *format,
187 #ifdef HAVE_VASPRINTF
188 vasprintf (&buffer, format, args1);
189 if (g_mem_vtable_is_set ())
191 gchar *buffer1 = g_strdup (buffer);
198 G_VA_COPY (args2, args1);
200 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
202 vsprintf (buffer, format, args2);
209 g_strdup_printf (const gchar *format,
215 va_start (args, format);
216 buffer = g_strdup_vprintf (format, args);
223 g_strconcat (const gchar *string1, ...)
231 g_return_val_if_fail (string1 != NULL, NULL);
233 l = 1 + strlen (string1);
234 va_start (args, string1);
235 s = va_arg (args, gchar*);
239 s = va_arg (args, gchar*);
243 concat = g_new (gchar, l);
246 ptr = g_stpcpy (ptr, string1);
247 va_start (args, string1);
248 s = va_arg (args, gchar*);
251 ptr = g_stpcpy (ptr, s);
252 s = va_arg (args, gchar*);
260 g_strtod (const gchar *nptr,
268 g_return_val_if_fail (nptr != NULL, 0);
273 val_1 = strtod (nptr, &fail_pos_1);
275 if (fail_pos_1 && fail_pos_1[0] != 0)
279 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
280 setlocale (LC_NUMERIC, "C");
281 val_2 = strtod (nptr, &fail_pos_2);
282 setlocale (LC_NUMERIC, old_locale);
286 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
289 *endptr = fail_pos_1;
295 *endptr = fail_pos_2;
300 G_CONST_RETURN gchar*
301 g_strerror (gint errnum)
303 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
307 return strerror (errnum);
312 case E2BIG: return "argument list too long";
315 case EACCES: return "permission denied";
318 case EADDRINUSE: return "address already in use";
321 case EADDRNOTAVAIL: return "can't assign requested address";
324 case EADV: return "advertise error";
327 case EAFNOSUPPORT: return "address family not supported by protocol family";
330 case EAGAIN: return "try again";
333 case EALIGN: return "EALIGN";
336 case EALREADY: return "operation already in progress";
339 case EBADE: return "bad exchange descriptor";
342 case EBADF: return "bad file number";
345 case EBADFD: return "file descriptor in bad state";
348 case EBADMSG: return "not a data message";
351 case EBADR: return "bad request descriptor";
354 case EBADRPC: return "RPC structure is bad";
357 case EBADRQC: return "bad request code";
360 case EBADSLT: return "invalid slot";
363 case EBFONT: return "bad font file format";
366 case EBUSY: return "mount device busy";
369 case ECHILD: return "no children";
372 case ECHRNG: return "channel number out of range";
375 case ECOMM: return "communication error on send";
378 case ECONNABORTED: return "software caused connection abort";
381 case ECONNREFUSED: return "connection refused";
384 case ECONNRESET: return "connection reset by peer";
386 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
387 case EDEADLK: return "resource deadlock avoided";
390 case EDEADLOCK: return "resource deadlock avoided";
393 case EDESTADDRREQ: return "destination address required";
396 case EDIRTY: return "mounting a dirty fs w/o force";
399 case EDOM: return "math argument out of range";
402 case EDOTDOT: return "cross mount point";
405 case EDQUOT: return "disk quota exceeded";
408 case EDUPPKG: return "duplicate package name";
411 case EEXIST: return "file already exists";
414 case EFAULT: return "bad address in system call argument";
417 case EFBIG: return "file too large";
420 case EHOSTDOWN: return "host is down";
423 case EHOSTUNREACH: return "host is unreachable";
426 case EIDRM: return "identifier removed";
429 case EINIT: return "initialization error";
432 case EINPROGRESS: return "operation now in progress";
435 case EINTR: return "interrupted system call";
438 case EINVAL: return "invalid argument";
441 case EIO: return "I/O error";
444 case EISCONN: return "socket is already connected";
447 case EISDIR: return "is a directory";
450 case EISNAM: return "is a name file";
453 case ELBIN: return "ELBIN";
456 case EL2HLT: return "level 2 halted";
459 case EL2NSYNC: return "level 2 not synchronized";
462 case EL3HLT: return "level 3 halted";
465 case EL3RST: return "level 3 reset";
468 case ELIBACC: return "can not access a needed shared library";
471 case ELIBBAD: return "accessing a corrupted shared library";
474 case ELIBEXEC: return "can not exec a shared library directly";
477 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
480 case ELIBSCN: return ".lib section in a.out corrupted";
483 case ELNRNG: return "link number out of range";
486 case ELOOP: return "too many levels of symbolic links";
489 case EMFILE: return "too many open files";
492 case EMLINK: return "too many links";
495 case EMSGSIZE: return "message too long";
498 case EMULTIHOP: return "multihop attempted";
501 case ENAMETOOLONG: return "file name too long";
504 case ENAVAIL: return "not available";
507 case ENET: return "ENET";
510 case ENETDOWN: return "network is down";
513 case ENETRESET: return "network dropped connection on reset";
516 case ENETUNREACH: return "network is unreachable";
519 case ENFILE: return "file table overflow";
522 case ENOANO: return "anode table overflow";
524 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
525 case ENOBUFS: return "no buffer space available";
528 case ENOCSI: return "no CSI structure available";
531 case ENODATA: return "no data available";
534 case ENODEV: return "no such device";
537 case ENOENT: return "no such file or directory";
540 case ENOEXEC: return "exec format error";
543 case ENOLCK: return "no locks available";
546 case ENOLINK: return "link has be severed";
549 case ENOMEM: return "not enough memory";
552 case ENOMSG: return "no message of desired type";
555 case ENONET: return "machine is not on the network";
558 case ENOPKG: return "package not installed";
561 case ENOPROTOOPT: return "bad proocol option";
564 case ENOSPC: return "no space left on device";
567 case ENOSR: return "out of stream resources";
570 case ENOSTR: return "not a stream device";
573 case ENOSYM: return "unresolved symbol name";
576 case ENOSYS: return "function not implemented";
579 case ENOTBLK: return "block device required";
582 case ENOTCONN: return "socket is not connected";
585 case ENOTDIR: return "not a directory";
588 case ENOTEMPTY: return "directory not empty";
591 case ENOTNAM: return "not a name file";
594 case ENOTSOCK: return "socket operation on non-socket";
597 case ENOTTY: return "inappropriate device for ioctl";
600 case ENOTUNIQ: return "name not unique on network";
603 case ENXIO: return "no such device or address";
606 case EOPNOTSUPP: return "operation not supported on socket";
609 case EPERM: return "not owner";
612 case EPFNOSUPPORT: return "protocol family not supported";
615 case EPIPE: return "broken pipe";
618 case EPROCLIM: return "too many processes";
621 case EPROCUNAVAIL: return "bad procedure for program";
624 case EPROGMISMATCH: return "program version wrong";
627 case EPROGUNAVAIL: return "RPC program not available";
630 case EPROTO: return "protocol error";
632 #ifdef EPROTONOSUPPORT
633 case EPROTONOSUPPORT: return "protocol not suppored";
636 case EPROTOTYPE: return "protocol wrong type for socket";
639 case ERANGE: return "math result unrepresentable";
641 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
642 case EREFUSED: return "EREFUSED";
645 case EREMCHG: return "remote address changed";
648 case EREMDEV: return "remote device";
651 case EREMOTE: return "pathname hit remote file system";
654 case EREMOTEIO: return "remote i/o error";
656 #ifdef EREMOTERELEASE
657 case EREMOTERELEASE: return "EREMOTERELEASE";
660 case EROFS: return "read-only file system";
663 case ERPCMISMATCH: return "RPC version is wrong";
666 case ERREMOTE: return "object is remote";
669 case ESHUTDOWN: return "can't send afer socket shutdown";
671 #ifdef ESOCKTNOSUPPORT
672 case ESOCKTNOSUPPORT: return "socket type not supported";
675 case ESPIPE: return "invalid seek";
678 case ESRCH: return "no such process";
681 case ESRMNT: return "srmount error";
684 case ESTALE: return "stale remote file handle";
687 case ESUCCESS: return "Error 0";
690 case ETIME: return "timer expired";
693 case ETIMEDOUT: return "connection timed out";
696 case ETOOMANYREFS: return "too many references: can't splice";
699 case ETXTBSY: return "text file or pseudo-device busy";
702 case EUCLEAN: return "structure needs cleaning";
705 case EUNATCH: return "protocol driver not attached";
708 case EUSERS: return "too many users";
711 case EVERSION: return "version mismatch";
713 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
714 case EWOULDBLOCK: return "operation would block";
717 case EXDEV: return "cross-domain link";
720 case EXFULL: return "message tables full";
723 #else /* NO_SYS_ERRLIST */
725 extern char *sys_errlist[];
727 if ((errnum > 0) && (errnum <= sys_nerr))
728 return sys_errlist [errnum];
729 #endif /* NO_SYS_ERRLIST */
731 msg = g_static_private_get (&msg_private);
734 msg = g_new (gchar, 64);
735 g_static_private_set (&msg_private, msg, g_free);
738 sprintf (msg, "unknown error (%d)", errnum);
743 G_CONST_RETURN gchar*
744 g_strsignal (gint signum)
746 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
749 #ifdef HAVE_STRSIGNAL
750 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
751 extern const char *strsignal(int);
753 /* this is declared differently (const) in string.h on BeOS */
754 extern char *strsignal (int sig);
755 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
756 return strsignal (signum);
761 case SIGHUP: return "Hangup";
764 case SIGINT: return "Interrupt";
767 case SIGQUIT: return "Quit";
770 case SIGILL: return "Illegal instruction";
773 case SIGTRAP: return "Trace/breakpoint trap";
776 case SIGABRT: return "IOT trap/Abort";
779 case SIGBUS: return "Bus error";
782 case SIGFPE: return "Floating point exception";
785 case SIGKILL: return "Killed";
788 case SIGUSR1: return "User defined signal 1";
791 case SIGSEGV: return "Segmentation fault";
794 case SIGUSR2: return "User defined signal 2";
797 case SIGPIPE: return "Broken pipe";
800 case SIGALRM: return "Alarm clock";
803 case SIGTERM: return "Terminated";
806 case SIGSTKFLT: return "Stack fault";
809 case SIGCHLD: return "Child exited";
812 case SIGCONT: return "Continued";
815 case SIGSTOP: return "Stopped (signal)";
818 case SIGTSTP: return "Stopped";
821 case SIGTTIN: return "Stopped (tty input)";
824 case SIGTTOU: return "Stopped (tty output)";
827 case SIGURG: return "Urgent condition";
830 case SIGXCPU: return "CPU time limit exceeded";
833 case SIGXFSZ: return "File size limit exceeded";
836 case SIGVTALRM: return "Virtual time alarm";
839 case SIGPROF: return "Profile signal";
842 case SIGWINCH: return "Window size changed";
845 case SIGIO: return "Possible I/O";
848 case SIGPWR: return "Power failure";
851 case SIGUNUSED: return "Unused signal";
854 #else /* NO_SYS_SIGLIST */
856 #ifdef NO_SYS_SIGLIST_DECL
857 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
860 return (char*) /* this function should return const --josh */ sys_siglist [signum];
861 #endif /* NO_SYS_SIGLIST */
863 msg = g_static_private_get (&msg_private);
866 msg = g_new (gchar, 64);
867 g_static_private_set (&msg_private, msg, g_free);
870 sprintf (msg, "unknown signal (%d)", signum);
875 /* Functions g_strlcpy and g_strlcat were originally developed by
876 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
877 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
878 * for more information.
882 /* Use the native ones, if available; they might be implemented in assembly */
884 g_strlcpy (gchar *dest,
888 g_return_val_if_fail (dest != NULL, 0);
889 g_return_val_if_fail (src != NULL, 0);
891 return strlcpy (dest, src, dest_size);
895 g_strlcat (gchar *dest,
899 g_return_val_if_fail (dest != NULL, 0);
900 g_return_val_if_fail (src != NULL, 0);
902 return strlcat (dest, src, dest_size);
905 #else /* ! HAVE_STRLCPY */
908 * Copy string src to buffer dest (of buffer size dest_size). At most
909 * dest_size-1 characters will be copied. Always NUL terminates
910 * (unless dest_size == 0). This function does NOT allocate memory.
911 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
912 * Returns size of attempted result, strlen(src),
913 * so if retval >= dest_size, truncation occurred.
916 g_strlcpy (gchar *dest,
920 register gchar *d = dest;
921 register const gchar *s = src;
922 register gsize n = dest_size;
924 g_return_val_if_fail (dest != NULL, 0);
925 g_return_val_if_fail (src != NULL, 0);
927 /* Copy as many bytes as will fit */
928 if (n != 0 && --n != 0)
931 register gchar c = *s++;
939 /* If not enough room in dest, add NUL and traverse rest of src */
948 return s - src - 1; /* count does not include NUL */
953 * Appends string src to buffer dest (of buffer size dest_size).
954 * At most dest_size-1 characters will be copied.
955 * Unlike strncat, dest_size is the full size of dest, not the space left over.
956 * This function does NOT allocate memory.
957 * This always NUL terminates (unless siz == 0 or there were no NUL characters
958 * in the dest_size characters of dest to start with).
959 * Returns size of attempted result, which is
960 * MIN (dest_size, strlen (original dest)) + strlen (src),
961 * so if retval >= dest_size, truncation occurred.
964 g_strlcat (gchar *dest,
968 register gchar *d = dest;
969 register const gchar *s = src;
970 register gsize bytes_left = dest_size;
971 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
973 g_return_val_if_fail (dest != NULL, 0);
974 g_return_val_if_fail (src != NULL, 0);
976 /* Find the end of dst and adjust bytes left but don't go past end */
977 while (*d != 0 && bytes_left-- != 0)
980 bytes_left = dest_size - dlength;
983 return dlength + strlen (s);
996 return dlength + (s - src); /* count does not include NUL */
998 #endif /* ! HAVE_STRLCPY */
1004 * Converts all upper case ASCII letters to lower case ASCII letters.
1006 * Return value: a newly allocated string, with all the upper case
1007 * characters in @string converted to lower case, with
1008 * semantics that exactly match g_ascii_tolower. (Note
1009 * that this is unlike the old g_strdown, which modified
1010 * the string in place.)
1013 g_ascii_strdown (const gchar *string)
1017 g_return_val_if_fail (string != NULL, NULL);
1019 result = g_strdup (string);
1020 for (s = result; *s; s++)
1021 *s = g_ascii_tolower (*s);
1030 * Converts all lower case ASCII letters to upper case ASCII letters.
1032 * Return value: a newly allocated string, with all the lower case
1033 * characters in @string converted to upper case, with
1034 * semantics that exactly match g_ascii_toupper. (Note
1035 * that this is unlike the old g_strup, which modified
1036 * the string in place.)
1039 g_ascii_strup (const gchar *string)
1043 g_return_val_if_fail (string != NULL, NULL);
1045 result = g_strdup (string);
1046 for (s = result; *s; s++)
1047 *s = g_ascii_toupper (*s);
1053 g_strdown (gchar *string)
1057 g_return_val_if_fail (string != NULL, NULL);
1059 s = (guchar *) string;
1068 return (gchar *) string;
1072 g_strup (gchar *string)
1076 g_return_val_if_fail (string != NULL, NULL);
1078 s = (guchar *) string;
1087 return (gchar *) string;
1091 g_strreverse (gchar *string)
1093 g_return_val_if_fail (string != NULL, NULL);
1097 register gchar *h, *t;
1100 t = string + strlen (string) - 1;
1121 * Convert a character to ASCII lower case.
1123 * Unlike the standard C library tolower function, this only
1124 * recognizes standard ASCII letters and ignores the locale, returning
1125 * all non-ASCII characters unchanged, even if they are lower case
1126 * letters in a particular character set. Also unlike the standard
1127 * library function, this takes and returns a char, not an int, so
1128 * don't call it on EOF but no need to worry about casting to guchar
1129 * before passing a possibly non-ASCII character in.
1131 * Return value: the result of converting @c to lower case.
1132 * If @c is not an ASCII upper case letter,
1133 * @c is returned unchanged.
1136 g_ascii_tolower (gchar c)
1138 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1145 * Convert a character to ASCII upper case.
1147 * Unlike the standard C library toupper function, this only
1148 * recognizes standard ASCII letters and ignores the locale, returning
1149 * all non-ASCII characters unchanged, even if they are upper case
1150 * letters in a particular character set. Also unlike the standard
1151 * library function, this takes and returns a char, not an int, so
1152 * don't call it on EOF but no need to worry about casting to guchar
1153 * before passing a possibly non-ASCII character in.
1155 * Return value: the result of converting @c to upper case.
1156 * If @c is not an ASCII lower case letter,
1157 * @c is returned unchanged.
1160 g_ascii_toupper (gchar c)
1162 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1166 * g_ascii_digit_value:
1167 * @c: an ASCII character
1169 * Determines the numeric value of a character as a decimal
1170 * digit. Differs from g_unichar_digit_value because it takes
1171 * a char, so there's no worry about sign extension if characters
1174 * Return value: If @c is a decimal digit (according to
1175 * `g_ascii_isdigit'), its numeric value. Otherwise, -1.
1178 g_ascii_digit_value (gchar c)
1180 if (g_ascii_isdigit (c))
1186 * g_ascii_xdigit_value:
1187 * @c: an ASCII character
1189 * Determines the numeric value of a character as a hexidecimal
1190 * digit. Differs from g_unichar_xdigit_value because it takes
1191 * a char, so there's no worry about sign extension if characters
1194 * Return value: If @c is a hex digit (according to
1195 * `g_ascii_isxdigit'), its numeric value. Otherwise, -1.
1198 g_ascii_xdigit_value (gchar c)
1200 if (c >= 'A' && c <= 'F')
1201 return c - 'A' + 10;
1202 if (c >= 'a' && c <= 'f')
1203 return c - 'a' + 10;
1204 return g_ascii_digit_value (c);
1208 * g_ascii_strcasecmp:
1209 * @s1: string to compare with @s2
1210 * @s2: string to compare with @s1
1212 * Compare two strings, ignoring the case of ASCII characters.
1214 * Unlike the BSD strcasecmp function, this only recognizes standard
1215 * ASCII letters and ignores the locale, treating all non-ASCII
1216 * characters as if they are not letters.
1218 * Return value: an integer less than, equal to, or greater than
1219 * zero if @s1 is found, respectively, to be less than,
1220 * to match, or to be greater than @s2.
1223 g_ascii_strcasecmp (const gchar *s1,
1228 g_return_val_if_fail (s1 != NULL, 0);
1229 g_return_val_if_fail (s2 != NULL, 0);
1233 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1234 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1240 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1244 * g_ascii_strncasecmp:
1245 * @s1: string to compare with @s2
1246 * @s2: string to compare with @s1
1247 * @n: number of characters to compare
1249 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1250 * characters after the first @n in each string.
1252 * Unlike the BSD strcasecmp function, this only recognizes standard
1253 * ASCII letters and ignores the locale, treating all non-ASCII
1254 * characters as if they are not letters.
1256 * Return value: an integer less than, equal to, or greater than zero
1257 * if the first @n bytes of @s1 is found, respectively,
1258 * to be less than, to match, or to be greater than the
1259 * first @n bytes of @s2.
1262 g_ascii_strncasecmp (const gchar *s1,
1268 g_return_val_if_fail (s1 != NULL, 0);
1269 g_return_val_if_fail (s2 != NULL, 0);
1271 while (n && *s1 && *s2)
1274 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1275 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1282 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1288 g_strcasecmp (const gchar *s1,
1291 #ifdef HAVE_STRCASECMP
1292 g_return_val_if_fail (s1 != NULL, 0);
1293 g_return_val_if_fail (s2 != NULL, 0);
1295 return strcasecmp (s1, s2);
1299 g_return_val_if_fail (s1 != NULL, 0);
1300 g_return_val_if_fail (s2 != NULL, 0);
1304 /* According to A. Cox, some platforms have islower's that
1305 * don't work right on non-uppercase
1307 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1308 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1314 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1319 g_strncasecmp (const gchar *s1,
1323 #ifdef HAVE_STRNCASECMP
1324 return strncasecmp (s1, s2, n);
1328 g_return_val_if_fail (s1 != NULL, 0);
1329 g_return_val_if_fail (s2 != NULL, 0);
1331 while (n && *s1 && *s2)
1334 /* According to A. Cox, some platforms have islower's that
1335 * don't work right on non-uppercase
1337 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1338 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1345 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1352 g_strdelimit (gchar *string,
1353 const gchar *delimiters,
1358 g_return_val_if_fail (string != NULL, NULL);
1361 delimiters = G_STR_DELIMITERS;
1363 for (c = string; *c; c++)
1365 if (strchr (delimiters, *c))
1373 g_strcanon (gchar *string,
1374 const gchar *valid_chars,
1379 g_return_val_if_fail (string != NULL, NULL);
1380 g_return_val_if_fail (valid_chars != NULL, NULL);
1382 for (c = string; *c; c++)
1384 if (!strchr (valid_chars, *c))
1392 g_strcompress (const gchar *source)
1394 const gchar *p = source, *octal;
1395 gchar *dest = g_malloc (strlen (source) + 1);
1405 case '0': case '1': case '2': case '3': case '4':
1406 case '5': case '6': case '7':
1409 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1411 *q = (*q * 8) + (*p - '0');
1432 default: /* Also handles \" and \\ */
1447 g_strescape (const gchar *source,
1448 const gchar *exceptions)
1455 g_return_val_if_fail (source != NULL, NULL);
1457 p = (guchar *) source;
1458 /* Each source byte needs maximally four destination chars (\777) */
1459 q = dest = g_malloc (strlen (source) * 4 + 1);
1461 memset (excmap, 0, 256);
1464 guchar *e = (guchar *) exceptions;
1510 if ((*p < ' ') || (*p >= 0177))
1513 *q++ = '0' + (((*p) >> 6) & 07);
1514 *q++ = '0' + (((*p) >> 3) & 07);
1515 *q++ = '0' + ((*p) & 07);
1529 g_strchug (gchar *string)
1533 g_return_val_if_fail (string != NULL, NULL);
1535 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
1538 g_memmove (string, start, strlen ((gchar *) start) + 1);
1544 g_strchomp (gchar *string)
1548 g_return_val_if_fail (string != NULL, NULL);
1553 for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s);
1562 * @string: a string to split.
1563 * @delimiter: a string which specifies the places at which to split the string.
1564 * The delimiter is not included in any of the resulting strings, unless
1565 * max_tokens is reached.
1566 * @max_tokens: the maximum number of pieces to split @string into. If this is
1567 * less than 1, the string is split completely.
1569 * Splits a string into a maximum of @max_tokens pieces, using the given
1570 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
1571 * to the last token.
1573 * As a special case, the result of splitting the empty string "" is an empty
1574 * vector, not a vector containing a single string. The reason for this
1575 * special case is that being able to represent a empty vector is typically
1576 * more useful than consistent handling of empty elements. If you do need
1577 * to represent empty elements, you'll need to check for the empty string
1578 * before calling g_strsplit().
1580 * Return value: a newly-allocated %NULL-terminated array of strings. Use g_strfreev()
1584 g_strsplit (const gchar *string,
1585 const gchar *delimiter,
1588 GSList *string_list = NULL, *slist;
1589 gchar **str_array, *s;
1591 const gchar *remainder;
1593 g_return_val_if_fail (string != NULL, NULL);
1594 g_return_val_if_fail (delimiter != NULL, NULL);
1595 g_return_val_if_fail (delimiter[0] != '\0', NULL);
1598 max_tokens = G_MAXINT;
1603 s = strstr (remainder, delimiter);
1606 gsize delimiter_len = strlen (delimiter);
1613 len = s - remainder;
1614 new_string = g_new (gchar, len + 1);
1615 strncpy (new_string, remainder, len);
1616 new_string[len] = 0;
1617 string_list = g_slist_prepend (string_list, new_string);
1619 remainder = s + delimiter_len;
1620 s = strstr (remainder, delimiter);
1622 while (--max_tokens && s);
1627 string_list = g_slist_prepend (string_list, g_strdup (remainder));
1630 str_array = g_new (gchar*, n + 1);
1632 str_array[n--] = NULL;
1633 for (slist = string_list; slist; slist = slist->next)
1634 str_array[n--] = slist->data;
1636 g_slist_free (string_list);
1642 g_strfreev (gchar **str_array)
1648 for(i = 0; str_array[i] != NULL; i++)
1649 g_free(str_array[i]);
1657 * @str_array: %NULL-terminated array of strings
1659 * Copies %NULL-terminated array of strings. The copy is a deep copy;
1660 * the new array should be freed by first freeing each string, then
1661 * the array itself. g_strfreev() does this for you. If called
1662 * on a %NULL value, g_strdupv() simply returns %NULL.
1664 * Return value: a new %NULL-terminated array of strings
1667 g_strdupv (gchar **str_array)
1675 while (str_array[i])
1678 retval = g_new (gchar*, i + 1);
1681 while (str_array[i])
1683 retval[i] = g_strdup (str_array[i]);
1695 g_strjoinv (const gchar *separator,
1701 g_return_val_if_fail (str_array != NULL, NULL);
1703 if (separator == NULL)
1710 gsize separator_len;
1712 separator_len = strlen (separator);
1713 /* First part, getting length */
1714 len = 1 + strlen (str_array[0]);
1715 for (i = 1; str_array[i] != NULL; i++)
1716 len += strlen (str_array[i]);
1717 len += separator_len * (i - 1);
1719 /* Second part, building string */
1720 string = g_new (gchar, len);
1721 ptr = g_stpcpy (string, *str_array);
1722 for (i = 1; str_array[i] != NULL; i++)
1724 ptr = g_stpcpy (ptr, separator);
1725 ptr = g_stpcpy (ptr, str_array[i]);
1729 string = g_strdup ("");
1735 g_strjoin (const gchar *separator,
1741 gsize separator_len;
1744 if (separator == NULL)
1747 separator_len = strlen (separator);
1749 va_start (args, separator);
1751 s = va_arg (args, gchar*);
1755 /* First part, getting length */
1756 len = 1 + strlen (s);
1758 s = va_arg (args, gchar*);
1761 len += separator_len + strlen (s);
1762 s = va_arg (args, gchar*);
1766 /* Second part, building string */
1767 string = g_new (gchar, len);
1769 va_start (args, separator);
1771 s = va_arg (args, gchar*);
1772 ptr = g_stpcpy (string, s);
1774 s = va_arg (args, gchar*);
1777 ptr = g_stpcpy (ptr, separator);
1778 ptr = g_stpcpy (ptr, s);
1779 s = va_arg (args, gchar*);
1783 string = g_strdup ("");
1793 * @haystack: a string
1794 * @haystack_len: The maximum length of haystack
1795 * @needle: The string to search for.
1797 * Searches the string haystack for the first occurrence
1798 * of the string needle, limiting the length of the search
1801 * Return value: A pointer to the found occurrence, or
1802 * NULL if not found.
1805 g_strstr_len (const gchar *haystack,
1806 gssize haystack_len,
1807 const gchar *needle)
1809 g_return_val_if_fail (haystack != NULL, NULL);
1810 g_return_val_if_fail (needle != NULL, NULL);
1812 if (haystack_len < 0)
1813 return strstr (haystack, needle);
1816 const gchar *p = haystack;
1817 gsize needle_len = strlen (needle);
1821 if (needle_len == 0)
1822 return (gchar *)haystack;
1824 if (haystack_len < needle_len)
1827 end = haystack + haystack_len - needle_len;
1829 while (*p && p <= end)
1831 for (i = 0; i < needle_len; i++)
1832 if (p[i] != needle[i])
1847 * @haystack: a nul-terminated string
1848 * @needle: The nul-terminated string to search for.
1850 * Searches the string haystack for the last occurrence
1851 * of the string needle.
1853 * Return value: A pointer to the found occurrence, or
1854 * NULL if not found.
1857 g_strrstr (const gchar *haystack,
1858 const gchar *needle)
1865 g_return_val_if_fail (haystack != NULL, NULL);
1866 g_return_val_if_fail (needle != NULL, NULL);
1868 needle_len = strlen (needle);
1869 haystack_len = strlen (haystack);
1871 if (needle_len == 0)
1872 return (gchar *)haystack;
1874 if (haystack_len < needle_len)
1877 p = haystack + haystack_len - needle_len;
1879 while (p >= haystack)
1881 for (i = 0; i < needle_len; i++)
1882 if (p[i] != needle[i])
1896 * @haystack: a nul-terminated string
1897 * @haystack_len: The maximum length of haystack
1898 * @needle: The nul-terminated string to search for.
1900 * Searches the string haystack for the last occurrence
1901 * of the string needle, limiting the length of the search
1904 * Return value: A pointer to the found occurrence, or
1905 * NULL if not found.
1908 g_strrstr_len (const gchar *haystack,
1909 gssize haystack_len,
1910 const gchar *needle)
1912 g_return_val_if_fail (haystack != NULL, NULL);
1913 g_return_val_if_fail (needle != NULL, NULL);
1915 if (haystack_len < 0)
1916 return g_strrstr (haystack, needle);
1919 gsize needle_len = strlen (needle);
1920 const gchar *haystack_max = haystack + haystack_len;
1921 const gchar *p = haystack;
1924 while (p < haystack_max && *p)
1927 if (p < haystack + needle_len)
1932 while (p >= haystack)
1934 for (i = 0; i < needle_len; i++)
1935 if (p[i] != needle[i])