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.
24 #include <ctype.h> /* For tolower() */
27 const guint glib_major_version = GLIB_MAJOR_VERSION;
28 const guint glib_minor_version = GLIB_MINOR_VERSION;
29 const guint glib_micro_version = GLIB_MICRO_VERSION;
31 extern char* g_vsprintf (const gchar *fmt, va_list *args, va_list *args2);
34 g_strdup (const gchar *str)
41 new_str = g_new (char, strlen (str) + 1);
42 strcpy (new_str, str);
49 g_strconcat (const gchar *string1, ...)
56 g_return_val_if_fail (string1 != NULL, NULL);
58 l = 1 + strlen (string1);
59 va_start (args, string1);
60 s = va_arg (args, gchar*);
64 s = va_arg (args, gchar*);
68 concat = g_new (gchar, l);
71 strcat (concat, string1);
72 va_start (args, string1);
73 s = va_arg (args, gchar*);
77 s = va_arg (args, gchar*);
85 g_strtod (const gchar *nptr,
93 g_return_val_if_fail (nptr != NULL, 0);
98 val_1 = strtod (nptr, &fail_pos_1);
100 if (fail_pos_1 && fail_pos_1[0] != 0)
104 old_locale = setlocale (LC_NUMERIC, "C");
105 val_2 = strtod (nptr, &fail_pos_2);
106 setlocale (LC_NUMERIC, old_locale);
109 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
112 *endptr = fail_pos_1;
118 *endptr = fail_pos_2;
124 g_strerror (gint errnum)
129 return strerror (errnum);
134 case E2BIG: return "argument list too long";
137 case EACCES: return "permission denied";
140 case EADDRINUSE: return "address already in use";
143 case EADDRNOTAVAIL: return "can't assign requested address";
146 case EADV: return "advertise error";
149 case EAFNOSUPPORT: return "address family not supported by protocol family";
152 case EAGAIN: return "try again";
155 case EALIGN: return "EALIGN";
158 case EALREADY: return "operation already in progress";
161 case EBADE: return "bad exchange descriptor";
164 case EBADF: return "bad file number";
167 case EBADFD: return "file descriptor in bad state";
170 case EBADMSG: return "not a data message";
173 case EBADR: return "bad request descriptor";
176 case EBADRPC: return "RPC structure is bad";
179 case EBADRQC: return "bad request code";
182 case EBADSLT: return "invalid slot";
185 case EBFONT: return "bad font file format";
188 case EBUSY: return "mount device busy";
191 case ECHILD: return "no children";
194 case ECHRNG: return "channel number out of range";
197 case ECOMM: return "communication error on send";
200 case ECONNABORTED: return "software caused connection abort";
203 case ECONNREFUSED: return "connection refused";
206 case ECONNRESET: return "connection reset by peer";
208 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
209 case EDEADLK: return "resource deadlock avoided";
212 case EDEADLOCK: return "resource deadlock avoided";
215 case EDESTADDRREQ: return "destination address required";
218 case EDIRTY: return "mounting a dirty fs w/o force";
221 case EDOM: return "math argument out of range";
224 case EDOTDOT: return "cross mount point";
227 case EDQUOT: return "disk quota exceeded";
230 case EDUPPKG: return "duplicate package name";
233 case EEXIST: return "file already exists";
236 case EFAULT: return "bad address in system call argument";
239 case EFBIG: return "file too large";
242 case EHOSTDOWN: return "host is down";
245 case EHOSTUNREACH: return "host is unreachable";
248 case EIDRM: return "identifier removed";
251 case EINIT: return "initialization error";
254 case EINPROGRESS: return "operation now in progress";
257 case EINTR: return "interrupted system call";
260 case EINVAL: return "invalid argument";
263 case EIO: return "I/O error";
266 case EISCONN: return "socket is already connected";
269 case EISDIR: return "illegal operation on a directory";
272 case EISNAM: return "is a name file";
275 case ELBIN: return "ELBIN";
278 case EL2HLT: return "level 2 halted";
281 case EL2NSYNC: return "level 2 not synchronized";
284 case EL3HLT: return "level 3 halted";
287 case EL3RST: return "level 3 reset";
290 case ELIBACC: return "can not access a needed shared library";
293 case ELIBBAD: return "accessing a corrupted shared library";
296 case ELIBEXEC: return "can not exec a shared library directly";
299 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
302 case ELIBSCN: return ".lib section in a.out corrupted";
305 case ELNRNG: return "link number out of range";
308 case ELOOP: return "too many levels of symbolic links";
311 case EMFILE: return "too many open files";
314 case EMLINK: return "too many links";
317 case EMSGSIZE: return "message too long";
320 case EMULTIHOP: return "multihop attempted";
323 case ENAMETOOLONG: return "file name too long";
326 case ENAVAIL: return "not available";
329 case ENET: return "ENET";
332 case ENETDOWN: return "network is down";
335 case ENETRESET: return "network dropped connection on reset";
338 case ENETUNREACH: return "network is unreachable";
341 case ENFILE: return "file table overflow";
344 case ENOANO: return "anode table overflow";
346 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
347 case ENOBUFS: return "no buffer space available";
350 case ENOCSI: return "no CSI structure available";
353 case ENODATA: return "no data available";
356 case ENODEV: return "no such device";
359 case ENOENT: return "no such file or directory";
362 case ENOEXEC: return "exec format error";
365 case ENOLCK: return "no locks available";
368 case ENOLINK: return "link has be severed";
371 case ENOMEM: return "not enough memory";
374 case ENOMSG: return "no message of desired type";
377 case ENONET: return "machine is not on the network";
380 case ENOPKG: return "package not installed";
383 case ENOPROTOOPT: return "bad proocol option";
386 case ENOSPC: return "no space left on device";
389 case ENOSR: return "out of stream resources";
392 case ENOSTR: return "not a stream device";
395 case ENOSYM: return "unresolved symbol name";
398 case ENOSYS: return "function not implemented";
401 case ENOTBLK: return "block device required";
404 case ENOTCONN: return "socket is not connected";
407 case ENOTDIR: return "not a directory";
410 case ENOTEMPTY: return "directory not empty";
413 case ENOTNAM: return "not a name file";
416 case ENOTSOCK: return "socket operation on non-socket";
419 case ENOTTY: return "inappropriate device for ioctl";
422 case ENOTUNIQ: return "name not unique on network";
425 case ENXIO: return "no such device or address";
428 case EOPNOTSUPP: return "operation not supported on socket";
431 case EPERM: return "not owner";
434 case EPFNOSUPPORT: return "protocol family not supported";
437 case EPIPE: return "broken pipe";
440 case EPROCLIM: return "too many processes";
443 case EPROCUNAVAIL: return "bad procedure for program";
446 case EPROGMISMATCH: return "program version wrong";
449 case EPROGUNAVAIL: return "RPC program not available";
452 case EPROTO: return "protocol error";
454 #ifdef EPROTONOSUPPORT
455 case EPROTONOSUPPORT: return "protocol not suppored";
458 case EPROTOTYPE: return "protocol wrong type for socket";
461 case ERANGE: return "math result unrepresentable";
463 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
464 case EREFUSED: return "EREFUSED";
467 case EREMCHG: return "remote address changed";
470 case EREMDEV: return "remote device";
473 case EREMOTE: return "pathname hit remote file system";
476 case EREMOTEIO: return "remote i/o error";
478 #ifdef EREMOTERELEASE
479 case EREMOTERELEASE: return "EREMOTERELEASE";
482 case EROFS: return "read-only file system";
485 case ERPCMISMATCH: return "RPC version is wrong";
488 case ERREMOTE: return "object is remote";
491 case ESHUTDOWN: return "can't send afer socket shutdown";
493 #ifdef ESOCKTNOSUPPORT
494 case ESOCKTNOSUPPORT: return "socket type not supported";
497 case ESPIPE: return "invalid seek";
500 case ESRCH: return "no such process";
503 case ESRMNT: return "srmount error";
506 case ESTALE: return "stale remote file handle";
509 case ESUCCESS: return "Error 0";
512 case ETIME: return "timer expired";
515 case ETIMEDOUT: return "connection timed out";
518 case ETOOMANYREFS: return "too many references: can't splice";
521 case ETXTBSY: return "text file or pseudo-device busy";
524 case EUCLEAN: return "structure needs cleaning";
527 case EUNATCH: return "protocol driver not attached";
530 case EUSERS: return "too many users";
533 case EVERSION: return "version mismatch";
535 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
536 case EWOULDBLOCK: return "operation would block";
539 case EXDEV: return "cross-domain link";
542 case EXFULL: return "message tables full";
545 #else /* NO_SYS_ERRLIST */
547 extern char *sys_errlist[];
549 if ((errnum > 0) && (errnum <= sys_nerr))
550 return sys_errlist [errnum];
551 #endif /* NO_SYS_ERRLIST */
553 sprintf (msg, "unknown error (%d)", errnum);
558 g_strsignal (gint signum)
562 #ifdef HAVE_STRSIGNAL
563 extern char *strsignal (int sig);
564 return strsignal (signum);
569 case SIGHUP: return "Hangup";
572 case SIGINT: return "Interrupt";
575 case SIGQUIT: return "Quit";
578 case SIGILL: return "Illegal instruction";
581 case SIGTRAP: return "Trace/breakpoint trap";
584 case SIGABRT: return "IOT trap/Abort";
587 case SIGBUS: return "Bus error";
590 case SIGFPE: return "Floating point exception";
593 case SIGKILL: return "Killed";
596 case SIGUSR1: return "User defined signal 1";
599 case SIGSEGV: return "Segmentation fault";
602 case SIGUSR2: return "User defined signal 2";
605 case SIGPIPE: return "Broken pipe";
608 case SIGALRM: return "Alarm clock";
611 case SIGTERM: return "Terminated";
614 case SIGSTKFLT: return "Stack fault";
617 case SIGCHLD: return "Child exited";
620 case SIGCONT: return "Continued";
623 case SIGSTOP: return "Stopped (signal)";
626 case SIGTSTP: return "Stopped";
629 case SIGTTIN: return "Stopped (tty input)";
632 case SIGTTOU: return "Stopped (tty output)";
635 case SIGURG: return "Urgent condition";
638 case SIGXCPU: return "CPU time limit exceeded";
641 case SIGXFSZ: return "File size limit exceeded";
644 case SIGVTALRM: return "Virtual time alarm";
647 case SIGPROF: return "Profile signal";
650 case SIGWINCH: return "Window size changed";
653 case SIGIO: return "Possible I/O";
656 case SIGPWR: return "Power failure";
659 case SIGUNUSED: return "Unused signal";
662 #else /* NO_SYS_SIGLIST */
663 extern char *sys_siglist[];
664 return sys_siglist [signum];
665 #endif /* NO_SYS_SIGLIST */
667 sprintf (msg, "unknown signal (%d)", signum);
672 g_snprintf (gchar *str,
677 #ifdef HAVE_VSNPRINTF
681 va_start (args, fmt);
682 retval = vsnprintf (str, n, fmt, args);
691 va_start (args, fmt);
692 va_start (args2, fmt);
694 printed = g_vsprintf (fmt, &args, &args2);
695 strncpy (str, printed, n);
707 g_strdown (gchar *string)
711 g_return_if_fail (string != NULL);
723 g_strup (gchar *string)
727 g_return_if_fail (string != NULL);
739 g_strreverse (gchar *string)
741 g_return_if_fail (string != NULL);
745 register gchar *h, *t;
748 t = string + strlen (string) - 1;
764 g_strcasecmp (const gchar *s1,
767 #ifdef HAVE_STRCASECMP
768 return strcasecmp (s1, s2);
774 /* According to A. Cox, some platforms have islower's that
775 * don't work right on non-uppercase
777 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
778 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
784 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
789 g_strdelimit (gchar *string,
790 const gchar *delimiters,
795 g_return_if_fail (string != NULL);
798 delimiters = G_STR_DELIMITERS;
800 for (c = string; *c; c++)
802 if (strchr (delimiters, *c))
808 g_parse_debug_string (const gchar *string,
815 g_return_val_if_fail (string != NULL, 0);
817 if (!g_strcasecmp (string, "all"))
819 for (i=0; i<nkeys; i++)
820 result |= keys[i].value;
824 gchar *str = g_strdup (string);
827 gboolean done = FALSE;
840 for (i=0; i<nkeys; i++)
841 if (!g_strcasecmp(keys[i].key, p))
842 result |= keys[i].value;
854 g_direct_hash(gconstpointer v)
856 return GPOINTER_TO_UINT (v);
860 g_direct_equal(gconstpointer v, gconstpointer v2)
862 return GPOINTER_TO_UINT (v) == GPOINTER_TO_UINT (v2);
866 g_int_equal (gconstpointer v, gconstpointer v2)
868 return *((const gint*) v) == *((const gint*) v2);
872 g_int_hash (gconstpointer v)
874 return *(const gint*) v;