Remove the semicolon from the definition of g_once(), so that
[glib.git] / glib / gstrfuncs.c
blob30072a0a6fddba7e769dbf12bd31766bd44a17aa
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/.
28 * MT safe
31 #include "config.h"
33 #define _GNU_SOURCE /* For stpcpy */
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <locale.h>
40 #include <errno.h>
41 #include <ctype.h> /* For tolower() */
42 #if !defined (HAVE_STRSIGNAL) || !defined(NO_SYS_SIGLIST_DECL)
43 #include <signal.h>
44 #endif
46 #include "glib.h"
47 #include "gprintf.h"
48 #include "gprintfint.h"
50 #ifdef G_OS_WIN32
51 #include <windows.h>
52 #endif
54 /* do not include <unistd.h> in this place since it
55 * interferes with g_strsignal() on some OSes
58 static const guint16 ascii_table_data[256] = {
59 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
60 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
63 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
64 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
65 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
66 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
68 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
69 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
70 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
71 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
72 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
73 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
74 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
75 /* the upper 128 are all zeroes */
78 #if defined(G_PLATFORM_WIN32) && defined(__GNUC__)
79 __declspec(dllexport)
80 #endif
81 const guint16 * const g_ascii_table = ascii_table_data;
83 gchar*
84 g_strdup (const gchar *str)
86 gchar *new_str;
87 gsize length;
89 if (str)
91 length = strlen (str) + 1;
92 new_str = g_new (char, length);
93 memcpy (new_str, str, length);
95 else
96 new_str = NULL;
98 return new_str;
101 gpointer
102 g_memdup (gconstpointer mem,
103 guint byte_size)
105 gpointer new_mem;
107 if (mem)
109 new_mem = g_malloc (byte_size);
110 memcpy (new_mem, mem, byte_size);
112 else
113 new_mem = NULL;
115 return new_mem;
118 gchar*
119 g_strndup (const gchar *str,
120 gsize n)
122 gchar *new_str;
124 if (str)
126 new_str = g_new (gchar, n + 1);
127 strncpy (new_str, str, n);
128 new_str[n] = '\0';
130 else
131 new_str = NULL;
133 return new_str;
136 gchar*
137 g_strnfill (gsize length,
138 gchar fill_char)
140 gchar *str;
142 str = g_new (gchar, length + 1);
143 memset (str, (guchar)fill_char, length);
144 str[length] = '\0';
146 return str;
150 * g_stpcpy:
151 * @dest: destination buffer.
152 * @src: source string.
154 * Copies a nul-terminated string into the dest buffer, include the
155 * trailing nul, and return a pointer to the trailing nul byte.
156 * This is useful for concatenating multiple strings together
157 * without having to repeatedly scan for the end.
159 * Return value: a pointer to trailing nul byte.
161 gchar *
162 g_stpcpy (gchar *dest,
163 const gchar *src)
165 #ifdef HAVE_STPCPY
166 g_return_val_if_fail (dest != NULL, NULL);
167 g_return_val_if_fail (src != NULL, NULL);
168 return stpcpy (dest, src);
169 #else
170 register gchar *d = dest;
171 register const gchar *s = src;
173 g_return_val_if_fail (dest != NULL, NULL);
174 g_return_val_if_fail (src != NULL, NULL);
176 *d++ = *s;
177 while (*s++ != '\0');
179 return d - 1;
180 #endif
183 gchar*
184 g_strdup_vprintf (const gchar *format,
185 va_list args)
187 gchar *string = NULL;
189 g_vasprintf (&string, format, args);
191 return string;
194 gchar*
195 g_strdup_printf (const gchar *format,
196 ...)
198 gchar *buffer;
199 va_list args;
201 va_start (args, format);
202 buffer = g_strdup_vprintf (format, args);
203 va_end (args);
205 return buffer;
208 gchar*
209 g_strconcat (const gchar *string1, ...)
211 gsize l;
212 va_list args;
213 gchar *s;
214 gchar *concat;
215 gchar *ptr;
217 g_return_val_if_fail (string1 != NULL, NULL);
219 l = 1 + strlen (string1);
220 va_start (args, string1);
221 s = va_arg (args, gchar*);
222 while (s)
224 l += strlen (s);
225 s = va_arg (args, gchar*);
227 va_end (args);
229 concat = g_new (gchar, l);
230 ptr = concat;
232 ptr = g_stpcpy (ptr, string1);
233 va_start (args, string1);
234 s = va_arg (args, gchar*);
235 while (s)
237 ptr = g_stpcpy (ptr, s);
238 s = va_arg (args, gchar*);
240 va_end (args);
242 return concat;
246 * g_strtod:
247 * @nptr: the string to convert to a numeric value.
248 * @endptr: if non-%NULL, it returns the character after
249 * the last character used in the conversion.
251 * Converts a string to a #gdouble value.
252 * It calls the standard strtod() function to handle the conversion, but
253 * if the string is not completely converted it attempts the conversion
254 * again with g_ascii_strtod(), and returns the best match.
256 * This function should seldomly be used. The normal situation when reading
257 * numbers not for human consumption is to use g_ascii_strtod(). Only when
258 * you know that you must expect both locale formatted and C formatted numbers
259 * should you use this. Make sure that you don't pass strings such as comma
260 * separated lists of values, since the commas may be interpreted as a decimal
261 * point in some locales, causing unexpected results.
263 * Return value: the #gdouble value.
265 gdouble
266 g_strtod (const gchar *nptr,
267 gchar **endptr)
269 gchar *fail_pos_1;
270 gchar *fail_pos_2;
271 gdouble val_1;
272 gdouble val_2 = 0;
274 g_return_val_if_fail (nptr != NULL, 0);
276 fail_pos_1 = NULL;
277 fail_pos_2 = NULL;
279 val_1 = strtod (nptr, &fail_pos_1);
281 if (fail_pos_1 && fail_pos_1[0] != 0)
282 val_2 = g_ascii_strtod (nptr, &fail_pos_2);
284 if (!fail_pos_1 || fail_pos_1[0] == 0 || fail_pos_1 >= fail_pos_2)
286 if (endptr)
287 *endptr = fail_pos_1;
288 return val_1;
290 else
292 if (endptr)
293 *endptr = fail_pos_2;
294 return val_2;
299 * g_ascii_strtod:
300 * @nptr: the string to convert to a numeric value.
301 * @endptr: if non-%NULL, it returns the character after
302 * the last character used in the conversion.
304 * Converts a string to a #gdouble value.
305 * This function behaves like the standard strtod() function
306 * does in the C locale. It does this without actually
307 * changing the current locale, since that would not be
308 * thread-safe.
310 * This function is typically used when reading configuration
311 * files or other non-user input that should be locale independent.
312 * To handle input from the user you should normally use the
313 * locale-sensitive system strtod() function.
315 * To convert from a #gdouble to a string in a locale-insensitive
316 * way, use g_ascii_dtostr().
318 * If the correct value would cause overflow, plus or minus %HUGE_VAL
319 * is returned (according to the sign of the value), and %ERANGE is
320 * stored in %errno. If the correct value would cause underflow,
321 * zero is returned and %ERANGE is stored in %errno.
323 * This function resets %errno before calling strtod() so that
324 * you can reliably detect overflow and underflow.
326 * Return value: the #gdouble value.
328 gdouble
329 g_ascii_strtod (const gchar *nptr,
330 gchar **endptr)
332 gchar *fail_pos;
333 gdouble val;
334 struct lconv *locale_data;
335 const char *decimal_point;
336 int decimal_point_len;
337 const char *p, *decimal_point_pos;
338 const char *end = NULL; /* Silence gcc */
340 g_return_val_if_fail (nptr != NULL, 0);
342 fail_pos = NULL;
344 locale_data = localeconv ();
345 decimal_point = locale_data->decimal_point;
346 decimal_point_len = strlen (decimal_point);
348 g_assert (decimal_point_len != 0);
350 decimal_point_pos = NULL;
351 if (decimal_point[0] != '.' ||
352 decimal_point[1] != 0)
354 p = nptr;
355 /* Skip leading space */
356 while (g_ascii_isspace (*p))
357 p++;
359 /* Skip leading optional sign */
360 if (*p == '+' || *p == '-')
361 p++;
363 if (p[0] == '0' &&
364 (p[1] == 'x' || p[1] == 'X'))
366 p += 2;
367 /* HEX - find the (optional) decimal point */
369 while (g_ascii_isxdigit (*p))
370 p++;
372 if (*p == '.')
374 decimal_point_pos = p++;
376 while (g_ascii_isxdigit (*p))
377 p++;
379 if (*p == 'p' || *p == 'P')
380 p++;
381 if (*p == '+' || *p == '-')
382 p++;
383 while (g_ascii_isdigit (*p))
384 p++;
385 end = p;
388 else
390 while (g_ascii_isdigit (*p))
391 p++;
393 if (*p == '.')
395 decimal_point_pos = p++;
397 while (g_ascii_isdigit (*p))
398 p++;
400 if (*p == 'e' || *p == 'E')
401 p++;
402 if (*p == '+' || *p == '-')
403 p++;
404 while (g_ascii_isdigit (*p))
405 p++;
406 end = p;
409 /* For the other cases, we need not convert the decimal point */
412 /* Set errno to zero, so that we can distinguish zero results
413 and underflows */
414 errno = 0;
416 if (decimal_point_pos)
418 char *copy, *c;
420 /* We need to convert the '.' to the locale specific decimal point */
421 copy = g_malloc (end - nptr + 1 + decimal_point_len);
423 c = copy;
424 memcpy (c, nptr, decimal_point_pos - nptr);
425 c += decimal_point_pos - nptr;
426 memcpy (c, decimal_point, decimal_point_len);
427 c += decimal_point_len;
428 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
429 c += end - (decimal_point_pos + 1);
430 *c = 0;
432 val = strtod (copy, &fail_pos);
434 if (fail_pos)
436 if (fail_pos > decimal_point_pos)
437 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
438 else
439 fail_pos = (char *)nptr + (fail_pos - copy);
442 g_free (copy);
445 else
446 val = strtod (nptr, &fail_pos);
448 if (endptr)
449 *endptr = fail_pos;
451 return val;
456 * g_ascii_dtostr:
457 * @buffer: A buffer to place the resulting string in
458 * @buf_len: The length of the buffer.
459 * @d: The #gdouble to convert
461 * Converts a #gdouble to a string, using the '.' as
462 * decimal point.
464 * This functions generates enough precision that converting
465 * the string back using g_ascii_strtod() gives the same machine-number
466 * (on machines with IEEE compatible 64bit doubles). It is
467 * guaranteed that the size of the resulting string will never
468 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
470 * Return value: The pointer to the buffer with the converted string.
472 gchar *
473 g_ascii_dtostr (gchar *buffer,
474 gint buf_len,
475 gdouble d)
477 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
481 * g_ascii_formatd:
482 * @buffer: A buffer to place the resulting string in
483 * @buf_len: The length of the buffer.
484 * @format: The printf()-style format to use for the
485 * code to use for converting.
486 * @d: The #gdouble to convert
488 * Converts a #gdouble to a string, using the '.' as
489 * decimal point. To format the number you pass in
490 * a printf()-style format string. Allowed conversion
491 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
493 * If you just want to want to serialize the value into a
494 * string, use g_ascii_dtostr().
496 * Return value: The pointer to the buffer with the converted string.
498 gchar *
499 g_ascii_formatd (gchar *buffer,
500 gint buf_len,
501 const gchar *format,
502 gdouble d)
504 struct lconv *locale_data;
505 const char *decimal_point;
506 int decimal_point_len;
507 gchar *p;
508 int rest_len;
509 gchar format_char;
511 g_return_val_if_fail (buffer != NULL, NULL);
512 g_return_val_if_fail (format[0] == '%', NULL);
513 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
515 format_char = format[strlen (format) - 1];
517 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
518 format_char == 'f' || format_char == 'F' ||
519 format_char == 'g' || format_char == 'G',
520 NULL);
522 if (format[0] != '%')
523 return NULL;
525 if (strpbrk (format + 1, "'l%"))
526 return NULL;
528 if (!(format_char == 'e' || format_char == 'E' ||
529 format_char == 'f' || format_char == 'F' ||
530 format_char == 'g' || format_char == 'G'))
531 return NULL;
534 _g_snprintf (buffer, buf_len, format, d);
536 locale_data = localeconv ();
537 decimal_point = locale_data->decimal_point;
538 decimal_point_len = strlen (decimal_point);
540 g_assert (decimal_point_len != 0);
542 if (decimal_point[0] != '.' ||
543 decimal_point[1] != 0)
545 p = buffer;
547 if (*p == '+' || *p == '-')
548 p++;
550 while (isdigit ((guchar)*p))
551 p++;
553 if (strncmp (p, decimal_point, decimal_point_len) == 0)
555 *p = '.';
556 p++;
557 if (decimal_point_len > 1) {
558 rest_len = strlen (p + (decimal_point_len-1));
559 memmove (p, p + (decimal_point_len-1),
560 rest_len);
561 p[rest_len] = 0;
567 return buffer;
571 * g_ascii_strtoull:
572 * @nptr: the string to convert to a numeric value.
573 * @endptr: if non-%NULL, it returns the character after
574 * the last character used in the conversion.
575 * @base: to be used for the conversion, 2..36 or 0
577 * Converts a string to a #guint64 value.
578 * This function behaves like the standard strtoull() function
579 * does in the C locale. It does this without actually
580 * changing the current locale, since that would not be
581 * thread-safe.
583 * This function is typically used when reading configuration
584 * files or other non-user input that should be locale independent.
585 * To handle input from the user you should normally use the
586 * locale-sensitive system strtoull() function.
588 * If the correct value would cause overflow, %G_MAXUINT64
589 * is returned, and %ERANGE is stored in %errno.
591 * Return value: the #guint64 value.
593 * Since: 2.2
595 guint64
596 g_ascii_strtoull (const gchar *nptr,
597 gchar **endptr,
598 guint base)
600 /* this code is based on on the strtol(3) code from GNU libc released under
601 * the GNU Lesser General Public License.
603 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
604 * Free Software Foundation, Inc.
606 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
607 (c) == '\r' || (c) == '\t' || (c) == '\v')
608 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
609 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
610 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
611 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
612 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
613 gboolean negative, overflow;
614 guint64 cutoff;
615 guint64 cutlim;
616 guint64 ui64;
617 const gchar *s, *save;
618 guchar c;
620 g_return_val_if_fail (nptr != NULL, 0);
622 if (base == 1 || base > 36)
624 errno = EINVAL;
625 return 0;
628 save = s = nptr;
630 /* Skip white space. */
631 while (ISSPACE (*s))
632 ++s;
633 if (!*s)
634 goto noconv;
636 /* Check for a sign. */
637 negative = FALSE;
638 if (*s == '-')
640 negative = TRUE;
641 ++s;
643 else if (*s == '+')
644 ++s;
646 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
647 if (*s == '0')
649 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
651 s += 2;
652 base = 16;
654 else if (base == 0)
655 base = 8;
657 else if (base == 0)
658 base = 10;
660 /* Save the pointer so we can check later if anything happened. */
661 save = s;
662 cutoff = G_MAXUINT64 / base;
663 cutlim = G_MAXUINT64 % base;
665 overflow = FALSE;
666 ui64 = 0;
667 c = *s;
668 for (; c; c = *++s)
670 if (c >= '0' && c <= '9')
671 c -= '0';
672 else if (ISALPHA (c))
673 c = TOUPPER (c) - 'A' + 10;
674 else
675 break;
676 if (c >= base)
677 break;
678 /* Check for overflow. */
679 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
680 overflow = TRUE;
681 else
683 ui64 *= base;
684 ui64 += c;
688 /* Check if anything actually happened. */
689 if (s == save)
690 goto noconv;
692 /* Store in ENDPTR the address of one character
693 past the last character we converted. */
694 if (endptr)
695 *endptr = (gchar*) s;
697 if (overflow)
699 errno = ERANGE;
700 return G_MAXUINT64;
703 /* Return the result of the appropriate sign. */
704 return negative ? -ui64 : ui64;
706 noconv:
707 /* We must handle a special case here: the base is 0 or 16 and the
708 first two characters are '0' and 'x', but the rest are no
709 hexadecimal digits. This is no error case. We return 0 and
710 ENDPTR points to the `x`. */
711 if (endptr)
713 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
714 && save[-2] == '0')
715 *endptr = (gchar*) &save[-1];
716 else
717 /* There was no number to convert. */
718 *endptr = (gchar*) nptr;
720 return 0;
724 G_CONST_RETURN gchar*
725 g_strerror (gint errnum)
727 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
728 char *msg;
730 #ifdef HAVE_STRERROR
731 const char *msg_locale;
733 msg_locale = strerror (errnum);
734 if (g_get_charset (NULL))
735 return msg_locale;
736 else
738 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
739 if (msg_utf8)
741 /* Stick in the quark table so that we can return a static result
743 GQuark msg_quark = g_quark_from_string (msg_utf8);
744 g_free (msg_utf8);
746 return g_quark_to_string (msg_quark);
749 #elif NO_SYS_ERRLIST
750 switch (errnum)
752 #ifdef E2BIG
753 case E2BIG: return "argument list too long";
754 #endif
755 #ifdef EACCES
756 case EACCES: return "permission denied";
757 #endif
758 #ifdef EADDRINUSE
759 case EADDRINUSE: return "address already in use";
760 #endif
761 #ifdef EADDRNOTAVAIL
762 case EADDRNOTAVAIL: return "can't assign requested address";
763 #endif
764 #ifdef EADV
765 case EADV: return "advertise error";
766 #endif
767 #ifdef EAFNOSUPPORT
768 case EAFNOSUPPORT: return "address family not supported by protocol family";
769 #endif
770 #ifdef EAGAIN
771 case EAGAIN: return "try again";
772 #endif
773 #ifdef EALIGN
774 case EALIGN: return "EALIGN";
775 #endif
776 #ifdef EALREADY
777 case EALREADY: return "operation already in progress";
778 #endif
779 #ifdef EBADE
780 case EBADE: return "bad exchange descriptor";
781 #endif
782 #ifdef EBADF
783 case EBADF: return "bad file number";
784 #endif
785 #ifdef EBADFD
786 case EBADFD: return "file descriptor in bad state";
787 #endif
788 #ifdef EBADMSG
789 case EBADMSG: return "not a data message";
790 #endif
791 #ifdef EBADR
792 case EBADR: return "bad request descriptor";
793 #endif
794 #ifdef EBADRPC
795 case EBADRPC: return "RPC structure is bad";
796 #endif
797 #ifdef EBADRQC
798 case EBADRQC: return "bad request code";
799 #endif
800 #ifdef EBADSLT
801 case EBADSLT: return "invalid slot";
802 #endif
803 #ifdef EBFONT
804 case EBFONT: return "bad font file format";
805 #endif
806 #ifdef EBUSY
807 case EBUSY: return "mount device busy";
808 #endif
809 #ifdef ECHILD
810 case ECHILD: return "no children";
811 #endif
812 #ifdef ECHRNG
813 case ECHRNG: return "channel number out of range";
814 #endif
815 #ifdef ECOMM
816 case ECOMM: return "communication error on send";
817 #endif
818 #ifdef ECONNABORTED
819 case ECONNABORTED: return "software caused connection abort";
820 #endif
821 #ifdef ECONNREFUSED
822 case ECONNREFUSED: return "connection refused";
823 #endif
824 #ifdef ECONNRESET
825 case ECONNRESET: return "connection reset by peer";
826 #endif
827 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
828 case EDEADLK: return "resource deadlock avoided";
829 #endif
830 #ifdef EDEADLOCK
831 case EDEADLOCK: return "resource deadlock avoided";
832 #endif
833 #ifdef EDESTADDRREQ
834 case EDESTADDRREQ: return "destination address required";
835 #endif
836 #ifdef EDIRTY
837 case EDIRTY: return "mounting a dirty fs w/o force";
838 #endif
839 #ifdef EDOM
840 case EDOM: return "math argument out of range";
841 #endif
842 #ifdef EDOTDOT
843 case EDOTDOT: return "cross mount point";
844 #endif
845 #ifdef EDQUOT
846 case EDQUOT: return "disk quota exceeded";
847 #endif
848 #ifdef EDUPPKG
849 case EDUPPKG: return "duplicate package name";
850 #endif
851 #ifdef EEXIST
852 case EEXIST: return "file already exists";
853 #endif
854 #ifdef EFAULT
855 case EFAULT: return "bad address in system call argument";
856 #endif
857 #ifdef EFBIG
858 case EFBIG: return "file too large";
859 #endif
860 #ifdef EHOSTDOWN
861 case EHOSTDOWN: return "host is down";
862 #endif
863 #ifdef EHOSTUNREACH
864 case EHOSTUNREACH: return "host is unreachable";
865 #endif
866 #ifdef EIDRM
867 case EIDRM: return "identifier removed";
868 #endif
869 #ifdef EINIT
870 case EINIT: return "initialization error";
871 #endif
872 #ifdef EINPROGRESS
873 case EINPROGRESS: return "operation now in progress";
874 #endif
875 #ifdef EINTR
876 case EINTR: return "interrupted system call";
877 #endif
878 #ifdef EINVAL
879 case EINVAL: return "invalid argument";
880 #endif
881 #ifdef EIO
882 case EIO: return "I/O error";
883 #endif
884 #ifdef EISCONN
885 case EISCONN: return "socket is already connected";
886 #endif
887 #ifdef EISDIR
888 case EISDIR: return "is a directory";
889 #endif
890 #ifdef EISNAME
891 case EISNAM: return "is a name file";
892 #endif
893 #ifdef ELBIN
894 case ELBIN: return "ELBIN";
895 #endif
896 #ifdef EL2HLT
897 case EL2HLT: return "level 2 halted";
898 #endif
899 #ifdef EL2NSYNC
900 case EL2NSYNC: return "level 2 not synchronized";
901 #endif
902 #ifdef EL3HLT
903 case EL3HLT: return "level 3 halted";
904 #endif
905 #ifdef EL3RST
906 case EL3RST: return "level 3 reset";
907 #endif
908 #ifdef ELIBACC
909 case ELIBACC: return "can not access a needed shared library";
910 #endif
911 #ifdef ELIBBAD
912 case ELIBBAD: return "accessing a corrupted shared library";
913 #endif
914 #ifdef ELIBEXEC
915 case ELIBEXEC: return "can not exec a shared library directly";
916 #endif
917 #ifdef ELIBMAX
918 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
919 #endif
920 #ifdef ELIBSCN
921 case ELIBSCN: return ".lib section in a.out corrupted";
922 #endif
923 #ifdef ELNRNG
924 case ELNRNG: return "link number out of range";
925 #endif
926 #ifdef ELOOP
927 case ELOOP: return "too many levels of symbolic links";
928 #endif
929 #ifdef EMFILE
930 case EMFILE: return "too many open files";
931 #endif
932 #ifdef EMLINK
933 case EMLINK: return "too many links";
934 #endif
935 #ifdef EMSGSIZE
936 case EMSGSIZE: return "message too long";
937 #endif
938 #ifdef EMULTIHOP
939 case EMULTIHOP: return "multihop attempted";
940 #endif
941 #ifdef ENAMETOOLONG
942 case ENAMETOOLONG: return "file name too long";
943 #endif
944 #ifdef ENAVAIL
945 case ENAVAIL: return "not available";
946 #endif
947 #ifdef ENET
948 case ENET: return "ENET";
949 #endif
950 #ifdef ENETDOWN
951 case ENETDOWN: return "network is down";
952 #endif
953 #ifdef ENETRESET
954 case ENETRESET: return "network dropped connection on reset";
955 #endif
956 #ifdef ENETUNREACH
957 case ENETUNREACH: return "network is unreachable";
958 #endif
959 #ifdef ENFILE
960 case ENFILE: return "file table overflow";
961 #endif
962 #ifdef ENOANO
963 case ENOANO: return "anode table overflow";
964 #endif
965 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
966 case ENOBUFS: return "no buffer space available";
967 #endif
968 #ifdef ENOCSI
969 case ENOCSI: return "no CSI structure available";
970 #endif
971 #ifdef ENODATA
972 case ENODATA: return "no data available";
973 #endif
974 #ifdef ENODEV
975 case ENODEV: return "no such device";
976 #endif
977 #ifdef ENOENT
978 case ENOENT: return "no such file or directory";
979 #endif
980 #ifdef ENOEXEC
981 case ENOEXEC: return "exec format error";
982 #endif
983 #ifdef ENOLCK
984 case ENOLCK: return "no locks available";
985 #endif
986 #ifdef ENOLINK
987 case ENOLINK: return "link has be severed";
988 #endif
989 #ifdef ENOMEM
990 case ENOMEM: return "not enough memory";
991 #endif
992 #ifdef ENOMSG
993 case ENOMSG: return "no message of desired type";
994 #endif
995 #ifdef ENONET
996 case ENONET: return "machine is not on the network";
997 #endif
998 #ifdef ENOPKG
999 case ENOPKG: return "package not installed";
1000 #endif
1001 #ifdef ENOPROTOOPT
1002 case ENOPROTOOPT: return "bad proocol option";
1003 #endif
1004 #ifdef ENOSPC
1005 case ENOSPC: return "no space left on device";
1006 #endif
1007 #ifdef ENOSR
1008 case ENOSR: return "out of stream resources";
1009 #endif
1010 #ifdef ENOSTR
1011 case ENOSTR: return "not a stream device";
1012 #endif
1013 #ifdef ENOSYM
1014 case ENOSYM: return "unresolved symbol name";
1015 #endif
1016 #ifdef ENOSYS
1017 case ENOSYS: return "function not implemented";
1018 #endif
1019 #ifdef ENOTBLK
1020 case ENOTBLK: return "block device required";
1021 #endif
1022 #ifdef ENOTCONN
1023 case ENOTCONN: return "socket is not connected";
1024 #endif
1025 #ifdef ENOTDIR
1026 case ENOTDIR: return "not a directory";
1027 #endif
1028 #ifdef ENOTEMPTY
1029 case ENOTEMPTY: return "directory not empty";
1030 #endif
1031 #ifdef ENOTNAM
1032 case ENOTNAM: return "not a name file";
1033 #endif
1034 #ifdef ENOTSOCK
1035 case ENOTSOCK: return "socket operation on non-socket";
1036 #endif
1037 #ifdef ENOTTY
1038 case ENOTTY: return "inappropriate device for ioctl";
1039 #endif
1040 #ifdef ENOTUNIQ
1041 case ENOTUNIQ: return "name not unique on network";
1042 #endif
1043 #ifdef ENXIO
1044 case ENXIO: return "no such device or address";
1045 #endif
1046 #ifdef EOPNOTSUPP
1047 case EOPNOTSUPP: return "operation not supported on socket";
1048 #endif
1049 #ifdef EPERM
1050 case EPERM: return "not owner";
1051 #endif
1052 #ifdef EPFNOSUPPORT
1053 case EPFNOSUPPORT: return "protocol family not supported";
1054 #endif
1055 #ifdef EPIPE
1056 case EPIPE: return "broken pipe";
1057 #endif
1058 #ifdef EPROCLIM
1059 case EPROCLIM: return "too many processes";
1060 #endif
1061 #ifdef EPROCUNAVAIL
1062 case EPROCUNAVAIL: return "bad procedure for program";
1063 #endif
1064 #ifdef EPROGMISMATCH
1065 case EPROGMISMATCH: return "program version wrong";
1066 #endif
1067 #ifdef EPROGUNAVAIL
1068 case EPROGUNAVAIL: return "RPC program not available";
1069 #endif
1070 #ifdef EPROTO
1071 case EPROTO: return "protocol error";
1072 #endif
1073 #ifdef EPROTONOSUPPORT
1074 case EPROTONOSUPPORT: return "protocol not suppored";
1075 #endif
1076 #ifdef EPROTOTYPE
1077 case EPROTOTYPE: return "protocol wrong type for socket";
1078 #endif
1079 #ifdef ERANGE
1080 case ERANGE: return "math result unrepresentable";
1081 #endif
1082 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1083 case EREFUSED: return "EREFUSED";
1084 #endif
1085 #ifdef EREMCHG
1086 case EREMCHG: return "remote address changed";
1087 #endif
1088 #ifdef EREMDEV
1089 case EREMDEV: return "remote device";
1090 #endif
1091 #ifdef EREMOTE
1092 case EREMOTE: return "pathname hit remote file system";
1093 #endif
1094 #ifdef EREMOTEIO
1095 case EREMOTEIO: return "remote i/o error";
1096 #endif
1097 #ifdef EREMOTERELEASE
1098 case EREMOTERELEASE: return "EREMOTERELEASE";
1099 #endif
1100 #ifdef EROFS
1101 case EROFS: return "read-only file system";
1102 #endif
1103 #ifdef ERPCMISMATCH
1104 case ERPCMISMATCH: return "RPC version is wrong";
1105 #endif
1106 #ifdef ERREMOTE
1107 case ERREMOTE: return "object is remote";
1108 #endif
1109 #ifdef ESHUTDOWN
1110 case ESHUTDOWN: return "can't send afer socket shutdown";
1111 #endif
1112 #ifdef ESOCKTNOSUPPORT
1113 case ESOCKTNOSUPPORT: return "socket type not supported";
1114 #endif
1115 #ifdef ESPIPE
1116 case ESPIPE: return "invalid seek";
1117 #endif
1118 #ifdef ESRCH
1119 case ESRCH: return "no such process";
1120 #endif
1121 #ifdef ESRMNT
1122 case ESRMNT: return "srmount error";
1123 #endif
1124 #ifdef ESTALE
1125 case ESTALE: return "stale remote file handle";
1126 #endif
1127 #ifdef ESUCCESS
1128 case ESUCCESS: return "Error 0";
1129 #endif
1130 #ifdef ETIME
1131 case ETIME: return "timer expired";
1132 #endif
1133 #ifdef ETIMEDOUT
1134 case ETIMEDOUT: return "connection timed out";
1135 #endif
1136 #ifdef ETOOMANYREFS
1137 case ETOOMANYREFS: return "too many references: can't splice";
1138 #endif
1139 #ifdef ETXTBSY
1140 case ETXTBSY: return "text file or pseudo-device busy";
1141 #endif
1142 #ifdef EUCLEAN
1143 case EUCLEAN: return "structure needs cleaning";
1144 #endif
1145 #ifdef EUNATCH
1146 case EUNATCH: return "protocol driver not attached";
1147 #endif
1148 #ifdef EUSERS
1149 case EUSERS: return "too many users";
1150 #endif
1151 #ifdef EVERSION
1152 case EVERSION: return "version mismatch";
1153 #endif
1154 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1155 case EWOULDBLOCK: return "operation would block";
1156 #endif
1157 #ifdef EXDEV
1158 case EXDEV: return "cross-domain link";
1159 #endif
1160 #ifdef EXFULL
1161 case EXFULL: return "message tables full";
1162 #endif
1164 #else /* NO_SYS_ERRLIST */
1165 extern int sys_nerr;
1166 extern char *sys_errlist[];
1168 if ((errnum > 0) && (errnum <= sys_nerr))
1169 return sys_errlist [errnum];
1170 #endif /* NO_SYS_ERRLIST */
1172 msg = g_static_private_get (&msg_private);
1173 if (!msg)
1175 msg = g_new (gchar, 64);
1176 g_static_private_set (&msg_private, msg, g_free);
1179 _g_sprintf (msg, "unknown error (%d)", errnum);
1181 return msg;
1184 G_CONST_RETURN gchar*
1185 g_strsignal (gint signum)
1187 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1188 char *msg;
1190 #ifdef HAVE_STRSIGNAL
1191 const char *msg_locale;
1193 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1194 extern const char *strsignal(int);
1195 #else
1196 /* this is declared differently (const) in string.h on BeOS */
1197 extern char *strsignal (int sig);
1198 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1199 msg_locale = strsignal (signum);
1200 if (g_get_charset (NULL))
1201 return msg_locale;
1202 else
1204 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1205 if (msg_utf8)
1207 /* Stick in the quark table so that we can return a static result
1209 GQuark msg_quark = g_quark_from_string (msg_utf8);
1210 g_free (msg_utf8);
1212 return g_quark_to_string (msg_quark);
1215 #elif NO_SYS_SIGLIST
1216 switch (signum)
1218 #ifdef SIGHUP
1219 case SIGHUP: return "Hangup";
1220 #endif
1221 #ifdef SIGINT
1222 case SIGINT: return "Interrupt";
1223 #endif
1224 #ifdef SIGQUIT
1225 case SIGQUIT: return "Quit";
1226 #endif
1227 #ifdef SIGILL
1228 case SIGILL: return "Illegal instruction";
1229 #endif
1230 #ifdef SIGTRAP
1231 case SIGTRAP: return "Trace/breakpoint trap";
1232 #endif
1233 #ifdef SIGABRT
1234 case SIGABRT: return "IOT trap/Abort";
1235 #endif
1236 #ifdef SIGBUS
1237 case SIGBUS: return "Bus error";
1238 #endif
1239 #ifdef SIGFPE
1240 case SIGFPE: return "Floating point exception";
1241 #endif
1242 #ifdef SIGKILL
1243 case SIGKILL: return "Killed";
1244 #endif
1245 #ifdef SIGUSR1
1246 case SIGUSR1: return "User defined signal 1";
1247 #endif
1248 #ifdef SIGSEGV
1249 case SIGSEGV: return "Segmentation fault";
1250 #endif
1251 #ifdef SIGUSR2
1252 case SIGUSR2: return "User defined signal 2";
1253 #endif
1254 #ifdef SIGPIPE
1255 case SIGPIPE: return "Broken pipe";
1256 #endif
1257 #ifdef SIGALRM
1258 case SIGALRM: return "Alarm clock";
1259 #endif
1260 #ifdef SIGTERM
1261 case SIGTERM: return "Terminated";
1262 #endif
1263 #ifdef SIGSTKFLT
1264 case SIGSTKFLT: return "Stack fault";
1265 #endif
1266 #ifdef SIGCHLD
1267 case SIGCHLD: return "Child exited";
1268 #endif
1269 #ifdef SIGCONT
1270 case SIGCONT: return "Continued";
1271 #endif
1272 #ifdef SIGSTOP
1273 case SIGSTOP: return "Stopped (signal)";
1274 #endif
1275 #ifdef SIGTSTP
1276 case SIGTSTP: return "Stopped";
1277 #endif
1278 #ifdef SIGTTIN
1279 case SIGTTIN: return "Stopped (tty input)";
1280 #endif
1281 #ifdef SIGTTOU
1282 case SIGTTOU: return "Stopped (tty output)";
1283 #endif
1284 #ifdef SIGURG
1285 case SIGURG: return "Urgent condition";
1286 #endif
1287 #ifdef SIGXCPU
1288 case SIGXCPU: return "CPU time limit exceeded";
1289 #endif
1290 #ifdef SIGXFSZ
1291 case SIGXFSZ: return "File size limit exceeded";
1292 #endif
1293 #ifdef SIGVTALRM
1294 case SIGVTALRM: return "Virtual time alarm";
1295 #endif
1296 #ifdef SIGPROF
1297 case SIGPROF: return "Profile signal";
1298 #endif
1299 #ifdef SIGWINCH
1300 case SIGWINCH: return "Window size changed";
1301 #endif
1302 #ifdef SIGIO
1303 case SIGIO: return "Possible I/O";
1304 #endif
1305 #ifdef SIGPWR
1306 case SIGPWR: return "Power failure";
1307 #endif
1308 #ifdef SIGUNUSED
1309 case SIGUNUSED: return "Unused signal";
1310 #endif
1312 #else /* NO_SYS_SIGLIST */
1314 #ifdef NO_SYS_SIGLIST_DECL
1315 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1316 #endif
1318 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1319 #endif /* NO_SYS_SIGLIST */
1321 msg = g_static_private_get (&msg_private);
1322 if (!msg)
1324 msg = g_new (gchar, 64);
1325 g_static_private_set (&msg_private, msg, g_free);
1328 _g_sprintf (msg, "unknown signal (%d)", signum);
1330 return msg;
1333 /* Functions g_strlcpy and g_strlcat were originally developed by
1334 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1335 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1336 * for more information.
1339 #ifdef HAVE_STRLCPY
1340 /* Use the native ones, if available; they might be implemented in assembly */
1341 gsize
1342 g_strlcpy (gchar *dest,
1343 const gchar *src,
1344 gsize dest_size)
1346 g_return_val_if_fail (dest != NULL, 0);
1347 g_return_val_if_fail (src != NULL, 0);
1349 return strlcpy (dest, src, dest_size);
1352 gsize
1353 g_strlcat (gchar *dest,
1354 const gchar *src,
1355 gsize dest_size)
1357 g_return_val_if_fail (dest != NULL, 0);
1358 g_return_val_if_fail (src != NULL, 0);
1360 return strlcat (dest, src, dest_size);
1363 #else /* ! HAVE_STRLCPY */
1364 /* g_strlcpy
1366 * Copy string src to buffer dest (of buffer size dest_size). At most
1367 * dest_size-1 characters will be copied. Always NUL terminates
1368 * (unless dest_size == 0). This function does NOT allocate memory.
1369 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1370 * Returns size of attempted result, strlen(src),
1371 * so if retval >= dest_size, truncation occurred.
1373 gsize
1374 g_strlcpy (gchar *dest,
1375 const gchar *src,
1376 gsize dest_size)
1378 register gchar *d = dest;
1379 register const gchar *s = src;
1380 register gsize n = dest_size;
1382 g_return_val_if_fail (dest != NULL, 0);
1383 g_return_val_if_fail (src != NULL, 0);
1385 /* Copy as many bytes as will fit */
1386 if (n != 0 && --n != 0)
1389 register gchar c = *s++;
1391 *d++ = c;
1392 if (c == 0)
1393 break;
1395 while (--n != 0);
1397 /* If not enough room in dest, add NUL and traverse rest of src */
1398 if (n == 0)
1400 if (dest_size != 0)
1401 *d = 0;
1402 while (*s++)
1406 return s - src - 1; /* count does not include NUL */
1409 /* g_strlcat
1411 * Appends string src to buffer dest (of buffer size dest_size).
1412 * At most dest_size-1 characters will be copied.
1413 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1414 * This function does NOT allocate memory.
1415 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1416 * in the dest_size characters of dest to start with).
1417 * Returns size of attempted result, which is
1418 * MIN (dest_size, strlen (original dest)) + strlen (src),
1419 * so if retval >= dest_size, truncation occurred.
1421 gsize
1422 g_strlcat (gchar *dest,
1423 const gchar *src,
1424 gsize dest_size)
1426 register gchar *d = dest;
1427 register const gchar *s = src;
1428 register gsize bytes_left = dest_size;
1429 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1431 g_return_val_if_fail (dest != NULL, 0);
1432 g_return_val_if_fail (src != NULL, 0);
1434 /* Find the end of dst and adjust bytes left but don't go past end */
1435 while (*d != 0 && bytes_left-- != 0)
1436 d++;
1437 dlength = d - dest;
1438 bytes_left = dest_size - dlength;
1440 if (bytes_left == 0)
1441 return dlength + strlen (s);
1443 while (*s != 0)
1445 if (bytes_left != 1)
1447 *d++ = *s;
1448 bytes_left--;
1450 s++;
1452 *d = 0;
1454 return dlength + (s - src); /* count does not include NUL */
1456 #endif /* ! HAVE_STRLCPY */
1459 * g_ascii_strdown:
1460 * @str: a string.
1461 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1463 * Converts all upper case ASCII letters to lower case ASCII letters.
1465 * Return value: a newly-allocated string, with all the upper case
1466 * characters in @str converted to lower case, with
1467 * semantics that exactly match g_ascii_tolower(). (Note
1468 * that this is unlike the old g_strdown(), which modified
1469 * the string in place.)
1471 gchar*
1472 g_ascii_strdown (const gchar *str,
1473 gssize len)
1475 gchar *result, *s;
1477 g_return_val_if_fail (str != NULL, NULL);
1479 if (len < 0)
1480 len = strlen (str);
1482 result = g_strndup (str, len);
1483 for (s = result; *s; s++)
1484 *s = g_ascii_tolower (*s);
1486 return result;
1490 * g_ascii_strup:
1491 * @str: a string.
1492 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1494 * Converts all lower case ASCII letters to upper case ASCII letters.
1496 * Return value: a newly allocated string, with all the lower case
1497 * characters in @str converted to upper case, with
1498 * semantics that exactly match g_ascii_toupper(). (Note
1499 * that this is unlike the old g_strup(), which modified
1500 * the string in place.)
1502 gchar*
1503 g_ascii_strup (const gchar *str,
1504 gssize len)
1506 gchar *result, *s;
1508 g_return_val_if_fail (str != NULL, NULL);
1510 if (len < 0)
1511 len = strlen (str);
1513 result = g_strndup (str, len);
1514 for (s = result; *s; s++)
1515 *s = g_ascii_toupper (*s);
1517 return result;
1521 * g_strdown:
1522 * @string: the string to convert.
1524 * Converts a string to lower case.
1526 * Return value: the string
1528 * Deprecated: This function is totally broken for the reasons discussed in
1529 * the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1530 * instead.
1532 gchar*
1533 g_strdown (gchar *string)
1535 register guchar *s;
1537 g_return_val_if_fail (string != NULL, NULL);
1539 s = (guchar *) string;
1541 while (*s)
1543 if (isupper (*s))
1544 *s = tolower (*s);
1545 s++;
1548 return (gchar *) string;
1552 * g_strup:
1553 * @string: the string to convert.
1555 * Converts a string to upper case.
1557 * Return value: the string
1559 * Deprecated: This function is totally broken for the reasons discussed in
1560 * the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1562 gchar*
1563 g_strup (gchar *string)
1565 register guchar *s;
1567 g_return_val_if_fail (string != NULL, NULL);
1569 s = (guchar *) string;
1571 while (*s)
1573 if (islower (*s))
1574 *s = toupper (*s);
1575 s++;
1578 return (gchar *) string;
1581 gchar*
1582 g_strreverse (gchar *string)
1584 g_return_val_if_fail (string != NULL, NULL);
1586 if (*string)
1588 register gchar *h, *t;
1590 h = string;
1591 t = string + strlen (string) - 1;
1593 while (h < t)
1595 register gchar c;
1597 c = *h;
1598 *h = *t;
1599 h++;
1600 *t = c;
1601 t--;
1605 return string;
1609 * g_ascii_tolower:
1610 * @c: any character.
1612 * Convert a character to ASCII lower case.
1614 * Unlike the standard C library tolower() function, this only
1615 * recognizes standard ASCII letters and ignores the locale, returning
1616 * all non-ASCII characters unchanged, even if they are lower case
1617 * letters in a particular character set. Also unlike the standard
1618 * library function, this takes and returns a char, not an int, so
1619 * don't call it on %EOF but no need to worry about casting to #guchar
1620 * before passing a possibly non-ASCII character in.
1622 * Return value: the result of converting @c to lower case.
1623 * If @c is not an ASCII upper case letter,
1624 * @c is returned unchanged.
1626 gchar
1627 g_ascii_tolower (gchar c)
1629 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1633 * g_ascii_toupper:
1634 * @c: any character.
1636 * Convert a character to ASCII upper case.
1638 * Unlike the standard C library toupper() function, this only
1639 * recognizes standard ASCII letters and ignores the locale, returning
1640 * all non-ASCII characters unchanged, even if they are upper case
1641 * letters in a particular character set. Also unlike the standard
1642 * library function, this takes and returns a char, not an int, so
1643 * don't call it on %EOF but no need to worry about casting to #guchar
1644 * before passing a possibly non-ASCII character in.
1646 * Return value: the result of converting @c to upper case.
1647 * If @c is not an ASCII lower case letter,
1648 * @c is returned unchanged.
1650 gchar
1651 g_ascii_toupper (gchar c)
1653 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1657 * g_ascii_digit_value:
1658 * @c: an ASCII character.
1660 * Determines the numeric value of a character as a decimal
1661 * digit. Differs from g_unichar_digit_value() because it takes
1662 * a char, so there's no worry about sign extension if characters
1663 * are signed.
1665 * Return value: If @c is a decimal digit (according to
1666 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1669 g_ascii_digit_value (gchar c)
1671 if (g_ascii_isdigit (c))
1672 return c - '0';
1673 return -1;
1677 * g_ascii_xdigit_value:
1678 * @c: an ASCII character.
1680 * Determines the numeric value of a character as a hexidecimal
1681 * digit. Differs from g_unichar_xdigit_value() because it takes
1682 * a char, so there's no worry about sign extension if characters
1683 * are signed.
1685 * Return value: If @c is a hex digit (according to
1686 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1689 g_ascii_xdigit_value (gchar c)
1691 if (c >= 'A' && c <= 'F')
1692 return c - 'A' + 10;
1693 if (c >= 'a' && c <= 'f')
1694 return c - 'a' + 10;
1695 return g_ascii_digit_value (c);
1699 * g_ascii_strcasecmp:
1700 * @s1: string to compare with @s2.
1701 * @s2: string to compare with @s1.
1703 * Compare two strings, ignoring the case of ASCII characters.
1705 * Unlike the BSD strcasecmp() function, this only recognizes standard
1706 * ASCII letters and ignores the locale, treating all non-ASCII
1707 * characters as if they are not letters.
1709 * Return value: an integer less than, equal to, or greater than
1710 * zero if @s1 is found, respectively, to be less than,
1711 * to match, or to be greater than @s2.
1713 gint
1714 g_ascii_strcasecmp (const gchar *s1,
1715 const gchar *s2)
1717 gint c1, c2;
1719 g_return_val_if_fail (s1 != NULL, 0);
1720 g_return_val_if_fail (s2 != NULL, 0);
1722 while (*s1 && *s2)
1724 c1 = (gint)(guchar) TOLOWER (*s1);
1725 c2 = (gint)(guchar) TOLOWER (*s2);
1726 if (c1 != c2)
1727 return (c1 - c2);
1728 s1++; s2++;
1731 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1735 * g_ascii_strncasecmp:
1736 * @s1: string to compare with @s2.
1737 * @s2: string to compare with @s1.
1738 * @n: number of characters to compare.
1740 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1741 * characters after the first @n in each string.
1743 * Unlike the BSD strcasecmp() function, this only recognizes standard
1744 * ASCII letters and ignores the locale, treating all non-ASCII
1745 * characters as if they are not letters.
1747 * Return value: an integer less than, equal to, or greater than zero
1748 * if the first @n bytes of @s1 is found, respectively,
1749 * to be less than, to match, or to be greater than the
1750 * first @n bytes of @s2.
1752 gint
1753 g_ascii_strncasecmp (const gchar *s1,
1754 const gchar *s2,
1755 gsize n)
1757 gint c1, c2;
1759 g_return_val_if_fail (s1 != NULL, 0);
1760 g_return_val_if_fail (s2 != NULL, 0);
1762 while (n && *s1 && *s2)
1764 n -= 1;
1765 c1 = (gint)(guchar) TOLOWER (*s1);
1766 c2 = (gint)(guchar) TOLOWER (*s2);
1767 if (c1 != c2)
1768 return (c1 - c2);
1769 s1++; s2++;
1772 if (n)
1773 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1774 else
1775 return 0;
1779 * g_strcasecmp:
1780 * @s1: a string.
1781 * @s2: a string to compare with @s1.
1783 * A case-insensitive string comparison, corresponding to the standard
1784 * strcasecmp() function on platforms which support it.
1786 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1787 * or a positive value if @s1 &gt; @s2.
1789 * Deprecated: See g_strncasecmp() for a discussion of why this function is
1790 * deprecated and how to replace it.
1792 gint
1793 g_strcasecmp (const gchar *s1,
1794 const gchar *s2)
1796 #ifdef HAVE_STRCASECMP
1797 g_return_val_if_fail (s1 != NULL, 0);
1798 g_return_val_if_fail (s2 != NULL, 0);
1800 return strcasecmp (s1, s2);
1801 #else
1802 gint c1, c2;
1804 g_return_val_if_fail (s1 != NULL, 0);
1805 g_return_val_if_fail (s2 != NULL, 0);
1807 while (*s1 && *s2)
1809 /* According to A. Cox, some platforms have islower's that
1810 * don't work right on non-uppercase
1812 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1813 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1814 if (c1 != c2)
1815 return (c1 - c2);
1816 s1++; s2++;
1819 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1820 #endif
1824 * g_strncasecmp:
1825 * @s1: a string.
1826 * @s2: a string to compare with @s1.
1827 * @n: the maximum number of characters to compare.
1829 * A case-insensitive string comparison, corresponding to the standard
1830 * strncasecmp() function on platforms which support it.
1831 * It is similar to g_strcasecmp() except it only compares the first @n
1832 * characters of the strings.
1834 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1835 * or a positive value if @s1 &gt; @s2.
1837 * Deprecated: The problem with g_strncasecmp() is that it does the
1838 * comparison by calling toupper()/tolower(). These functions are
1839 * locale-specific and operate on single bytes. However, it is impossible
1840 * to handle things correctly from an I18N standpoint by operating on
1841 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1842 * broken if your string is guaranteed to be ASCII, since it's
1843 * locale-sensitive, and it's broken if your string is localized, since
1844 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1845 * etc.
1847 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1848 * which only works on ASCII and is not locale-sensitive, and
1849 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1851 gint
1852 g_strncasecmp (const gchar *s1,
1853 const gchar *s2,
1854 gsize n)
1856 #ifdef HAVE_STRNCASECMP
1857 return strncasecmp (s1, s2, n);
1858 #else
1859 gint c1, c2;
1861 g_return_val_if_fail (s1 != NULL, 0);
1862 g_return_val_if_fail (s2 != NULL, 0);
1864 while (n && *s1 && *s2)
1866 n -= 1;
1867 /* According to A. Cox, some platforms have islower's that
1868 * don't work right on non-uppercase
1870 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1871 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1872 if (c1 != c2)
1873 return (c1 - c2);
1874 s1++; s2++;
1877 if (n)
1878 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1879 else
1880 return 0;
1881 #endif
1884 gchar*
1885 g_strdelimit (gchar *string,
1886 const gchar *delimiters,
1887 gchar new_delim)
1889 register gchar *c;
1891 g_return_val_if_fail (string != NULL, NULL);
1893 if (!delimiters)
1894 delimiters = G_STR_DELIMITERS;
1896 for (c = string; *c; c++)
1898 if (strchr (delimiters, *c))
1899 *c = new_delim;
1902 return string;
1905 gchar*
1906 g_strcanon (gchar *string,
1907 const gchar *valid_chars,
1908 gchar substitutor)
1910 register gchar *c;
1912 g_return_val_if_fail (string != NULL, NULL);
1913 g_return_val_if_fail (valid_chars != NULL, NULL);
1915 for (c = string; *c; c++)
1917 if (!strchr (valid_chars, *c))
1918 *c = substitutor;
1921 return string;
1924 gchar*
1925 g_strcompress (const gchar *source)
1927 const gchar *p = source, *octal;
1928 gchar *dest = g_malloc (strlen (source) + 1);
1929 gchar *q = dest;
1931 while (*p)
1933 if (*p == '\\')
1935 p++;
1936 switch (*p)
1938 case '0': case '1': case '2': case '3': case '4':
1939 case '5': case '6': case '7':
1940 *q = 0;
1941 octal = p;
1942 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
1944 *q = (*q * 8) + (*p - '0');
1945 p++;
1947 q++;
1948 p--;
1949 break;
1950 case 'b':
1951 *q++ = '\b';
1952 break;
1953 case 'f':
1954 *q++ = '\f';
1955 break;
1956 case 'n':
1957 *q++ = '\n';
1958 break;
1959 case 'r':
1960 *q++ = '\r';
1961 break;
1962 case 't':
1963 *q++ = '\t';
1964 break;
1965 default: /* Also handles \" and \\ */
1966 *q++ = *p;
1967 break;
1970 else
1971 *q++ = *p;
1972 p++;
1974 *q = 0;
1976 return dest;
1979 gchar *
1980 g_strescape (const gchar *source,
1981 const gchar *exceptions)
1983 const guchar *p;
1984 gchar *dest;
1985 gchar *q;
1986 guchar excmap[256];
1988 g_return_val_if_fail (source != NULL, NULL);
1990 p = (guchar *) source;
1991 /* Each source byte needs maximally four destination chars (\777) */
1992 q = dest = g_malloc (strlen (source) * 4 + 1);
1994 memset (excmap, 0, 256);
1995 if (exceptions)
1997 guchar *e = (guchar *) exceptions;
1999 while (*e)
2001 excmap[*e] = 1;
2002 e++;
2006 while (*p)
2008 if (excmap[*p])
2009 *q++ = *p;
2010 else
2012 switch (*p)
2014 case '\b':
2015 *q++ = '\\';
2016 *q++ = 'b';
2017 break;
2018 case '\f':
2019 *q++ = '\\';
2020 *q++ = 'f';
2021 break;
2022 case '\n':
2023 *q++ = '\\';
2024 *q++ = 'n';
2025 break;
2026 case '\r':
2027 *q++ = '\\';
2028 *q++ = 'r';
2029 break;
2030 case '\t':
2031 *q++ = '\\';
2032 *q++ = 't';
2033 break;
2034 case '\\':
2035 *q++ = '\\';
2036 *q++ = '\\';
2037 break;
2038 case '"':
2039 *q++ = '\\';
2040 *q++ = '"';
2041 break;
2042 default:
2043 if ((*p < ' ') || (*p >= 0177))
2045 *q++ = '\\';
2046 *q++ = '0' + (((*p) >> 6) & 07);
2047 *q++ = '0' + (((*p) >> 3) & 07);
2048 *q++ = '0' + ((*p) & 07);
2050 else
2051 *q++ = *p;
2052 break;
2055 p++;
2057 *q = 0;
2058 return dest;
2061 gchar*
2062 g_strchug (gchar *string)
2064 guchar *start;
2066 g_return_val_if_fail (string != NULL, NULL);
2068 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2071 g_memmove (string, start, strlen ((gchar *) start) + 1);
2073 return string;
2076 gchar*
2077 g_strchomp (gchar *string)
2079 gsize len;
2081 g_return_val_if_fail (string != NULL, NULL);
2083 len = strlen (string);
2084 while (len--)
2086 if (g_ascii_isspace ((guchar) string[len]))
2087 string[len] = '\0';
2088 else
2089 break;
2092 return string;
2096 * g_strsplit:
2097 * @string: a string to split.
2098 * @delimiter: a string which specifies the places at which to split the string.
2099 * The delimiter is not included in any of the resulting strings, unless
2100 * @max_tokens is reached.
2101 * @max_tokens: the maximum number of pieces to split @string into. If this is
2102 * less than 1, the string is split completely.
2104 * Splits a string into a maximum of @max_tokens pieces, using the given
2105 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2106 * to the last token.
2108 * As a special case, the result of splitting the empty string "" is an empty
2109 * vector, not a vector containing a single string. The reason for this
2110 * special case is that being able to represent a empty vector is typically
2111 * more useful than consistent handling of empty elements. If you do need
2112 * to represent empty elements, you'll need to check for the empty string
2113 * before calling g_strsplit().
2115 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2116 * g_strfreev() to free it.
2118 gchar**
2119 g_strsplit (const gchar *string,
2120 const gchar *delimiter,
2121 gint max_tokens)
2123 GSList *string_list = NULL, *slist;
2124 gchar **str_array, *s;
2125 guint n = 0;
2126 const gchar *remainder;
2128 g_return_val_if_fail (string != NULL, NULL);
2129 g_return_val_if_fail (delimiter != NULL, NULL);
2130 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2132 if (max_tokens < 1)
2133 max_tokens = G_MAXINT;
2135 remainder = string;
2136 s = strstr (remainder, delimiter);
2137 if (s)
2139 gsize delimiter_len = strlen (delimiter);
2141 while (--max_tokens && s)
2143 gsize len;
2144 gchar *new_string;
2146 len = s - remainder;
2147 new_string = g_new (gchar, len + 1);
2148 strncpy (new_string, remainder, len);
2149 new_string[len] = 0;
2150 string_list = g_slist_prepend (string_list, new_string);
2151 n++;
2152 remainder = s + delimiter_len;
2153 s = strstr (remainder, delimiter);
2156 if (*string)
2158 n++;
2159 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2162 str_array = g_new (gchar*, n + 1);
2164 str_array[n--] = NULL;
2165 for (slist = string_list; slist; slist = slist->next)
2166 str_array[n--] = slist->data;
2168 g_slist_free (string_list);
2170 return str_array;
2174 * g_strfreev:
2175 * @str_array: a %NULL-terminated array of strings to free.
2177 * Frees a %NULL-terminated array of strings, and the array itself.
2178 * If called on a %NULL value, g_strfreev() simply returns.
2180 void
2181 g_strfreev (gchar **str_array)
2183 if (str_array)
2185 int i;
2187 for(i = 0; str_array[i] != NULL; i++)
2188 g_free(str_array[i]);
2190 g_free (str_array);
2195 * g_strdupv:
2196 * @str_array: %NULL-terminated array of strings.
2198 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2199 * the new array should be freed by first freeing each string, then
2200 * the array itself. g_strfreev() does this for you. If called
2201 * on a %NULL value, g_strdupv() simply returns %NULL.
2203 * Return value: a new %NULL-terminated array of strings.
2205 gchar**
2206 g_strdupv (gchar **str_array)
2208 if (str_array)
2210 gint i;
2211 gchar **retval;
2213 i = 0;
2214 while (str_array[i])
2215 ++i;
2217 retval = g_new (gchar*, i + 1);
2219 i = 0;
2220 while (str_array[i])
2222 retval[i] = g_strdup (str_array[i]);
2223 ++i;
2225 retval[i] = NULL;
2227 return retval;
2229 else
2230 return NULL;
2233 gchar*
2234 g_strjoinv (const gchar *separator,
2235 gchar **str_array)
2237 gchar *string;
2238 gchar *ptr;
2240 g_return_val_if_fail (str_array != NULL, NULL);
2242 if (separator == NULL)
2243 separator = "";
2245 if (*str_array)
2247 gint i;
2248 gsize len;
2249 gsize separator_len;
2251 separator_len = strlen (separator);
2252 /* First part, getting length */
2253 len = 1 + strlen (str_array[0]);
2254 for (i = 1; str_array[i] != NULL; i++)
2255 len += strlen (str_array[i]);
2256 len += separator_len * (i - 1);
2258 /* Second part, building string */
2259 string = g_new (gchar, len);
2260 ptr = g_stpcpy (string, *str_array);
2261 for (i = 1; str_array[i] != NULL; i++)
2263 ptr = g_stpcpy (ptr, separator);
2264 ptr = g_stpcpy (ptr, str_array[i]);
2267 else
2268 string = g_strdup ("");
2270 return string;
2273 gchar*
2274 g_strjoin (const gchar *separator,
2275 ...)
2277 gchar *string, *s;
2278 va_list args;
2279 gsize len;
2280 gsize separator_len;
2281 gchar *ptr;
2283 if (separator == NULL)
2284 separator = "";
2286 separator_len = strlen (separator);
2288 va_start (args, separator);
2290 s = va_arg (args, gchar*);
2292 if (s)
2294 /* First part, getting length */
2295 len = 1 + strlen (s);
2297 s = va_arg (args, gchar*);
2298 while (s)
2300 len += separator_len + strlen (s);
2301 s = va_arg (args, gchar*);
2303 va_end (args);
2305 /* Second part, building string */
2306 string = g_new (gchar, len);
2308 va_start (args, separator);
2310 s = va_arg (args, gchar*);
2311 ptr = g_stpcpy (string, s);
2313 s = va_arg (args, gchar*);
2314 while (s)
2316 ptr = g_stpcpy (ptr, separator);
2317 ptr = g_stpcpy (ptr, s);
2318 s = va_arg (args, gchar*);
2321 else
2322 string = g_strdup ("");
2324 va_end (args);
2326 return string;
2331 * g_strstr_len:
2332 * @haystack: a string.
2333 * @haystack_len: the maximum length of @haystack.
2334 * @needle: the string to search for.
2336 * Searches the string @haystack for the first occurrence
2337 * of the string @needle, limiting the length of the search
2338 * to @haystack_len.
2340 * Return value: a pointer to the found occurrence, or
2341 * %NULL if not found.
2343 gchar *
2344 g_strstr_len (const gchar *haystack,
2345 gssize haystack_len,
2346 const gchar *needle)
2348 g_return_val_if_fail (haystack != NULL, NULL);
2349 g_return_val_if_fail (needle != NULL, NULL);
2351 if (haystack_len < 0)
2352 return strstr (haystack, needle);
2353 else
2355 const gchar *p = haystack;
2356 gsize needle_len = strlen (needle);
2357 const gchar *end;
2358 gsize i;
2360 if (needle_len == 0)
2361 return (gchar *)haystack;
2363 if (haystack_len < needle_len)
2364 return NULL;
2366 end = haystack + haystack_len - needle_len;
2368 while (*p && p <= end)
2370 for (i = 0; i < needle_len; i++)
2371 if (p[i] != needle[i])
2372 goto next;
2374 return (gchar *)p;
2376 next:
2377 p++;
2380 return NULL;
2385 * g_strrstr:
2386 * @haystack: a nul-terminated string.
2387 * @needle: the nul-terminated string to search for.
2389 * Searches the string @haystack for the last occurrence
2390 * of the string @needle.
2392 * Return value: a pointer to the found occurrence, or
2393 * %NULL if not found.
2395 gchar *
2396 g_strrstr (const gchar *haystack,
2397 const gchar *needle)
2399 gsize i;
2400 gsize needle_len;
2401 gsize haystack_len;
2402 const gchar *p;
2404 g_return_val_if_fail (haystack != NULL, NULL);
2405 g_return_val_if_fail (needle != NULL, NULL);
2407 needle_len = strlen (needle);
2408 haystack_len = strlen (haystack);
2410 if (needle_len == 0)
2411 return (gchar *)haystack;
2413 if (haystack_len < needle_len)
2414 return NULL;
2416 p = haystack + haystack_len - needle_len;
2418 while (p >= haystack)
2420 for (i = 0; i < needle_len; i++)
2421 if (p[i] != needle[i])
2422 goto next;
2424 return (gchar *)p;
2426 next:
2427 p--;
2430 return NULL;
2434 * g_strrstr_len:
2435 * @haystack: a nul-terminated string.
2436 * @haystack_len: the maximum length of @haystack.
2437 * @needle: the nul-terminated string to search for.
2439 * Searches the string @haystack for the last occurrence
2440 * of the string @needle, limiting the length of the search
2441 * to @haystack_len.
2443 * Return value: a pointer to the found occurrence, or
2444 * %NULL if not found.
2446 gchar *
2447 g_strrstr_len (const gchar *haystack,
2448 gssize haystack_len,
2449 const gchar *needle)
2451 g_return_val_if_fail (haystack != NULL, NULL);
2452 g_return_val_if_fail (needle != NULL, NULL);
2454 if (haystack_len < 0)
2455 return g_strrstr (haystack, needle);
2456 else
2458 gsize needle_len = strlen (needle);
2459 const gchar *haystack_max = haystack + haystack_len;
2460 const gchar *p = haystack;
2461 gsize i;
2463 while (p < haystack_max && *p)
2464 p++;
2466 if (p < haystack + needle_len)
2467 return NULL;
2469 p -= needle_len;
2471 while (p >= haystack)
2473 for (i = 0; i < needle_len; i++)
2474 if (p[i] != needle[i])
2475 goto next;
2477 return (gchar *)p;
2479 next:
2480 p--;
2483 return NULL;
2489 * g_str_has_suffix:
2490 * @str: a nul-terminated string.
2491 * @suffix: the nul-terminated suffix to look for.
2493 * Looks whether the string @str ends with @suffix.
2495 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2497 * Since: 2.2
2499 gboolean
2500 g_str_has_suffix (const gchar *str,
2501 const gchar *suffix)
2503 int str_len;
2504 int suffix_len;
2506 g_return_val_if_fail (str != NULL, FALSE);
2507 g_return_val_if_fail (suffix != NULL, FALSE);
2509 str_len = strlen (str);
2510 suffix_len = strlen (suffix);
2512 if (str_len < suffix_len)
2513 return FALSE;
2515 return strcmp (str + str_len - suffix_len, suffix) == 0;
2519 * g_str_has_prefix:
2520 * @str: a nul-terminated string.
2521 * @prefix: the nul-terminated prefix to look for.
2523 * Looks whether the string @str begins with @prefix.
2525 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2527 * Since: 2.2
2529 gboolean
2530 g_str_has_prefix (const gchar *str,
2531 const gchar *prefix)
2533 int str_len;
2534 int prefix_len;
2536 g_return_val_if_fail (str != NULL, FALSE);
2537 g_return_val_if_fail (prefix != NULL, FALSE);
2539 str_len = strlen (str);
2540 prefix_len = strlen (prefix);
2542 if (str_len < prefix_len)
2543 return FALSE;
2545 return strncmp (str, prefix, prefix_len) == 0;