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 */
43 #include <ctype.h> /* For tolower() */
44 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
53 /* do not include <unistd.h> in this place since it
54 * inteferes with g_strsignal() on some OSes
57 static const guint16 ascii_table_data[256] = {
58 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
59 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
60 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
63 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
64 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
65 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
67 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
68 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
69 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
70 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
71 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
72 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
73 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
74 /* the upper 128 are all zeroes */
77 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
80 const guint16 * const g_ascii_table = ascii_table_data;
83 g_strdup (const gchar *str)
89 new_str = g_new (char, strlen (str) + 1);
90 strcpy (new_str, str);
99 g_memdup (gconstpointer mem,
106 new_mem = g_malloc (byte_size);
107 memcpy (new_mem, mem, byte_size);
116 g_strndup (const gchar *str,
123 new_str = g_new (gchar, n + 1);
124 strncpy (new_str, str, n);
134 g_strnfill (gsize length,
139 str = g_new (gchar, length + 1);
140 memset (str, (guchar)fill_char, length);
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,
185 #ifdef HAVE_VASPRINTF
186 vasprintf (&buffer, format, args1);
187 if (!g_mem_is_system_malloc ())
189 gchar *buffer1 = g_strdup (buffer);
196 G_VA_COPY (args2, args1);
198 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
200 vsprintf (buffer, format, args2);
207 g_strdup_printf (const gchar *format,
213 va_start (args, format);
214 buffer = g_strdup_vprintf (format, args);
221 g_strconcat (const gchar *string1, ...)
229 g_return_val_if_fail (string1 != NULL, NULL);
231 l = 1 + strlen (string1);
232 va_start (args, string1);
233 s = va_arg (args, gchar*);
237 s = va_arg (args, gchar*);
241 concat = g_new (gchar, l);
244 ptr = g_stpcpy (ptr, string1);
245 va_start (args, string1);
246 s = va_arg (args, gchar*);
249 ptr = g_stpcpy (ptr, s);
250 s = va_arg (args, gchar*);
259 * @nptr: the string to convert to a numeric value.
260 * @endptr: if non-NULL, it returns the character after
261 * the last character used in the conversion.
263 * Converts a string to a gdouble value.
264 * It calls the standard strtod() function to handle the conversion, but
265 * if the string is not completely converted it attempts the conversion
266 * again with @g_ascii_strtod, and returns the best match.
268 * This function should seldom be used. The normal situation when reading
269 * numbers not for human consumption is to use @g_ascii_strtod(). Only when
270 * you know that you must expect both locale formated and C formated numbers
271 * should you use this. Make sure that you don't pass strings such as comma
272 * separated lists of values, since the commas may be interpreted as a decimal
273 * point in some locales, causing unexpected results.
275 * Return value: the gdouble value.
278 g_strtod (const gchar *nptr,
286 g_return_val_if_fail (nptr != NULL, 0);
291 val_1 = strtod (nptr, &fail_pos_1);
293 if (fail_pos_1 && fail_pos_1[0] != 0)
294 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
296 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
299 *endptr = fail_pos_1;
305 *endptr = fail_pos_2;
312 * @nptr: the string to convert to a numeric value.
313 * @endptr: if non-NULL, it returns the character after
314 * the last character used in the conversion.
316 * Converts a string to a gdouble value.
317 * This function behaves like the standard strtod() function
318 * does in the C locale. It does this without actually
319 * changing the current locale, since that would not be
322 * This function is typically used when reading configuration
323 * files or other non-user input that should be locale dependent.
324 * To handle input from the user you should normally use the
325 * locale-sensitive system strtod function.
327 * To convert from a string to double in a locale-insensitive
328 * way, use @g_ascii_dtostr.
330 * If the correct value would cause overflow, plus or minus HUGE_VAL
331 * is returned (according to the sign of the value), and ERANGE is
332 * stored in errno. If the correct value would cause underflow,
333 * zero is returned and ERANGE is stored in errno.
335 * This function resets errno before calling strtod() so that
336 * you can reliably detect overflow and underflow.
338 * Return value: the gdouble value.
341 g_ascii_strtod (const gchar *nptr,
346 struct lconv *locale_data;
347 const char *decimal_point;
348 int decimal_point_len;
349 const char *p, *decimal_point_pos;
350 const char *end = NULL; /* Silence gcc */
352 g_return_val_if_fail (nptr != NULL, 0);
356 locale_data = localeconv ();
357 decimal_point = locale_data->decimal_point;
358 decimal_point_len = strlen (decimal_point);
360 g_assert (decimal_point_len != 0);
362 decimal_point_pos = NULL;
363 if (decimal_point[0] != '.' ||
364 decimal_point[1] != 0)
367 /* Skip leading space */
368 while (g_ascii_isspace (*p))
371 /* Skip leading optional sign */
372 if (*p == '+' || *p == '-')
376 (p[1] == 'x' || p[1] == 'X'))
379 /* HEX - find the (optional) decimal point */
381 while (g_ascii_isxdigit (*p))
386 decimal_point_pos = p++;
388 while (g_ascii_isxdigit (*p))
391 if (*p == 'p' || *p == 'P')
393 if (*p == '+' || *p == '-')
395 while (g_ascii_isdigit (*p))
402 while (g_ascii_isdigit (*p))
407 decimal_point_pos = p++;
409 while (g_ascii_isdigit (*p))
412 if (*p == 'e' || *p == 'E')
414 if (*p == '+' || *p == '-')
416 while (g_ascii_isdigit (*p))
421 /* For the other cases, we need not convert the decimal point */
424 /* Set errno to zero, so that we can distinguish zero results
428 if (decimal_point_pos)
432 /* We need to convert the '.' to the locale specific decimal point */
433 copy = g_malloc (end - nptr + 1 + decimal_point_len);
436 memcpy (c, nptr, decimal_point_pos - nptr);
437 c += decimal_point_pos - nptr;
438 memcpy (c, decimal_point, decimal_point_len);
439 c += decimal_point_len;
440 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
441 c += end - (decimal_point_pos + 1);
444 val = strtod (copy, &fail_pos);
448 if (fail_pos > decimal_point_pos)
449 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
451 fail_pos = (char *)nptr + (fail_pos - copy);
458 val = strtod (nptr, &fail_pos);
468 * @buffer: A buffer to place the resulting string in
469 * @buf_len: The length of the buffer.
470 * @d: The double to convert
472 * Converts a double to a string, using the '.' as
475 * This functions generates enough precision that converting
476 * the string back using @g_strtod gives the same machine-number
477 * (on machines with IEEE compatible 64bit doubles). It is
478 * guaranteed that the size of the resulting string will never
479 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
481 * Return value: The pointer to the buffer with the converted string.
484 g_ascii_dtostr (gchar *buffer,
488 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
493 * @buffer: A buffer to place the resulting string in
494 * @buf_len: The length of the buffer.
495 * @format: The printf-style format to use for the
496 * code to use for converting.
497 * @d: The double to convert
499 * Converts a double to a string, using the '.' as
500 * decimal_point. To format the number you pass in
501 * a printf-style formating string. Allowed conversion
502 * specifiers are eEfFgG.
504 * If you just want to want to serialize the value into a
505 * string, use @g_ascii_dtostr.
507 * Return value: The pointer to the buffer with the converted string.
510 g_ascii_formatd (gchar *buffer,
515 struct lconv *locale_data;
516 const char *decimal_point;
517 int decimal_point_len;
522 g_return_val_if_fail (buffer != NULL, NULL);
523 g_return_val_if_fail (format[0] == '%', NULL);
524 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
526 format_char = format[strlen (format) - 1];
528 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
529 format_char == 'f' || format_char == 'F' ||
530 format_char == 'g' || format_char == 'G',
533 if (format[0] != '%')
536 if (strpbrk (format + 1, "'l%"))
539 if (!(format_char == 'e' || format_char == 'E' ||
540 format_char == 'f' || format_char == 'F' ||
541 format_char == 'g' || format_char == 'G'))
545 g_snprintf (buffer, buf_len, format, d);
547 locale_data = localeconv ();
548 decimal_point = locale_data->decimal_point;
549 decimal_point_len = strlen (decimal_point);
551 g_assert (decimal_point_len != 0);
553 if (decimal_point[0] != '.' ||
554 decimal_point[1] != 0)
558 if (*p == '+' || *p == '-')
561 while (isdigit ((guchar)*p))
564 if (strncmp (p, decimal_point, decimal_point_len) == 0)
568 if (decimal_point_len > 1) {
569 rest_len = strlen (p + (decimal_point_len-1));
570 memmove (p, p + (decimal_point_len-1),
582 G_CONST_RETURN gchar*
583 g_strerror (gint errnum)
585 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
589 return strerror (errnum);
594 case E2BIG: return "argument list too long";
597 case EACCES: return "permission denied";
600 case EADDRINUSE: return "address already in use";
603 case EADDRNOTAVAIL: return "can't assign requested address";
606 case EADV: return "advertise error";
609 case EAFNOSUPPORT: return "address family not supported by protocol family";
612 case EAGAIN: return "try again";
615 case EALIGN: return "EALIGN";
618 case EALREADY: return "operation already in progress";
621 case EBADE: return "bad exchange descriptor";
624 case EBADF: return "bad file number";
627 case EBADFD: return "file descriptor in bad state";
630 case EBADMSG: return "not a data message";
633 case EBADR: return "bad request descriptor";
636 case EBADRPC: return "RPC structure is bad";
639 case EBADRQC: return "bad request code";
642 case EBADSLT: return "invalid slot";
645 case EBFONT: return "bad font file format";
648 case EBUSY: return "mount device busy";
651 case ECHILD: return "no children";
654 case ECHRNG: return "channel number out of range";
657 case ECOMM: return "communication error on send";
660 case ECONNABORTED: return "software caused connection abort";
663 case ECONNREFUSED: return "connection refused";
666 case ECONNRESET: return "connection reset by peer";
668 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
669 case EDEADLK: return "resource deadlock avoided";
672 case EDEADLOCK: return "resource deadlock avoided";
675 case EDESTADDRREQ: return "destination address required";
678 case EDIRTY: return "mounting a dirty fs w/o force";
681 case EDOM: return "math argument out of range";
684 case EDOTDOT: return "cross mount point";
687 case EDQUOT: return "disk quota exceeded";
690 case EDUPPKG: return "duplicate package name";
693 case EEXIST: return "file already exists";
696 case EFAULT: return "bad address in system call argument";
699 case EFBIG: return "file too large";
702 case EHOSTDOWN: return "host is down";
705 case EHOSTUNREACH: return "host is unreachable";
708 case EIDRM: return "identifier removed";
711 case EINIT: return "initialization error";
714 case EINPROGRESS: return "operation now in progress";
717 case EINTR: return "interrupted system call";
720 case EINVAL: return "invalid argument";
723 case EIO: return "I/O error";
726 case EISCONN: return "socket is already connected";
729 case EISDIR: return "is a directory";
732 case EISNAM: return "is a name file";
735 case ELBIN: return "ELBIN";
738 case EL2HLT: return "level 2 halted";
741 case EL2NSYNC: return "level 2 not synchronized";
744 case EL3HLT: return "level 3 halted";
747 case EL3RST: return "level 3 reset";
750 case ELIBACC: return "can not access a needed shared library";
753 case ELIBBAD: return "accessing a corrupted shared library";
756 case ELIBEXEC: return "can not exec a shared library directly";
759 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
762 case ELIBSCN: return ".lib section in a.out corrupted";
765 case ELNRNG: return "link number out of range";
768 case ELOOP: return "too many levels of symbolic links";
771 case EMFILE: return "too many open files";
774 case EMLINK: return "too many links";
777 case EMSGSIZE: return "message too long";
780 case EMULTIHOP: return "multihop attempted";
783 case ENAMETOOLONG: return "file name too long";
786 case ENAVAIL: return "not available";
789 case ENET: return "ENET";
792 case ENETDOWN: return "network is down";
795 case ENETRESET: return "network dropped connection on reset";
798 case ENETUNREACH: return "network is unreachable";
801 case ENFILE: return "file table overflow";
804 case ENOANO: return "anode table overflow";
806 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
807 case ENOBUFS: return "no buffer space available";
810 case ENOCSI: return "no CSI structure available";
813 case ENODATA: return "no data available";
816 case ENODEV: return "no such device";
819 case ENOENT: return "no such file or directory";
822 case ENOEXEC: return "exec format error";
825 case ENOLCK: return "no locks available";
828 case ENOLINK: return "link has be severed";
831 case ENOMEM: return "not enough memory";
834 case ENOMSG: return "no message of desired type";
837 case ENONET: return "machine is not on the network";
840 case ENOPKG: return "package not installed";
843 case ENOPROTOOPT: return "bad proocol option";
846 case ENOSPC: return "no space left on device";
849 case ENOSR: return "out of stream resources";
852 case ENOSTR: return "not a stream device";
855 case ENOSYM: return "unresolved symbol name";
858 case ENOSYS: return "function not implemented";
861 case ENOTBLK: return "block device required";
864 case ENOTCONN: return "socket is not connected";
867 case ENOTDIR: return "not a directory";
870 case ENOTEMPTY: return "directory not empty";
873 case ENOTNAM: return "not a name file";
876 case ENOTSOCK: return "socket operation on non-socket";
879 case ENOTTY: return "inappropriate device for ioctl";
882 case ENOTUNIQ: return "name not unique on network";
885 case ENXIO: return "no such device or address";
888 case EOPNOTSUPP: return "operation not supported on socket";
891 case EPERM: return "not owner";
894 case EPFNOSUPPORT: return "protocol family not supported";
897 case EPIPE: return "broken pipe";
900 case EPROCLIM: return "too many processes";
903 case EPROCUNAVAIL: return "bad procedure for program";
906 case EPROGMISMATCH: return "program version wrong";
909 case EPROGUNAVAIL: return "RPC program not available";
912 case EPROTO: return "protocol error";
914 #ifdef EPROTONOSUPPORT
915 case EPROTONOSUPPORT: return "protocol not suppored";
918 case EPROTOTYPE: return "protocol wrong type for socket";
921 case ERANGE: return "math result unrepresentable";
923 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
924 case EREFUSED: return "EREFUSED";
927 case EREMCHG: return "remote address changed";
930 case EREMDEV: return "remote device";
933 case EREMOTE: return "pathname hit remote file system";
936 case EREMOTEIO: return "remote i/o error";
938 #ifdef EREMOTERELEASE
939 case EREMOTERELEASE: return "EREMOTERELEASE";
942 case EROFS: return "read-only file system";
945 case ERPCMISMATCH: return "RPC version is wrong";
948 case ERREMOTE: return "object is remote";
951 case ESHUTDOWN: return "can't send afer socket shutdown";
953 #ifdef ESOCKTNOSUPPORT
954 case ESOCKTNOSUPPORT: return "socket type not supported";
957 case ESPIPE: return "invalid seek";
960 case ESRCH: return "no such process";
963 case ESRMNT: return "srmount error";
966 case ESTALE: return "stale remote file handle";
969 case ESUCCESS: return "Error 0";
972 case ETIME: return "timer expired";
975 case ETIMEDOUT: return "connection timed out";
978 case ETOOMANYREFS: return "too many references: can't splice";
981 case ETXTBSY: return "text file or pseudo-device busy";
984 case EUCLEAN: return "structure needs cleaning";
987 case EUNATCH: return "protocol driver not attached";
990 case EUSERS: return "too many users";
993 case EVERSION: return "version mismatch";
995 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
996 case EWOULDBLOCK: return "operation would block";
999 case EXDEV: return "cross-domain link";
1002 case EXFULL: return "message tables full";
1005 #else /* NO_SYS_ERRLIST */
1006 extern int sys_nerr;
1007 extern char *sys_errlist[];
1009 if ((errnum > 0) && (errnum <= sys_nerr))
1010 return sys_errlist [errnum];
1011 #endif /* NO_SYS_ERRLIST */
1013 msg = g_static_private_get (&msg_private);
1016 msg = g_new (gchar, 64);
1017 g_static_private_set (&msg_private, msg, g_free);
1020 sprintf (msg, "unknown error (%d)", errnum);
1025 G_CONST_RETURN gchar*
1026 g_strsignal (gint signum)
1028 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1031 #ifdef HAVE_STRSIGNAL
1032 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1033 extern const char *strsignal(int);
1035 /* this is declared differently (const) in string.h on BeOS */
1036 extern char *strsignal (int sig);
1037 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1038 return strsignal (signum);
1039 #elif NO_SYS_SIGLIST
1043 case SIGHUP: return "Hangup";
1046 case SIGINT: return "Interrupt";
1049 case SIGQUIT: return "Quit";
1052 case SIGILL: return "Illegal instruction";
1055 case SIGTRAP: return "Trace/breakpoint trap";
1058 case SIGABRT: return "IOT trap/Abort";
1061 case SIGBUS: return "Bus error";
1064 case SIGFPE: return "Floating point exception";
1067 case SIGKILL: return "Killed";
1070 case SIGUSR1: return "User defined signal 1";
1073 case SIGSEGV: return "Segmentation fault";
1076 case SIGUSR2: return "User defined signal 2";
1079 case SIGPIPE: return "Broken pipe";
1082 case SIGALRM: return "Alarm clock";
1085 case SIGTERM: return "Terminated";
1088 case SIGSTKFLT: return "Stack fault";
1091 case SIGCHLD: return "Child exited";
1094 case SIGCONT: return "Continued";
1097 case SIGSTOP: return "Stopped (signal)";
1100 case SIGTSTP: return "Stopped";
1103 case SIGTTIN: return "Stopped (tty input)";
1106 case SIGTTOU: return "Stopped (tty output)";
1109 case SIGURG: return "Urgent condition";
1112 case SIGXCPU: return "CPU time limit exceeded";
1115 case SIGXFSZ: return "File size limit exceeded";
1118 case SIGVTALRM: return "Virtual time alarm";
1121 case SIGPROF: return "Profile signal";
1124 case SIGWINCH: return "Window size changed";
1127 case SIGIO: return "Possible I/O";
1130 case SIGPWR: return "Power failure";
1133 case SIGUNUSED: return "Unused signal";
1136 #else /* NO_SYS_SIGLIST */
1138 #ifdef NO_SYS_SIGLIST_DECL
1139 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1142 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1143 #endif /* NO_SYS_SIGLIST */
1145 msg = g_static_private_get (&msg_private);
1148 msg = g_new (gchar, 64);
1149 g_static_private_set (&msg_private, msg, g_free);
1152 sprintf (msg, "unknown signal (%d)", signum);
1157 /* Functions g_strlcpy and g_strlcat were originally developed by
1158 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1159 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1160 * for more information.
1164 /* Use the native ones, if available; they might be implemented in assembly */
1166 g_strlcpy (gchar *dest,
1170 g_return_val_if_fail (dest != NULL, 0);
1171 g_return_val_if_fail (src != NULL, 0);
1173 return strlcpy (dest, src, dest_size);
1177 g_strlcat (gchar *dest,
1181 g_return_val_if_fail (dest != NULL, 0);
1182 g_return_val_if_fail (src != NULL, 0);
1184 return strlcat (dest, src, dest_size);
1187 #else /* ! HAVE_STRLCPY */
1190 * Copy string src to buffer dest (of buffer size dest_size). At most
1191 * dest_size-1 characters will be copied. Always NUL terminates
1192 * (unless dest_size == 0). This function does NOT allocate memory.
1193 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1194 * Returns size of attempted result, strlen(src),
1195 * so if retval >= dest_size, truncation occurred.
1198 g_strlcpy (gchar *dest,
1202 register gchar *d = dest;
1203 register const gchar *s = src;
1204 register gsize n = dest_size;
1206 g_return_val_if_fail (dest != NULL, 0);
1207 g_return_val_if_fail (src != NULL, 0);
1209 /* Copy as many bytes as will fit */
1210 if (n != 0 && --n != 0)
1213 register gchar c = *s++;
1221 /* If not enough room in dest, add NUL and traverse rest of src */
1230 return s - src - 1; /* count does not include NUL */
1235 * Appends string src to buffer dest (of buffer size dest_size).
1236 * At most dest_size-1 characters will be copied.
1237 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1238 * This function does NOT allocate memory.
1239 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1240 * in the dest_size characters of dest to start with).
1241 * Returns size of attempted result, which is
1242 * MIN (dest_size, strlen (original dest)) + strlen (src),
1243 * so if retval >= dest_size, truncation occurred.
1246 g_strlcat (gchar *dest,
1250 register gchar *d = dest;
1251 register const gchar *s = src;
1252 register gsize bytes_left = dest_size;
1253 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1255 g_return_val_if_fail (dest != NULL, 0);
1256 g_return_val_if_fail (src != NULL, 0);
1258 /* Find the end of dst and adjust bytes left but don't go past end */
1259 while (*d != 0 && bytes_left-- != 0)
1262 bytes_left = dest_size - dlength;
1264 if (bytes_left == 0)
1265 return dlength + strlen (s);
1269 if (bytes_left != 1)
1278 return dlength + (s - src); /* count does not include NUL */
1280 #endif /* ! HAVE_STRLCPY */
1285 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1287 * Converts all upper case ASCII letters to lower case ASCII letters.
1289 * Return value: a newly-allocated string, with all the upper case
1290 * characters in @str converted to lower case, with
1291 * semantics that exactly match g_ascii_tolower(). (Note
1292 * that this is unlike the old g_strdown(), which modified
1293 * the string in place.)
1296 g_ascii_strdown (const gchar *str,
1301 g_return_val_if_fail (str != NULL, NULL);
1306 result = g_strndup (str, len);
1307 for (s = result; *s; s++)
1308 *s = g_ascii_tolower (*s);
1316 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1318 * Converts all lower case ASCII letters to upper case ASCII letters.
1320 * Return value: a newly allocated string, with all the lower case
1321 * characters in @str converted to upper case, with
1322 * semantics that exactly match g_ascii_toupper(). (Note
1323 * that this is unlike the old g_strup(), which modified
1324 * the string in place.)
1327 g_ascii_strup (const gchar *str,
1332 g_return_val_if_fail (str != NULL, NULL);
1337 result = g_strndup (str, len);
1338 for (s = result; *s; s++)
1339 *s = g_ascii_toupper (*s);
1345 g_strdown (gchar *string)
1349 g_return_val_if_fail (string != NULL, NULL);
1351 s = (guchar *) string;
1360 return (gchar *) string;
1364 g_strup (gchar *string)
1368 g_return_val_if_fail (string != NULL, NULL);
1370 s = (guchar *) string;
1379 return (gchar *) string;
1383 g_strreverse (gchar *string)
1385 g_return_val_if_fail (string != NULL, NULL);
1389 register gchar *h, *t;
1392 t = string + strlen (string) - 1;
1411 * @c: any character.
1413 * Convert a character to ASCII lower case.
1415 * Unlike the standard C library tolower() function, this only
1416 * recognizes standard ASCII letters and ignores the locale, returning
1417 * all non-ASCII characters unchanged, even if they are lower case
1418 * letters in a particular character set. Also unlike the standard
1419 * library function, this takes and returns a char, not an int, so
1420 * don't call it on %EOF but no need to worry about casting to #guchar
1421 * before passing a possibly non-ASCII character in.
1423 * Return value: the result of converting @c to lower case.
1424 * If @c is not an ASCII upper case letter,
1425 * @c is returned unchanged.
1428 g_ascii_tolower (gchar c)
1430 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1435 * @c: any character.
1437 * Convert a character to ASCII upper case.
1439 * Unlike the standard C library toupper() function, this only
1440 * recognizes standard ASCII letters and ignores the locale, returning
1441 * all non-ASCII characters unchanged, even if they are upper case
1442 * letters in a particular character set. Also unlike the standard
1443 * library function, this takes and returns a char, not an int, so
1444 * don't call it on %EOF but no need to worry about casting to #guchar
1445 * before passing a possibly non-ASCII character in.
1447 * Return value: the result of converting @c to upper case.
1448 * If @c is not an ASCII lower case letter,
1449 * @c is returned unchanged.
1452 g_ascii_toupper (gchar c)
1454 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1458 * g_ascii_digit_value:
1459 * @c: an ASCII character.
1461 * Determines the numeric value of a character as a decimal
1462 * digit. Differs from g_unichar_digit_value() because it takes
1463 * a char, so there's no worry about sign extension if characters
1466 * Return value: If @c is a decimal digit (according to
1467 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1470 g_ascii_digit_value (gchar c)
1472 if (g_ascii_isdigit (c))
1478 * g_ascii_xdigit_value:
1479 * @c: an ASCII character.
1481 * Determines the numeric value of a character as a hexidecimal
1482 * digit. Differs from g_unichar_xdigit_value() because it takes
1483 * a char, so there's no worry about sign extension if characters
1486 * Return value: If @c is a hex digit (according to
1487 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1490 g_ascii_xdigit_value (gchar c)
1492 if (c >= 'A' && c <= 'F')
1493 return c - 'A' + 10;
1494 if (c >= 'a' && c <= 'f')
1495 return c - 'a' + 10;
1496 return g_ascii_digit_value (c);
1500 * g_ascii_strcasecmp:
1501 * @s1: string to compare with @s2.
1502 * @s2: string to compare with @s1.
1504 * Compare two strings, ignoring the case of ASCII characters.
1506 * Unlike the BSD strcasecmp() function, this only recognizes standard
1507 * ASCII letters and ignores the locale, treating all non-ASCII
1508 * characters as if they are not letters.
1510 * Return value: an integer less than, equal to, or greater than
1511 * zero if @s1 is found, respectively, to be less than,
1512 * to match, or to be greater than @s2.
1515 g_ascii_strcasecmp (const gchar *s1,
1520 g_return_val_if_fail (s1 != NULL, 0);
1521 g_return_val_if_fail (s2 != NULL, 0);
1525 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1526 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1532 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1536 * g_ascii_strncasecmp:
1537 * @s1: string to compare with @s2.
1538 * @s2: string to compare with @s1.
1539 * @n: number of characters to compare.
1541 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1542 * characters after the first @n in each string.
1544 * Unlike the BSD strcasecmp() function, this only recognizes standard
1545 * ASCII letters and ignores the locale, treating all non-ASCII
1546 * characters as if they are not letters.
1548 * Return value: an integer less than, equal to, or greater than zero
1549 * if the first @n bytes of @s1 is found, respectively,
1550 * to be less than, to match, or to be greater than the
1551 * first @n bytes of @s2.
1554 g_ascii_strncasecmp (const gchar *s1,
1560 g_return_val_if_fail (s1 != NULL, 0);
1561 g_return_val_if_fail (s2 != NULL, 0);
1563 while (n && *s1 && *s2)
1566 c1 = (gint)(guchar) g_ascii_tolower (*s1);
1567 c2 = (gint)(guchar) g_ascii_tolower (*s2);
1574 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1580 g_strcasecmp (const gchar *s1,
1583 #ifdef HAVE_STRCASECMP
1584 g_return_val_if_fail (s1 != NULL, 0);
1585 g_return_val_if_fail (s2 != NULL, 0);
1587 return strcasecmp (s1, s2);
1591 g_return_val_if_fail (s1 != NULL, 0);
1592 g_return_val_if_fail (s2 != NULL, 0);
1596 /* According to A. Cox, some platforms have islower's that
1597 * don't work right on non-uppercase
1599 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1600 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1606 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1611 g_strncasecmp (const gchar *s1,
1615 #ifdef HAVE_STRNCASECMP
1616 return strncasecmp (s1, s2, n);
1620 g_return_val_if_fail (s1 != NULL, 0);
1621 g_return_val_if_fail (s2 != NULL, 0);
1623 while (n && *s1 && *s2)
1626 /* According to A. Cox, some platforms have islower's that
1627 * don't work right on non-uppercase
1629 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1630 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1637 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1644 g_strdelimit (gchar *string,
1645 const gchar *delimiters,
1650 g_return_val_if_fail (string != NULL, NULL);
1653 delimiters = G_STR_DELIMITERS;
1655 for (c = string; *c; c++)
1657 if (strchr (delimiters, *c))
1665 g_strcanon (gchar *string,
1666 const gchar *valid_chars,
1671 g_return_val_if_fail (string != NULL, NULL);
1672 g_return_val_if_fail (valid_chars != NULL, NULL);
1674 for (c = string; *c; c++)
1676 if (!strchr (valid_chars, *c))
1684 g_strcompress (const gchar *source)
1686 const gchar *p = source, *octal;
1687 gchar *dest = g_malloc (strlen (source) + 1);
1697 case '0': case '1': case '2': case '3': case '4':
1698 case '5': case '6': case '7':
1701 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1703 *q = (*q * 8) + (*p - '0');
1724 default: /* Also handles \" and \\ */
1739 g_strescape (const gchar *source,
1740 const gchar *exceptions)
1747 g_return_val_if_fail (source != NULL, NULL);
1749 p = (guchar *) source;
1750 /* Each source byte needs maximally four destination chars (\777) */
1751 q = dest = g_malloc (strlen (source) * 4 + 1);
1753 memset (excmap, 0, 256);
1756 guchar *e = (guchar *) exceptions;
1802 if ((*p < ' ') || (*p >= 0177))
1805 *q++ = '0' + (((*p) >> 6) & 07);
1806 *q++ = '0' + (((*p) >> 3) & 07);
1807 *q++ = '0' + ((*p) & 07);
1821 g_strchug (gchar *string)
1825 g_return_val_if_fail (string != NULL, NULL);
1827 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
1830 g_memmove (string, start, strlen ((gchar *) start) + 1);
1836 g_strchomp (gchar *string)
1840 g_return_val_if_fail (string != NULL, NULL);
1845 for (s = string + strlen (string) - 1; s >= string && g_ascii_isspace ((guchar)*s);
1854 * @string: a string to split.
1855 * @delimiter: a string which specifies the places at which to split the string.
1856 * The delimiter is not included in any of the resulting strings, unless
1857 * @max_tokens is reached.
1858 * @max_tokens: the maximum number of pieces to split @string into. If this is
1859 * less than 1, the string is split completely.
1861 * Splits a string into a maximum of @max_tokens pieces, using the given
1862 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
1863 * to the last token.
1865 * As a special case, the result of splitting the empty string "" is an empty
1866 * vector, not a vector containing a single string. The reason for this
1867 * special case is that being able to represent a empty vector is typically
1868 * more useful than consistent handling of empty elements. If you do need
1869 * to represent empty elements, you'll need to check for the empty string
1870 * before calling g_strsplit().
1872 * Return value: a newly-allocated %NULL-terminated array of strings. Use
1873 * g_strfreev() to free it.
1876 g_strsplit (const gchar *string,
1877 const gchar *delimiter,
1880 GSList *string_list = NULL, *slist;
1881 gchar **str_array, *s;
1883 const gchar *remainder;
1885 g_return_val_if_fail (string != NULL, NULL);
1886 g_return_val_if_fail (delimiter != NULL, NULL);
1887 g_return_val_if_fail (delimiter[0] != '\0', NULL);
1890 max_tokens = G_MAXINT;
1893 s = strstr (remainder, delimiter);
1896 gsize delimiter_len = strlen (delimiter);
1898 while (--max_tokens && s)
1903 len = s - remainder;
1904 new_string = g_new (gchar, len + 1);
1905 strncpy (new_string, remainder, len);
1906 new_string[len] = 0;
1907 string_list = g_slist_prepend (string_list, new_string);
1909 remainder = s + delimiter_len;
1910 s = strstr (remainder, delimiter);
1916 string_list = g_slist_prepend (string_list, g_strdup (remainder));
1919 str_array = g_new (gchar*, n + 1);
1921 str_array[n--] = NULL;
1922 for (slist = string_list; slist; slist = slist->next)
1923 str_array[n--] = slist->data;
1925 g_slist_free (string_list);
1931 g_strfreev (gchar **str_array)
1937 for(i = 0; str_array[i] != NULL; i++)
1938 g_free(str_array[i]);
1946 * @str_array: %NULL-terminated array of strings.
1948 * Copies %NULL-terminated array of strings. The copy is a deep copy;
1949 * the new array should be freed by first freeing each string, then
1950 * the array itself. g_strfreev() does this for you. If called
1951 * on a %NULL value, g_strdupv() simply returns %NULL.
1953 * Return value: a new %NULL-terminated array of strings.
1956 g_strdupv (gchar **str_array)
1964 while (str_array[i])
1967 retval = g_new (gchar*, i + 1);
1970 while (str_array[i])
1972 retval[i] = g_strdup (str_array[i]);
1984 g_strjoinv (const gchar *separator,
1990 g_return_val_if_fail (str_array != NULL, NULL);
1992 if (separator == NULL)
1999 gsize separator_len;
2001 separator_len = strlen (separator);
2002 /* First part, getting length */
2003 len = 1 + strlen (str_array[0]);
2004 for (i = 1; str_array[i] != NULL; i++)
2005 len += strlen (str_array[i]);
2006 len += separator_len * (i - 1);
2008 /* Second part, building string */
2009 string = g_new (gchar, len);
2010 ptr = g_stpcpy (string, *str_array);
2011 for (i = 1; str_array[i] != NULL; i++)
2013 ptr = g_stpcpy (ptr, separator);
2014 ptr = g_stpcpy (ptr, str_array[i]);
2018 string = g_strdup ("");
2024 g_strjoin (const gchar *separator,
2030 gsize separator_len;
2033 if (separator == NULL)
2036 separator_len = strlen (separator);
2038 va_start (args, separator);
2040 s = va_arg (args, gchar*);
2044 /* First part, getting length */
2045 len = 1 + strlen (s);
2047 s = va_arg (args, gchar*);
2050 len += separator_len + strlen (s);
2051 s = va_arg (args, gchar*);
2055 /* Second part, building string */
2056 string = g_new (gchar, len);
2058 va_start (args, separator);
2060 s = va_arg (args, gchar*);
2061 ptr = g_stpcpy (string, s);
2063 s = va_arg (args, gchar*);
2066 ptr = g_stpcpy (ptr, separator);
2067 ptr = g_stpcpy (ptr, s);
2068 s = va_arg (args, gchar*);
2072 string = g_strdup ("");
2082 * @haystack: a string.
2083 * @haystack_len: the maximum length of @haystack.
2084 * @needle: the string to search for.
2086 * Searches the string @haystack for the first occurrence
2087 * of the string @needle, limiting the length of the search
2090 * Return value: a pointer to the found occurrence, or
2091 * %NULL if not found.
2094 g_strstr_len (const gchar *haystack,
2095 gssize haystack_len,
2096 const gchar *needle)
2098 g_return_val_if_fail (haystack != NULL, NULL);
2099 g_return_val_if_fail (needle != NULL, NULL);
2101 if (haystack_len < 0)
2102 return strstr (haystack, needle);
2105 const gchar *p = haystack;
2106 gsize needle_len = strlen (needle);
2110 if (needle_len == 0)
2111 return (gchar *)haystack;
2113 if (haystack_len < needle_len)
2116 end = haystack + haystack_len - needle_len;
2118 while (*p && p <= end)
2120 for (i = 0; i < needle_len; i++)
2121 if (p[i] != needle[i])
2136 * @haystack: a nul-terminated string.
2137 * @needle: the nul-terminated string to search for.
2139 * Searches the string @haystack for the last occurrence
2140 * of the string @needle.
2142 * Return value: a pointer to the found occurrence, or
2143 * %NULL if not found.
2146 g_strrstr (const gchar *haystack,
2147 const gchar *needle)
2154 g_return_val_if_fail (haystack != NULL, NULL);
2155 g_return_val_if_fail (needle != NULL, NULL);
2157 needle_len = strlen (needle);
2158 haystack_len = strlen (haystack);
2160 if (needle_len == 0)
2161 return (gchar *)haystack;
2163 if (haystack_len < needle_len)
2166 p = haystack + haystack_len - needle_len;
2168 while (p >= haystack)
2170 for (i = 0; i < needle_len; i++)
2171 if (p[i] != needle[i])
2185 * @haystack: a nul-terminated string.
2186 * @haystack_len: the maximum length of @haystack.
2187 * @needle: the nul-terminated string to search for.
2189 * Searches the string @haystack for the last occurrence
2190 * of the string @needle, limiting the length of the search
2193 * Return value: a pointer to the found occurrence, or
2194 * %NULL if not found.
2197 g_strrstr_len (const gchar *haystack,
2198 gssize haystack_len,
2199 const gchar *needle)
2201 g_return_val_if_fail (haystack != NULL, NULL);
2202 g_return_val_if_fail (needle != NULL, NULL);
2204 if (haystack_len < 0)
2205 return g_strrstr (haystack, needle);
2208 gsize needle_len = strlen (needle);
2209 const gchar *haystack_max = haystack + haystack_len;
2210 const gchar *p = haystack;
2213 while (p < haystack_max && *p)
2216 if (p < haystack + needle_len)
2221 while (p >= haystack)
2223 for (i = 0; i < needle_len; i++)
2224 if (p[i] != needle[i])