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,
138 str = g_new (gchar, length + 1);
139 memset (str, (guchar)fill_char, length);
147 * @dest: destination buffer
148 * @src: source string
150 * Copies a nul-terminated string into the dest buffer, include the
151 * trailing nul, and return a pointer to the trailing nul byte.
152 * This is useful for concatenating multiple strings together
153 * without having to repeatedly scan for the end.
155 * Return value: a pointer to trailing nul byte.
158 g_stpcpy (gchar *dest,
162 g_return_val_if_fail (dest != NULL, NULL);
163 g_return_val_if_fail (src != NULL, NULL);
164 return stpcpy (dest, src);
166 register gchar *d = dest;
167 register const gchar *s = src;
169 g_return_val_if_fail (dest != NULL, NULL);
170 g_return_val_if_fail (src != NULL, NULL);
173 while (*s++ != '\0');
180 g_strdup_vprintf (const gchar *format,
184 #ifdef HAVE_VASPRINTF
185 vasprintf (&buffer, format, args1);
186 if (!g_mem_is_system_malloc ())
188 gchar *buffer1 = g_strdup (buffer);
195 G_VA_COPY (args2, args1);
197 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
199 vsprintf (buffer, format, args2);
206 g_strdup_printf (const gchar *format,
212 va_start (args, format);
213 buffer = g_strdup_vprintf (format, args);
220 g_strconcat (const gchar *string1, ...)
228 g_return_val_if_fail (string1 != NULL, NULL);
230 l = 1 + strlen (string1);
231 va_start (args, string1);
232 s = va_arg (args, gchar*);
236 s = va_arg (args, gchar*);
240 concat = g_new (gchar, l);
243 ptr = g_stpcpy (ptr, string1);
244 va_start (args, string1);
245 s = va_arg (args, gchar*);
248 ptr = g_stpcpy (ptr, s);
249 s = va_arg (args, gchar*);
257 g_strtod (const gchar *nptr,
265 g_return_val_if_fail (nptr != NULL, 0);
270 val_1 = strtod (nptr, &fail_pos_1);
272 if (fail_pos_1 && fail_pos_1[0] != 0)
276 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
277 setlocale (LC_NUMERIC, "C");
278 val_2 = strtod (nptr, &fail_pos_2);
279 setlocale (LC_NUMERIC, old_locale);
283 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
286 *endptr = fail_pos_1;
292 *endptr = fail_pos_2;
297 G_CONST_RETURN gchar*
298 g_strerror (gint errnum)
300 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
304 return strerror (errnum);
309 case E2BIG: return "argument list too long";
312 case EACCES: return "permission denied";
315 case EADDRINUSE: return "address already in use";
318 case EADDRNOTAVAIL: return "can't assign requested address";
321 case EADV: return "advertise error";
324 case EAFNOSUPPORT: return "address family not supported by protocol family";
327 case EAGAIN: return "try again";
330 case EALIGN: return "EALIGN";
333 case EALREADY: return "operation already in progress";
336 case EBADE: return "bad exchange descriptor";
339 case EBADF: return "bad file number";
342 case EBADFD: return "file descriptor in bad state";
345 case EBADMSG: return "not a data message";
348 case EBADR: return "bad request descriptor";
351 case EBADRPC: return "RPC structure is bad";
354 case EBADRQC: return "bad request code";
357 case EBADSLT: return "invalid slot";
360 case EBFONT: return "bad font file format";
363 case EBUSY: return "mount device busy";
366 case ECHILD: return "no children";
369 case ECHRNG: return "channel number out of range";
372 case ECOMM: return "communication error on send";
375 case ECONNABORTED: return "software caused connection abort";
378 case ECONNREFUSED: return "connection refused";
381 case ECONNRESET: return "connection reset by peer";
383 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
384 case EDEADLK: return "resource deadlock avoided";
387 case EDEADLOCK: return "resource deadlock avoided";
390 case EDESTADDRREQ: return "destination address required";
393 case EDIRTY: return "mounting a dirty fs w/o force";
396 case EDOM: return "math argument out of range";
399 case EDOTDOT: return "cross mount point";
402 case EDQUOT: return "disk quota exceeded";
405 case EDUPPKG: return "duplicate package name";
408 case EEXIST: return "file already exists";
411 case EFAULT: return "bad address in system call argument";
414 case EFBIG: return "file too large";
417 case EHOSTDOWN: return "host is down";
420 case EHOSTUNREACH: return "host is unreachable";
423 case EIDRM: return "identifier removed";
426 case EINIT: return "initialization error";
429 case EINPROGRESS: return "operation now in progress";
432 case EINTR: return "interrupted system call";
435 case EINVAL: return "invalid argument";
438 case EIO: return "I/O error";
441 case EISCONN: return "socket is already connected";
444 case EISDIR: return "is a directory";
447 case EISNAM: return "is a name file";
450 case ELBIN: return "ELBIN";
453 case EL2HLT: return "level 2 halted";
456 case EL2NSYNC: return "level 2 not synchronized";
459 case EL3HLT: return "level 3 halted";
462 case EL3RST: return "level 3 reset";
465 case ELIBACC: return "can not access a needed shared library";
468 case ELIBBAD: return "accessing a corrupted shared library";
471 case ELIBEXEC: return "can not exec a shared library directly";
474 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
477 case ELIBSCN: return ".lib section in a.out corrupted";
480 case ELNRNG: return "link number out of range";
483 case ELOOP: return "too many levels of symbolic links";
486 case EMFILE: return "too many open files";
489 case EMLINK: return "too many links";
492 case EMSGSIZE: return "message too long";
495 case EMULTIHOP: return "multihop attempted";
498 case ENAMETOOLONG: return "file name too long";
501 case ENAVAIL: return "not available";
504 case ENET: return "ENET";
507 case ENETDOWN: return "network is down";
510 case ENETRESET: return "network dropped connection on reset";
513 case ENETUNREACH: return "network is unreachable";
516 case ENFILE: return "file table overflow";
519 case ENOANO: return "anode table overflow";
521 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
522 case ENOBUFS: return "no buffer space available";
525 case ENOCSI: return "no CSI structure available";
528 case ENODATA: return "no data available";
531 case ENODEV: return "no such device";
534 case ENOENT: return "no such file or directory";
537 case ENOEXEC: return "exec format error";
540 case ENOLCK: return "no locks available";
543 case ENOLINK: return "link has be severed";
546 case ENOMEM: return "not enough memory";
549 case ENOMSG: return "no message of desired type";
552 case ENONET: return "machine is not on the network";
555 case ENOPKG: return "package not installed";
558 case ENOPROTOOPT: return "bad proocol option";
561 case ENOSPC: return "no space left on device";
564 case ENOSR: return "out of stream resources";
567 case ENOSTR: return "not a stream device";
570 case ENOSYM: return "unresolved symbol name";
573 case ENOSYS: return "function not implemented";
576 case ENOTBLK: return "block device required";
579 case ENOTCONN: return "socket is not connected";
582 case ENOTDIR: return "not a directory";
585 case ENOTEMPTY: return "directory not empty";
588 case ENOTNAM: return "not a name file";
591 case ENOTSOCK: return "socket operation on non-socket";
594 case ENOTTY: return "inappropriate device for ioctl";
597 case ENOTUNIQ: return "name not unique on network";
600 case ENXIO: return "no such device or address";
603 case EOPNOTSUPP: return "operation not supported on socket";
606 case EPERM: return "not owner";
609 case EPFNOSUPPORT: return "protocol family not supported";
612 case EPIPE: return "broken pipe";
615 case EPROCLIM: return "too many processes";
618 case EPROCUNAVAIL: return "bad procedure for program";
621 case EPROGMISMATCH: return "program version wrong";
624 case EPROGUNAVAIL: return "RPC program not available";
627 case EPROTO: return "protocol error";
629 #ifdef EPROTONOSUPPORT
630 case EPROTONOSUPPORT: return "protocol not suppored";
633 case EPROTOTYPE: return "protocol wrong type for socket";
636 case ERANGE: return "math result unrepresentable";
638 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
639 case EREFUSED: return "EREFUSED";
642 case EREMCHG: return "remote address changed";
645 case EREMDEV: return "remote device";
648 case EREMOTE: return "pathname hit remote file system";
651 case EREMOTEIO: return "remote i/o error";
653 #ifdef EREMOTERELEASE
654 case EREMOTERELEASE: return "EREMOTERELEASE";
657 case EROFS: return "read-only file system";
660 case ERPCMISMATCH: return "RPC version is wrong";
663 case ERREMOTE: return "object is remote";
666 case ESHUTDOWN: return "can't send afer socket shutdown";
668 #ifdef ESOCKTNOSUPPORT
669 case ESOCKTNOSUPPORT: return "socket type not supported";
672 case ESPIPE: return "invalid seek";
675 case ESRCH: return "no such process";
678 case ESRMNT: return "srmount error";
681 case ESTALE: return "stale remote file handle";
684 case ESUCCESS: return "Error 0";
687 case ETIME: return "timer expired";
690 case ETIMEDOUT: return "connection timed out";
693 case ETOOMANYREFS: return "too many references: can't splice";
696 case ETXTBSY: return "text file or pseudo-device busy";
699 case EUCLEAN: return "structure needs cleaning";
702 case EUNATCH: return "protocol driver not attached";
705 case EUSERS: return "too many users";
708 case EVERSION: return "version mismatch";
710 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
711 case EWOULDBLOCK: return "operation would block";
714 case EXDEV: return "cross-domain link";
717 case EXFULL: return "message tables full";
720 #else /* NO_SYS_ERRLIST */
722 extern char *sys_errlist[];
724 if ((errnum > 0) && (errnum <= sys_nerr))
725 return sys_errlist [errnum];
726 #endif /* NO_SYS_ERRLIST */
728 msg = g_static_private_get (&msg_private);
731 msg = g_new (gchar, 64);
732 g_static_private_set (&msg_private, msg, g_free);
735 sprintf (msg, "unknown error (%d)", errnum);
740 G_CONST_RETURN gchar*
741 g_strsignal (gint signum)
743 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
746 #ifdef HAVE_STRSIGNAL
747 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
748 extern const char *strsignal(int);
750 /* this is declared differently (const) in string.h on BeOS */
751 extern char *strsignal (int sig);
752 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
753 return strsignal (signum);
758 case SIGHUP: return "Hangup";
761 case SIGINT: return "Interrupt";
764 case SIGQUIT: return "Quit";
767 case SIGILL: return "Illegal instruction";
770 case SIGTRAP: return "Trace/breakpoint trap";
773 case SIGABRT: return "IOT trap/Abort";
776 case SIGBUS: return "Bus error";
779 case SIGFPE: return "Floating point exception";
782 case SIGKILL: return "Killed";
785 case SIGUSR1: return "User defined signal 1";
788 case SIGSEGV: return "Segmentation fault";
791 case SIGUSR2: return "User defined signal 2";
794 case SIGPIPE: return "Broken pipe";
797 case SIGALRM: return "Alarm clock";
800 case SIGTERM: return "Terminated";
803 case SIGSTKFLT: return "Stack fault";
806 case SIGCHLD: return "Child exited";
809 case SIGCONT: return "Continued";
812 case SIGSTOP: return "Stopped (signal)";
815 case SIGTSTP: return "Stopped";
818 case SIGTTIN: return "Stopped (tty input)";
821 case SIGTTOU: return "Stopped (tty output)";
824 case SIGURG: return "Urgent condition";
827 case SIGXCPU: return "CPU time limit exceeded";
830 case SIGXFSZ: return "File size limit exceeded";
833 case SIGVTALRM: return "Virtual time alarm";
836 case SIGPROF: return "Profile signal";
839 case SIGWINCH: return "Window size changed";
842 case SIGIO: return "Possible I/O";
845 case SIGPWR: return "Power failure";
848 case SIGUNUSED: return "Unused signal";
851 #else /* NO_SYS_SIGLIST */
853 #ifdef NO_SYS_SIGLIST_DECL
854 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
857 return (char*) /* this function should return const --josh */ sys_siglist [signum];
858 #endif /* NO_SYS_SIGLIST */
860 msg = g_static_private_get (&msg_private);
863 msg = g_new (gchar, 64);
864 g_static_private_set (&msg_private, msg, g_free);
867 sprintf (msg, "unknown signal (%d)", signum);
872 /* Functions g_strlcpy and g_strlcat were originally developed by
873 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
874 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
875 * for more information.
879 /* Use the native ones, if available; they might be implemented in assembly */
881 g_strlcpy (gchar *dest,
885 g_return_val_if_fail (dest != NULL, 0);
886 g_return_val_if_fail (src != NULL, 0);
888 return strlcpy (dest, src, dest_size);
892 g_strlcat (gchar *dest,
896 g_return_val_if_fail (dest != NULL, 0);
897 g_return_val_if_fail (src != NULL, 0);
899 return strlcat (dest, src, dest_size);
902 #else /* ! HAVE_STRLCPY */
905 * Copy string src to buffer dest (of buffer size dest_size). At most
906 * dest_size-1 characters will be copied. Always NUL terminates
907 * (unless dest_size == 0). This function does NOT allocate memory.
908 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
909 * Returns size of attempted result, strlen(src),
910 * so if retval >= dest_size, truncation occurred.
913 g_strlcpy (gchar *dest,
917 register gchar *d = dest;
918 register const gchar *s = src;
919 register gsize n = dest_size;
921 g_return_val_if_fail (dest != NULL, 0);
922 g_return_val_if_fail (src != NULL, 0);
924 /* Copy as many bytes as will fit */
925 if (n != 0 && --n != 0)
928 register gchar c = *s++;
936 /* If not enough room in dest, add NUL and traverse rest of src */
945 return s - src - 1; /* count does not include NUL */
950 * Appends string src to buffer dest (of buffer size dest_size).
951 * At most dest_size-1 characters will be copied.
952 * Unlike strncat, dest_size is the full size of dest, not the space left over.
953 * This function does NOT allocate memory.
954 * This always NUL terminates (unless siz == 0 or there were no NUL characters
955 * in the dest_size characters of dest to start with).
956 * Returns size of attempted result, which is
957 * MIN (dest_size, strlen (original dest)) + strlen (src),
958 * so if retval >= dest_size, truncation occurred.
961 g_strlcat (gchar *dest,
965 register gchar *d = dest;
966 register const gchar *s = src;
967 register gsize bytes_left = dest_size;
968 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
970 g_return_val_if_fail (dest != NULL, 0);
971 g_return_val_if_fail (src != NULL, 0);
973 /* Find the end of dst and adjust bytes left but don't go past end */
974 while (*d != 0 && bytes_left-- != 0)
977 bytes_left = dest_size - dlength;
980 return dlength + strlen (s);
993 return dlength + (s - src); /* count does not include NUL */
995 #endif /* ! HAVE_STRLCPY */
1000 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1002 * Converts all upper case ASCII letters to lower case ASCII letters.
1004 * Return value: a newly allocated string, with all the upper case
1005 * characters in @str converted to lower case, with
1006 * semantics that exactly match g_ascii_tolower. (Note
1007 * that this is unlike the old g_strdown, which modified
1008 * the string in place.)
1011 g_ascii_strdown (const gchar *str,
1016 g_return_val_if_fail (str != NULL, NULL);
1021 result = g_strndup (str, len);
1022 for (s = result; *s; s++)
1023 *s = g_ascii_tolower (*s);
1031 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1033 * Converts all lower case ASCII letters to upper case ASCII letters.
1035 * Return value: a newly allocated string, with all the lower case
1036 * characters in @str converted to upper case, with
1037 * semantics that exactly match g_ascii_toupper. (Note
1038 * that this is unlike the old g_strup, which modified
1039 * the string in place.)
1042 g_ascii_strup (const gchar *str,
1047 g_return_val_if_fail (str != NULL, NULL);
1052 result = g_strndup (str, len);
1053 for (s = result; *s; s++)
1054 *s = g_ascii_toupper (*s);
1060 g_strdown (gchar *string)
1064 g_return_val_if_fail (string != NULL, NULL);
1066 s = (guchar *) string;
1075 return (gchar *) string;
1079 g_strup (gchar *string)
1083 g_return_val_if_fail (string != NULL, NULL);
1085 s = (guchar *) string;
1094 return (gchar *) string;
1098 g_strreverse (gchar *string)
1100 g_return_val_if_fail (string != NULL, NULL);
1104 register gchar *h, *t;
1107 t = string + strlen (string) - 1;
1128 * Convert a character to ASCII lower case.
1130 * Unlike the standard C library tolower function, this only
1131 * recognizes standard ASCII letters and ignores the locale, returning
1132 * all non-ASCII characters unchanged, even if they are lower case
1133 * letters in a particular character set. Also unlike the standard
1134 * library function, this takes and returns a char, not an int, so
1135 * don't call it on EOF but no need to worry about casting to guchar
1136 * before passing a possibly non-ASCII character in.
1138 * Return value: the result of converting @c to lower case.
1139 * If @c is not an ASCII upper case letter,
1140 * @c is returned unchanged.
1143 g_ascii_tolower (gchar c)
1145 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1152 * Convert a character to ASCII upper case.
1154 * Unlike the standard C library toupper function, this only
1155 * recognizes standard ASCII letters and ignores the locale, returning
1156 * all non-ASCII characters unchanged, even if they are upper case
1157 * letters in a particular character set. Also unlike the standard
1158 * library function, this takes and returns a char, not an int, so
1159 * don't call it on EOF but no need to worry about casting to guchar
1160 * before passing a possibly non-ASCII character in.
1162 * Return value: the result of converting @c to upper case.
1163 * If @c is not an ASCII lower case letter,
1164 * @c is returned unchanged.
1167 g_ascii_toupper (gchar c)
1169 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1173 * g_ascii_digit_value:
1174 * @c: an ASCII character
1176 * Determines the numeric value of a character as a decimal
1177 * digit. Differs from g_unichar_digit_value because it takes
1178 * a char, so there's no worry about sign extension if characters
1181 * Return value: If @c is a decimal digit (according to
1182 * `g_ascii_isdigit'), its numeric value. Otherwise, -1.
1185 g_ascii_digit_value (gchar c)
1187 if (g_ascii_isdigit (c))
1193 * g_ascii_xdigit_value:
1194 * @c: an ASCII character
1196 * Determines the numeric value of a character as a hexidecimal
1197 * digit. Differs from g_unichar_xdigit_value because it takes
1198 * a char, so there's no worry about sign extension if characters
1201 * Return value: If @c is a hex digit (according to
1202 * `g_ascii_isxdigit'), its numeric value. Otherwise, -1.
1205 g_ascii_xdigit_value (gchar c)
1207 if (c >= 'A' && c <= 'F')
1208 return c - 'A' + 10;
1209 if (c >= 'a' && c <= 'f')
1210 return c - 'a' + 10;
1211 return g_ascii_digit_value (c);
1215 * g_ascii_strcasecmp:
1216 * @s1: string to compare with @s2
1217 * @s2: string to compare with @s1
1219 * Compare two strings, ignoring the case of ASCII characters.
1221 * Unlike the BSD strcasecmp function, this only recognizes standard
1222 * ASCII letters and ignores the locale, treating all non-ASCII
1223 * characters as if they are not letters.
1225 * Return value: an integer less than, equal to, or greater than
1226 * zero if @s1 is found, respectively, to be less than,
1227 * to match, or to be greater than @s2.
1230 g_ascii_strcasecmp (const gchar *s1,
1235 g_return_val_if_fail (s1 != NULL, 0);
1236 g_return_val_if_fail (s2 != NULL, 0);
1240 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1241 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1247 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1251 * g_ascii_strncasecmp:
1252 * @s1: string to compare with @s2
1253 * @s2: string to compare with @s1
1254 * @n: number of characters to compare
1256 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1257 * characters after the first @n in each string.
1259 * Unlike the BSD strcasecmp function, this only recognizes standard
1260 * ASCII letters and ignores the locale, treating all non-ASCII
1261 * characters as if they are not letters.
1263 * Return value: an integer less than, equal to, or greater than zero
1264 * if the first @n bytes of @s1 is found, respectively,
1265 * to be less than, to match, or to be greater than the
1266 * first @n bytes of @s2.
1269 g_ascii_strncasecmp (const gchar *s1,
1275 g_return_val_if_fail (s1 != NULL, 0);
1276 g_return_val_if_fail (s2 != NULL, 0);
1278 while (n && *s1 && *s2)
1281 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1282 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1289 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1295 g_strcasecmp (const gchar *s1,
1298 #ifdef HAVE_STRCASECMP
1299 g_return_val_if_fail (s1 != NULL, 0);
1300 g_return_val_if_fail (s2 != NULL, 0);
1302 return strcasecmp (s1, s2);
1306 g_return_val_if_fail (s1 != NULL, 0);
1307 g_return_val_if_fail (s2 != NULL, 0);
1311 /* According to A. Cox, some platforms have islower's that
1312 * don't work right on non-uppercase
1314 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1315 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1321 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1326 g_strncasecmp (const gchar *s1,
1330 #ifdef HAVE_STRNCASECMP
1331 return strncasecmp (s1, s2, n);
1335 g_return_val_if_fail (s1 != NULL, 0);
1336 g_return_val_if_fail (s2 != NULL, 0);
1338 while (n && *s1 && *s2)
1341 /* According to A. Cox, some platforms have islower's that
1342 * don't work right on non-uppercase
1344 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1345 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1352 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1359 g_strdelimit (gchar *string,
1360 const gchar *delimiters,
1365 g_return_val_if_fail (string != NULL, NULL);
1368 delimiters = G_STR_DELIMITERS;
1370 for (c = string; *c; c++)
1372 if (strchr (delimiters, *c))
1380 g_strcanon (gchar *string,
1381 const gchar *valid_chars,
1386 g_return_val_if_fail (string != NULL, NULL);
1387 g_return_val_if_fail (valid_chars != NULL, NULL);
1389 for (c = string; *c; c++)
1391 if (!strchr (valid_chars, *c))
1399 g_strcompress (const gchar *source)
1401 const gchar *p = source, *octal;
1402 gchar *dest = g_malloc (strlen (source) + 1);
1412 case '0': case '1': case '2': case '3': case '4':
1413 case '5': case '6': case '7':
1416 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1418 *q = (*q * 8) + (*p - '0');
1439 default: /* Also handles \" and \\ */
1454 g_strescape (const gchar *source,
1455 const gchar *exceptions)
1462 g_return_val_if_fail (source != NULL, NULL);
1464 p = (guchar *) source;
1465 /* Each source byte needs maximally four destination chars (\777) */
1466 q = dest = g_malloc (strlen (source) * 4 + 1);
1468 memset (excmap, 0, 256);
1471 guchar *e = (guchar *) exceptions;
1517 if ((*p < ' ') || (*p >= 0177))
1520 *q++ = '0' + (((*p) >> 6) & 07);
1521 *q++ = '0' + (((*p) >> 3) & 07);
1522 *q++ = '0' + ((*p) & 07);
1536 g_strchug (gchar *string)
1540 g_return_val_if_fail (string != NULL, NULL);
1542 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
1545 g_memmove (string, start, strlen ((gchar *) start) + 1);
1551 g_strchomp (gchar *string)
1555 g_return_val_if_fail (string != NULL, NULL);
1560 for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s);
1569 * @string: a string to split.
1570 * @delimiter: a string which specifies the places at which to split the string.
1571 * The delimiter is not included in any of the resulting strings, unless
1572 * max_tokens is reached.
1573 * @max_tokens: the maximum number of pieces to split @string into. If this is
1574 * less than 1, the string is split completely.
1576 * Splits a string into a maximum of @max_tokens pieces, using the given
1577 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
1578 * to the last token.
1580 * As a special case, the result of splitting the empty string "" is an empty
1581 * vector, not a vector containing a single string. The reason for this
1582 * special case is that being able to represent a empty vector is typically
1583 * more useful than consistent handling of empty elements. If you do need
1584 * to represent empty elements, you'll need to check for the empty string
1585 * before calling g_strsplit().
1587 * Return value: a newly-allocated %NULL-terminated array of strings. Use g_strfreev()
1591 g_strsplit (const gchar *string,
1592 const gchar *delimiter,
1595 GSList *string_list = NULL, *slist;
1596 gchar **str_array, *s;
1598 const gchar *remainder;
1600 g_return_val_if_fail (string != NULL, NULL);
1601 g_return_val_if_fail (delimiter != NULL, NULL);
1602 g_return_val_if_fail (delimiter[0] != '\0', NULL);
1605 max_tokens = G_MAXINT;
1610 s = strstr (remainder, delimiter);
1613 gsize delimiter_len = strlen (delimiter);
1620 len = s - remainder;
1621 new_string = g_new (gchar, len + 1);
1622 strncpy (new_string, remainder, len);
1623 new_string[len] = 0;
1624 string_list = g_slist_prepend (string_list, new_string);
1626 remainder = s + delimiter_len;
1627 s = strstr (remainder, delimiter);
1629 while (--max_tokens && s);
1634 string_list = g_slist_prepend (string_list, g_strdup (remainder));
1637 str_array = g_new (gchar*, n + 1);
1639 str_array[n--] = NULL;
1640 for (slist = string_list; slist; slist = slist->next)
1641 str_array[n--] = slist->data;
1643 g_slist_free (string_list);
1649 g_strfreev (gchar **str_array)
1655 for(i = 0; str_array[i] != NULL; i++)
1656 g_free(str_array[i]);
1664 * @str_array: %NULL-terminated array of strings
1666 * Copies %NULL-terminated array of strings. The copy is a deep copy;
1667 * the new array should be freed by first freeing each string, then
1668 * the array itself. g_strfreev() does this for you. If called
1669 * on a %NULL value, g_strdupv() simply returns %NULL.
1671 * Return value: a new %NULL-terminated array of strings
1674 g_strdupv (gchar **str_array)
1682 while (str_array[i])
1685 retval = g_new (gchar*, i + 1);
1688 while (str_array[i])
1690 retval[i] = g_strdup (str_array[i]);
1702 g_strjoinv (const gchar *separator,
1708 g_return_val_if_fail (str_array != NULL, NULL);
1710 if (separator == NULL)
1717 gsize separator_len;
1719 separator_len = strlen (separator);
1720 /* First part, getting length */
1721 len = 1 + strlen (str_array[0]);
1722 for (i = 1; str_array[i] != NULL; i++)
1723 len += strlen (str_array[i]);
1724 len += separator_len * (i - 1);
1726 /* Second part, building string */
1727 string = g_new (gchar, len);
1728 ptr = g_stpcpy (string, *str_array);
1729 for (i = 1; str_array[i] != NULL; i++)
1731 ptr = g_stpcpy (ptr, separator);
1732 ptr = g_stpcpy (ptr, str_array[i]);
1736 string = g_strdup ("");
1742 g_strjoin (const gchar *separator,
1748 gsize separator_len;
1751 if (separator == NULL)
1754 separator_len = strlen (separator);
1756 va_start (args, separator);
1758 s = va_arg (args, gchar*);
1762 /* First part, getting length */
1763 len = 1 + strlen (s);
1765 s = va_arg (args, gchar*);
1768 len += separator_len + strlen (s);
1769 s = va_arg (args, gchar*);
1773 /* Second part, building string */
1774 string = g_new (gchar, len);
1776 va_start (args, separator);
1778 s = va_arg (args, gchar*);
1779 ptr = g_stpcpy (string, s);
1781 s = va_arg (args, gchar*);
1784 ptr = g_stpcpy (ptr, separator);
1785 ptr = g_stpcpy (ptr, s);
1786 s = va_arg (args, gchar*);
1790 string = g_strdup ("");
1800 * @haystack: a string
1801 * @haystack_len: The maximum length of haystack
1802 * @needle: The string to search for.
1804 * Searches the string haystack for the first occurrence
1805 * of the string needle, limiting the length of the search
1808 * Return value: A pointer to the found occurrence, or
1809 * NULL if not found.
1812 g_strstr_len (const gchar *haystack,
1813 gssize haystack_len,
1814 const gchar *needle)
1816 g_return_val_if_fail (haystack != NULL, NULL);
1817 g_return_val_if_fail (needle != NULL, NULL);
1819 if (haystack_len < 0)
1820 return strstr (haystack, needle);
1823 const gchar *p = haystack;
1824 gsize needle_len = strlen (needle);
1828 if (needle_len == 0)
1829 return (gchar *)haystack;
1831 if (haystack_len < needle_len)
1834 end = haystack + haystack_len - needle_len;
1836 while (*p && p <= end)
1838 for (i = 0; i < needle_len; i++)
1839 if (p[i] != needle[i])
1854 * @haystack: a nul-terminated string
1855 * @needle: The nul-terminated string to search for.
1857 * Searches the string haystack for the last occurrence
1858 * of the string needle.
1860 * Return value: A pointer to the found occurrence, or
1861 * NULL if not found.
1864 g_strrstr (const gchar *haystack,
1865 const gchar *needle)
1872 g_return_val_if_fail (haystack != NULL, NULL);
1873 g_return_val_if_fail (needle != NULL, NULL);
1875 needle_len = strlen (needle);
1876 haystack_len = strlen (haystack);
1878 if (needle_len == 0)
1879 return (gchar *)haystack;
1881 if (haystack_len < needle_len)
1884 p = haystack + haystack_len - needle_len;
1886 while (p >= haystack)
1888 for (i = 0; i < needle_len; i++)
1889 if (p[i] != needle[i])
1903 * @haystack: a nul-terminated string
1904 * @haystack_len: The maximum length of haystack
1905 * @needle: The nul-terminated string to search for.
1907 * Searches the string haystack for the last occurrence
1908 * of the string needle, limiting the length of the search
1911 * Return value: A pointer to the found occurrence, or
1912 * NULL if not found.
1915 g_strrstr_len (const gchar *haystack,
1916 gssize haystack_len,
1917 const gchar *needle)
1919 g_return_val_if_fail (haystack != NULL, NULL);
1920 g_return_val_if_fail (needle != NULL, NULL);
1922 if (haystack_len < 0)
1923 return g_strrstr (haystack, needle);
1926 gsize needle_len = strlen (needle);
1927 const gchar *haystack_max = haystack + haystack_len;
1928 const gchar *p = haystack;
1931 while (p < haystack_max && *p)
1934 if (p < haystack + needle_len)
1939 while (p >= haystack)
1941 for (i = 0; i < needle_len; i++)
1942 if (p[i] != needle[i])