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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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/.
41 #include <ctype.h> /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
46 /* do not include <unistd.h> in this place since it
47 * inteferes with g_strsignal() on some OSes
50 #define G_GNUC_PRETTY_FUNCTION
52 typedef union _GDoubleIEEE754 GDoubleIEEE754
;
53 #define G_IEEE754_DOUBLE_BIAS (1023)
54 /* multiply with base2 exponent to get base10 exponent (nomal numbers) */
55 #define G_LOG_2_BASE_10 (0.30102999566398119521)
56 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
61 guint mantissa_low
: 32;
62 guint mantissa_high
: 20;
63 guint biased_exponent
: 11;
67 #elif G_BYTE_ORDER == G_BIG_ENDIAN
73 guint biased_exponent
: 11;
74 guint mantissa_high
: 20;
75 guint mantissa_low
: 32;
78 #else /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
79 #error unknown ENDIAN type
80 #endif /* !G_LITTLE_ENDIAN && !G_BIG_ENDIAN */
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 (guint length
,
137 register gchar
*str
, *s
, *end
;
139 str
= g_new (gchar
, length
+ 1);
150 g_strdup_vprintf (const gchar
*format
,
156 G_VA_COPY (args2
, args1
);
158 buffer
= g_new (gchar
, g_printf_string_upper_bound (format
, args1
));
160 vsprintf (buffer
, format
, args2
);
167 g_strdup_printf (const gchar
*format
,
173 va_start (args
, format
);
174 buffer
= g_strdup_vprintf (format
, args
);
181 g_strconcat (const gchar
*string1
, ...)
188 g_return_val_if_fail (string1
!= NULL
, NULL
);
190 l
= 1 + strlen (string1
);
191 va_start (args
, string1
);
192 s
= va_arg (args
, gchar
*);
196 s
= va_arg (args
, gchar
*);
200 concat
= g_new (gchar
, l
);
203 strcat (concat
, string1
);
204 va_start (args
, string1
);
205 s
= va_arg (args
, gchar
*);
209 s
= va_arg (args
, gchar
*);
217 g_strtod (const gchar
*nptr
,
225 g_return_val_if_fail (nptr
!= NULL
, 0);
230 val_1
= strtod (nptr
, &fail_pos_1
);
232 if (fail_pos_1
&& fail_pos_1
[0] != 0)
236 old_locale
= g_strdup (setlocale (LC_NUMERIC
, NULL
));
237 setlocale (LC_NUMERIC
, "C");
238 val_2
= strtod (nptr
, &fail_pos_2
);
239 setlocale (LC_NUMERIC
, old_locale
);
243 if (!fail_pos_1
|| fail_pos_1
[0] == 0 || fail_pos_1
>= fail_pos_2
)
246 *endptr
= fail_pos_1
;
252 *endptr
= fail_pos_2
;
258 g_strerror (gint errnum
)
260 static GStaticPrivate msg_private
= G_STATIC_PRIVATE_INIT
;
264 return strerror (errnum
);
269 case E2BIG
: return "argument list too long";
272 case EACCES
: return "permission denied";
275 case EADDRINUSE
: return "address already in use";
278 case EADDRNOTAVAIL
: return "can't assign requested address";
281 case EADV
: return "advertise error";
284 case EAFNOSUPPORT
: return "address family not supported by protocol family";
287 case EAGAIN
: return "try again";
290 case EALIGN
: return "EALIGN";
293 case EALREADY
: return "operation already in progress";
296 case EBADE
: return "bad exchange descriptor";
299 case EBADF
: return "bad file number";
302 case EBADFD
: return "file descriptor in bad state";
305 case EBADMSG
: return "not a data message";
308 case EBADR
: return "bad request descriptor";
311 case EBADRPC
: return "RPC structure is bad";
314 case EBADRQC
: return "bad request code";
317 case EBADSLT
: return "invalid slot";
320 case EBFONT
: return "bad font file format";
323 case EBUSY
: return "mount device busy";
326 case ECHILD
: return "no children";
329 case ECHRNG
: return "channel number out of range";
332 case ECOMM
: return "communication error on send";
335 case ECONNABORTED
: return "software caused connection abort";
338 case ECONNREFUSED
: return "connection refused";
341 case ECONNRESET
: return "connection reset by peer";
343 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
344 case EDEADLK
: return "resource deadlock avoided";
347 case EDEADLOCK
: return "resource deadlock avoided";
350 case EDESTADDRREQ
: return "destination address required";
353 case EDIRTY
: return "mounting a dirty fs w/o force";
356 case EDOM
: return "math argument out of range";
359 case EDOTDOT
: return "cross mount point";
362 case EDQUOT
: return "disk quota exceeded";
365 case EDUPPKG
: return "duplicate package name";
368 case EEXIST
: return "file already exists";
371 case EFAULT
: return "bad address in system call argument";
374 case EFBIG
: return "file too large";
377 case EHOSTDOWN
: return "host is down";
380 case EHOSTUNREACH
: return "host is unreachable";
383 case EIDRM
: return "identifier removed";
386 case EINIT
: return "initialization error";
389 case EINPROGRESS
: return "operation now in progress";
392 case EINTR
: return "interrupted system call";
395 case EINVAL
: return "invalid argument";
398 case EIO
: return "I/O error";
401 case EISCONN
: return "socket is already connected";
404 case EISDIR
: return "illegal operation on a directory";
407 case EISNAM
: return "is a name file";
410 case ELBIN
: return "ELBIN";
413 case EL2HLT
: return "level 2 halted";
416 case EL2NSYNC
: return "level 2 not synchronized";
419 case EL3HLT
: return "level 3 halted";
422 case EL3RST
: return "level 3 reset";
425 case ELIBACC
: return "can not access a needed shared library";
428 case ELIBBAD
: return "accessing a corrupted shared library";
431 case ELIBEXEC
: return "can not exec a shared library directly";
434 case ELIBMAX
: return "attempting to link in more shared libraries than system limit";
437 case ELIBSCN
: return ".lib section in a.out corrupted";
440 case ELNRNG
: return "link number out of range";
443 case ELOOP
: return "too many levels of symbolic links";
446 case EMFILE
: return "too many open files";
449 case EMLINK
: return "too many links";
452 case EMSGSIZE
: return "message too long";
455 case EMULTIHOP
: return "multihop attempted";
458 case ENAMETOOLONG
: return "file name too long";
461 case ENAVAIL
: return "not available";
464 case ENET
: return "ENET";
467 case ENETDOWN
: return "network is down";
470 case ENETRESET
: return "network dropped connection on reset";
473 case ENETUNREACH
: return "network is unreachable";
476 case ENFILE
: return "file table overflow";
479 case ENOANO
: return "anode table overflow";
481 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
482 case ENOBUFS
: return "no buffer space available";
485 case ENOCSI
: return "no CSI structure available";
488 case ENODATA
: return "no data available";
491 case ENODEV
: return "no such device";
494 case ENOENT
: return "no such file or directory";
497 case ENOEXEC
: return "exec format error";
500 case ENOLCK
: return "no locks available";
503 case ENOLINK
: return "link has be severed";
506 case ENOMEM
: return "not enough memory";
509 case ENOMSG
: return "no message of desired type";
512 case ENONET
: return "machine is not on the network";
515 case ENOPKG
: return "package not installed";
518 case ENOPROTOOPT
: return "bad proocol option";
521 case ENOSPC
: return "no space left on device";
524 case ENOSR
: return "out of stream resources";
527 case ENOSTR
: return "not a stream device";
530 case ENOSYM
: return "unresolved symbol name";
533 case ENOSYS
: return "function not implemented";
536 case ENOTBLK
: return "block device required";
539 case ENOTCONN
: return "socket is not connected";
542 case ENOTDIR
: return "not a directory";
545 case ENOTEMPTY
: return "directory not empty";
548 case ENOTNAM
: return "not a name file";
551 case ENOTSOCK
: return "socket operation on non-socket";
554 case ENOTTY
: return "inappropriate device for ioctl";
557 case ENOTUNIQ
: return "name not unique on network";
560 case ENXIO
: return "no such device or address";
563 case EOPNOTSUPP
: return "operation not supported on socket";
566 case EPERM
: return "not owner";
569 case EPFNOSUPPORT
: return "protocol family not supported";
572 case EPIPE
: return "broken pipe";
575 case EPROCLIM
: return "too many processes";
578 case EPROCUNAVAIL
: return "bad procedure for program";
581 case EPROGMISMATCH
: return "program version wrong";
584 case EPROGUNAVAIL
: return "RPC program not available";
587 case EPROTO
: return "protocol error";
589 #ifdef EPROTONOSUPPORT
590 case EPROTONOSUPPORT
: return "protocol not suppored";
593 case EPROTOTYPE
: return "protocol wrong type for socket";
596 case ERANGE
: return "math result unrepresentable";
598 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
599 case EREFUSED
: return "EREFUSED";
602 case EREMCHG
: return "remote address changed";
605 case EREMDEV
: return "remote device";
608 case EREMOTE
: return "pathname hit remote file system";
611 case EREMOTEIO
: return "remote i/o error";
613 #ifdef EREMOTERELEASE
614 case EREMOTERELEASE
: return "EREMOTERELEASE";
617 case EROFS
: return "read-only file system";
620 case ERPCMISMATCH
: return "RPC version is wrong";
623 case ERREMOTE
: return "object is remote";
626 case ESHUTDOWN
: return "can't send afer socket shutdown";
628 #ifdef ESOCKTNOSUPPORT
629 case ESOCKTNOSUPPORT
: return "socket type not supported";
632 case ESPIPE
: return "invalid seek";
635 case ESRCH
: return "no such process";
638 case ESRMNT
: return "srmount error";
641 case ESTALE
: return "stale remote file handle";
644 case ESUCCESS
: return "Error 0";
647 case ETIME
: return "timer expired";
650 case ETIMEDOUT
: return "connection timed out";
653 case ETOOMANYREFS
: return "too many references: can't splice";
656 case ETXTBSY
: return "text file or pseudo-device busy";
659 case EUCLEAN
: return "structure needs cleaning";
662 case EUNATCH
: return "protocol driver not attached";
665 case EUSERS
: return "too many users";
668 case EVERSION
: return "version mismatch";
670 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
671 case EWOULDBLOCK
: return "operation would block";
674 case EXDEV
: return "cross-domain link";
677 case EXFULL
: return "message tables full";
680 #else /* NO_SYS_ERRLIST */
682 extern char *sys_errlist
[];
684 if ((errnum
> 0) && (errnum
<= sys_nerr
))
685 return sys_errlist
[errnum
];
686 #endif /* NO_SYS_ERRLIST */
688 msg
= g_static_private_get (&msg_private
);
691 msg
= g_new (gchar
, 64);
692 g_static_private_set (&msg_private
, msg
, g_free
);
695 sprintf (msg
, "unknown error (%d)", errnum
);
701 g_strsignal (gint signum
)
703 static GStaticPrivate msg_private
= G_STATIC_PRIVATE_INIT
;
706 #ifdef HAVE_STRSIGNAL
707 extern char *strsignal (int sig
);
708 return strsignal (signum
);
713 case SIGHUP
: return "Hangup";
716 case SIGINT
: return "Interrupt";
719 case SIGQUIT
: return "Quit";
722 case SIGILL
: return "Illegal instruction";
725 case SIGTRAP
: return "Trace/breakpoint trap";
728 case SIGABRT
: return "IOT trap/Abort";
731 case SIGBUS
: return "Bus error";
734 case SIGFPE
: return "Floating point exception";
737 case SIGKILL
: return "Killed";
740 case SIGUSR1
: return "User defined signal 1";
743 case SIGSEGV
: return "Segmentation fault";
746 case SIGUSR2
: return "User defined signal 2";
749 case SIGPIPE
: return "Broken pipe";
752 case SIGALRM
: return "Alarm clock";
755 case SIGTERM
: return "Terminated";
758 case SIGSTKFLT
: return "Stack fault";
761 case SIGCHLD
: return "Child exited";
764 case SIGCONT
: return "Continued";
767 case SIGSTOP
: return "Stopped (signal)";
770 case SIGTSTP
: return "Stopped";
773 case SIGTTIN
: return "Stopped (tty input)";
776 case SIGTTOU
: return "Stopped (tty output)";
779 case SIGURG
: return "Urgent condition";
782 case SIGXCPU
: return "CPU time limit exceeded";
785 case SIGXFSZ
: return "File size limit exceeded";
788 case SIGVTALRM
: return "Virtual time alarm";
791 case SIGPROF
: return "Profile signal";
794 case SIGWINCH
: return "Window size changed";
797 case SIGIO
: return "Possible I/O";
800 case SIGPWR
: return "Power failure";
803 case SIGUNUSED
: return "Unused signal";
806 #else /* NO_SYS_SIGLIST */
808 #ifdef NO_SYS_SIGLIST_DECL
809 extern char *sys_siglist
[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
812 return (char*) /* this function should return const --josh */ sys_siglist
[signum
];
813 #endif /* NO_SYS_SIGLIST */
815 msg
= g_static_private_get (&msg_private
);
818 msg
= g_new (gchar
, 64);
819 g_static_private_set (&msg_private
, msg
, g_free
);
822 sprintf (msg
, "unknown signal (%d)", signum
);
831 gboolean alternate_format
, zero_padding
, adjust_left
, locale_grouping
;
832 gboolean add_space
, add_sign
, possible_sign
, seen_precision
;
833 gboolean mod_half
, mod_long
, mod_extra_long
;
837 g_printf_string_upper_bound (const gchar
* format
,
840 static const gboolean honour_longs
= SIZEOF_LONG
> 4 || SIZEOF_VOID_P
> 4;
848 register gchar c
= *format
++;
852 else /* (c == '%') */
854 PrintfArgSpec spec
= { 0, };
855 gboolean seen_l
= FALSE
, conv_done
= FALSE
;
857 const gchar
*spec_start
= format
;
864 GDoubleIEEE754 u_double
;
867 const gchar
*v_string
;
869 /* beware of positional parameters
872 g_warning (G_GNUC_PRETTY_FUNCTION
873 "(): unable to handle positional parameters (%%n$)");
874 len
+= 1024; /* try adding some safety padding */
880 spec
.alternate_format
= TRUE
;
883 spec
.zero_padding
= TRUE
;
886 spec
.adjust_left
= TRUE
;
889 spec
.add_space
= TRUE
;
892 spec
.add_sign
= TRUE
;
895 spec
.locale_grouping
= TRUE
;
898 /* parse output size specifications
901 spec
.seen_precision
= TRUE
;
914 while (c
>= '0' && c
<= '9')
917 v_uint
= v_uint
* 10 + c
- '0';
920 if (spec
.seen_precision
)
921 spec
.precision
= MAX (spec
.precision
, v_uint
);
923 spec
.min_width
= MAX (spec
.min_width
, v_uint
);
926 v_int
= va_arg (args
, int);
927 if (spec
.seen_precision
)
929 /* forget about negative precision */
931 spec
.precision
= MAX (spec
.precision
, v_int
);
938 spec
.adjust_left
= TRUE
;
940 spec
.min_width
= MAX (spec
.min_width
, v_int
);
944 /* parse type modifiers
947 spec
.mod_half
= TRUE
;
952 spec
.mod_long
= TRUE
;
956 /* else, fall through */
959 spec
.mod_long
= TRUE
;
960 spec
.mod_extra_long
= TRUE
;
964 #if GLIB_SIZEOF_SIZE_T > 4
965 spec
.mod_long
= TRUE
;
966 spec
.mod_extra_long
= TRUE
;
967 #endif /* GLIB_SIZEOF_SIZE_T > 4 */
970 #if GLIB_SIZEOF_PTRDIFF_T > 4
971 spec
.mod_long
= TRUE
;
972 spec
.mod_extra_long
= TRUE
;
973 #endif /* GLIB_SIZEOF_PTRDIFF_T > 4 */
976 #if GLIB_SIZEOF_INTMAX_T > 4
977 spec
.mod_long
= TRUE
;
978 spec
.mod_extra_long
= TRUE
;
979 #endif /* GLIB_SIZEOF_INTMAX_T > 4 */
982 /* parse output conversions
991 /* some C libraries feature long variants for these as well? */
992 spec
.mod_long
= TRUE
;
999 conv_len
+= 1; /* sign */
1006 spec
.possible_sign
= TRUE
;
1008 if (spec
.mod_long
&& honour_longs
)
1010 if (spec
.mod_extra_long
)
1012 if (spec
.mod_extra_long
)
1014 #ifdef G_HAVE_GINT64
1015 (void) va_arg (args
, gint64
);
1016 #else /* !G_HAVE_GINT64 */
1017 (void) va_arg (args
, long);
1018 #endif /* !G_HAVE_GINT64 */
1020 else if (spec
.mod_long
)
1021 (void) va_arg (args
, long);
1023 (void) va_arg (args
, int);
1035 spec
.possible_sign
= TRUE
;
1036 /* n . dddddddddddddddddddddddd E +- eeee */
1037 conv_len
+= 1 + 1 + MAX (24, spec
.precision
) + 1 + 1 + 4;
1038 if (spec
.mod_extra_long
)
1039 g_warning (G_GNUC_PRETTY_FUNCTION
1040 "(): unable to handle long double, collecting double only");
1041 #ifdef HAVE_LONG_DOUBLE
1042 #error need to implement special handling for long double
1044 u_double
.v_double
= va_arg (args
, double);
1045 /* %f can expand up to all significant digits before '.' (308) */
1047 u_double
.mpn
.biased_exponent
> 0 && u_double
.mpn
.biased_exponent
< 2047)
1049 gint exp
= u_double
.mpn
.biased_exponent
;
1051 exp
-= G_IEEE754_DOUBLE_BIAS
;
1052 exp
= exp
* G_LOG_2_BASE_10
+ 1;
1055 /* some printf() implementations require extra padding for rounding */
1057 /* we can't really handle locale specific grouping here */
1058 if (spec
.locale_grouping
)
1062 spec
.mod_long
= TRUE
;
1065 conv_len
+= spec
.mod_long
? MB_LEN_MAX
: 1;
1066 (void) va_arg (args
, int);
1069 spec
.mod_long
= TRUE
;
1072 v_string
= va_arg (args
, char*);
1074 conv_len
+= 8; /* hold "(null)" */
1075 else if (spec
.seen_precision
)
1076 conv_len
+= spec
.precision
;
1078 conv_len
+= strlen (v_string
);
1082 g_warning (G_GNUC_PRETTY_FUNCTION
1083 "(): unable to handle wide char strings");
1084 len
+= 1024; /* try adding some safety padding */
1087 case 'P': /* do we actually need this? */
1090 spec
.alternate_format
= TRUE
;
1097 (void) va_arg (args
, void*);
1100 /* there's not much we can do to be clever */
1101 v_string
= g_strerror (errno
);
1102 v_uint
= v_string
? strlen (v_string
) : 0;
1103 conv_len
+= MAX (256, v_uint
);
1106 /* handle invalid cases
1109 /* no conversion specification, bad bad */
1110 conv_len
+= format
- spec_start
;
1113 g_warning (G_GNUC_PRETTY_FUNCTION
1114 "(): unable to handle `%c' while parsing format",
1118 conv_done
|= conv_len
> 0;
1121 /* handle width specifications */
1122 conv_len
= MAX (conv_len
, MAX (spec
.precision
, spec
.min_width
));
1124 conv_len
+= spec
.alternate_format
? 2 : 0;
1125 conv_len
+= (spec
.add_space
|| spec
.add_sign
|| spec
.possible_sign
);
1128 } /* else (c == '%') */
1129 } /* while (*format) */
1135 g_strdown (gchar
*string
)
1139 g_return_if_fail (string
!= NULL
);
1151 g_strup (gchar
*string
)
1155 g_return_if_fail (string
!= NULL
);
1167 g_strreverse (gchar
*string
)
1169 g_return_if_fail (string
!= NULL
);
1173 register gchar
*h
, *t
;
1176 t
= string
+ strlen (string
) - 1;
1192 g_strcasecmp (const gchar
*s1
,
1195 #ifdef HAVE_STRCASECMP
1196 g_return_val_if_fail (s1
!= NULL
, 0);
1197 g_return_val_if_fail (s2
!= NULL
, 0);
1199 return strcasecmp (s1
, s2
);
1203 g_return_val_if_fail (s1
!= NULL
, 0);
1204 g_return_val_if_fail (s2
!= NULL
, 0);
1208 /* According to A. Cox, some platforms have islower's that
1209 * don't work right on non-uppercase
1211 c1
= isupper ((guchar
)*s1
) ? tolower ((guchar
)*s1
) : *s1
;
1212 c2
= isupper ((guchar
)*s2
) ? tolower ((guchar
)*s2
) : *s2
;
1218 return (((gint
)(guchar
) *s1
) - ((gint
)(guchar
) *s2
));
1223 g_strncasecmp (const gchar
*s1
,
1227 #ifdef HAVE_STRNCASECMP
1228 return strncasecmp (s1
, s2
, n
);
1232 g_return_val_if_fail (s1
!= NULL
, 0);
1233 g_return_val_if_fail (s2
!= NULL
, 0);
1235 while (n
&& *s1
&& *s2
)
1238 /* According to A. Cox, some platforms have islower's that
1239 * don't work right on non-uppercase
1241 c1
= isupper ((guchar
)*s1
) ? tolower ((guchar
)*s1
) : *s1
;
1242 c2
= isupper ((guchar
)*s2
) ? tolower ((guchar
)*s2
) : *s2
;
1249 return (((gint
) (guchar
) *s1
) - ((gint
) (guchar
) *s2
));
1256 g_strdelimit (gchar
*string
,
1257 const gchar
*delimiters
,
1262 g_return_val_if_fail (string
!= NULL
, NULL
);
1265 delimiters
= G_STR_DELIMITERS
;
1267 for (c
= string
; *c
; c
++)
1269 if (strchr (delimiters
, *c
))
1277 g_strescape (gchar
*string
)
1281 guint backslashes
= 0;
1284 g_return_val_if_fail (string
!= NULL
, NULL
);
1286 while (*p
!= '\000')
1287 backslashes
+= (*p
++ == '\\');
1290 return g_strdup (string
);
1292 escaped
= g_new (gchar
, strlen (string
) + backslashes
+ 1);
1297 while (*p
!= '\000')
1308 /* blame Elliot for these next five routines */
1310 g_strchug (gchar
*string
)
1314 g_return_val_if_fail (string
!= NULL
, NULL
);
1316 for (start
= string
; *start
&& isspace (*start
); start
++)
1319 g_memmove(string
, start
, strlen(start
) + 1);
1325 g_strchomp (gchar
*string
)
1329 g_return_val_if_fail (string
!= NULL
, NULL
);
1334 for (s
= string
+ strlen (string
) - 1; s
>= string
&& isspace ((guchar
)*s
);
1342 g_strsplit (const gchar
*string
,
1343 const gchar
*delimiter
,
1346 GSList
*string_list
= NULL
, *slist
;
1347 gchar
**str_array
, *s
;
1350 g_return_val_if_fail (string
!= NULL
, NULL
);
1351 g_return_val_if_fail (delimiter
!= NULL
, NULL
);
1354 max_tokens
= G_MAXINT
;
1356 s
= strstr (string
, delimiter
);
1359 guint delimiter_len
= strlen (delimiter
);
1367 new_string
= g_new (gchar
, len
+ 1);
1368 strncpy (new_string
, string
, len
);
1369 new_string
[len
] = 0;
1370 string_list
= g_slist_prepend (string_list
, new_string
);
1372 string
= s
+ delimiter_len
;
1373 s
= strstr (string
, delimiter
);
1375 while (--max_tokens
&& s
);
1380 string_list
= g_slist_prepend (string_list
, g_strdup (string
));
1383 str_array
= g_new (gchar
*, n
);
1387 str_array
[i
--] = NULL
;
1388 for (slist
= string_list
; slist
; slist
= slist
->next
)
1389 str_array
[i
--] = slist
->data
;
1391 g_slist_free (string_list
);
1397 g_strfreev (gchar
**str_array
)
1403 for(i
= 0; str_array
[i
] != NULL
; i
++)
1404 g_free(str_array
[i
]);
1411 g_strjoinv (const gchar
*separator
,
1416 g_return_val_if_fail (str_array
!= NULL
, NULL
);
1418 if (separator
== NULL
)
1424 guint separator_len
;
1426 separator_len
= strlen (separator
);
1427 len
= 1 + strlen (str_array
[0]);
1428 for(i
= 1; str_array
[i
] != NULL
; i
++)
1429 len
+= separator_len
+ strlen(str_array
[i
]);
1431 string
= g_new (gchar
, len
);
1433 strcat (string
, *str_array
);
1434 for (i
= 1; str_array
[i
] != NULL
; i
++)
1436 strcat (string
, separator
);
1437 strcat (string
, str_array
[i
]);
1441 string
= g_strdup ("");
1447 g_strjoin (const gchar
*separator
,
1453 guint separator_len
;
1455 if (separator
== NULL
)
1458 separator_len
= strlen (separator
);
1460 va_start (args
, separator
);
1462 s
= va_arg (args
, gchar
*);
1468 s
= va_arg (args
, gchar
*);
1471 len
+= separator_len
+ strlen (s
);
1472 s
= va_arg (args
, gchar
*);
1476 string
= g_new (gchar
, len
+ 1);
1479 va_start (args
, separator
);
1481 s
= va_arg (args
, gchar
*);
1484 s
= va_arg (args
, gchar
*);
1487 strcat (string
, separator
);
1489 s
= va_arg (args
, gchar
*);
1493 string
= g_strdup ("");