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/.
40 #include <ctype.h> /* For tolower() */
41 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
50 /* do not include <unistd.h> in this place since it
51 * inteferes with g_strsignal() on some OSes
55 g_strdup (const gchar *str)
61 new_str = g_new (char, strlen (str) + 1);
62 strcpy (new_str, str);
71 g_memdup (gconstpointer mem,
78 new_mem = g_malloc (byte_size);
79 memcpy (new_mem, mem, byte_size);
88 g_strndup (const gchar *str,
95 new_str = g_new (gchar, n + 1);
96 strncpy (new_str, str, n);
106 g_strnfill (guint length,
109 register gchar *str, *s, *end;
111 str = g_new (gchar, length + 1);
122 g_strdup_vprintf (const gchar *format,
128 G_VA_COPY (args2, args1);
130 buffer = g_new (gchar, g_printf_string_upper_bound (format, args1));
132 vsprintf (buffer, format, args2);
139 g_strdup_printf (const gchar *format,
145 va_start (args, format);
146 buffer = g_strdup_vprintf (format, args);
153 g_strconcat (const gchar *string1, ...)
160 g_return_val_if_fail (string1 != NULL, NULL);
162 l = 1 + strlen (string1);
163 va_start (args, string1);
164 s = va_arg (args, gchar*);
168 s = va_arg (args, gchar*);
172 concat = g_new (gchar, l);
175 strcat (concat, string1);
176 va_start (args, string1);
177 s = va_arg (args, gchar*);
181 s = va_arg (args, gchar*);
189 g_strtod (const gchar *nptr,
197 g_return_val_if_fail (nptr != NULL, 0);
202 val_1 = strtod (nptr, &fail_pos_1);
204 if (fail_pos_1 && fail_pos_1[0] != 0)
208 old_locale = g_strdup (setlocale (LC_NUMERIC, NULL));
209 setlocale (LC_NUMERIC, "C");
210 val_2 = strtod (nptr, &fail_pos_2);
211 setlocale (LC_NUMERIC, old_locale);
215 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
218 *endptr = fail_pos_1;
224 *endptr = fail_pos_2;
230 g_strerror (gint errnum)
232 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
236 return strerror (errnum);
241 case E2BIG: return "argument list too long";
244 case EACCES: return "permission denied";
247 case EADDRINUSE: return "address already in use";
250 case EADDRNOTAVAIL: return "can't assign requested address";
253 case EADV: return "advertise error";
256 case EAFNOSUPPORT: return "address family not supported by protocol family";
259 case EAGAIN: return "try again";
262 case EALIGN: return "EALIGN";
265 case EALREADY: return "operation already in progress";
268 case EBADE: return "bad exchange descriptor";
271 case EBADF: return "bad file number";
274 case EBADFD: return "file descriptor in bad state";
277 case EBADMSG: return "not a data message";
280 case EBADR: return "bad request descriptor";
283 case EBADRPC: return "RPC structure is bad";
286 case EBADRQC: return "bad request code";
289 case EBADSLT: return "invalid slot";
292 case EBFONT: return "bad font file format";
295 case EBUSY: return "mount device busy";
298 case ECHILD: return "no children";
301 case ECHRNG: return "channel number out of range";
304 case ECOMM: return "communication error on send";
307 case ECONNABORTED: return "software caused connection abort";
310 case ECONNREFUSED: return "connection refused";
313 case ECONNRESET: return "connection reset by peer";
315 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
316 case EDEADLK: return "resource deadlock avoided";
319 case EDEADLOCK: return "resource deadlock avoided";
322 case EDESTADDRREQ: return "destination address required";
325 case EDIRTY: return "mounting a dirty fs w/o force";
328 case EDOM: return "math argument out of range";
331 case EDOTDOT: return "cross mount point";
334 case EDQUOT: return "disk quota exceeded";
337 case EDUPPKG: return "duplicate package name";
340 case EEXIST: return "file already exists";
343 case EFAULT: return "bad address in system call argument";
346 case EFBIG: return "file too large";
349 case EHOSTDOWN: return "host is down";
352 case EHOSTUNREACH: return "host is unreachable";
355 case EIDRM: return "identifier removed";
358 case EINIT: return "initialization error";
361 case EINPROGRESS: return "operation now in progress";
364 case EINTR: return "interrupted system call";
367 case EINVAL: return "invalid argument";
370 case EIO: return "I/O error";
373 case EISCONN: return "socket is already connected";
376 case EISDIR: return "illegal operation on a directory";
379 case EISNAM: return "is a name file";
382 case ELBIN: return "ELBIN";
385 case EL2HLT: return "level 2 halted";
388 case EL2NSYNC: return "level 2 not synchronized";
391 case EL3HLT: return "level 3 halted";
394 case EL3RST: return "level 3 reset";
397 case ELIBACC: return "can not access a needed shared library";
400 case ELIBBAD: return "accessing a corrupted shared library";
403 case ELIBEXEC: return "can not exec a shared library directly";
406 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
409 case ELIBSCN: return ".lib section in a.out corrupted";
412 case ELNRNG: return "link number out of range";
415 case ELOOP: return "too many levels of symbolic links";
418 case EMFILE: return "too many open files";
421 case EMLINK: return "too many links";
424 case EMSGSIZE: return "message too long";
427 case EMULTIHOP: return "multihop attempted";
430 case ENAMETOOLONG: return "file name too long";
433 case ENAVAIL: return "not available";
436 case ENET: return "ENET";
439 case ENETDOWN: return "network is down";
442 case ENETRESET: return "network dropped connection on reset";
445 case ENETUNREACH: return "network is unreachable";
448 case ENFILE: return "file table overflow";
451 case ENOANO: return "anode table overflow";
453 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
454 case ENOBUFS: return "no buffer space available";
457 case ENOCSI: return "no CSI structure available";
460 case ENODATA: return "no data available";
463 case ENODEV: return "no such device";
466 case ENOENT: return "no such file or directory";
469 case ENOEXEC: return "exec format error";
472 case ENOLCK: return "no locks available";
475 case ENOLINK: return "link has be severed";
478 case ENOMEM: return "not enough memory";
481 case ENOMSG: return "no message of desired type";
484 case ENONET: return "machine is not on the network";
487 case ENOPKG: return "package not installed";
490 case ENOPROTOOPT: return "bad proocol option";
493 case ENOSPC: return "no space left on device";
496 case ENOSR: return "out of stream resources";
499 case ENOSTR: return "not a stream device";
502 case ENOSYM: return "unresolved symbol name";
505 case ENOSYS: return "function not implemented";
508 case ENOTBLK: return "block device required";
511 case ENOTCONN: return "socket is not connected";
514 case ENOTDIR: return "not a directory";
517 case ENOTEMPTY: return "directory not empty";
520 case ENOTNAM: return "not a name file";
523 case ENOTSOCK: return "socket operation on non-socket";
526 case ENOTTY: return "inappropriate device for ioctl";
529 case ENOTUNIQ: return "name not unique on network";
532 case ENXIO: return "no such device or address";
535 case EOPNOTSUPP: return "operation not supported on socket";
538 case EPERM: return "not owner";
541 case EPFNOSUPPORT: return "protocol family not supported";
544 case EPIPE: return "broken pipe";
547 case EPROCLIM: return "too many processes";
550 case EPROCUNAVAIL: return "bad procedure for program";
553 case EPROGMISMATCH: return "program version wrong";
556 case EPROGUNAVAIL: return "RPC program not available";
559 case EPROTO: return "protocol error";
561 #ifdef EPROTONOSUPPORT
562 case EPROTONOSUPPORT: return "protocol not suppored";
565 case EPROTOTYPE: return "protocol wrong type for socket";
568 case ERANGE: return "math result unrepresentable";
570 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
571 case EREFUSED: return "EREFUSED";
574 case EREMCHG: return "remote address changed";
577 case EREMDEV: return "remote device";
580 case EREMOTE: return "pathname hit remote file system";
583 case EREMOTEIO: return "remote i/o error";
585 #ifdef EREMOTERELEASE
586 case EREMOTERELEASE: return "EREMOTERELEASE";
589 case EROFS: return "read-only file system";
592 case ERPCMISMATCH: return "RPC version is wrong";
595 case ERREMOTE: return "object is remote";
598 case ESHUTDOWN: return "can't send afer socket shutdown";
600 #ifdef ESOCKTNOSUPPORT
601 case ESOCKTNOSUPPORT: return "socket type not supported";
604 case ESPIPE: return "invalid seek";
607 case ESRCH: return "no such process";
610 case ESRMNT: return "srmount error";
613 case ESTALE: return "stale remote file handle";
616 case ESUCCESS: return "Error 0";
619 case ETIME: return "timer expired";
622 case ETIMEDOUT: return "connection timed out";
625 case ETOOMANYREFS: return "too many references: can't splice";
628 case ETXTBSY: return "text file or pseudo-device busy";
631 case EUCLEAN: return "structure needs cleaning";
634 case EUNATCH: return "protocol driver not attached";
637 case EUSERS: return "too many users";
640 case EVERSION: return "version mismatch";
642 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
643 case EWOULDBLOCK: return "operation would block";
646 case EXDEV: return "cross-domain link";
649 case EXFULL: return "message tables full";
652 #else /* NO_SYS_ERRLIST */
654 extern char *sys_errlist[];
656 if ((errnum > 0) && (errnum <= sys_nerr))
657 return sys_errlist [errnum];
658 #endif /* NO_SYS_ERRLIST */
660 msg = g_static_private_get (&msg_private);
663 msg = g_new (gchar, 64);
664 g_static_private_set (&msg_private, msg, g_free);
667 sprintf (msg, "unknown error (%d)", errnum);
673 g_strsignal (gint signum)
675 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
678 #ifdef HAVE_STRSIGNAL
680 extern const char * strsignal(int);
681 #else /* !G_OS_BEOS */
682 /* this is declared differently (const) in string.h on BeOS */
683 extern char *strsignal (int sig);
684 #endif /* !G_OS_BEOS */
685 return strsignal (signum);
690 case SIGHUP: return "Hangup";
693 case SIGINT: return "Interrupt";
696 case SIGQUIT: return "Quit";
699 case SIGILL: return "Illegal instruction";
702 case SIGTRAP: return "Trace/breakpoint trap";
705 case SIGABRT: return "IOT trap/Abort";
708 case SIGBUS: return "Bus error";
711 case SIGFPE: return "Floating point exception";
714 case SIGKILL: return "Killed";
717 case SIGUSR1: return "User defined signal 1";
720 case SIGSEGV: return "Segmentation fault";
723 case SIGUSR2: return "User defined signal 2";
726 case SIGPIPE: return "Broken pipe";
729 case SIGALRM: return "Alarm clock";
732 case SIGTERM: return "Terminated";
735 case SIGSTKFLT: return "Stack fault";
738 case SIGCHLD: return "Child exited";
741 case SIGCONT: return "Continued";
744 case SIGSTOP: return "Stopped (signal)";
747 case SIGTSTP: return "Stopped";
750 case SIGTTIN: return "Stopped (tty input)";
753 case SIGTTOU: return "Stopped (tty output)";
756 case SIGURG: return "Urgent condition";
759 case SIGXCPU: return "CPU time limit exceeded";
762 case SIGXFSZ: return "File size limit exceeded";
765 case SIGVTALRM: return "Virtual time alarm";
768 case SIGPROF: return "Profile signal";
771 case SIGWINCH: return "Window size changed";
774 case SIGIO: return "Possible I/O";
777 case SIGPWR: return "Power failure";
780 case SIGUNUSED: return "Unused signal";
783 #else /* NO_SYS_SIGLIST */
785 #ifdef NO_SYS_SIGLIST_DECL
786 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
789 return (char*) /* this function should return const --josh */ sys_siglist [signum];
790 #endif /* NO_SYS_SIGLIST */
792 msg = g_static_private_get (&msg_private);
795 msg = g_new (gchar, 64);
796 g_static_private_set (&msg_private, msg, g_free);
799 sprintf (msg, "unknown signal (%d)", signum);
805 g_strdown (gchar *string)
809 g_return_if_fail (string != NULL);
821 g_strup (gchar *string)
825 g_return_if_fail (string != NULL);
837 g_strreverse (gchar *string)
839 g_return_if_fail (string != NULL);
843 register gchar *h, *t;
846 t = string + strlen (string) - 1;
862 g_strcasecmp (const gchar *s1,
865 #ifdef HAVE_STRCASECMP
866 g_return_val_if_fail (s1 != NULL, 0);
867 g_return_val_if_fail (s2 != NULL, 0);
869 return strcasecmp (s1, s2);
873 g_return_val_if_fail (s1 != NULL, 0);
874 g_return_val_if_fail (s2 != NULL, 0);
878 /* According to A. Cox, some platforms have islower's that
879 * don't work right on non-uppercase
881 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
882 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
888 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
893 g_strncasecmp (const gchar *s1,
897 #ifdef HAVE_STRNCASECMP
898 return strncasecmp (s1, s2, n);
902 g_return_val_if_fail (s1 != NULL, 0);
903 g_return_val_if_fail (s2 != NULL, 0);
905 while (n-- && *s1 && *s2)
907 /* According to A. Cox, some platforms have islower's that
908 * don't work right on non-uppercase
910 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
911 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
918 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
925 g_strdelimit (gchar *string,
926 const gchar *delimiters,
931 g_return_val_if_fail (string != NULL, NULL);
934 delimiters = G_STR_DELIMITERS;
936 for (c = string; *c; c++)
938 if (strchr (delimiters, *c))
946 g_strcompress (const gchar *source)
948 const gchar *p = source, *octal;
949 gchar *dest = g_malloc (strlen (source) + 1);
959 case '0': case '1': case '2': case '3': case '4':
960 case '5': case '6': case '7':
963 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
965 *q = (*q * 8) + (*p - '0');
986 default: /* Also handles \" and \\ */
1000 g_strescape (const gchar *source,
1001 const gchar *exceptions)
1003 const guchar *p = (guchar *) source;
1004 /* Each source byte needs maximally four destination chars (\777) */
1005 gchar *dest = g_malloc (strlen (source) * 4 + 1);
1009 memset (excmap, 0, 256);
1012 guchar *e = (guchar *) exceptions;
1058 if ((*p < ' ') || (*p >= 0177))
1061 *q++ = '0' + (((*p) >> 6) & 07);
1062 *q++ = '0' + (((*p) >> 3) & 07);
1063 *q++ = '0' + ((*p) & 07);
1077 * g_filename_to_utf8
1079 * Converts a string which is in the encoding used for file names by
1080 * the C runtime (usually the same as that used by the operating
1081 * system) in the current locale into a UTF-8 string.
1085 g_filename_to_utf8 (const gchar *opsysstring)
1089 gint i, clen, wclen, first;
1090 const gint len = strlen (opsysstring);
1095 wcs = g_new (wchar_t, len);
1096 wclen = MultiByteToWideChar (CP_ACP, 0, opsysstring, len, wcs, len);
1100 for (i = 0; i < wclen; i++)
1106 else if (wc < 0x800)
1108 else if (wc < 0x10000)
1110 else if (wc < 0x200000)
1112 else if (wc < 0x4000000)
1118 result = g_malloc (clen + 1);
1122 for (i = 0; i < wclen; i++)
1131 else if (wc < 0x800)
1136 else if (wc < 0x10000)
1141 else if (wc < 0x200000)
1146 else if (wc < 0x4000000)
1160 case 6: bp[5] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
1161 case 5: bp[4] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
1162 case 4: bp[3] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
1163 case 3: bp[2] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
1164 case 2: bp[1] = (wc & 0x3f) | 0x80; wc >>= 6; /* Fall through */
1165 case 1: bp[0] = wc | first;
1178 return g_strdup (opsysstring);
1184 * g_filename_from_utf8
1186 * The reverse of g_filename_to_utf8.
1190 g_filename_from_utf8 (const gchar *utf8string)
1194 gint i, mask, clen, wclen, mblen;
1195 const gint len = strlen (utf8string);
1198 guchar *cp, *end, c;
1201 /* First convert to wide chars */
1202 cp = (guchar *) utf8string;
1205 wcs = g_new (wchar_t, len + 1);
1217 else if ((c & 0xe0) == 0xc0)
1222 else if ((c & 0xf0) == 0xe0)
1227 else if ((c & 0xf8) == 0xf0)
1232 else if ((c & 0xfc) == 0xf8)
1237 else if ((c & 0xfc) == 0xfc)
1248 if (cp + clen > end)
1254 *wcp = (cp[0] & mask);
1255 for (i = 1; i < clen; i++)
1257 if ((cp[i] & 0xc0) != 0x80)
1263 *wcp |= (cp[i] & 0x3f);
1276 /* n is the number of wide chars constructed */
1278 /* Convert to a string in the current ANSI codepage */
1280 result = g_new (gchar, 3 * n + 1);
1281 mblen = WideCharToMultiByte (CP_ACP, 0, wcs, n, result, 3*n, NULL, NULL);
1289 return g_strdup (utf8string);
1295 g_strchug (gchar *string)
1299 g_return_val_if_fail (string != NULL, NULL);
1301 for (start = string; *start && isspace (*start); start++)
1304 g_memmove (string, start, strlen( start) + 1);
1310 g_strchomp (gchar *string)
1314 g_return_val_if_fail (string != NULL, NULL);
1319 for (s = string + strlen (string) - 1; s >= string && isspace ((guchar)*s);
1327 g_strsplit (const gchar *string,
1328 const gchar *delimiter,
1331 GSList *string_list = NULL, *slist;
1332 gchar **str_array, *s;
1335 g_return_val_if_fail (string != NULL, NULL);
1336 g_return_val_if_fail (delimiter != NULL, NULL);
1339 max_tokens = G_MAXINT;
1341 s = strstr (string, delimiter);
1344 guint delimiter_len = strlen (delimiter);
1352 new_string = g_new (gchar, len + 1);
1353 strncpy (new_string, string, len);
1354 new_string[len] = 0;
1355 string_list = g_slist_prepend (string_list, new_string);
1357 string = s + delimiter_len;
1358 s = strstr (string, delimiter);
1360 while (--max_tokens && s);
1365 string_list = g_slist_prepend (string_list, g_strdup (string));
1368 str_array = g_new (gchar*, n);
1372 str_array[i--] = NULL;
1373 for (slist = string_list; slist; slist = slist->next)
1374 str_array[i--] = slist->data;
1376 g_slist_free (string_list);
1382 g_strfreev (gchar **str_array)
1388 for(i = 0; str_array[i] != NULL; i++)
1389 g_free(str_array[i]);
1396 g_strjoinv (const gchar *separator,
1401 g_return_val_if_fail (str_array != NULL, NULL);
1403 if (separator == NULL)
1409 guint separator_len;
1411 separator_len = strlen (separator);
1412 len = 1 + strlen (str_array[0]);
1413 for(i = 1; str_array[i] != NULL; i++)
1414 len += separator_len + strlen(str_array[i]);
1416 string = g_new (gchar, len);
1418 strcat (string, *str_array);
1419 for (i = 1; str_array[i] != NULL; i++)
1421 strcat (string, separator);
1422 strcat (string, str_array[i]);
1426 string = g_strdup ("");
1432 g_strjoin (const gchar *separator,
1438 guint separator_len;
1440 if (separator == NULL)
1443 separator_len = strlen (separator);
1445 va_start (args, separator);
1447 s = va_arg (args, gchar*);
1453 s = va_arg (args, gchar*);
1456 len += separator_len + strlen (s);
1457 s = va_arg (args, gchar*);
1461 string = g_new (gchar, len + 1);
1464 va_start (args, separator);
1466 s = va_arg (args, gchar*);
1469 s = va_arg (args, gchar*);
1472 strcat (string, separator);
1474 s = va_arg (args, gchar*);
1478 string = g_strdup ("");