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 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
59 const guint16 g_ascii_table[256] = {
60 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
62 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
64 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
67 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
68 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
69 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
70 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
71 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
72 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
73 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
74 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
75 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
76 /* the upper 128 are all zeroes */
80 g_strdup (const gchar *str)
86 new_str = g_new (char, strlen (str) + 1);
87 strcpy (new_str, str);
96 g_memdup (gconstpointer mem,
103 new_mem = g_malloc (byte_size);
104 memcpy (new_mem, mem, byte_size);
113 g_strndup (const gchar *str,
120 new_str = g_new (gchar, n + 1);
121 strncpy (new_str, str, n);
131 g_strnfill (gsize length,
134 register gchar *str, *s, *end;
136 str = g_new (gchar, length + 1);
148 * @dest: destination buffer
149 * @src: source string
151 * Copies a nul-terminated string into the dest buffer, include the
152 * trailing nul, and return a pointer to the trailing nul byte.
153 * This is useful for concatenating multiple strings together
154 * without having to repeatedly scan for the end.
156 * Return value: a pointer to trailing nul byte.
159 g_stpcpy (gchar *dest,
163 g_return_val_if_fail (dest != NULL, NULL);
164 g_return_val_if_fail (src != NULL, NULL);
165 return stpcpy (dest, src);
167 register gchar *d = dest;
168 register const gchar *s = src;
170 g_return_val_if_fail (dest != NULL, NULL);
171 g_return_val_if_fail (src != NULL, NULL);
174 while (*s++ != '\0');
181 g_strdup_vprintf (const gchar *format,
187 G_VA_COPY (args2, args1);
189 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
191 vsprintf (buffer, format, args2);
198 g_strdup_printf (const gchar *format,
204 va_start (args, format);
205 buffer = g_strdup_vprintf (format, args);
212 g_strconcat (const gchar *string1, ...)
220 g_return_val_if_fail (string1 != NULL, NULL);
222 l = 1 + strlen (string1);
223 va_start (args, string1);
224 s = va_arg (args, gchar*);
228 s = va_arg (args, gchar*);
232 concat = g_new (gchar, l);
235 ptr = g_stpcpy (ptr, string1);
236 va_start (args, string1);
237 s = va_arg (args, gchar*);
240 ptr = g_stpcpy (ptr, s);
241 s = va_arg (args, gchar*);
249 g_strtod (const gchar *nptr,
257 g_return_val_if_fail (nptr != NULL, 0);
262 val_1 = strtod (nptr, &fail_pos_1);
264 if (fail_pos_1 && fail_pos_1[0] != 0)
268 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
269 setlocale (LC_NUMERIC, "C");
270 val_2 = strtod (nptr, &fail_pos_2);
271 setlocale (LC_NUMERIC, old_locale);
275 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
278 *endptr = fail_pos_1;
284 *endptr = fail_pos_2;
289 G_CONST_RETURN gchar*
290 g_strerror (gint errnum)
292 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
296 return strerror (errnum);
301 case E2BIG: return "argument list too long";
304 case EACCES: return "permission denied";
307 case EADDRINUSE: return "address already in use";
310 case EADDRNOTAVAIL: return "can't assign requested address";
313 case EADV: return "advertise error";
316 case EAFNOSUPPORT: return "address family not supported by protocol family";
319 case EAGAIN: return "try again";
322 case EALIGN: return "EALIGN";
325 case EALREADY: return "operation already in progress";
328 case EBADE: return "bad exchange descriptor";
331 case EBADF: return "bad file number";
334 case EBADFD: return "file descriptor in bad state";
337 case EBADMSG: return "not a data message";
340 case EBADR: return "bad request descriptor";
343 case EBADRPC: return "RPC structure is bad";
346 case EBADRQC: return "bad request code";
349 case EBADSLT: return "invalid slot";
352 case EBFONT: return "bad font file format";
355 case EBUSY: return "mount device busy";
358 case ECHILD: return "no children";
361 case ECHRNG: return "channel number out of range";
364 case ECOMM: return "communication error on send";
367 case ECONNABORTED: return "software caused connection abort";
370 case ECONNREFUSED: return "connection refused";
373 case ECONNRESET: return "connection reset by peer";
375 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
376 case EDEADLK: return "resource deadlock avoided";
379 case EDEADLOCK: return "resource deadlock avoided";
382 case EDESTADDRREQ: return "destination address required";
385 case EDIRTY: return "mounting a dirty fs w/o force";
388 case EDOM: return "math argument out of range";
391 case EDOTDOT: return "cross mount point";
394 case EDQUOT: return "disk quota exceeded";
397 case EDUPPKG: return "duplicate package name";
400 case EEXIST: return "file already exists";
403 case EFAULT: return "bad address in system call argument";
406 case EFBIG: return "file too large";
409 case EHOSTDOWN: return "host is down";
412 case EHOSTUNREACH: return "host is unreachable";
415 case EIDRM: return "identifier removed";
418 case EINIT: return "initialization error";
421 case EINPROGRESS: return "operation now in progress";
424 case EINTR: return "interrupted system call";
427 case EINVAL: return "invalid argument";
430 case EIO: return "I/O error";
433 case EISCONN: return "socket is already connected";
436 case EISDIR: return "illegal operation on a directory";
439 case EISNAM: return "is a name file";
442 case ELBIN: return "ELBIN";
445 case EL2HLT: return "level 2 halted";
448 case EL2NSYNC: return "level 2 not synchronized";
451 case EL3HLT: return "level 3 halted";
454 case EL3RST: return "level 3 reset";
457 case ELIBACC: return "can not access a needed shared library";
460 case ELIBBAD: return "accessing a corrupted shared library";
463 case ELIBEXEC: return "can not exec a shared library directly";
466 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
469 case ELIBSCN: return ".lib section in a.out corrupted";
472 case ELNRNG: return "link number out of range";
475 case ELOOP: return "too many levels of symbolic links";
478 case EMFILE: return "too many open files";
481 case EMLINK: return "too many links";
484 case EMSGSIZE: return "message too long";
487 case EMULTIHOP: return "multihop attempted";
490 case ENAMETOOLONG: return "file name too long";
493 case ENAVAIL: return "not available";
496 case ENET: return "ENET";
499 case ENETDOWN: return "network is down";
502 case ENETRESET: return "network dropped connection on reset";
505 case ENETUNREACH: return "network is unreachable";
508 case ENFILE: return "file table overflow";
511 case ENOANO: return "anode table overflow";
513 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
514 case ENOBUFS: return "no buffer space available";
517 case ENOCSI: return "no CSI structure available";
520 case ENODATA: return "no data available";
523 case ENODEV: return "no such device";
526 case ENOENT: return "no such file or directory";
529 case ENOEXEC: return "exec format error";
532 case ENOLCK: return "no locks available";
535 case ENOLINK: return "link has be severed";
538 case ENOMEM: return "not enough memory";
541 case ENOMSG: return "no message of desired type";
544 case ENONET: return "machine is not on the network";
547 case ENOPKG: return "package not installed";
550 case ENOPROTOOPT: return "bad proocol option";
553 case ENOSPC: return "no space left on device";
556 case ENOSR: return "out of stream resources";
559 case ENOSTR: return "not a stream device";
562 case ENOSYM: return "unresolved symbol name";
565 case ENOSYS: return "function not implemented";
568 case ENOTBLK: return "block device required";
571 case ENOTCONN: return "socket is not connected";
574 case ENOTDIR: return "not a directory";
577 case ENOTEMPTY: return "directory not empty";
580 case ENOTNAM: return "not a name file";
583 case ENOTSOCK: return "socket operation on non-socket";
586 case ENOTTY: return "inappropriate device for ioctl";
589 case ENOTUNIQ: return "name not unique on network";
592 case ENXIO: return "no such device or address";
595 case EOPNOTSUPP: return "operation not supported on socket";
598 case EPERM: return "not owner";
601 case EPFNOSUPPORT: return "protocol family not supported";
604 case EPIPE: return "broken pipe";
607 case EPROCLIM: return "too many processes";
610 case EPROCUNAVAIL: return "bad procedure for program";
613 case EPROGMISMATCH: return "program version wrong";
616 case EPROGUNAVAIL: return "RPC program not available";
619 case EPROTO: return "protocol error";
621 #ifdef EPROTONOSUPPORT
622 case EPROTONOSUPPORT: return "protocol not suppored";
625 case EPROTOTYPE: return "protocol wrong type for socket";
628 case ERANGE: return "math result unrepresentable";
630 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
631 case EREFUSED: return "EREFUSED";
634 case EREMCHG: return "remote address changed";
637 case EREMDEV: return "remote device";
640 case EREMOTE: return "pathname hit remote file system";
643 case EREMOTEIO: return "remote i/o error";
645 #ifdef EREMOTERELEASE
646 case EREMOTERELEASE: return "EREMOTERELEASE";
649 case EROFS: return "read-only file system";
652 case ERPCMISMATCH: return "RPC version is wrong";
655 case ERREMOTE: return "object is remote";
658 case ESHUTDOWN: return "can't send afer socket shutdown";
660 #ifdef ESOCKTNOSUPPORT
661 case ESOCKTNOSUPPORT: return "socket type not supported";
664 case ESPIPE: return "invalid seek";
667 case ESRCH: return "no such process";
670 case ESRMNT: return "srmount error";
673 case ESTALE: return "stale remote file handle";
676 case ESUCCESS: return "Error 0";
679 case ETIME: return "timer expired";
682 case ETIMEDOUT: return "connection timed out";
685 case ETOOMANYREFS: return "too many references: can't splice";
688 case ETXTBSY: return "text file or pseudo-device busy";
691 case EUCLEAN: return "structure needs cleaning";
694 case EUNATCH: return "protocol driver not attached";
697 case EUSERS: return "too many users";
700 case EVERSION: return "version mismatch";
702 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
703 case EWOULDBLOCK: return "operation would block";
706 case EXDEV: return "cross-domain link";
709 case EXFULL: return "message tables full";
712 #else /* NO_SYS_ERRLIST */
714 extern char *sys_errlist[];
716 if ((errnum > 0) && (errnum <= sys_nerr))
717 return sys_errlist [errnum];
718 #endif /* NO_SYS_ERRLIST */
720 msg = g_static_private_get (&msg_private);
723 msg = g_new (gchar, 64);
724 g_static_private_set (&msg_private, msg, g_free);
727 sprintf (msg, "unknown error (%d)", errnum);
732 G_CONST_RETURN gchar*
733 g_strsignal (gint signum)
735 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
738 #ifdef HAVE_STRSIGNAL
739 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
740 extern const char *strsignal(int);
742 /* this is declared differently (const) in string.h on BeOS */
743 extern char *strsignal (int sig);
744 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
745 return strsignal (signum);
750 case SIGHUP: return "Hangup";
753 case SIGINT: return "Interrupt";
756 case SIGQUIT: return "Quit";
759 case SIGILL: return "Illegal instruction";
762 case SIGTRAP: return "Trace/breakpoint trap";
765 case SIGABRT: return "IOT trap/Abort";
768 case SIGBUS: return "Bus error";
771 case SIGFPE: return "Floating point exception";
774 case SIGKILL: return "Killed";
777 case SIGUSR1: return "User defined signal 1";
780 case SIGSEGV: return "Segmentation fault";
783 case SIGUSR2: return "User defined signal 2";
786 case SIGPIPE: return "Broken pipe";
789 case SIGALRM: return "Alarm clock";
792 case SIGTERM: return "Terminated";
795 case SIGSTKFLT: return "Stack fault";
798 case SIGCHLD: return "Child exited";
801 case SIGCONT: return "Continued";
804 case SIGSTOP: return "Stopped (signal)";
807 case SIGTSTP: return "Stopped";
810 case SIGTTIN: return "Stopped (tty input)";
813 case SIGTTOU: return "Stopped (tty output)";
816 case SIGURG: return "Urgent condition";
819 case SIGXCPU: return "CPU time limit exceeded";
822 case SIGXFSZ: return "File size limit exceeded";
825 case SIGVTALRM: return "Virtual time alarm";
828 case SIGPROF: return "Profile signal";
831 case SIGWINCH: return "Window size changed";
834 case SIGIO: return "Possible I/O";
837 case SIGPWR: return "Power failure";
840 case SIGUNUSED: return "Unused signal";
843 #else /* NO_SYS_SIGLIST */
845 #ifdef NO_SYS_SIGLIST_DECL
846 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
849 return (char*) /* this function should return const --josh */ sys_siglist [signum];
850 #endif /* NO_SYS_SIGLIST */
852 msg = g_static_private_get (&msg_private);
855 msg = g_new (gchar, 64);
856 g_static_private_set (&msg_private, msg, g_free);
859 sprintf (msg, "unknown signal (%d)", signum);
864 /* Functions g_strlcpy and g_strlcat were originally developed by
865 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
866 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
867 * for more information.
871 /* Use the native ones, if available; they might be implemented in assembly */
873 g_strlcpy (gchar *dest,
877 g_return_val_if_fail (dest != NULL, 0);
878 g_return_val_if_fail (src != NULL, 0);
880 return strlcpy (dest, src, dest_size);
884 g_strlcat (gchar *dest,
888 g_return_val_if_fail (dest != NULL, 0);
889 g_return_val_if_fail (src != NULL, 0);
891 return strlcat (dest, src, dest_size);
894 #else /* ! HAVE_STRLCPY */
897 * Copy string src to buffer dest (of buffer size dest_size). At most
898 * dest_size-1 characters will be copied. Always NUL terminates
899 * (unless dest_size == 0). This function does NOT allocate memory.
900 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
901 * Returns size of attempted result, strlen(src),
902 * so if retval >= dest_size, truncation occurred.
905 g_strlcpy (gchar *dest,
909 register gchar *d = dest;
910 register const gchar *s = src;
911 register gsize n = dest_size;
913 g_return_val_if_fail (dest != NULL, 0);
914 g_return_val_if_fail (src != NULL, 0);
916 /* Copy as many bytes as will fit */
917 if (n != 0 && --n != 0)
920 register gchar c = *s++;
928 /* If not enough room in dest, add NUL and traverse rest of src */
937 return s - src - 1; /* count does not include NUL */
942 * Appends string src to buffer dest (of buffer size dest_size).
943 * At most dest_size-1 characters will be copied.
944 * Unlike strncat, dest_size is the full size of dest, not the space left over.
945 * This function does NOT allocate memory.
946 * This always NUL terminates (unless siz == 0 or there were no NUL characters
947 * in the dest_size characters of dest to start with).
948 * Returns size of attempted result, which is
949 * MIN (dest_size, strlen (original dest)) + strlen (src),
950 * so if retval >= dest_size, truncation occurred.
953 g_strlcat (gchar *dest,
957 register gchar *d = dest;
958 register const gchar *s = src;
959 register gsize bytes_left = dest_size;
960 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
962 g_return_val_if_fail (dest != NULL, 0);
963 g_return_val_if_fail (src != NULL, 0);
965 /* Find the end of dst and adjust bytes left but don't go past end */
966 while (*d != 0 && bytes_left-- != 0)
969 bytes_left = dest_size - dlength;
972 return dlength + strlen (s);
985 return dlength + (s - src); /* count does not include NUL */
987 #endif /* ! HAVE_STRLCPY */
993 * Converts all upper case ASCII letters to lower case ASCII letters.
995 * Return value: a newly allocated string, with all the upper case
996 * characters in @string converted to lower case, with
997 * semantics that exactly match g_ascii_tolower. (Note
998 * that this is unlike the old g_strdown, which modified
999 * the string in place.)
1002 g_ascii_strdown (const gchar *string)
1006 g_return_val_if_fail (string != NULL, NULL);
1008 result = g_strdup (string);
1009 for (s = result; *s; s++)
1010 *s = g_ascii_tolower (*s);
1019 * Converts all lower case ASCII letters to upper case ASCII letters.
1021 * Return value: a newly allocated string, with all the lower case
1022 * characters in @string converted to upper case, with
1023 * semantics that exactly match g_ascii_toupper. (Note
1024 * that this is unlike the old g_strup, which modified
1025 * the string in place.)
1028 g_ascii_strup (const gchar *string)
1032 g_return_val_if_fail (string != NULL, NULL);
1034 result = g_strdup (string);
1035 for (s = result; *s; s++)
1036 *s = g_ascii_toupper (*s);
1042 g_strdown (gchar *string)
1046 g_return_val_if_fail (string != NULL, NULL);
1048 s = (guchar *) string;
1057 return (gchar *) string;
1061 g_strup (gchar *string)
1065 g_return_val_if_fail (string != NULL, NULL);
1067 s = (guchar *) string;
1076 return (gchar *) string;
1080 g_strreverse (gchar *string)
1082 g_return_val_if_fail (string != NULL, NULL);
1086 register gchar *h, *t;
1089 t = string + strlen (string) - 1;
1110 * Convert a character to ASCII lower case.
1112 * Unlike the standard C library tolower function, this only
1113 * recognizes standard ASCII letters and ignores the locale, returning
1114 * all non-ASCII characters unchanged, even if they are lower case
1115 * letters in a particular character set. Also unlike the standard
1116 * library function, this takes and returns a char, not an int, so
1117 * don't call it on EOF but no need to worry about casting to guchar
1118 * before passing a possibly non-ASCII character in.
1120 * Return value: the result of converting @c to lower case.
1121 * If @c is not an ASCII upper case letter,
1122 * @c is returned unchanged.
1125 g_ascii_tolower (gchar c)
1127 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1134 * Convert a character to ASCII upper case.
1136 * Unlike the standard C library toupper function, this only
1137 * recognizes standard ASCII letters and ignores the locale, returning
1138 * all non-ASCII characters unchanged, even if they are upper case
1139 * letters in a particular character set. Also unlike the standard
1140 * library function, this takes and returns a char, not an int, so
1141 * don't call it on EOF but no need to worry about casting to guchar
1142 * before passing a possibly non-ASCII character in.
1144 * Return value: the result of converting @c to upper case.
1145 * If @c is not an ASCII lower case letter,
1146 * @c is returned unchanged.
1149 g_ascii_toupper (gchar c)
1151 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1155 * g_ascii_digit_value:
1156 * @c: an ASCII character
1158 * Determines the numeric value of a character as a decimal
1159 * digit. Differs from g_unichar_digit_value because it takes
1160 * a char, so there's no worry about sign extension if characters
1163 * Return value: If @c is a decimal digit (according to
1164 * `g_ascii_isdigit'), its numeric value. Otherwise, -1.
1167 g_ascii_digit_value (gchar c)
1169 if (g_ascii_isdigit (c))
1175 * g_ascii_xdigit_value:
1176 * @c: an ASCII character
1178 * Determines the numeric value of a character as a hexidecimal
1179 * digit. Differs from g_unichar_xdigit_value because it takes
1180 * a char, so there's no worry about sign extension if characters
1183 * Return value: If @c is a hex digit (according to
1184 * `g_ascii_isxdigit'), its numeric value. Otherwise, -1.
1187 g_ascii_xdigit_value (gchar c)
1189 if (c >= 'A' && c <= 'F')
1190 return c - 'A' + 10;
1191 if (c >= 'a' && c <= 'f')
1192 return c - 'a' + 10;
1193 return g_ascii_digit_value (c);
1197 * g_ascii_strcasecmp:
1198 * @s1: string to compare with @s2
1199 * @s2: string to compare with @s1
1201 * Compare two strings, ignoring the case of ASCII characters.
1203 * Unlike the BSD strcasecmp function, this only recognizes standard
1204 * ASCII letters and ignores the locale, treating all non-ASCII
1205 * characters as if they are not letters.
1207 * Return value: an integer less than, equal to, or greater than
1208 * zero if @s1 is found, respectively, to be less than,
1209 * to match, or to be greater than @s2.
1212 g_ascii_strcasecmp (const gchar *s1,
1217 g_return_val_if_fail (s1 != NULL, 0);
1218 g_return_val_if_fail (s2 != NULL, 0);
1222 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1223 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1229 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1233 * g_ascii_strncasecmp:
1234 * @s1: string to compare with @s2
1235 * @s2: string to compare with @s1
1236 * @n: number of characters to compare
1238 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1239 * characters after the first @n in each string.
1241 * Unlike the BSD strcasecmp function, this only recognizes standard
1242 * ASCII letters and ignores the locale, treating all non-ASCII
1243 * characters as if they are not letters.
1245 * Return value: an integer less than, equal to, or greater than zero
1246 * if the first @n bytes of @s1 is found, respectively,
1247 * to be less than, to match, or to be greater than the
1248 * first @n bytes of @s2.
1251 g_ascii_strncasecmp (const gchar *s1,
1257 g_return_val_if_fail (s1 != NULL, 0);
1258 g_return_val_if_fail (s2 != NULL, 0);
1260 while (n && *s1 && *s2)
1263 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1264 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1271 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1277 g_strcasecmp (const gchar *s1,
1280 #ifdef HAVE_STRCASECMP
1281 g_return_val_if_fail (s1 != NULL, 0);
1282 g_return_val_if_fail (s2 != NULL, 0);
1284 return strcasecmp (s1, s2);
1288 g_return_val_if_fail (s1 != NULL, 0);
1289 g_return_val_if_fail (s2 != NULL, 0);
1293 /* According to A. Cox, some platforms have islower's that
1294 * don't work right on non-uppercase
1296 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1297 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1303 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1308 g_strncasecmp (const gchar *s1,
1312 #ifdef HAVE_STRNCASECMP
1313 return strncasecmp (s1, s2, n);
1317 g_return_val_if_fail (s1 != NULL, 0);
1318 g_return_val_if_fail (s2 != NULL, 0);
1320 while (n && *s1 && *s2)
1323 /* According to A. Cox, some platforms have islower's that
1324 * don't work right on non-uppercase
1326 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1327 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1334 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1341 g_strdelimit (gchar *string,
1342 const gchar *delimiters,
1347 g_return_val_if_fail (string != NULL, NULL);
1350 delimiters = G_STR_DELIMITERS;
1352 for (c = string; *c; c++)
1354 if (strchr (delimiters, *c))
1362 g_strcanon (gchar *string,
1363 const gchar *valid_chars,
1368 g_return_val_if_fail (string != NULL, NULL);
1369 g_return_val_if_fail (valid_chars != NULL, NULL);
1371 for (c = string; *c; c++)
1373 if (!strchr (valid_chars, *c))
1381 g_strcompress (const gchar *source)
1383 const gchar *p = source, *octal;
1384 gchar *dest = g_malloc (strlen (source) + 1);
1394 case '0': case '1': case '2': case '3': case '4':
1395 case '5': case '6': case '7':
1398 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1400 *q = (*q * 8) + (*p - '0');
1421 default: /* Also handles \" and \\ */
1436 g_strescape (const gchar *source,
1437 const gchar *exceptions)
1444 g_return_val_if_fail (source != NULL, NULL);
1446 p = (guchar *) source;
1447 /* Each source byte needs maximally four destination chars (\777) */
1448 q = dest = g_malloc (strlen (source) * 4 + 1);
1450 memset (excmap, 0, 256);
1453 guchar *e = (guchar *) exceptions;
1499 if ((*p < ' ') || (*p >= 0177))
1502 *q++ = '0' + (((*p) >> 6) & 07);
1503 *q++ = '0' + (((*p) >> 3) & 07);
1504 *q++ = '0' + ((*p) & 07);
1518 g_strchug (gchar *string)
1522 g_return_val_if_fail (string != NULL, NULL);
1524 for (start = (guchar*) string; *start && isspace (*start); start++)
1527 g_memmove (string, start, strlen ((gchar *) start) + 1);
1533 g_strchomp (gchar *string)
1537 g_return_val_if_fail (string != NULL, NULL);
1542 for (s = string + strlen (string) - 1; s >= string && isspace ((guchar)*s);
1551 * @string: a string to split.
1552 * @delimiter: a string which specifies the places at which to split the string.
1553 * The delimiter is not included in any of the resulting strings, unless
1554 * max_tokens is reached.
1555 * @max_tokens: the maximum number of pieces to split @string into. If this is
1556 * less than 1, the string is split completely.
1558 * Splits a string into a maximum of @max_tokens pieces, using the given
1559 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
1560 * to the last token.
1562 * As a special case, the result of splitting the empty string "" is an empty
1563 * vector, not a vector containing a single string. The reason for this
1564 * special case is that being able to represent a empty vector is typically
1565 * more useful than consistent handling of empty elements. If you do need
1566 * to represent empty elements, you'll need to check for the empty string
1567 * before calling g_strsplit().
1569 * Return value: a newly-allocated %NULL-terminated array of strings. Use g_strfreev()
1573 g_strsplit (const gchar *string,
1574 const gchar *delimiter,
1577 GSList *string_list = NULL, *slist;
1578 gchar **str_array, *s;
1580 const gchar *remainder;
1582 g_return_val_if_fail (string != NULL, NULL);
1583 g_return_val_if_fail (delimiter != NULL, NULL);
1584 g_return_val_if_fail (delimiter[0] != '\0', NULL);
1587 max_tokens = G_MAXINT;
1592 s = strstr (remainder, delimiter);
1595 gsize delimiter_len = strlen (delimiter);
1602 len = s - remainder;
1603 new_string = g_new (gchar, len + 1);
1604 strncpy (new_string, remainder, len);
1605 new_string[len] = 0;
1606 string_list = g_slist_prepend (string_list, new_string);
1608 remainder = s + delimiter_len;
1609 s = strstr (remainder, delimiter);
1611 while (--max_tokens && s);
1616 string_list = g_slist_prepend (string_list, g_strdup (remainder));
1619 str_array = g_new (gchar*, n + 1);
1621 str_array[n--] = NULL;
1622 for (slist = string_list; slist; slist = slist->next)
1623 str_array[n--] = slist->data;
1625 g_slist_free (string_list);
1631 g_strfreev (gchar **str_array)
1637 for(i = 0; str_array[i] != NULL; i++)
1638 g_free(str_array[i]);
1646 * @str_array: %NULL-terminated array of strings
1648 * Copies %NULL-terminated array of strings. The copy is a deep copy;
1649 * the new array should be freed by first freeing each string, then
1650 * the array itself. g_strfreev() does this for you. If called
1651 * on a %NULL value, g_strdupv() simply returns %NULL.
1653 * Return value: a new %NULL-terminated array of strings
1656 g_strdupv (gchar **str_array)
1664 while (str_array[i])
1667 retval = g_new (gchar*, i + 1);
1670 while (str_array[i])
1672 retval[i] = g_strdup (str_array[i]);
1684 g_strjoinv (const gchar *separator,
1690 g_return_val_if_fail (str_array != NULL, NULL);
1692 if (separator == NULL)
1699 gsize separator_len;
1701 separator_len = strlen (separator);
1702 /* First part, getting length */
1703 len = 1 + strlen (str_array[0]);
1704 for (i = 1; str_array[i] != NULL; i++)
1705 len += strlen (str_array[i]);
1706 len += separator_len * (i - 1);
1708 /* Second part, building string */
1709 string = g_new (gchar, len);
1710 ptr = g_stpcpy (string, *str_array);
1711 for (i = 1; str_array[i] != NULL; i++)
1713 ptr = g_stpcpy (ptr, separator);
1714 ptr = g_stpcpy (ptr, str_array[i]);
1718 string = g_strdup ("");
1724 g_strjoin (const gchar *separator,
1730 gsize separator_len;
1733 if (separator == NULL)
1736 separator_len = strlen (separator);
1738 va_start (args, separator);
1740 s = va_arg (args, gchar*);
1744 /* First part, getting length */
1745 len = 1 + strlen (s);
1747 s = va_arg (args, gchar*);
1750 len += separator_len + strlen (s);
1751 s = va_arg (args, gchar*);
1755 /* Second part, building string */
1756 string = g_new (gchar, len);
1758 va_start (args, separator);
1760 s = va_arg (args, gchar*);
1761 ptr = g_stpcpy (string, s);
1763 s = va_arg (args, gchar*);
1766 ptr = g_stpcpy (ptr, separator);
1767 ptr = g_stpcpy (ptr, s);
1768 s = va_arg (args, gchar*);
1772 string = g_strdup ("");
1782 * @haystack: a string
1783 * @haystack_len: The maximum length of haystack
1784 * @needle: The string to search for.
1786 * Searches the string haystack for the first occurrence
1787 * of the string needle, limiting the length of the search
1790 * Return value: A pointer to the found occurrence, or
1791 * NULL if not found.
1794 g_strstr_len (const gchar *haystack,
1795 gssize haystack_len,
1796 const gchar *needle)
1798 g_return_val_if_fail (haystack != NULL, NULL);
1799 g_return_val_if_fail (needle != NULL, NULL);
1801 if (haystack_len < 0)
1802 return strstr (haystack, needle);
1805 const gchar *p = haystack;
1806 gsize needle_len = strlen (needle);
1810 if (needle_len == 0)
1811 return (gchar *)haystack;
1813 if (haystack_len < needle_len)
1816 end = haystack + haystack_len - needle_len;
1818 while (*p && p <= end)
1820 for (i = 0; i < needle_len; i++)
1821 if (p[i] != needle[i])
1836 * @haystack: a nul-terminated string
1837 * @needle: The nul-terminated string to search for.
1839 * Searches the string haystack for the last occurrence
1840 * of the string needle.
1842 * Return value: A pointer to the found occurrence, or
1843 * NULL if not found.
1846 g_strrstr (const gchar *haystack,
1847 const gchar *needle)
1854 g_return_val_if_fail (haystack != NULL, NULL);
1855 g_return_val_if_fail (needle != NULL, NULL);
1857 needle_len = strlen (needle);
1858 haystack_len = strlen (haystack);
1860 if (needle_len == 0)
1861 return (gchar *)haystack;
1863 if (haystack_len < needle_len)
1866 p = haystack + haystack_len - needle_len;
1868 while (p >= haystack)
1870 for (i = 0; i < needle_len; i++)
1871 if (p[i] != needle[i])
1885 * @haystack: a nul-terminated string
1886 * @haystack_len: The maximum length of haystack
1887 * @needle: The nul-terminated string to search for.
1889 * Searches the string haystack for the last occurrence
1890 * of the string needle, limiting the length of the search
1893 * Return value: A pointer to the found occurrence, or
1894 * NULL if not found.
1897 g_strrstr_len (const gchar *haystack,
1898 gssize haystack_len,
1899 const gchar *needle)
1901 g_return_val_if_fail (haystack != NULL, NULL);
1902 g_return_val_if_fail (needle != NULL, NULL);
1904 if (haystack_len < 0)
1905 return g_strrstr (haystack, needle);
1908 gsize needle_len = strlen (needle);
1909 const gchar *haystack_max = haystack + haystack_len;
1910 const gchar *p = haystack;
1913 while (p < haystack_max && *p)
1916 if (p < haystack + needle_len)
1921 while (p >= haystack)
1923 for (i = 0; i < needle_len; i++)
1924 if (p[i] != needle[i])