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,
189 G_VA_COPY (args2, args1);
191 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
193 vsprintf (buffer, format, args2);
200 g_strdup_printf (const gchar *format,
206 va_start (args, format);
207 buffer = g_strdup_vprintf (format, args);
214 g_strconcat (const gchar *string1, ...)
222 g_return_val_if_fail (string1 != NULL, NULL);
224 l = 1 + strlen (string1);
225 va_start (args, string1);
226 s = va_arg (args, gchar*);
230 s = va_arg (args, gchar*);
234 concat = g_new (gchar, l);
237 ptr = g_stpcpy (ptr, string1);
238 va_start (args, string1);
239 s = va_arg (args, gchar*);
242 ptr = g_stpcpy (ptr, s);
243 s = va_arg (args, gchar*);
251 g_strtod (const gchar *nptr,
259 g_return_val_if_fail (nptr != NULL, 0);
264 val_1 = strtod (nptr, &fail_pos_1);
266 if (fail_pos_1 && fail_pos_1[0] != 0)
270 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
271 setlocale (LC_NUMERIC, "C");
272 val_2 = strtod (nptr, &fail_pos_2);
273 setlocale (LC_NUMERIC, old_locale);
277 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
280 *endptr = fail_pos_1;
286 *endptr = fail_pos_2;
291 G_CONST_RETURN gchar*
292 g_strerror (gint errnum)
294 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
298 return strerror (errnum);
303 case E2BIG: return "argument list too long";
306 case EACCES: return "permission denied";
309 case EADDRINUSE: return "address already in use";
312 case EADDRNOTAVAIL: return "can't assign requested address";
315 case EADV: return "advertise error";
318 case EAFNOSUPPORT: return "address family not supported by protocol family";
321 case EAGAIN: return "try again";
324 case EALIGN: return "EALIGN";
327 case EALREADY: return "operation already in progress";
330 case EBADE: return "bad exchange descriptor";
333 case EBADF: return "bad file number";
336 case EBADFD: return "file descriptor in bad state";
339 case EBADMSG: return "not a data message";
342 case EBADR: return "bad request descriptor";
345 case EBADRPC: return "RPC structure is bad";
348 case EBADRQC: return "bad request code";
351 case EBADSLT: return "invalid slot";
354 case EBFONT: return "bad font file format";
357 case EBUSY: return "mount device busy";
360 case ECHILD: return "no children";
363 case ECHRNG: return "channel number out of range";
366 case ECOMM: return "communication error on send";
369 case ECONNABORTED: return "software caused connection abort";
372 case ECONNREFUSED: return "connection refused";
375 case ECONNRESET: return "connection reset by peer";
377 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
378 case EDEADLK: return "resource deadlock avoided";
381 case EDEADLOCK: return "resource deadlock avoided";
384 case EDESTADDRREQ: return "destination address required";
387 case EDIRTY: return "mounting a dirty fs w/o force";
390 case EDOM: return "math argument out of range";
393 case EDOTDOT: return "cross mount point";
396 case EDQUOT: return "disk quota exceeded";
399 case EDUPPKG: return "duplicate package name";
402 case EEXIST: return "file already exists";
405 case EFAULT: return "bad address in system call argument";
408 case EFBIG: return "file too large";
411 case EHOSTDOWN: return "host is down";
414 case EHOSTUNREACH: return "host is unreachable";
417 case EIDRM: return "identifier removed";
420 case EINIT: return "initialization error";
423 case EINPROGRESS: return "operation now in progress";
426 case EINTR: return "interrupted system call";
429 case EINVAL: return "invalid argument";
432 case EIO: return "I/O error";
435 case EISCONN: return "socket is already connected";
438 case EISDIR: return "illegal operation on a directory";
441 case EISNAM: return "is a name file";
444 case ELBIN: return "ELBIN";
447 case EL2HLT: return "level 2 halted";
450 case EL2NSYNC: return "level 2 not synchronized";
453 case EL3HLT: return "level 3 halted";
456 case EL3RST: return "level 3 reset";
459 case ELIBACC: return "can not access a needed shared library";
462 case ELIBBAD: return "accessing a corrupted shared library";
465 case ELIBEXEC: return "can not exec a shared library directly";
468 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
471 case ELIBSCN: return ".lib section in a.out corrupted";
474 case ELNRNG: return "link number out of range";
477 case ELOOP: return "too many levels of symbolic links";
480 case EMFILE: return "too many open files";
483 case EMLINK: return "too many links";
486 case EMSGSIZE: return "message too long";
489 case EMULTIHOP: return "multihop attempted";
492 case ENAMETOOLONG: return "file name too long";
495 case ENAVAIL: return "not available";
498 case ENET: return "ENET";
501 case ENETDOWN: return "network is down";
504 case ENETRESET: return "network dropped connection on reset";
507 case ENETUNREACH: return "network is unreachable";
510 case ENFILE: return "file table overflow";
513 case ENOANO: return "anode table overflow";
515 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
516 case ENOBUFS: return "no buffer space available";
519 case ENOCSI: return "no CSI structure available";
522 case ENODATA: return "no data available";
525 case ENODEV: return "no such device";
528 case ENOENT: return "no such file or directory";
531 case ENOEXEC: return "exec format error";
534 case ENOLCK: return "no locks available";
537 case ENOLINK: return "link has be severed";
540 case ENOMEM: return "not enough memory";
543 case ENOMSG: return "no message of desired type";
546 case ENONET: return "machine is not on the network";
549 case ENOPKG: return "package not installed";
552 case ENOPROTOOPT: return "bad proocol option";
555 case ENOSPC: return "no space left on device";
558 case ENOSR: return "out of stream resources";
561 case ENOSTR: return "not a stream device";
564 case ENOSYM: return "unresolved symbol name";
567 case ENOSYS: return "function not implemented";
570 case ENOTBLK: return "block device required";
573 case ENOTCONN: return "socket is not connected";
576 case ENOTDIR: return "not a directory";
579 case ENOTEMPTY: return "directory not empty";
582 case ENOTNAM: return "not a name file";
585 case ENOTSOCK: return "socket operation on non-socket";
588 case ENOTTY: return "inappropriate device for ioctl";
591 case ENOTUNIQ: return "name not unique on network";
594 case ENXIO: return "no such device or address";
597 case EOPNOTSUPP: return "operation not supported on socket";
600 case EPERM: return "not owner";
603 case EPFNOSUPPORT: return "protocol family not supported";
606 case EPIPE: return "broken pipe";
609 case EPROCLIM: return "too many processes";
612 case EPROCUNAVAIL: return "bad procedure for program";
615 case EPROGMISMATCH: return "program version wrong";
618 case EPROGUNAVAIL: return "RPC program not available";
621 case EPROTO: return "protocol error";
623 #ifdef EPROTONOSUPPORT
624 case EPROTONOSUPPORT: return "protocol not suppored";
627 case EPROTOTYPE: return "protocol wrong type for socket";
630 case ERANGE: return "math result unrepresentable";
632 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
633 case EREFUSED: return "EREFUSED";
636 case EREMCHG: return "remote address changed";
639 case EREMDEV: return "remote device";
642 case EREMOTE: return "pathname hit remote file system";
645 case EREMOTEIO: return "remote i/o error";
647 #ifdef EREMOTERELEASE
648 case EREMOTERELEASE: return "EREMOTERELEASE";
651 case EROFS: return "read-only file system";
654 case ERPCMISMATCH: return "RPC version is wrong";
657 case ERREMOTE: return "object is remote";
660 case ESHUTDOWN: return "can't send afer socket shutdown";
662 #ifdef ESOCKTNOSUPPORT
663 case ESOCKTNOSUPPORT: return "socket type not supported";
666 case ESPIPE: return "invalid seek";
669 case ESRCH: return "no such process";
672 case ESRMNT: return "srmount error";
675 case ESTALE: return "stale remote file handle";
678 case ESUCCESS: return "Error 0";
681 case ETIME: return "timer expired";
684 case ETIMEDOUT: return "connection timed out";
687 case ETOOMANYREFS: return "too many references: can't splice";
690 case ETXTBSY: return "text file or pseudo-device busy";
693 case EUCLEAN: return "structure needs cleaning";
696 case EUNATCH: return "protocol driver not attached";
699 case EUSERS: return "too many users";
702 case EVERSION: return "version mismatch";
704 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
705 case EWOULDBLOCK: return "operation would block";
708 case EXDEV: return "cross-domain link";
711 case EXFULL: return "message tables full";
714 #else /* NO_SYS_ERRLIST */
716 extern char *sys_errlist[];
718 if ((errnum > 0) && (errnum <= sys_nerr))
719 return sys_errlist [errnum];
720 #endif /* NO_SYS_ERRLIST */
722 msg = g_static_private_get (&msg_private);
725 msg = g_new (gchar, 64);
726 g_static_private_set (&msg_private, msg, g_free);
729 sprintf (msg, "unknown error (%d)", errnum);
734 G_CONST_RETURN gchar*
735 g_strsignal (gint signum)
737 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
740 #ifdef HAVE_STRSIGNAL
741 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
742 extern const char *strsignal(int);
744 /* this is declared differently (const) in string.h on BeOS */
745 extern char *strsignal (int sig);
746 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
747 return strsignal (signum);
752 case SIGHUP: return "Hangup";
755 case SIGINT: return "Interrupt";
758 case SIGQUIT: return "Quit";
761 case SIGILL: return "Illegal instruction";
764 case SIGTRAP: return "Trace/breakpoint trap";
767 case SIGABRT: return "IOT trap/Abort";
770 case SIGBUS: return "Bus error";
773 case SIGFPE: return "Floating point exception";
776 case SIGKILL: return "Killed";
779 case SIGUSR1: return "User defined signal 1";
782 case SIGSEGV: return "Segmentation fault";
785 case SIGUSR2: return "User defined signal 2";
788 case SIGPIPE: return "Broken pipe";
791 case SIGALRM: return "Alarm clock";
794 case SIGTERM: return "Terminated";
797 case SIGSTKFLT: return "Stack fault";
800 case SIGCHLD: return "Child exited";
803 case SIGCONT: return "Continued";
806 case SIGSTOP: return "Stopped (signal)";
809 case SIGTSTP: return "Stopped";
812 case SIGTTIN: return "Stopped (tty input)";
815 case SIGTTOU: return "Stopped (tty output)";
818 case SIGURG: return "Urgent condition";
821 case SIGXCPU: return "CPU time limit exceeded";
824 case SIGXFSZ: return "File size limit exceeded";
827 case SIGVTALRM: return "Virtual time alarm";
830 case SIGPROF: return "Profile signal";
833 case SIGWINCH: return "Window size changed";
836 case SIGIO: return "Possible I/O";
839 case SIGPWR: return "Power failure";
842 case SIGUNUSED: return "Unused signal";
845 #else /* NO_SYS_SIGLIST */
847 #ifdef NO_SYS_SIGLIST_DECL
848 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
851 return (char*) /* this function should return const --josh */ sys_siglist [signum];
852 #endif /* NO_SYS_SIGLIST */
854 msg = g_static_private_get (&msg_private);
857 msg = g_new (gchar, 64);
858 g_static_private_set (&msg_private, msg, g_free);
861 sprintf (msg, "unknown signal (%d)", signum);
866 /* Functions g_strlcpy and g_strlcat were originally developed by
867 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
868 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
869 * for more information.
873 /* Use the native ones, if available; they might be implemented in assembly */
875 g_strlcpy (gchar *dest,
879 g_return_val_if_fail (dest != NULL, 0);
880 g_return_val_if_fail (src != NULL, 0);
882 return strlcpy (dest, src, dest_size);
886 g_strlcat (gchar *dest,
890 g_return_val_if_fail (dest != NULL, 0);
891 g_return_val_if_fail (src != NULL, 0);
893 return strlcat (dest, src, dest_size);
896 #else /* ! HAVE_STRLCPY */
899 * Copy string src to buffer dest (of buffer size dest_size). At most
900 * dest_size-1 characters will be copied. Always NUL terminates
901 * (unless dest_size == 0). This function does NOT allocate memory.
902 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
903 * Returns size of attempted result, strlen(src),
904 * so if retval >= dest_size, truncation occurred.
907 g_strlcpy (gchar *dest,
911 register gchar *d = dest;
912 register const gchar *s = src;
913 register gsize n = dest_size;
915 g_return_val_if_fail (dest != NULL, 0);
916 g_return_val_if_fail (src != NULL, 0);
918 /* Copy as many bytes as will fit */
919 if (n != 0 && --n != 0)
922 register gchar c = *s++;
930 /* If not enough room in dest, add NUL and traverse rest of src */
939 return s - src - 1; /* count does not include NUL */
944 * Appends string src to buffer dest (of buffer size dest_size).
945 * At most dest_size-1 characters will be copied.
946 * Unlike strncat, dest_size is the full size of dest, not the space left over.
947 * This function does NOT allocate memory.
948 * This always NUL terminates (unless siz == 0 or there were no NUL characters
949 * in the dest_size characters of dest to start with).
950 * Returns size of attempted result, which is
951 * MIN (dest_size, strlen (original dest)) + strlen (src),
952 * so if retval >= dest_size, truncation occurred.
955 g_strlcat (gchar *dest,
959 register gchar *d = dest;
960 register const gchar *s = src;
961 register gsize bytes_left = dest_size;
962 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
964 g_return_val_if_fail (dest != NULL, 0);
965 g_return_val_if_fail (src != NULL, 0);
967 /* Find the end of dst and adjust bytes left but don't go past end */
968 while (*d != 0 && bytes_left-- != 0)
971 bytes_left = dest_size - dlength;
974 return dlength + strlen (s);
987 return dlength + (s - src); /* count does not include NUL */
989 #endif /* ! HAVE_STRLCPY */
995 * Converts all upper case ASCII letters to lower case ASCII letters.
997 * Return value: a newly allocated string, with all the upper case
998 * characters in @string converted to lower case, with
999 * semantics that exactly match g_ascii_tolower. (Note
1000 * that this is unlike the old g_strdown, which modified
1001 * the string in place.)
1004 g_ascii_strdown (const gchar *string)
1008 g_return_val_if_fail (string != NULL, NULL);
1010 result = g_strdup (string);
1011 for (s = result; *s; s++)
1012 *s = g_ascii_tolower (*s);
1021 * Converts all lower case ASCII letters to upper case ASCII letters.
1023 * Return value: a newly allocated string, with all the lower case
1024 * characters in @string converted to upper case, with
1025 * semantics that exactly match g_ascii_toupper. (Note
1026 * that this is unlike the old g_strup, which modified
1027 * the string in place.)
1030 g_ascii_strup (const gchar *string)
1034 g_return_val_if_fail (string != NULL, NULL);
1036 result = g_strdup (string);
1037 for (s = result; *s; s++)
1038 *s = g_ascii_toupper (*s);
1044 g_strdown (gchar *string)
1048 g_return_val_if_fail (string != NULL, NULL);
1050 s = (guchar *) string;
1059 return (gchar *) string;
1063 g_strup (gchar *string)
1067 g_return_val_if_fail (string != NULL, NULL);
1069 s = (guchar *) string;
1078 return (gchar *) string;
1082 g_strreverse (gchar *string)
1084 g_return_val_if_fail (string != NULL, NULL);
1088 register gchar *h, *t;
1091 t = string + strlen (string) - 1;
1112 * Convert a character to ASCII lower case.
1114 * Unlike the standard C library tolower function, this only
1115 * recognizes standard ASCII letters and ignores the locale, returning
1116 * all non-ASCII characters unchanged, even if they are lower case
1117 * letters in a particular character set. Also unlike the standard
1118 * library function, this takes and returns a char, not an int, so
1119 * don't call it on EOF but no need to worry about casting to guchar
1120 * before passing a possibly non-ASCII character in.
1122 * Return value: the result of converting @c to lower case.
1123 * If @c is not an ASCII upper case letter,
1124 * @c is returned unchanged.
1127 g_ascii_tolower (gchar c)
1129 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1136 * Convert a character to ASCII upper case.
1138 * Unlike the standard C library toupper function, this only
1139 * recognizes standard ASCII letters and ignores the locale, returning
1140 * all non-ASCII characters unchanged, even if they are upper case
1141 * letters in a particular character set. Also unlike the standard
1142 * library function, this takes and returns a char, not an int, so
1143 * don't call it on EOF but no need to worry about casting to guchar
1144 * before passing a possibly non-ASCII character in.
1146 * Return value: the result of converting @c to upper case.
1147 * If @c is not an ASCII lower case letter,
1148 * @c is returned unchanged.
1151 g_ascii_toupper (gchar c)
1153 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1157 * g_ascii_digit_value:
1158 * @c: an ASCII character
1160 * Determines the numeric value of a character as a decimal
1161 * digit. Differs from g_unichar_digit_value because it takes
1162 * a char, so there's no worry about sign extension if characters
1165 * Return value: If @c is a decimal digit (according to
1166 * `g_ascii_isdigit'), its numeric value. Otherwise, -1.
1169 g_ascii_digit_value (gchar c)
1171 if (g_ascii_isdigit (c))
1177 * g_ascii_xdigit_value:
1178 * @c: an ASCII character
1180 * Determines the numeric value of a character as a hexidecimal
1181 * digit. Differs from g_unichar_xdigit_value because it takes
1182 * a char, so there's no worry about sign extension if characters
1185 * Return value: If @c is a hex digit (according to
1186 * `g_ascii_isxdigit'), its numeric value. Otherwise, -1.
1189 g_ascii_xdigit_value (gchar c)
1191 if (c >= 'A' && c <= 'F')
1192 return c - 'A' + 10;
1193 if (c >= 'a' && c <= 'f')
1194 return c - 'a' + 10;
1195 return g_ascii_digit_value (c);
1199 * g_ascii_strcasecmp:
1200 * @s1: string to compare with @s2
1201 * @s2: string to compare with @s1
1203 * Compare two strings, ignoring the case of ASCII characters.
1205 * Unlike the BSD strcasecmp function, this only recognizes standard
1206 * ASCII letters and ignores the locale, treating all non-ASCII
1207 * characters as if they are not letters.
1209 * Return value: an integer less than, equal to, or greater than
1210 * zero if @s1 is found, respectively, to be less than,
1211 * to match, or to be greater than @s2.
1214 g_ascii_strcasecmp (const gchar *s1,
1219 g_return_val_if_fail (s1 != NULL, 0);
1220 g_return_val_if_fail (s2 != NULL, 0);
1224 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1225 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1231 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1235 * g_ascii_strncasecmp:
1236 * @s1: string to compare with @s2
1237 * @s2: string to compare with @s1
1238 * @n: number of characters to compare
1240 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1241 * characters after the first @n in each string.
1243 * Unlike the BSD strcasecmp function, this only recognizes standard
1244 * ASCII letters and ignores the locale, treating all non-ASCII
1245 * characters as if they are not letters.
1247 * Return value: an integer less than, equal to, or greater than zero
1248 * if the first @n bytes of @s1 is found, respectively,
1249 * to be less than, to match, or to be greater than the
1250 * first @n bytes of @s2.
1253 g_ascii_strncasecmp (const gchar *s1,
1259 g_return_val_if_fail (s1 != NULL, 0);
1260 g_return_val_if_fail (s2 != NULL, 0);
1262 while (n && *s1 && *s2)
1265 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1266 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1273 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1279 g_strcasecmp (const gchar *s1,
1282 #ifdef HAVE_STRCASECMP
1283 g_return_val_if_fail (s1 != NULL, 0);
1284 g_return_val_if_fail (s2 != NULL, 0);
1286 return strcasecmp (s1, s2);
1290 g_return_val_if_fail (s1 != NULL, 0);
1291 g_return_val_if_fail (s2 != NULL, 0);
1295 /* According to A. Cox, some platforms have islower's that
1296 * don't work right on non-uppercase
1298 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1299 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1305 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1310 g_strncasecmp (const gchar *s1,
1314 #ifdef HAVE_STRNCASECMP
1315 return strncasecmp (s1, s2, n);
1319 g_return_val_if_fail (s1 != NULL, 0);
1320 g_return_val_if_fail (s2 != NULL, 0);
1322 while (n && *s1 && *s2)
1325 /* According to A. Cox, some platforms have islower's that
1326 * don't work right on non-uppercase
1328 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1329 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1336 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1343 g_strdelimit (gchar *string,
1344 const gchar *delimiters,
1349 g_return_val_if_fail (string != NULL, NULL);
1352 delimiters = G_STR_DELIMITERS;
1354 for (c = string; *c; c++)
1356 if (strchr (delimiters, *c))
1364 g_strcanon (gchar *string,
1365 const gchar *valid_chars,
1370 g_return_val_if_fail (string != NULL, NULL);
1371 g_return_val_if_fail (valid_chars != NULL, NULL);
1373 for (c = string; *c; c++)
1375 if (!strchr (valid_chars, *c))
1383 g_strcompress (const gchar *source)
1385 const gchar *p = source, *octal;
1386 gchar *dest = g_malloc (strlen (source) + 1);
1396 case '0': case '1': case '2': case '3': case '4':
1397 case '5': case '6': case '7':
1400 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1402 *q = (*q * 8) + (*p - '0');
1423 default: /* Also handles \" and \\ */
1438 g_strescape (const gchar *source,
1439 const gchar *exceptions)
1446 g_return_val_if_fail (source != NULL, NULL);
1448 p = (guchar *) source;
1449 /* Each source byte needs maximally four destination chars (\777) */
1450 q = dest = g_malloc (strlen (source) * 4 + 1);
1452 memset (excmap, 0, 256);
1455 guchar *e = (guchar *) exceptions;
1501 if ((*p < ' ') || (*p >= 0177))
1504 *q++ = '0' + (((*p) >> 6) & 07);
1505 *q++ = '0' + (((*p) >> 3) & 07);
1506 *q++ = '0' + ((*p) & 07);
1520 g_strchug (gchar *string)
1524 g_return_val_if_fail (string != NULL, NULL);
1526 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
1529 g_memmove (string, start, strlen ((gchar *) start) + 1);
1535 g_strchomp (gchar *string)
1539 g_return_val_if_fail (string != NULL, NULL);
1544 for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s);
1553 * @string: a string to split.
1554 * @delimiter: a string which specifies the places at which to split the string.
1555 * The delimiter is not included in any of the resulting strings, unless
1556 * max_tokens is reached.
1557 * @max_tokens: the maximum number of pieces to split @string into. If this is
1558 * less than 1, the string is split completely.
1560 * Splits a string into a maximum of @max_tokens pieces, using the given
1561 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
1562 * to the last token.
1564 * As a special case, the result of splitting the empty string "" is an empty
1565 * vector, not a vector containing a single string. The reason for this
1566 * special case is that being able to represent a empty vector is typically
1567 * more useful than consistent handling of empty elements. If you do need
1568 * to represent empty elements, you'll need to check for the empty string
1569 * before calling g_strsplit().
1571 * Return value: a newly-allocated %NULL-terminated array of strings. Use g_strfreev()
1575 g_strsplit (const gchar *string,
1576 const gchar *delimiter,
1579 GSList *string_list = NULL, *slist;
1580 gchar **str_array, *s;
1582 const gchar *remainder;
1584 g_return_val_if_fail (string != NULL, NULL);
1585 g_return_val_if_fail (delimiter != NULL, NULL);
1586 g_return_val_if_fail (delimiter[0] != '\0', NULL);
1589 max_tokens = G_MAXINT;
1594 s = strstr (remainder, delimiter);
1597 gsize delimiter_len = strlen (delimiter);
1604 len = s - remainder;
1605 new_string = g_new (gchar, len + 1);
1606 strncpy (new_string, remainder, len);
1607 new_string[len] = 0;
1608 string_list = g_slist_prepend (string_list, new_string);
1610 remainder = s + delimiter_len;
1611 s = strstr (remainder, delimiter);
1613 while (--max_tokens && s);
1618 string_list = g_slist_prepend (string_list, g_strdup (remainder));
1621 str_array = g_new (gchar*, n + 1);
1623 str_array[n--] = NULL;
1624 for (slist = string_list; slist; slist = slist->next)
1625 str_array[n--] = slist->data;
1627 g_slist_free (string_list);
1633 g_strfreev (gchar **str_array)
1639 for(i = 0; str_array[i] != NULL; i++)
1640 g_free(str_array[i]);
1648 * @str_array: %NULL-terminated array of strings
1650 * Copies %NULL-terminated array of strings. The copy is a deep copy;
1651 * the new array should be freed by first freeing each string, then
1652 * the array itself. g_strfreev() does this for you. If called
1653 * on a %NULL value, g_strdupv() simply returns %NULL.
1655 * Return value: a new %NULL-terminated array of strings
1658 g_strdupv (gchar **str_array)
1666 while (str_array[i])
1669 retval = g_new (gchar*, i + 1);
1672 while (str_array[i])
1674 retval[i] = g_strdup (str_array[i]);
1686 g_strjoinv (const gchar *separator,
1692 g_return_val_if_fail (str_array != NULL, NULL);
1694 if (separator == NULL)
1701 gsize separator_len;
1703 separator_len = strlen (separator);
1704 /* First part, getting length */
1705 len = 1 + strlen (str_array[0]);
1706 for (i = 1; str_array[i] != NULL; i++)
1707 len += strlen (str_array[i]);
1708 len += separator_len * (i - 1);
1710 /* Second part, building string */
1711 string = g_new (gchar, len);
1712 ptr = g_stpcpy (string, *str_array);
1713 for (i = 1; str_array[i] != NULL; i++)
1715 ptr = g_stpcpy (ptr, separator);
1716 ptr = g_stpcpy (ptr, str_array[i]);
1720 string = g_strdup ("");
1726 g_strjoin (const gchar *separator,
1732 gsize separator_len;
1735 if (separator == NULL)
1738 separator_len = strlen (separator);
1740 va_start (args, separator);
1742 s = va_arg (args, gchar*);
1746 /* First part, getting length */
1747 len = 1 + strlen (s);
1749 s = va_arg (args, gchar*);
1752 len += separator_len + strlen (s);
1753 s = va_arg (args, gchar*);
1757 /* Second part, building string */
1758 string = g_new (gchar, len);
1760 va_start (args, separator);
1762 s = va_arg (args, gchar*);
1763 ptr = g_stpcpy (string, s);
1765 s = va_arg (args, gchar*);
1768 ptr = g_stpcpy (ptr, separator);
1769 ptr = g_stpcpy (ptr, s);
1770 s = va_arg (args, gchar*);
1774 string = g_strdup ("");
1784 * @haystack: a string
1785 * @haystack_len: The maximum length of haystack
1786 * @needle: The string to search for.
1788 * Searches the string haystack for the first occurrence
1789 * of the string needle, limiting the length of the search
1792 * Return value: A pointer to the found occurrence, or
1793 * NULL if not found.
1796 g_strstr_len (const gchar *haystack,
1797 gssize haystack_len,
1798 const gchar *needle)
1800 g_return_val_if_fail (haystack != NULL, NULL);
1801 g_return_val_if_fail (needle != NULL, NULL);
1803 if (haystack_len < 0)
1804 return strstr (haystack, needle);
1807 const gchar *p = haystack;
1808 gsize needle_len = strlen (needle);
1812 if (needle_len == 0)
1813 return (gchar *)haystack;
1815 if (haystack_len < needle_len)
1818 end = haystack + haystack_len - needle_len;
1820 while (*p && p <= end)
1822 for (i = 0; i < needle_len; i++)
1823 if (p[i] != needle[i])
1838 * @haystack: a nul-terminated string
1839 * @needle: The nul-terminated string to search for.
1841 * Searches the string haystack for the last occurrence
1842 * of the string needle.
1844 * Return value: A pointer to the found occurrence, or
1845 * NULL if not found.
1848 g_strrstr (const gchar *haystack,
1849 const gchar *needle)
1856 g_return_val_if_fail (haystack != NULL, NULL);
1857 g_return_val_if_fail (needle != NULL, NULL);
1859 needle_len = strlen (needle);
1860 haystack_len = strlen (haystack);
1862 if (needle_len == 0)
1863 return (gchar *)haystack;
1865 if (haystack_len < needle_len)
1868 p = haystack + haystack_len - needle_len;
1870 while (p >= haystack)
1872 for (i = 0; i < needle_len; i++)
1873 if (p[i] != needle[i])
1887 * @haystack: a nul-terminated string
1888 * @haystack_len: The maximum length of haystack
1889 * @needle: The nul-terminated string to search for.
1891 * Searches the string haystack for the last occurrence
1892 * of the string needle, limiting the length of the search
1895 * Return value: A pointer to the found occurrence, or
1896 * NULL if not found.
1899 g_strrstr_len (const gchar *haystack,
1900 gssize haystack_len,
1901 const gchar *needle)
1903 g_return_val_if_fail (haystack != NULL, NULL);
1904 g_return_val_if_fail (needle != NULL, NULL);
1906 if (haystack_len < 0)
1907 return g_strrstr (haystack, needle);
1910 gsize needle_len = strlen (needle);
1911 const gchar *haystack_max = haystack + haystack_len;
1912 const gchar *p = haystack;
1915 while (p < haystack_max && *p)
1918 if (p < haystack + needle_len)
1923 while (p >= haystack)
1925 for (i = 0; i < needle_len; i++)
1926 if (p[i] != needle[i])