Return negative values. (#416062)
[glib.git] / glib / gstrfuncs.c
blob7786f106ce124bb389304fd7e96ea02e016cf0af
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 #include "galias.h"
52 #ifdef G_OS_WIN32
53 #include <windows.h>
54 #endif
56 /* do not include <unistd.h> in this place since it
57 * interferes with g_strsignal() on some OSes
60 static const guint16 ascii_table_data[256] = {
61 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
62 0x004, 0x104, 0x104, 0x004, 0x104, 0x104, 0x004, 0x004,
63 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
64 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004, 0x004,
65 0x140, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
66 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
67 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459, 0x459,
68 0x459, 0x459, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
69 0x0d0, 0x653, 0x653, 0x653, 0x653, 0x653, 0x653, 0x253,
70 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
71 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253, 0x253,
72 0x253, 0x253, 0x253, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x0d0,
73 0x0d0, 0x473, 0x473, 0x473, 0x473, 0x473, 0x473, 0x073,
74 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
75 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073, 0x073,
76 0x073, 0x073, 0x073, 0x0d0, 0x0d0, 0x0d0, 0x0d0, 0x004
77 /* the upper 128 are all zeroes */
80 const guint16 * const g_ascii_table = ascii_table_data;
82 gchar*
83 g_strdup (const gchar *str)
85 gchar *new_str;
86 gsize length;
88 if (str)
90 length = strlen (str) + 1;
91 new_str = g_new (char, length);
92 memcpy (new_str, str, length);
94 else
95 new_str = NULL;
97 return new_str;
100 gpointer
101 g_memdup (gconstpointer mem,
102 guint byte_size)
104 gpointer new_mem;
106 if (mem)
108 new_mem = g_malloc (byte_size);
109 memcpy (new_mem, mem, byte_size);
111 else
112 new_mem = NULL;
114 return new_mem;
117 gchar*
118 g_strndup (const gchar *str,
119 gsize n)
121 gchar *new_str;
123 if (str)
125 new_str = g_new (gchar, n + 1);
126 strncpy (new_str, str, n);
127 new_str[n] = '\0';
129 else
130 new_str = NULL;
132 return new_str;
135 gchar*
136 g_strnfill (gsize length,
137 gchar fill_char)
139 gchar *str;
141 str = g_new (gchar, length + 1);
142 memset (str, (guchar)fill_char, length);
143 str[length] = '\0';
145 return str;
149 * g_stpcpy:
150 * @dest: destination buffer.
151 * @src: source string.
153 * Copies a nul-terminated string into the dest buffer, include the
154 * trailing nul, and return a pointer to the trailing nul byte.
155 * This is useful for concatenating multiple strings together
156 * without having to repeatedly scan for the end.
158 * Return value: a pointer to trailing nul byte.
160 gchar *
161 g_stpcpy (gchar *dest,
162 const gchar *src)
164 #ifdef HAVE_STPCPY
165 g_return_val_if_fail (dest != NULL, NULL);
166 g_return_val_if_fail (src != NULL, NULL);
167 return stpcpy (dest, src);
168 #else
169 register gchar *d = dest;
170 register const gchar *s = src;
172 g_return_val_if_fail (dest != NULL, NULL);
173 g_return_val_if_fail (src != NULL, NULL);
175 *d++ = *s;
176 while (*s++ != '\0');
178 return d - 1;
179 #endif
182 gchar*
183 g_strdup_vprintf (const gchar *format,
184 va_list args)
186 gchar *string = NULL;
188 g_vasprintf (&string, format, args);
190 return string;
193 gchar*
194 g_strdup_printf (const gchar *format,
195 ...)
197 gchar *buffer;
198 va_list args;
200 va_start (args, format);
201 buffer = g_strdup_vprintf (format, args);
202 va_end (args);
204 return buffer;
207 gchar*
208 g_strconcat (const gchar *string1, ...)
210 gsize l;
211 va_list args;
212 gchar *s;
213 gchar *concat;
214 gchar *ptr;
216 if (!string1)
217 return 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 */
339 int strtod_errno;
341 g_return_val_if_fail (nptr != NULL, 0);
343 fail_pos = NULL;
345 locale_data = localeconv ();
346 decimal_point = locale_data->decimal_point;
347 decimal_point_len = strlen (decimal_point);
349 g_assert (decimal_point_len != 0);
351 decimal_point_pos = NULL;
352 end = NULL;
354 if (decimal_point[0] != '.' ||
355 decimal_point[1] != 0)
357 p = nptr;
358 /* Skip leading space */
359 while (g_ascii_isspace (*p))
360 p++;
362 /* Skip leading optional sign */
363 if (*p == '+' || *p == '-')
364 p++;
366 if (p[0] == '0' &&
367 (p[1] == 'x' || p[1] == 'X'))
369 p += 2;
370 /* HEX - find the (optional) decimal point */
372 while (g_ascii_isxdigit (*p))
373 p++;
375 if (*p == '.')
376 decimal_point_pos = p++;
378 while (g_ascii_isxdigit (*p))
379 p++;
381 if (*p == 'p' || *p == 'P')
382 p++;
383 if (*p == '+' || *p == '-')
384 p++;
385 while (g_ascii_isdigit (*p))
386 p++;
388 end = p;
390 else if (g_ascii_isdigit (*p) || *p == '.')
392 while (g_ascii_isdigit (*p))
393 p++;
395 if (*p == '.')
396 decimal_point_pos = p++;
398 while (g_ascii_isdigit (*p))
399 p++;
401 if (*p == 'e' || *p == 'E')
402 p++;
403 if (*p == '+' || *p == '-')
404 p++;
405 while (g_ascii_isdigit (*p))
406 p++;
408 end = p;
410 /* For the other cases, we need not convert the decimal point */
413 if (decimal_point_pos)
415 char *copy, *c;
417 /* We need to convert the '.' to the locale specific decimal point */
418 copy = g_malloc (end - nptr + 1 + decimal_point_len);
420 c = copy;
421 memcpy (c, nptr, decimal_point_pos - nptr);
422 c += decimal_point_pos - nptr;
423 memcpy (c, decimal_point, decimal_point_len);
424 c += decimal_point_len;
425 memcpy (c, decimal_point_pos + 1, end - (decimal_point_pos + 1));
426 c += end - (decimal_point_pos + 1);
427 *c = 0;
429 errno = 0;
430 val = strtod (copy, &fail_pos);
431 strtod_errno = errno;
433 if (fail_pos)
435 if (fail_pos - copy > decimal_point_pos - nptr)
436 fail_pos = (char *)nptr + (fail_pos - copy) - (decimal_point_len - 1);
437 else
438 fail_pos = (char *)nptr + (fail_pos - copy);
441 g_free (copy);
444 else if (end)
446 char *copy;
448 copy = g_malloc (end - (char *)nptr + 1);
449 memcpy (copy, nptr, end - nptr);
450 *(copy + (end - (char *)nptr)) = 0;
452 errno = 0;
453 val = strtod (copy, &fail_pos);
454 strtod_errno = errno;
456 if (fail_pos)
458 fail_pos = (char *)nptr + (fail_pos - copy);
461 g_free (copy);
463 else
465 errno = 0;
466 val = strtod (nptr, &fail_pos);
467 strtod_errno = errno;
470 if (endptr)
471 *endptr = fail_pos;
473 errno = strtod_errno;
475 return val;
480 * g_ascii_dtostr:
481 * @buffer: A buffer to place the resulting string in
482 * @buf_len: The length of the buffer.
483 * @d: The #gdouble to convert
485 * Converts a #gdouble to a string, using the '.' as
486 * decimal point.
488 * This functions generates enough precision that converting
489 * the string back using g_ascii_strtod() gives the same machine-number
490 * (on machines with IEEE compatible 64bit doubles). It is
491 * guaranteed that the size of the resulting string will never
492 * be larger than @G_ASCII_DTOSTR_BUF_SIZE bytes.
494 * Return value: The pointer to the buffer with the converted string.
496 gchar *
497 g_ascii_dtostr (gchar *buffer,
498 gint buf_len,
499 gdouble d)
501 return g_ascii_formatd (buffer, buf_len, "%.17g", d);
505 * g_ascii_formatd:
506 * @buffer: A buffer to place the resulting string in
507 * @buf_len: The length of the buffer.
508 * @format: The printf()-style format to use for the
509 * code to use for converting.
510 * @d: The #gdouble to convert
512 * Converts a #gdouble to a string, using the '.' as
513 * decimal point. To format the number you pass in
514 * a printf()-style format string. Allowed conversion
515 * specifiers are 'e', 'E', 'f', 'F', 'g' and 'G'.
517 * If you just want to want to serialize the value into a
518 * string, use g_ascii_dtostr().
520 * Return value: The pointer to the buffer with the converted string.
522 gchar *
523 g_ascii_formatd (gchar *buffer,
524 gint buf_len,
525 const gchar *format,
526 gdouble d)
528 struct lconv *locale_data;
529 const char *decimal_point;
530 int decimal_point_len;
531 gchar *p;
532 int rest_len;
533 gchar format_char;
535 g_return_val_if_fail (buffer != NULL, NULL);
536 g_return_val_if_fail (format[0] == '%', NULL);
537 g_return_val_if_fail (strpbrk (format + 1, "'l%") == NULL, NULL);
539 format_char = format[strlen (format) - 1];
541 g_return_val_if_fail (format_char == 'e' || format_char == 'E' ||
542 format_char == 'f' || format_char == 'F' ||
543 format_char == 'g' || format_char == 'G',
544 NULL);
546 if (format[0] != '%')
547 return NULL;
549 if (strpbrk (format + 1, "'l%"))
550 return NULL;
552 if (!(format_char == 'e' || format_char == 'E' ||
553 format_char == 'f' || format_char == 'F' ||
554 format_char == 'g' || format_char == 'G'))
555 return NULL;
558 _g_snprintf (buffer, buf_len, format, d);
560 locale_data = localeconv ();
561 decimal_point = locale_data->decimal_point;
562 decimal_point_len = strlen (decimal_point);
564 g_assert (decimal_point_len != 0);
566 if (decimal_point[0] != '.' ||
567 decimal_point[1] != 0)
569 p = buffer;
571 while (g_ascii_isspace (*p))
572 p++;
574 if (*p == '+' || *p == '-')
575 p++;
577 while (isdigit ((guchar)*p))
578 p++;
580 if (strncmp (p, decimal_point, decimal_point_len) == 0)
582 *p = '.';
583 p++;
584 if (decimal_point_len > 1) {
585 rest_len = strlen (p + (decimal_point_len-1));
586 memmove (p, p + (decimal_point_len-1),
587 rest_len);
588 p[rest_len] = 0;
594 return buffer;
597 static guint64
598 g_parse_long_long (const gchar *nptr,
599 gchar **endptr,
600 guint base,
601 gboolean *negative)
603 /* this code is based on on the strtol(3) code from GNU libc released under
604 * the GNU Lesser General Public License.
606 * Copyright (C) 1991,92,94,95,96,97,98,99,2000,01,02
607 * Free Software Foundation, Inc.
609 #define ISSPACE(c) ((c) == ' ' || (c) == '\f' || (c) == '\n' || \
610 (c) == '\r' || (c) == '\t' || (c) == '\v')
611 #define ISUPPER(c) ((c) >= 'A' && (c) <= 'Z')
612 #define ISLOWER(c) ((c) >= 'a' && (c) <= 'z')
613 #define ISALPHA(c) (ISUPPER (c) || ISLOWER (c))
614 #define TOUPPER(c) (ISLOWER (c) ? (c) - 'a' + 'A' : (c))
615 #define TOLOWER(c) (ISUPPER (c) ? (c) - 'A' + 'a' : (c))
616 gboolean overflow;
617 guint64 cutoff;
618 guint64 cutlim;
619 guint64 ui64;
620 const gchar *s, *save;
621 guchar c;
623 g_return_val_if_fail (nptr != NULL, 0);
625 if (base == 1 || base > 36)
627 errno = EINVAL;
628 return 0;
631 save = s = nptr;
633 /* Skip white space. */
634 while (ISSPACE (*s))
635 ++s;
637 if (G_UNLIKELY (!*s))
638 goto noconv;
640 /* Check for a sign. */
641 *negative = FALSE;
642 if (*s == '-')
644 *negative = TRUE;
645 ++s;
647 else if (*s == '+')
648 ++s;
650 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
651 if (*s == '0')
653 if ((base == 0 || base == 16) && TOUPPER (s[1]) == 'X')
655 s += 2;
656 base = 16;
658 else if (base == 0)
659 base = 8;
661 else if (base == 0)
662 base = 10;
664 /* Save the pointer so we can check later if anything happened. */
665 save = s;
666 cutoff = G_MAXUINT64 / base;
667 cutlim = G_MAXUINT64 % base;
669 overflow = FALSE;
670 ui64 = 0;
671 c = *s;
672 for (; c; c = *++s)
674 if (c >= '0' && c <= '9')
675 c -= '0';
676 else if (ISALPHA (c))
677 c = TOUPPER (c) - 'A' + 10;
678 else
679 break;
680 if (c >= base)
681 break;
682 /* Check for overflow. */
683 if (ui64 > cutoff || (ui64 == cutoff && c > cutlim))
684 overflow = TRUE;
685 else
687 ui64 *= base;
688 ui64 += c;
692 /* Check if anything actually happened. */
693 if (s == save)
694 goto noconv;
696 /* Store in ENDPTR the address of one character
697 past the last character we converted. */
698 if (endptr)
699 *endptr = (gchar*) s;
701 if (G_UNLIKELY (overflow))
703 errno = ERANGE;
704 return G_MAXUINT64;
707 return ui64;
709 noconv:
710 /* We must handle a special case here: the base is 0 or 16 and the
711 first two characters are '0' and 'x', but the rest are no
712 hexadecimal digits. This is no error case. We return 0 and
713 ENDPTR points to the `x`. */
714 if (endptr)
716 if (save - nptr >= 2 && TOUPPER (save[-1]) == 'X'
717 && save[-2] == '0')
718 *endptr = (gchar*) &save[-1];
719 else
720 /* There was no number to convert. */
721 *endptr = (gchar*) nptr;
723 return 0;
727 * g_ascii_strtoull:
728 * @nptr: the string to convert to a numeric value.
729 * @endptr: if non-%NULL, it returns the character after
730 * the last character used in the conversion.
731 * @base: to be used for the conversion, 2..36 or 0
733 * Converts a string to a #guint64 value.
734 * This function behaves like the standard strtoull() function
735 * does in the C locale. It does this without actually
736 * changing the current locale, since that would not be
737 * thread-safe.
739 * This function is typically used when reading configuration
740 * files or other non-user input that should be locale independent.
741 * To handle input from the user you should normally use the
742 * locale-sensitive system strtoull() function.
744 * If the correct value would cause overflow, %G_MAXUINT64
745 * is returned, and %ERANGE is stored in %errno. If the base is
746 * outside the valid range, zero is returned, and %EINVAL is stored
747 * in %errno. If the string conversion fails, zero is returned, and
748 * @endptr returns @nptr (if @endptr is non-%NULL).
750 * Return value: the #guint64 value or zero on error.
752 * Since: 2.2
754 guint64
755 g_ascii_strtoull (const gchar *nptr,
756 gchar **endptr,
757 guint base)
759 gboolean negative;
760 guint64 result;
762 result = g_parse_long_long (nptr, endptr, base, &negative);
764 /* Return the result of the appropriate sign. */
765 return negative ? -result : result;
769 * g_ascii_strtoll:
770 * @nptr: the string to convert to a numeric value.
771 * @endptr: if non-%NULL, it returns the character after
772 * the last character used in the conversion.
773 * @base: to be used for the conversion, 2..36 or 0
775 * Converts a string to a #gint64 value.
776 * This function behaves like the standard strtoll() function
777 * does in the C locale. It does this without actually
778 * changing the current locale, since that would not be
779 * thread-safe.
781 * This function is typically used when reading configuration
782 * files or other non-user input that should be locale independent.
783 * To handle input from the user you should normally use the
784 * locale-sensitive system strtoll() function.
786 * If the correct value would cause overflow, %G_MAXINT64 or %G_MININT64
787 * is returned, and %ERANGE is stored in %errno. If the base is
788 * outside the valid range, zero is returned, and %EINVAL is stored
789 * in %errno. If the string conversion fails, zero is returned, and
790 * @endptr returns @nptr (if @endptr is non-%NULL).
792 * Return value: the #gint64 value or zero on error.
794 * Since: 2.12
796 gint64
797 g_ascii_strtoll (const gchar *nptr,
798 gchar **endptr,
799 guint base)
801 gboolean negative;
802 guint64 result;
804 result = g_parse_long_long (nptr, endptr, base, &negative);
806 if (negative && result > (guint64) G_MININT64)
808 errno = ERANGE;
809 return G_MININT64;
811 else if (!negative && result > (guint64) G_MAXINT64)
813 errno = ERANGE;
814 return G_MAXINT64;
816 else if (negative)
817 return - (gint64) result;
818 else
819 return (gint64) result;
822 G_CONST_RETURN gchar*
823 g_strerror (gint errnum)
825 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
826 char *msg;
827 int saved_errno = errno;
829 #ifdef HAVE_STRERROR
830 const char *msg_locale;
832 msg_locale = strerror (errnum);
833 if (g_get_charset (NULL))
835 errno = saved_errno;
836 return msg_locale;
838 else
840 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
841 if (msg_utf8)
843 /* Stick in the quark table so that we can return a static result
845 GQuark msg_quark = g_quark_from_string (msg_utf8);
846 g_free (msg_utf8);
848 msg_utf8 = (gchar *) g_quark_to_string (msg_quark);
849 errno = saved_errno;
850 return msg_utf8;
853 #elif NO_SYS_ERRLIST
854 switch (errnum)
856 #ifdef E2BIG
857 case E2BIG: return "argument list too long";
858 #endif
859 #ifdef EACCES
860 case EACCES: return "permission denied";
861 #endif
862 #ifdef EADDRINUSE
863 case EADDRINUSE: return "address already in use";
864 #endif
865 #ifdef EADDRNOTAVAIL
866 case EADDRNOTAVAIL: return "can't assign requested address";
867 #endif
868 #ifdef EADV
869 case EADV: return "advertise error";
870 #endif
871 #ifdef EAFNOSUPPORT
872 case EAFNOSUPPORT: return "address family not supported by protocol family";
873 #endif
874 #ifdef EAGAIN
875 case EAGAIN: return "try again";
876 #endif
877 #ifdef EALIGN
878 case EALIGN: return "EALIGN";
879 #endif
880 #ifdef EALREADY
881 case EALREADY: return "operation already in progress";
882 #endif
883 #ifdef EBADE
884 case EBADE: return "bad exchange descriptor";
885 #endif
886 #ifdef EBADF
887 case EBADF: return "bad file number";
888 #endif
889 #ifdef EBADFD
890 case EBADFD: return "file descriptor in bad state";
891 #endif
892 #ifdef EBADMSG
893 case EBADMSG: return "not a data message";
894 #endif
895 #ifdef EBADR
896 case EBADR: return "bad request descriptor";
897 #endif
898 #ifdef EBADRPC
899 case EBADRPC: return "RPC structure is bad";
900 #endif
901 #ifdef EBADRQC
902 case EBADRQC: return "bad request code";
903 #endif
904 #ifdef EBADSLT
905 case EBADSLT: return "invalid slot";
906 #endif
907 #ifdef EBFONT
908 case EBFONT: return "bad font file format";
909 #endif
910 #ifdef EBUSY
911 case EBUSY: return "mount device busy";
912 #endif
913 #ifdef ECHILD
914 case ECHILD: return "no children";
915 #endif
916 #ifdef ECHRNG
917 case ECHRNG: return "channel number out of range";
918 #endif
919 #ifdef ECOMM
920 case ECOMM: return "communication error on send";
921 #endif
922 #ifdef ECONNABORTED
923 case ECONNABORTED: return "software caused connection abort";
924 #endif
925 #ifdef ECONNREFUSED
926 case ECONNREFUSED: return "connection refused";
927 #endif
928 #ifdef ECONNRESET
929 case ECONNRESET: return "connection reset by peer";
930 #endif
931 #if defined(EDEADLK) && (!defined(EWOULDBLOCK) || (EDEADLK != EWOULDBLOCK))
932 case EDEADLK: return "resource deadlock avoided";
933 #endif
934 #ifdef EDEADLOCK
935 case EDEADLOCK: return "resource deadlock avoided";
936 #endif
937 #ifdef EDESTADDRREQ
938 case EDESTADDRREQ: return "destination address required";
939 #endif
940 #ifdef EDIRTY
941 case EDIRTY: return "mounting a dirty fs w/o force";
942 #endif
943 #ifdef EDOM
944 case EDOM: return "math argument out of range";
945 #endif
946 #ifdef EDOTDOT
947 case EDOTDOT: return "cross mount point";
948 #endif
949 #ifdef EDQUOT
950 case EDQUOT: return "disk quota exceeded";
951 #endif
952 #ifdef EDUPPKG
953 case EDUPPKG: return "duplicate package name";
954 #endif
955 #ifdef EEXIST
956 case EEXIST: return "file already exists";
957 #endif
958 #ifdef EFAULT
959 case EFAULT: return "bad address in system call argument";
960 #endif
961 #ifdef EFBIG
962 case EFBIG: return "file too large";
963 #endif
964 #ifdef EHOSTDOWN
965 case EHOSTDOWN: return "host is down";
966 #endif
967 #ifdef EHOSTUNREACH
968 case EHOSTUNREACH: return "host is unreachable";
969 #endif
970 #ifdef EIDRM
971 case EIDRM: return "identifier removed";
972 #endif
973 #ifdef EINIT
974 case EINIT: return "initialization error";
975 #endif
976 #ifdef EINPROGRESS
977 case EINPROGRESS: return "operation now in progress";
978 #endif
979 #ifdef EINTR
980 case EINTR: return "interrupted system call";
981 #endif
982 #ifdef EINVAL
983 case EINVAL: return "invalid argument";
984 #endif
985 #ifdef EIO
986 case EIO: return "I/O error";
987 #endif
988 #ifdef EISCONN
989 case EISCONN: return "socket is already connected";
990 #endif
991 #ifdef EISDIR
992 case EISDIR: return "is a directory";
993 #endif
994 #ifdef EISNAME
995 case EISNAM: return "is a name file";
996 #endif
997 #ifdef ELBIN
998 case ELBIN: return "ELBIN";
999 #endif
1000 #ifdef EL2HLT
1001 case EL2HLT: return "level 2 halted";
1002 #endif
1003 #ifdef EL2NSYNC
1004 case EL2NSYNC: return "level 2 not synchronized";
1005 #endif
1006 #ifdef EL3HLT
1007 case EL3HLT: return "level 3 halted";
1008 #endif
1009 #ifdef EL3RST
1010 case EL3RST: return "level 3 reset";
1011 #endif
1012 #ifdef ELIBACC
1013 case ELIBACC: return "can not access a needed shared library";
1014 #endif
1015 #ifdef ELIBBAD
1016 case ELIBBAD: return "accessing a corrupted shared library";
1017 #endif
1018 #ifdef ELIBEXEC
1019 case ELIBEXEC: return "can not exec a shared library directly";
1020 #endif
1021 #ifdef ELIBMAX
1022 case ELIBMAX: return "attempting to link in more shared libraries than system limit";
1023 #endif
1024 #ifdef ELIBSCN
1025 case ELIBSCN: return ".lib section in a.out corrupted";
1026 #endif
1027 #ifdef ELNRNG
1028 case ELNRNG: return "link number out of range";
1029 #endif
1030 #ifdef ELOOP
1031 case ELOOP: return "too many levels of symbolic links";
1032 #endif
1033 #ifdef EMFILE
1034 case EMFILE: return "too many open files";
1035 #endif
1036 #ifdef EMLINK
1037 case EMLINK: return "too many links";
1038 #endif
1039 #ifdef EMSGSIZE
1040 case EMSGSIZE: return "message too long";
1041 #endif
1042 #ifdef EMULTIHOP
1043 case EMULTIHOP: return "multihop attempted";
1044 #endif
1045 #ifdef ENAMETOOLONG
1046 case ENAMETOOLONG: return "file name too long";
1047 #endif
1048 #ifdef ENAVAIL
1049 case ENAVAIL: return "not available";
1050 #endif
1051 #ifdef ENET
1052 case ENET: return "ENET";
1053 #endif
1054 #ifdef ENETDOWN
1055 case ENETDOWN: return "network is down";
1056 #endif
1057 #ifdef ENETRESET
1058 case ENETRESET: return "network dropped connection on reset";
1059 #endif
1060 #ifdef ENETUNREACH
1061 case ENETUNREACH: return "network is unreachable";
1062 #endif
1063 #ifdef ENFILE
1064 case ENFILE: return "file table overflow";
1065 #endif
1066 #ifdef ENOANO
1067 case ENOANO: return "anode table overflow";
1068 #endif
1069 #if defined(ENOBUFS) && (!defined(ENOSR) || (ENOBUFS != ENOSR))
1070 case ENOBUFS: return "no buffer space available";
1071 #endif
1072 #ifdef ENOCSI
1073 case ENOCSI: return "no CSI structure available";
1074 #endif
1075 #ifdef ENODATA
1076 case ENODATA: return "no data available";
1077 #endif
1078 #ifdef ENODEV
1079 case ENODEV: return "no such device";
1080 #endif
1081 #ifdef ENOENT
1082 case ENOENT: return "no such file or directory";
1083 #endif
1084 #ifdef ENOEXEC
1085 case ENOEXEC: return "exec format error";
1086 #endif
1087 #ifdef ENOLCK
1088 case ENOLCK: return "no locks available";
1089 #endif
1090 #ifdef ENOLINK
1091 case ENOLINK: return "link has be severed";
1092 #endif
1093 #ifdef ENOMEM
1094 case ENOMEM: return "not enough memory";
1095 #endif
1096 #ifdef ENOMSG
1097 case ENOMSG: return "no message of desired type";
1098 #endif
1099 #ifdef ENONET
1100 case ENONET: return "machine is not on the network";
1101 #endif
1102 #ifdef ENOPKG
1103 case ENOPKG: return "package not installed";
1104 #endif
1105 #ifdef ENOPROTOOPT
1106 case ENOPROTOOPT: return "bad proocol option";
1107 #endif
1108 #ifdef ENOSPC
1109 case ENOSPC: return "no space left on device";
1110 #endif
1111 #ifdef ENOSR
1112 case ENOSR: return "out of stream resources";
1113 #endif
1114 #ifdef ENOSTR
1115 case ENOSTR: return "not a stream device";
1116 #endif
1117 #ifdef ENOSYM
1118 case ENOSYM: return "unresolved symbol name";
1119 #endif
1120 #ifdef ENOSYS
1121 case ENOSYS: return "function not implemented";
1122 #endif
1123 #ifdef ENOTBLK
1124 case ENOTBLK: return "block device required";
1125 #endif
1126 #ifdef ENOTCONN
1127 case ENOTCONN: return "socket is not connected";
1128 #endif
1129 #ifdef ENOTDIR
1130 case ENOTDIR: return "not a directory";
1131 #endif
1132 #ifdef ENOTEMPTY
1133 case ENOTEMPTY: return "directory not empty";
1134 #endif
1135 #ifdef ENOTNAM
1136 case ENOTNAM: return "not a name file";
1137 #endif
1138 #ifdef ENOTSOCK
1139 case ENOTSOCK: return "socket operation on non-socket";
1140 #endif
1141 #ifdef ENOTTY
1142 case ENOTTY: return "inappropriate device for ioctl";
1143 #endif
1144 #ifdef ENOTUNIQ
1145 case ENOTUNIQ: return "name not unique on network";
1146 #endif
1147 #ifdef ENXIO
1148 case ENXIO: return "no such device or address";
1149 #endif
1150 #ifdef EOPNOTSUPP
1151 case EOPNOTSUPP: return "operation not supported on socket";
1152 #endif
1153 #ifdef EPERM
1154 case EPERM: return "not owner";
1155 #endif
1156 #ifdef EPFNOSUPPORT
1157 case EPFNOSUPPORT: return "protocol family not supported";
1158 #endif
1159 #ifdef EPIPE
1160 case EPIPE: return "broken pipe";
1161 #endif
1162 #ifdef EPROCLIM
1163 case EPROCLIM: return "too many processes";
1164 #endif
1165 #ifdef EPROCUNAVAIL
1166 case EPROCUNAVAIL: return "bad procedure for program";
1167 #endif
1168 #ifdef EPROGMISMATCH
1169 case EPROGMISMATCH: return "program version wrong";
1170 #endif
1171 #ifdef EPROGUNAVAIL
1172 case EPROGUNAVAIL: return "RPC program not available";
1173 #endif
1174 #ifdef EPROTO
1175 case EPROTO: return "protocol error";
1176 #endif
1177 #ifdef EPROTONOSUPPORT
1178 case EPROTONOSUPPORT: return "protocol not suppored";
1179 #endif
1180 #ifdef EPROTOTYPE
1181 case EPROTOTYPE: return "protocol wrong type for socket";
1182 #endif
1183 #ifdef ERANGE
1184 case ERANGE: return "math result unrepresentable";
1185 #endif
1186 #if defined(EREFUSED) && (!defined(ECONNREFUSED) || (EREFUSED != ECONNREFUSED))
1187 case EREFUSED: return "EREFUSED";
1188 #endif
1189 #ifdef EREMCHG
1190 case EREMCHG: return "remote address changed";
1191 #endif
1192 #ifdef EREMDEV
1193 case EREMDEV: return "remote device";
1194 #endif
1195 #ifdef EREMOTE
1196 case EREMOTE: return "pathname hit remote file system";
1197 #endif
1198 #ifdef EREMOTEIO
1199 case EREMOTEIO: return "remote i/o error";
1200 #endif
1201 #ifdef EREMOTERELEASE
1202 case EREMOTERELEASE: return "EREMOTERELEASE";
1203 #endif
1204 #ifdef EROFS
1205 case EROFS: return "read-only file system";
1206 #endif
1207 #ifdef ERPCMISMATCH
1208 case ERPCMISMATCH: return "RPC version is wrong";
1209 #endif
1210 #ifdef ERREMOTE
1211 case ERREMOTE: return "object is remote";
1212 #endif
1213 #ifdef ESHUTDOWN
1214 case ESHUTDOWN: return "can't send afer socket shutdown";
1215 #endif
1216 #ifdef ESOCKTNOSUPPORT
1217 case ESOCKTNOSUPPORT: return "socket type not supported";
1218 #endif
1219 #ifdef ESPIPE
1220 case ESPIPE: return "invalid seek";
1221 #endif
1222 #ifdef ESRCH
1223 case ESRCH: return "no such process";
1224 #endif
1225 #ifdef ESRMNT
1226 case ESRMNT: return "srmount error";
1227 #endif
1228 #ifdef ESTALE
1229 case ESTALE: return "stale remote file handle";
1230 #endif
1231 #ifdef ESUCCESS
1232 case ESUCCESS: return "Error 0";
1233 #endif
1234 #ifdef ETIME
1235 case ETIME: return "timer expired";
1236 #endif
1237 #ifdef ETIMEDOUT
1238 case ETIMEDOUT: return "connection timed out";
1239 #endif
1240 #ifdef ETOOMANYREFS
1241 case ETOOMANYREFS: return "too many references: can't splice";
1242 #endif
1243 #ifdef ETXTBSY
1244 case ETXTBSY: return "text file or pseudo-device busy";
1245 #endif
1246 #ifdef EUCLEAN
1247 case EUCLEAN: return "structure needs cleaning";
1248 #endif
1249 #ifdef EUNATCH
1250 case EUNATCH: return "protocol driver not attached";
1251 #endif
1252 #ifdef EUSERS
1253 case EUSERS: return "too many users";
1254 #endif
1255 #ifdef EVERSION
1256 case EVERSION: return "version mismatch";
1257 #endif
1258 #if defined(EWOULDBLOCK) && (!defined(EAGAIN) || (EWOULDBLOCK != EAGAIN))
1259 case EWOULDBLOCK: return "operation would block";
1260 #endif
1261 #ifdef EXDEV
1262 case EXDEV: return "cross-domain link";
1263 #endif
1264 #ifdef EXFULL
1265 case EXFULL: return "message tables full";
1266 #endif
1268 #else /* NO_SYS_ERRLIST */
1269 extern int sys_nerr;
1270 extern char *sys_errlist[];
1272 if ((errnum > 0) && (errnum <= sys_nerr))
1273 return sys_errlist [errnum];
1274 #endif /* NO_SYS_ERRLIST */
1276 msg = g_static_private_get (&msg_private);
1277 if (!msg)
1279 msg = g_new (gchar, 64);
1280 g_static_private_set (&msg_private, msg, g_free);
1283 _g_sprintf (msg, "unknown error (%d)", errnum);
1285 errno = saved_errno;
1286 return msg;
1289 G_CONST_RETURN gchar*
1290 g_strsignal (gint signum)
1292 static GStaticPrivate msg_private = G_STATIC_PRIVATE_INIT;
1293 char *msg;
1295 #ifdef HAVE_STRSIGNAL
1296 const char *msg_locale;
1298 #if defined(G_OS_BEOS) || defined(G_WITH_CYGWIN)
1299 extern const char *strsignal(int);
1300 #else
1301 /* this is declared differently (const) in string.h on BeOS */
1302 extern char *strsignal (int sig);
1303 #endif /* !G_OS_BEOS && !G_WITH_CYGWIN */
1304 msg_locale = strsignal (signum);
1305 if (g_get_charset (NULL))
1306 return msg_locale;
1307 else
1309 gchar *msg_utf8 = g_locale_to_utf8 (msg_locale, -1, NULL, NULL, NULL);
1310 if (msg_utf8)
1312 /* Stick in the quark table so that we can return a static result
1314 GQuark msg_quark = g_quark_from_string (msg_utf8);
1315 g_free (msg_utf8);
1317 return g_quark_to_string (msg_quark);
1320 #elif NO_SYS_SIGLIST
1321 switch (signum)
1323 #ifdef SIGHUP
1324 case SIGHUP: return "Hangup";
1325 #endif
1326 #ifdef SIGINT
1327 case SIGINT: return "Interrupt";
1328 #endif
1329 #ifdef SIGQUIT
1330 case SIGQUIT: return "Quit";
1331 #endif
1332 #ifdef SIGILL
1333 case SIGILL: return "Illegal instruction";
1334 #endif
1335 #ifdef SIGTRAP
1336 case SIGTRAP: return "Trace/breakpoint trap";
1337 #endif
1338 #ifdef SIGABRT
1339 case SIGABRT: return "IOT trap/Abort";
1340 #endif
1341 #ifdef SIGBUS
1342 case SIGBUS: return "Bus error";
1343 #endif
1344 #ifdef SIGFPE
1345 case SIGFPE: return "Floating point exception";
1346 #endif
1347 #ifdef SIGKILL
1348 case SIGKILL: return "Killed";
1349 #endif
1350 #ifdef SIGUSR1
1351 case SIGUSR1: return "User defined signal 1";
1352 #endif
1353 #ifdef SIGSEGV
1354 case SIGSEGV: return "Segmentation fault";
1355 #endif
1356 #ifdef SIGUSR2
1357 case SIGUSR2: return "User defined signal 2";
1358 #endif
1359 #ifdef SIGPIPE
1360 case SIGPIPE: return "Broken pipe";
1361 #endif
1362 #ifdef SIGALRM
1363 case SIGALRM: return "Alarm clock";
1364 #endif
1365 #ifdef SIGTERM
1366 case SIGTERM: return "Terminated";
1367 #endif
1368 #ifdef SIGSTKFLT
1369 case SIGSTKFLT: return "Stack fault";
1370 #endif
1371 #ifdef SIGCHLD
1372 case SIGCHLD: return "Child exited";
1373 #endif
1374 #ifdef SIGCONT
1375 case SIGCONT: return "Continued";
1376 #endif
1377 #ifdef SIGSTOP
1378 case SIGSTOP: return "Stopped (signal)";
1379 #endif
1380 #ifdef SIGTSTP
1381 case SIGTSTP: return "Stopped";
1382 #endif
1383 #ifdef SIGTTIN
1384 case SIGTTIN: return "Stopped (tty input)";
1385 #endif
1386 #ifdef SIGTTOU
1387 case SIGTTOU: return "Stopped (tty output)";
1388 #endif
1389 #ifdef SIGURG
1390 case SIGURG: return "Urgent condition";
1391 #endif
1392 #ifdef SIGXCPU
1393 case SIGXCPU: return "CPU time limit exceeded";
1394 #endif
1395 #ifdef SIGXFSZ
1396 case SIGXFSZ: return "File size limit exceeded";
1397 #endif
1398 #ifdef SIGVTALRM
1399 case SIGVTALRM: return "Virtual time alarm";
1400 #endif
1401 #ifdef SIGPROF
1402 case SIGPROF: return "Profile signal";
1403 #endif
1404 #ifdef SIGWINCH
1405 case SIGWINCH: return "Window size changed";
1406 #endif
1407 #ifdef SIGIO
1408 case SIGIO: return "Possible I/O";
1409 #endif
1410 #ifdef SIGPWR
1411 case SIGPWR: return "Power failure";
1412 #endif
1413 #ifdef SIGUNUSED
1414 case SIGUNUSED: return "Unused signal";
1415 #endif
1417 #else /* NO_SYS_SIGLIST */
1419 #ifdef NO_SYS_SIGLIST_DECL
1420 extern char *sys_siglist[]; /*(see Tue Jan 19 00:44:24 1999 in changelog)*/
1421 #endif
1423 return (char*) /* this function should return const --josh */ sys_siglist [signum];
1424 #endif /* NO_SYS_SIGLIST */
1426 msg = g_static_private_get (&msg_private);
1427 if (!msg)
1429 msg = g_new (gchar, 64);
1430 g_static_private_set (&msg_private, msg, g_free);
1433 _g_sprintf (msg, "unknown signal (%d)", signum);
1435 return msg;
1438 /* Functions g_strlcpy and g_strlcat were originally developed by
1439 * Todd C. Miller <Todd.Miller@courtesan.com> to simplify writing secure code.
1440 * See ftp://ftp.openbsd.org/pub/OpenBSD/src/lib/libc/string/strlcpy.3
1441 * for more information.
1444 #ifdef HAVE_STRLCPY
1445 /* Use the native ones, if available; they might be implemented in assembly */
1446 gsize
1447 g_strlcpy (gchar *dest,
1448 const gchar *src,
1449 gsize dest_size)
1451 g_return_val_if_fail (dest != NULL, 0);
1452 g_return_val_if_fail (src != NULL, 0);
1454 return strlcpy (dest, src, dest_size);
1457 gsize
1458 g_strlcat (gchar *dest,
1459 const gchar *src,
1460 gsize dest_size)
1462 g_return_val_if_fail (dest != NULL, 0);
1463 g_return_val_if_fail (src != NULL, 0);
1465 return strlcat (dest, src, dest_size);
1468 #else /* ! HAVE_STRLCPY */
1469 /* g_strlcpy
1471 * Copy string src to buffer dest (of buffer size dest_size). At most
1472 * dest_size-1 characters will be copied. Always NUL terminates
1473 * (unless dest_size == 0). This function does NOT allocate memory.
1474 * Unlike strncpy, this function doesn't pad dest (so it's often faster).
1475 * Returns size of attempted result, strlen(src),
1476 * so if retval >= dest_size, truncation occurred.
1478 gsize
1479 g_strlcpy (gchar *dest,
1480 const gchar *src,
1481 gsize dest_size)
1483 register gchar *d = dest;
1484 register const gchar *s = src;
1485 register gsize n = dest_size;
1487 g_return_val_if_fail (dest != NULL, 0);
1488 g_return_val_if_fail (src != NULL, 0);
1490 /* Copy as many bytes as will fit */
1491 if (n != 0 && --n != 0)
1494 register gchar c = *s++;
1496 *d++ = c;
1497 if (c == 0)
1498 break;
1500 while (--n != 0);
1502 /* If not enough room in dest, add NUL and traverse rest of src */
1503 if (n == 0)
1505 if (dest_size != 0)
1506 *d = 0;
1507 while (*s++)
1511 return s - src - 1; /* count does not include NUL */
1514 /* g_strlcat
1516 * Appends string src to buffer dest (of buffer size dest_size).
1517 * At most dest_size-1 characters will be copied.
1518 * Unlike strncat, dest_size is the full size of dest, not the space left over.
1519 * This function does NOT allocate memory.
1520 * This always NUL terminates (unless siz == 0 or there were no NUL characters
1521 * in the dest_size characters of dest to start with).
1522 * Returns size of attempted result, which is
1523 * MIN (dest_size, strlen (original dest)) + strlen (src),
1524 * so if retval >= dest_size, truncation occurred.
1526 gsize
1527 g_strlcat (gchar *dest,
1528 const gchar *src,
1529 gsize dest_size)
1531 register gchar *d = dest;
1532 register const gchar *s = src;
1533 register gsize bytes_left = dest_size;
1534 gsize dlength; /* Logically, MIN (strlen (d), dest_size) */
1536 g_return_val_if_fail (dest != NULL, 0);
1537 g_return_val_if_fail (src != NULL, 0);
1539 /* Find the end of dst and adjust bytes left but don't go past end */
1540 while (*d != 0 && bytes_left-- != 0)
1541 d++;
1542 dlength = d - dest;
1543 bytes_left = dest_size - dlength;
1545 if (bytes_left == 0)
1546 return dlength + strlen (s);
1548 while (*s != 0)
1550 if (bytes_left != 1)
1552 *d++ = *s;
1553 bytes_left--;
1555 s++;
1557 *d = 0;
1559 return dlength + (s - src); /* count does not include NUL */
1561 #endif /* ! HAVE_STRLCPY */
1564 * g_ascii_strdown:
1565 * @str: a string.
1566 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1568 * Converts all upper case ASCII letters to lower case ASCII letters.
1570 * Return value: a newly-allocated string, with all the upper case
1571 * characters in @str converted to lower case, with
1572 * semantics that exactly match g_ascii_tolower(). (Note
1573 * that this is unlike the old g_strdown(), which modified
1574 * the string in place.)
1576 gchar*
1577 g_ascii_strdown (const gchar *str,
1578 gssize len)
1580 gchar *result, *s;
1582 g_return_val_if_fail (str != NULL, NULL);
1584 if (len < 0)
1585 len = strlen (str);
1587 result = g_strndup (str, len);
1588 for (s = result; *s; s++)
1589 *s = g_ascii_tolower (*s);
1591 return result;
1595 * g_ascii_strup:
1596 * @str: a string.
1597 * @len: length of @str in bytes, or -1 if @str is nul-terminated.
1599 * Converts all lower case ASCII letters to upper case ASCII letters.
1601 * Return value: a newly allocated string, with all the lower case
1602 * characters in @str converted to upper case, with
1603 * semantics that exactly match g_ascii_toupper(). (Note
1604 * that this is unlike the old g_strup(), which modified
1605 * the string in place.)
1607 gchar*
1608 g_ascii_strup (const gchar *str,
1609 gssize len)
1611 gchar *result, *s;
1613 g_return_val_if_fail (str != NULL, NULL);
1615 if (len < 0)
1616 len = strlen (str);
1618 result = g_strndup (str, len);
1619 for (s = result; *s; s++)
1620 *s = g_ascii_toupper (*s);
1622 return result;
1626 * g_strdown:
1627 * @string: the string to convert.
1629 * Converts a string to lower case.
1631 * Return value: the string
1633 * Deprecated:2.2: This function is totally broken for the reasons discussed
1634 * in the g_strncasecmp() docs - use g_ascii_strdown() or g_utf8_strdown()
1635 * instead.
1637 gchar*
1638 g_strdown (gchar *string)
1640 register guchar *s;
1642 g_return_val_if_fail (string != NULL, NULL);
1644 s = (guchar *) string;
1646 while (*s)
1648 if (isupper (*s))
1649 *s = tolower (*s);
1650 s++;
1653 return (gchar *) string;
1657 * g_strup:
1658 * @string: the string to convert.
1660 * Converts a string to upper case.
1662 * Return value: the string
1664 * Deprecated:2.2: This function is totally broken for the reasons discussed
1665 * in the g_strncasecmp() docs - use g_ascii_strup() or g_utf8_strup() instead.
1667 gchar*
1668 g_strup (gchar *string)
1670 register guchar *s;
1672 g_return_val_if_fail (string != NULL, NULL);
1674 s = (guchar *) string;
1676 while (*s)
1678 if (islower (*s))
1679 *s = toupper (*s);
1680 s++;
1683 return (gchar *) string;
1686 gchar*
1687 g_strreverse (gchar *string)
1689 g_return_val_if_fail (string != NULL, NULL);
1691 if (*string)
1693 register gchar *h, *t;
1695 h = string;
1696 t = string + strlen (string) - 1;
1698 while (h < t)
1700 register gchar c;
1702 c = *h;
1703 *h = *t;
1704 h++;
1705 *t = c;
1706 t--;
1710 return string;
1714 * g_ascii_tolower:
1715 * @c: any character.
1717 * Convert a character to ASCII lower case.
1719 * Unlike the standard C library tolower() function, this only
1720 * recognizes standard ASCII letters and ignores the locale, returning
1721 * all non-ASCII characters unchanged, even if they are lower case
1722 * letters in a particular character set. Also unlike the standard
1723 * library function, this takes and returns a char, not an int, so
1724 * don't call it on %EOF but no need to worry about casting to #guchar
1725 * before passing a possibly non-ASCII character in.
1727 * Return value: the result of converting @c to lower case.
1728 * If @c is not an ASCII upper case letter,
1729 * @c is returned unchanged.
1731 gchar
1732 g_ascii_tolower (gchar c)
1734 return g_ascii_isupper (c) ? c - 'A' + 'a' : c;
1738 * g_ascii_toupper:
1739 * @c: any character.
1741 * Convert a character to ASCII upper case.
1743 * Unlike the standard C library toupper() function, this only
1744 * recognizes standard ASCII letters and ignores the locale, returning
1745 * all non-ASCII characters unchanged, even if they are upper case
1746 * letters in a particular character set. Also unlike the standard
1747 * library function, this takes and returns a char, not an int, so
1748 * don't call it on %EOF but no need to worry about casting to #guchar
1749 * before passing a possibly non-ASCII character in.
1751 * Return value: the result of converting @c to upper case.
1752 * If @c is not an ASCII lower case letter,
1753 * @c is returned unchanged.
1755 gchar
1756 g_ascii_toupper (gchar c)
1758 return g_ascii_islower (c) ? c - 'a' + 'A' : c;
1762 * g_ascii_digit_value:
1763 * @c: an ASCII character.
1765 * Determines the numeric value of a character as a decimal
1766 * digit. Differs from g_unichar_digit_value() because it takes
1767 * a char, so there's no worry about sign extension if characters
1768 * are signed.
1770 * Return value: If @c is a decimal digit (according to
1771 * g_ascii_isdigit()), its numeric value. Otherwise, -1.
1774 g_ascii_digit_value (gchar c)
1776 if (g_ascii_isdigit (c))
1777 return c - '0';
1778 return -1;
1782 * g_ascii_xdigit_value:
1783 * @c: an ASCII character.
1785 * Determines the numeric value of a character as a hexidecimal
1786 * digit. Differs from g_unichar_xdigit_value() because it takes
1787 * a char, so there's no worry about sign extension if characters
1788 * are signed.
1790 * Return value: If @c is a hex digit (according to
1791 * g_ascii_isxdigit()), its numeric value. Otherwise, -1.
1794 g_ascii_xdigit_value (gchar c)
1796 if (c >= 'A' && c <= 'F')
1797 return c - 'A' + 10;
1798 if (c >= 'a' && c <= 'f')
1799 return c - 'a' + 10;
1800 return g_ascii_digit_value (c);
1804 * g_ascii_strcasecmp:
1805 * @s1: string to compare with @s2.
1806 * @s2: string to compare with @s1.
1808 * Compare two strings, ignoring the case of ASCII characters.
1810 * Unlike the BSD strcasecmp() function, this only recognizes standard
1811 * ASCII letters and ignores the locale, treating all non-ASCII
1812 * bytes as if they are not letters.
1814 * This function should be used only on strings that are known to be
1815 * in encodings where the bytes corresponding to ASCII letters always
1816 * represent themselves. This includes UTF-8 and the ISO-8859-*
1817 * charsets, but not for instance double-byte encodings like the
1818 * Windows Codepage 932, where the trailing bytes of double-byte
1819 * characters include all ASCII letters. If you compare two CP932
1820 * strings using this function, you will get false matches.
1822 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1823 * or a positive value if @s1 &gt; @s2.
1825 gint
1826 g_ascii_strcasecmp (const gchar *s1,
1827 const gchar *s2)
1829 gint c1, c2;
1831 g_return_val_if_fail (s1 != NULL, 0);
1832 g_return_val_if_fail (s2 != NULL, 0);
1834 while (*s1 && *s2)
1836 c1 = (gint)(guchar) TOLOWER (*s1);
1837 c2 = (gint)(guchar) TOLOWER (*s2);
1838 if (c1 != c2)
1839 return (c1 - c2);
1840 s1++; s2++;
1843 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1847 * g_ascii_strncasecmp:
1848 * @s1: string to compare with @s2.
1849 * @s2: string to compare with @s1.
1850 * @n: number of characters to compare.
1852 * Compare @s1 and @s2, ignoring the case of ASCII characters and any
1853 * characters after the first @n in each string.
1855 * Unlike the BSD strcasecmp() function, this only recognizes standard
1856 * ASCII letters and ignores the locale, treating all non-ASCII
1857 * characters as if they are not letters.
1859 * The same warning as in g_ascii_strcasecmp() applies: Use this
1860 * function only on strings known to be in encodings where bytes
1861 * corresponding to ASCII letters always represent themselves.
1863 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1864 * or a positive value if @s1 &gt; @s2.
1866 gint
1867 g_ascii_strncasecmp (const gchar *s1,
1868 const gchar *s2,
1869 gsize n)
1871 gint c1, c2;
1873 g_return_val_if_fail (s1 != NULL, 0);
1874 g_return_val_if_fail (s2 != NULL, 0);
1876 while (n && *s1 && *s2)
1878 n -= 1;
1879 c1 = (gint)(guchar) TOLOWER (*s1);
1880 c2 = (gint)(guchar) TOLOWER (*s2);
1881 if (c1 != c2)
1882 return (c1 - c2);
1883 s1++; s2++;
1886 if (n)
1887 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1888 else
1889 return 0;
1893 * g_strcasecmp:
1894 * @s1: a string.
1895 * @s2: a string to compare with @s1.
1897 * A case-insensitive string comparison, corresponding to the standard
1898 * strcasecmp() function on platforms which support it.
1900 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1901 * or a positive value if @s1 &gt; @s2.
1903 * Deprecated:2.2: See g_strncasecmp() for a discussion of why this function
1904 * is deprecated and how to replace it.
1906 gint
1907 g_strcasecmp (const gchar *s1,
1908 const gchar *s2)
1910 #ifdef HAVE_STRCASECMP
1911 g_return_val_if_fail (s1 != NULL, 0);
1912 g_return_val_if_fail (s2 != NULL, 0);
1914 return strcasecmp (s1, s2);
1915 #else
1916 gint c1, c2;
1918 g_return_val_if_fail (s1 != NULL, 0);
1919 g_return_val_if_fail (s2 != NULL, 0);
1921 while (*s1 && *s2)
1923 /* According to A. Cox, some platforms have islower's that
1924 * don't work right on non-uppercase
1926 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1927 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1928 if (c1 != c2)
1929 return (c1 - c2);
1930 s1++; s2++;
1933 return (((gint)(guchar) *s1) - ((gint)(guchar) *s2));
1934 #endif
1938 * g_strncasecmp:
1939 * @s1: a string.
1940 * @s2: a string to compare with @s1.
1941 * @n: the maximum number of characters to compare.
1943 * A case-insensitive string comparison, corresponding to the standard
1944 * strncasecmp() function on platforms which support it.
1945 * It is similar to g_strcasecmp() except it only compares the first @n
1946 * characters of the strings.
1948 * Return value: 0 if the strings match, a negative value if @s1 &lt; @s2,
1949 * or a positive value if @s1 &gt; @s2.
1951 * Deprecated:2.2: The problem with g_strncasecmp() is that it does the
1952 * comparison by calling toupper()/tolower(). These functions are
1953 * locale-specific and operate on single bytes. However, it is impossible
1954 * to handle things correctly from an I18N standpoint by operating on
1955 * bytes, since characters may be multibyte. Thus g_strncasecmp() is
1956 * broken if your string is guaranteed to be ASCII, since it's
1957 * locale-sensitive, and it's broken if your string is localized, since
1958 * it doesn't work on many encodings at all, including UTF-8, EUC-JP,
1959 * etc.
1961 * There are therefore two replacement functions: g_ascii_strncasecmp(),
1962 * which only works on ASCII and is not locale-sensitive, and
1963 * g_utf8_casefold(), which is good for case-insensitive sorting of UTF-8.
1965 gint
1966 g_strncasecmp (const gchar *s1,
1967 const gchar *s2,
1968 guint n)
1970 #ifdef HAVE_STRNCASECMP
1971 return strncasecmp (s1, s2, n);
1972 #else
1973 gint c1, c2;
1975 g_return_val_if_fail (s1 != NULL, 0);
1976 g_return_val_if_fail (s2 != NULL, 0);
1978 while (n && *s1 && *s2)
1980 n -= 1;
1981 /* According to A. Cox, some platforms have islower's that
1982 * don't work right on non-uppercase
1984 c1 = isupper ((guchar)*s1) ? tolower ((guchar)*s1) : *s1;
1985 c2 = isupper ((guchar)*s2) ? tolower ((guchar)*s2) : *s2;
1986 if (c1 != c2)
1987 return (c1 - c2);
1988 s1++; s2++;
1991 if (n)
1992 return (((gint) (guchar) *s1) - ((gint) (guchar) *s2));
1993 else
1994 return 0;
1995 #endif
1998 gchar*
1999 g_strdelimit (gchar *string,
2000 const gchar *delimiters,
2001 gchar new_delim)
2003 register gchar *c;
2005 g_return_val_if_fail (string != NULL, NULL);
2007 if (!delimiters)
2008 delimiters = G_STR_DELIMITERS;
2010 for (c = string; *c; c++)
2012 if (strchr (delimiters, *c))
2013 *c = new_delim;
2016 return string;
2019 gchar*
2020 g_strcanon (gchar *string,
2021 const gchar *valid_chars,
2022 gchar substitutor)
2024 register gchar *c;
2026 g_return_val_if_fail (string != NULL, NULL);
2027 g_return_val_if_fail (valid_chars != NULL, NULL);
2029 for (c = string; *c; c++)
2031 if (!strchr (valid_chars, *c))
2032 *c = substitutor;
2035 return string;
2038 gchar*
2039 g_strcompress (const gchar *source)
2041 const gchar *p = source, *octal;
2042 gchar *dest = g_malloc (strlen (source) + 1);
2043 gchar *q = dest;
2045 while (*p)
2047 if (*p == '\\')
2049 p++;
2050 switch (*p)
2052 case '\0':
2053 g_warning ("g_strcompress: trailing \\");
2054 goto out;
2055 case '0': case '1': case '2': case '3': case '4':
2056 case '5': case '6': case '7':
2057 *q = 0;
2058 octal = p;
2059 while ((p < octal + 3) && (*p >= '0') && (*p <= '7'))
2061 *q = (*q * 8) + (*p - '0');
2062 p++;
2064 q++;
2065 p--;
2066 break;
2067 case 'b':
2068 *q++ = '\b';
2069 break;
2070 case 'f':
2071 *q++ = '\f';
2072 break;
2073 case 'n':
2074 *q++ = '\n';
2075 break;
2076 case 'r':
2077 *q++ = '\r';
2078 break;
2079 case 't':
2080 *q++ = '\t';
2081 break;
2082 default: /* Also handles \" and \\ */
2083 *q++ = *p;
2084 break;
2087 else
2088 *q++ = *p;
2089 p++;
2091 out:
2092 *q = 0;
2094 return dest;
2097 gchar *
2098 g_strescape (const gchar *source,
2099 const gchar *exceptions)
2101 const guchar *p;
2102 gchar *dest;
2103 gchar *q;
2104 guchar excmap[256];
2106 g_return_val_if_fail (source != NULL, NULL);
2108 p = (guchar *) source;
2109 /* Each source byte needs maximally four destination chars (\777) */
2110 q = dest = g_malloc (strlen (source) * 4 + 1);
2112 memset (excmap, 0, 256);
2113 if (exceptions)
2115 guchar *e = (guchar *) exceptions;
2117 while (*e)
2119 excmap[*e] = 1;
2120 e++;
2124 while (*p)
2126 if (excmap[*p])
2127 *q++ = *p;
2128 else
2130 switch (*p)
2132 case '\b':
2133 *q++ = '\\';
2134 *q++ = 'b';
2135 break;
2136 case '\f':
2137 *q++ = '\\';
2138 *q++ = 'f';
2139 break;
2140 case '\n':
2141 *q++ = '\\';
2142 *q++ = 'n';
2143 break;
2144 case '\r':
2145 *q++ = '\\';
2146 *q++ = 'r';
2147 break;
2148 case '\t':
2149 *q++ = '\\';
2150 *q++ = 't';
2151 break;
2152 case '\\':
2153 *q++ = '\\';
2154 *q++ = '\\';
2155 break;
2156 case '"':
2157 *q++ = '\\';
2158 *q++ = '"';
2159 break;
2160 default:
2161 if ((*p < ' ') || (*p >= 0177))
2163 *q++ = '\\';
2164 *q++ = '0' + (((*p) >> 6) & 07);
2165 *q++ = '0' + (((*p) >> 3) & 07);
2166 *q++ = '0' + ((*p) & 07);
2168 else
2169 *q++ = *p;
2170 break;
2173 p++;
2175 *q = 0;
2176 return dest;
2179 gchar*
2180 g_strchug (gchar *string)
2182 guchar *start;
2184 g_return_val_if_fail (string != NULL, NULL);
2186 for (start = (guchar*) string; *start && g_ascii_isspace (*start); start++)
2189 g_memmove (string, start, strlen ((gchar *) start) + 1);
2191 return string;
2194 gchar*
2195 g_strchomp (gchar *string)
2197 gsize len;
2199 g_return_val_if_fail (string != NULL, NULL);
2201 len = strlen (string);
2202 while (len--)
2204 if (g_ascii_isspace ((guchar) string[len]))
2205 string[len] = '\0';
2206 else
2207 break;
2210 return string;
2214 * g_strsplit:
2215 * @string: a string to split.
2216 * @delimiter: a string which specifies the places at which to split the string.
2217 * The delimiter is not included in any of the resulting strings, unless
2218 * @max_tokens is reached.
2219 * @max_tokens: the maximum number of pieces to split @string into. If this is
2220 * less than 1, the string is split completely.
2222 * Splits a string into a maximum of @max_tokens pieces, using the given
2223 * @delimiter. If @max_tokens is reached, the remainder of @string is appended
2224 * to the last token.
2226 * As a special case, the result of splitting the empty string "" is an empty
2227 * vector, not a vector containing a single string. The reason for this
2228 * special case is that being able to represent a empty vector is typically
2229 * more useful than consistent handling of empty elements. If you do need
2230 * to represent empty elements, you'll need to check for the empty string
2231 * before calling g_strsplit().
2233 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2234 * g_strfreev() to free it.
2236 gchar**
2237 g_strsplit (const gchar *string,
2238 const gchar *delimiter,
2239 gint max_tokens)
2241 GSList *string_list = NULL, *slist;
2242 gchar **str_array, *s;
2243 guint n = 0;
2244 const gchar *remainder;
2246 g_return_val_if_fail (string != NULL, NULL);
2247 g_return_val_if_fail (delimiter != NULL, NULL);
2248 g_return_val_if_fail (delimiter[0] != '\0', NULL);
2250 if (max_tokens < 1)
2251 max_tokens = G_MAXINT;
2253 remainder = string;
2254 s = strstr (remainder, delimiter);
2255 if (s)
2257 gsize delimiter_len = strlen (delimiter);
2259 while (--max_tokens && s)
2261 gsize len;
2262 gchar *new_string;
2264 len = s - remainder;
2265 new_string = g_new (gchar, len + 1);
2266 strncpy (new_string, remainder, len);
2267 new_string[len] = 0;
2268 string_list = g_slist_prepend (string_list, new_string);
2269 n++;
2270 remainder = s + delimiter_len;
2271 s = strstr (remainder, delimiter);
2274 if (*string)
2276 n++;
2277 string_list = g_slist_prepend (string_list, g_strdup (remainder));
2280 str_array = g_new (gchar*, n + 1);
2282 str_array[n--] = NULL;
2283 for (slist = string_list; slist; slist = slist->next)
2284 str_array[n--] = slist->data;
2286 g_slist_free (string_list);
2288 return str_array;
2292 * g_strsplit_set:
2293 * @string: The string to be tokenized
2294 * @delimiters: A nul-terminated string containing bytes that are used
2295 * to split the string.
2296 * @max_tokens: The maximum number of tokens to split @string into.
2297 * If this is less than 1, the string is split completely
2299 * Splits @string into a number of tokens not containing any of the characters
2300 * in @delimiter. A token is the (possibly empty) longest string that does not
2301 * contain any of the characters in @delimiters. If @max_tokens is reached, the
2302 * remainder is appended to the last token.
2304 * For example the result of g_strsplit_set ("abc:def/ghi", ":/", -1) is a
2305 * %NULL-terminated vector containing the three strings "abc", "def",
2306 * and "ghi".
2308 * The result if g_strsplit_set (":def/ghi:", ":/", -1) is a %NULL-terminated
2309 * vector containing the four strings "", "def", "ghi", and "".
2311 * As a special case, the result of splitting the empty string "" is an empty
2312 * vector, not a vector containing a single string. The reason for this
2313 * special case is that being able to represent a empty vector is typically
2314 * more useful than consistent handling of empty elements. If you do need
2315 * to represent empty elements, you'll need to check for the empty string
2316 * before calling g_strsplit_set().
2318 * Note that this function works on bytes not characters, so it can't be used
2319 * to delimit UTF-8 strings for anything but ASCII characters.
2321 * Return value: a newly-allocated %NULL-terminated array of strings. Use
2322 * g_strfreev() to free it.
2324 * Since: 2.4
2326 gchar **
2327 g_strsplit_set (const gchar *string,
2328 const gchar *delimiters,
2329 gint max_tokens)
2331 gboolean delim_table[256];
2332 GSList *tokens, *list;
2333 gint n_tokens;
2334 const gchar *s;
2335 const gchar *current;
2336 gchar *token;
2337 gchar **result;
2339 g_return_val_if_fail (string != NULL, NULL);
2340 g_return_val_if_fail (delimiters != NULL, NULL);
2342 if (max_tokens < 1)
2343 max_tokens = G_MAXINT;
2345 if (*string == '\0')
2347 result = g_new (char *, 1);
2348 result[0] = NULL;
2349 return result;
2352 memset (delim_table, FALSE, sizeof (delim_table));
2353 for (s = delimiters; *s != '\0'; ++s)
2354 delim_table[*(guchar *)s] = TRUE;
2356 tokens = NULL;
2357 n_tokens = 0;
2359 s = current = string;
2360 while (*s != '\0')
2362 if (delim_table[*(guchar *)s] && n_tokens + 1 < max_tokens)
2364 gchar *token;
2366 token = g_strndup (current, s - current);
2367 tokens = g_slist_prepend (tokens, token);
2368 ++n_tokens;
2370 current = s + 1;
2373 ++s;
2376 token = g_strndup (current, s - current);
2377 tokens = g_slist_prepend (tokens, token);
2378 ++n_tokens;
2380 result = g_new (gchar *, n_tokens + 1);
2382 result[n_tokens] = NULL;
2383 for (list = tokens; list != NULL; list = list->next)
2384 result[--n_tokens] = list->data;
2386 g_slist_free (tokens);
2388 return result;
2392 * g_strfreev:
2393 * @str_array: a %NULL-terminated array of strings to free.
2395 * Frees a %NULL-terminated array of strings, and the array itself.
2396 * If called on a %NULL value, g_strfreev() simply returns.
2398 void
2399 g_strfreev (gchar **str_array)
2401 if (str_array)
2403 int i;
2405 for(i = 0; str_array[i] != NULL; i++)
2406 g_free(str_array[i]);
2408 g_free (str_array);
2413 * g_strdupv:
2414 * @str_array: %NULL-terminated array of strings.
2416 * Copies %NULL-terminated array of strings. The copy is a deep copy;
2417 * the new array should be freed by first freeing each string, then
2418 * the array itself. g_strfreev() does this for you. If called
2419 * on a %NULL value, g_strdupv() simply returns %NULL.
2421 * Return value: a new %NULL-terminated array of strings.
2423 gchar**
2424 g_strdupv (gchar **str_array)
2426 if (str_array)
2428 gint i;
2429 gchar **retval;
2431 i = 0;
2432 while (str_array[i])
2433 ++i;
2435 retval = g_new (gchar*, i + 1);
2437 i = 0;
2438 while (str_array[i])
2440 retval[i] = g_strdup (str_array[i]);
2441 ++i;
2443 retval[i] = NULL;
2445 return retval;
2447 else
2448 return NULL;
2451 gchar*
2452 g_strjoinv (const gchar *separator,
2453 gchar **str_array)
2455 gchar *string;
2456 gchar *ptr;
2458 g_return_val_if_fail (str_array != NULL, NULL);
2460 if (separator == NULL)
2461 separator = "";
2463 if (*str_array)
2465 gint i;
2466 gsize len;
2467 gsize separator_len;
2469 separator_len = strlen (separator);
2470 /* First part, getting length */
2471 len = 1 + strlen (str_array[0]);
2472 for (i = 1; str_array[i] != NULL; i++)
2473 len += strlen (str_array[i]);
2474 len += separator_len * (i - 1);
2476 /* Second part, building string */
2477 string = g_new (gchar, len);
2478 ptr = g_stpcpy (string, *str_array);
2479 for (i = 1; str_array[i] != NULL; i++)
2481 ptr = g_stpcpy (ptr, separator);
2482 ptr = g_stpcpy (ptr, str_array[i]);
2485 else
2486 string = g_strdup ("");
2488 return string;
2491 gchar*
2492 g_strjoin (const gchar *separator,
2493 ...)
2495 gchar *string, *s;
2496 va_list args;
2497 gsize len;
2498 gsize separator_len;
2499 gchar *ptr;
2501 if (separator == NULL)
2502 separator = "";
2504 separator_len = strlen (separator);
2506 va_start (args, separator);
2508 s = va_arg (args, gchar*);
2510 if (s)
2512 /* First part, getting length */
2513 len = 1 + strlen (s);
2515 s = va_arg (args, gchar*);
2516 while (s)
2518 len += separator_len + strlen (s);
2519 s = va_arg (args, gchar*);
2521 va_end (args);
2523 /* Second part, building string */
2524 string = g_new (gchar, len);
2526 va_start (args, separator);
2528 s = va_arg (args, gchar*);
2529 ptr = g_stpcpy (string, s);
2531 s = va_arg (args, gchar*);
2532 while (s)
2534 ptr = g_stpcpy (ptr, separator);
2535 ptr = g_stpcpy (ptr, s);
2536 s = va_arg (args, gchar*);
2539 else
2540 string = g_strdup ("");
2542 va_end (args);
2544 return string;
2549 * g_strstr_len:
2550 * @haystack: a string.
2551 * @haystack_len: the maximum length of @haystack.
2552 * @needle: the string to search for.
2554 * Searches the string @haystack for the first occurrence
2555 * of the string @needle, limiting the length of the search
2556 * to @haystack_len.
2558 * Return value: a pointer to the found occurrence, or
2559 * %NULL if not found.
2561 gchar *
2562 g_strstr_len (const gchar *haystack,
2563 gssize haystack_len,
2564 const gchar *needle)
2566 g_return_val_if_fail (haystack != NULL, NULL);
2567 g_return_val_if_fail (needle != NULL, NULL);
2569 if (haystack_len < 0)
2570 return strstr (haystack, needle);
2571 else
2573 const gchar *p = haystack;
2574 gsize needle_len = strlen (needle);
2575 const gchar *end;
2576 gsize i;
2578 if (needle_len == 0)
2579 return (gchar *)haystack;
2581 if (haystack_len < needle_len)
2582 return NULL;
2584 end = haystack + haystack_len - needle_len;
2586 while (*p && p <= end)
2588 for (i = 0; i < needle_len; i++)
2589 if (p[i] != needle[i])
2590 goto next;
2592 return (gchar *)p;
2594 next:
2595 p++;
2598 return NULL;
2603 * g_strrstr:
2604 * @haystack: a nul-terminated string.
2605 * @needle: the nul-terminated string to search for.
2607 * Searches the string @haystack for the last occurrence
2608 * of the string @needle.
2610 * Return value: a pointer to the found occurrence, or
2611 * %NULL if not found.
2613 gchar *
2614 g_strrstr (const gchar *haystack,
2615 const gchar *needle)
2617 gsize i;
2618 gsize needle_len;
2619 gsize haystack_len;
2620 const gchar *p;
2622 g_return_val_if_fail (haystack != NULL, NULL);
2623 g_return_val_if_fail (needle != NULL, NULL);
2625 needle_len = strlen (needle);
2626 haystack_len = strlen (haystack);
2628 if (needle_len == 0)
2629 return (gchar *)haystack;
2631 if (haystack_len < needle_len)
2632 return NULL;
2634 p = haystack + haystack_len - needle_len;
2636 while (p >= haystack)
2638 for (i = 0; i < needle_len; i++)
2639 if (p[i] != needle[i])
2640 goto next;
2642 return (gchar *)p;
2644 next:
2645 p--;
2648 return NULL;
2652 * g_strrstr_len:
2653 * @haystack: a nul-terminated string.
2654 * @haystack_len: the maximum length of @haystack.
2655 * @needle: the nul-terminated string to search for.
2657 * Searches the string @haystack for the last occurrence
2658 * of the string @needle, limiting the length of the search
2659 * to @haystack_len.
2661 * Return value: a pointer to the found occurrence, or
2662 * %NULL if not found.
2664 gchar *
2665 g_strrstr_len (const gchar *haystack,
2666 gssize haystack_len,
2667 const gchar *needle)
2669 g_return_val_if_fail (haystack != NULL, NULL);
2670 g_return_val_if_fail (needle != NULL, NULL);
2672 if (haystack_len < 0)
2673 return g_strrstr (haystack, needle);
2674 else
2676 gsize needle_len = strlen (needle);
2677 const gchar *haystack_max = haystack + haystack_len;
2678 const gchar *p = haystack;
2679 gsize i;
2681 while (p < haystack_max && *p)
2682 p++;
2684 if (p < haystack + needle_len)
2685 return NULL;
2687 p -= needle_len;
2689 while (p >= haystack)
2691 for (i = 0; i < needle_len; i++)
2692 if (p[i] != needle[i])
2693 goto next;
2695 return (gchar *)p;
2697 next:
2698 p--;
2701 return NULL;
2707 * g_str_has_suffix:
2708 * @str: a nul-terminated string.
2709 * @suffix: the nul-terminated suffix to look for.
2711 * Looks whether the string @str ends with @suffix.
2713 * Return value: %TRUE if @str end with @suffix, %FALSE otherwise.
2715 * Since: 2.2
2717 gboolean
2718 g_str_has_suffix (const gchar *str,
2719 const gchar *suffix)
2721 int str_len;
2722 int suffix_len;
2724 g_return_val_if_fail (str != NULL, FALSE);
2725 g_return_val_if_fail (suffix != NULL, FALSE);
2727 str_len = strlen (str);
2728 suffix_len = strlen (suffix);
2730 if (str_len < suffix_len)
2731 return FALSE;
2733 return strcmp (str + str_len - suffix_len, suffix) == 0;
2737 * g_str_has_prefix:
2738 * @str: a nul-terminated string.
2739 * @prefix: the nul-terminated prefix to look for.
2741 * Looks whether the string @str begins with @prefix.
2743 * Return value: %TRUE if @str begins with @prefix, %FALSE otherwise.
2745 * Since: 2.2
2747 gboolean
2748 g_str_has_prefix (const gchar *str,
2749 const gchar *prefix)
2751 int str_len;
2752 int prefix_len;
2754 g_return_val_if_fail (str != NULL, FALSE);
2755 g_return_val_if_fail (prefix != NULL, FALSE);
2757 str_len = strlen (str);
2758 prefix_len = strlen (prefix);
2760 if (str_len < prefix_len)
2761 return FALSE;
2763 return strncmp (str, prefix, prefix_len) == 0;
2768 * g_strip_context:
2769 * @msgid: a string
2770 * @msgval: another string
2772 * An auxiliary function for gettext() support (see Q_()).
2774 * Return value: @msgval, unless @msgval is identical to @msgid and contains
2775 * a '|' character, in which case a pointer to the substring of msgid after
2776 * the first '|' character is returned.
2778 * Since: 2.4
2780 G_CONST_RETURN gchar *
2781 g_strip_context (const gchar *msgid,
2782 const gchar *msgval)
2784 if (msgval == msgid)
2786 const char *c = strchr (msgid, '|');
2787 if (c != NULL)
2788 return c + 1;
2791 return msgval;
2796 * g_strv_length:
2797 * @str_array: a %NULL-terminated array of strings.
2799 * Returns the length of the given %NULL-terminated
2800 * string array @str_array.
2802 * Return value: length of @str_array.
2804 * Since: 2.6
2806 guint
2807 g_strv_length (gchar **str_array)
2809 guint i = 0;
2811 g_return_val_if_fail (str_array != NULL, 0);
2813 while (str_array[i])
2814 ++i;
2816 return i;
2819 #define __G_STRFUNCS_C__
2820 #include "galiasdef.c"