Home | History | Annotate | Download | only in glib
      1 /* GLIB - Library of useful routines for C programming
      2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
      3  *
      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.
      8  *
      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.
     13  *
     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.
     18  */
     19 
     20 /*
     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/.
     25  */
     26 
     27 #if defined(G_DISABLE_SINGLE_INCLUDES) && !defined (__GLIB_H_INSIDE__) && !defined (GLIB_COMPILATION)
     28 #error "Only <glib.h> can be included directly."
     29 #endif
     30 
     31 #ifndef __G_STRFUNCS_H__
     32 #define __G_STRFUNCS_H__
     33 
     34 #include <stdarg.h>
     35 #include <glib/gtypes.h>
     36 
     37 G_BEGIN_DECLS
     38 
     39 /* Functions like the ones in <ctype.h> that are not affected by locale. */
     40 typedef enum {
     41   G_ASCII_ALNUM  = 1 << 0,
     42   G_ASCII_ALPHA  = 1 << 1,
     43   G_ASCII_CNTRL  = 1 << 2,
     44   G_ASCII_DIGIT  = 1 << 3,
     45   G_ASCII_GRAPH  = 1 << 4,
     46   G_ASCII_LOWER  = 1 << 5,
     47   G_ASCII_PRINT  = 1 << 6,
     48   G_ASCII_PUNCT  = 1 << 7,
     49   G_ASCII_SPACE  = 1 << 8,
     50   G_ASCII_UPPER  = 1 << 9,
     51   G_ASCII_XDIGIT = 1 << 10
     52 } GAsciiType;
     53 
     54 GLIB_VAR const guint16 * const g_ascii_table;
     55 
     56 #define g_ascii_isalnum(c) \
     57   ((g_ascii_table[(guchar) (c)] & G_ASCII_ALNUM) != 0)
     58 
     59 #define g_ascii_isalpha(c) \
     60   ((g_ascii_table[(guchar) (c)] & G_ASCII_ALPHA) != 0)
     61 
     62 #define g_ascii_iscntrl(c) \
     63   ((g_ascii_table[(guchar) (c)] & G_ASCII_CNTRL) != 0)
     64 
     65 #define g_ascii_isdigit(c) \
     66   ((g_ascii_table[(guchar) (c)] & G_ASCII_DIGIT) != 0)
     67 
     68 #define g_ascii_isgraph(c) \
     69   ((g_ascii_table[(guchar) (c)] & G_ASCII_GRAPH) != 0)
     70 
     71 #define g_ascii_islower(c) \
     72   ((g_ascii_table[(guchar) (c)] & G_ASCII_LOWER) != 0)
     73 
     74 #define g_ascii_isprint(c) \
     75   ((g_ascii_table[(guchar) (c)] & G_ASCII_PRINT) != 0)
     76 
     77 #define g_ascii_ispunct(c) \
     78   ((g_ascii_table[(guchar) (c)] & G_ASCII_PUNCT) != 0)
     79 
     80 #define g_ascii_isspace(c) \
     81   ((g_ascii_table[(guchar) (c)] & G_ASCII_SPACE) != 0)
     82 
     83 #define g_ascii_isupper(c) \
     84   ((g_ascii_table[(guchar) (c)] & G_ASCII_UPPER) != 0)
     85 
     86 #define g_ascii_isxdigit(c) \
     87   ((g_ascii_table[(guchar) (c)] & G_ASCII_XDIGIT) != 0)
     88 
     89 gchar                 g_ascii_tolower  (gchar        c) G_GNUC_CONST;
     90 gchar                 g_ascii_toupper  (gchar        c) G_GNUC_CONST;
     91 
     92 gint                  g_ascii_digit_value  (gchar    c) G_GNUC_CONST;
     93 gint                  g_ascii_xdigit_value (gchar    c) G_GNUC_CONST;
     94 
     95 /* String utility functions that modify a string argument or
     96  * return a constant string that must not be freed.
     97  */
     98 #define	 G_STR_DELIMITERS	"_-|> <."
     99 gchar*	              g_strdelimit     (gchar	     *string,
    100 					const gchar  *delimiters,
    101 					gchar	      new_delimiter);
    102 gchar*	              g_strcanon       (gchar        *string,
    103 					const gchar  *valid_chars,
    104 					gchar         substitutor);
    105 G_CONST_RETURN gchar* g_strerror       (gint	      errnum) G_GNUC_CONST;
    106 G_CONST_RETURN gchar* g_strsignal      (gint	      signum) G_GNUC_CONST;
    107 gchar*	              g_strreverse     (gchar	     *string);
    108 gsize	              g_strlcpy	       (gchar	     *dest,
    109 					const gchar  *src,
    110 					gsize         dest_size);
    111 gsize	              g_strlcat        (gchar	     *dest,
    112 					const gchar  *src,
    113 					gsize         dest_size);
    114 gchar *               g_strstr_len     (const gchar  *haystack,
    115 					gssize        haystack_len,
    116 					const gchar  *needle);
    117 gchar *               g_strrstr        (const gchar  *haystack,
    118 					const gchar  *needle);
    119 gchar *               g_strrstr_len    (const gchar  *haystack,
    120 					gssize        haystack_len,
    121 					const gchar  *needle);
    122 
    123 gboolean              g_str_has_suffix (const gchar  *str,
    124 					const gchar  *suffix);
    125 gboolean              g_str_has_prefix (const gchar  *str,
    126 					const gchar  *prefix);
    127 
    128 /* String to/from double conversion functions */
    129 
    130 gdouble	              g_strtod         (const gchar  *nptr,
    131 					gchar	    **endptr);
    132 gdouble	              g_ascii_strtod   (const gchar  *nptr,
    133 					gchar	    **endptr);
    134 guint64		      g_ascii_strtoull (const gchar *nptr,
    135 					gchar      **endptr,
    136 					guint        base);
    137 gint64		      g_ascii_strtoll  (const gchar *nptr,
    138 					gchar      **endptr,
    139 					guint        base);
    140 /* 29 bytes should enough for all possible values that
    141  * g_ascii_dtostr can produce.
    142  * Then add 10 for good measure */
    143 #define G_ASCII_DTOSTR_BUF_SIZE (29 + 10)
    144 gchar *               g_ascii_dtostr   (gchar        *buffer,
    145 					gint          buf_len,
    146 					gdouble       d);
    147 gchar *               g_ascii_formatd  (gchar        *buffer,
    148 					gint          buf_len,
    149 					const gchar  *format,
    150 					gdouble       d);
    151 
    152 /* removes leading spaces */
    153 gchar*                g_strchug        (gchar        *string);
    154 /* removes trailing spaces */
    155 gchar*                g_strchomp       (gchar        *string);
    156 /* removes leading & trailing spaces */
    157 #define g_strstrip( string )	g_strchomp (g_strchug (string))
    158 
    159 gint                  g_ascii_strcasecmp  (const gchar *s1,
    160 					   const gchar *s2);
    161 gint                  g_ascii_strncasecmp (const gchar *s1,
    162 					   const gchar *s2,
    163 					   gsize        n);
    164 gchar*                g_ascii_strdown     (const gchar *str,
    165 					   gssize       len) G_GNUC_MALLOC;
    166 gchar*                g_ascii_strup       (const gchar *str,
    167 					   gssize       len) G_GNUC_MALLOC;
    168 
    169 #ifndef G_DISABLE_DEPRECATED
    170 
    171 /* The following four functions are deprecated and will be removed in
    172  * the next major release. They use the locale-specific tolower and
    173  * toupper, which is almost never the right thing.
    174  */
    175 
    176 gint	              g_strcasecmp     (const gchar *s1,
    177 					const gchar *s2);
    178 gint	              g_strncasecmp    (const gchar *s1,
    179 					const gchar *s2,
    180 					guint        n);
    181 gchar*	              g_strdown	       (gchar	     *string);
    182 gchar*	              g_strup	       (gchar	     *string);
    183 
    184 #endif /* G_DISABLE_DEPRECATED */
    185 
    186 /* String utility functions that return a newly allocated string which
    187  * ought to be freed with g_free from the caller at some point.
    188  */
    189 gchar*	              g_strdup	       (const gchar *str) G_GNUC_MALLOC;
    190 gchar*	              g_strdup_printf  (const gchar *format,
    191 					...) G_GNUC_PRINTF (1, 2) G_GNUC_MALLOC;
    192 gchar*	              g_strdup_vprintf (const gchar *format,
    193 					va_list      args) G_GNUC_MALLOC;
    194 gchar*	              g_strndup	       (const gchar *str,
    195 					gsize        n) G_GNUC_MALLOC;
    196 gchar*	              g_strnfill       (gsize        length,
    197 					gchar        fill_char) G_GNUC_MALLOC;
    198 gchar*	              g_strconcat      (const gchar *string1,
    199 					...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
    200 gchar*                g_strjoin	       (const gchar  *separator,
    201 					...) G_GNUC_MALLOC G_GNUC_NULL_TERMINATED;
    202 
    203 /* Make a copy of a string interpreting C string -style escape
    204  * sequences. Inverse of g_strescape. The recognized sequences are \b
    205  * \f \n \r \t \\ \" and the octal format.
    206  */
    207 gchar*                g_strcompress    (const gchar *source) G_GNUC_MALLOC;
    208 
    209 /* Copy a string escaping nonprintable characters like in C strings.
    210  * Inverse of g_strcompress. The exceptions parameter, if non-NULL, points
    211  * to a string containing characters that are not to be escaped.
    212  *
    213  * Deprecated API: gchar* g_strescape (const gchar *source);
    214  * Luckily this function wasn't used much, using NULL as second parameter
    215  * provides mostly identical semantics.
    216  */
    217 gchar*                g_strescape      (const gchar *source,
    218 					const gchar *exceptions) G_GNUC_MALLOC;
    219 
    220 gpointer              g_memdup	       (gconstpointer mem,
    221 					guint	       byte_size) G_GNUC_MALLOC G_GNUC_ALLOC_SIZE(2);
    222 
    223 /* NULL terminated string arrays.
    224  * g_strsplit(), g_strsplit_set() split up string into max_tokens tokens
    225  * at delim and return a newly allocated string array.
    226  * g_strjoinv() concatenates all of str_array's strings, sliding in an
    227  * optional separator, the returned string is newly allocated.
    228  * g_strfreev() frees the array itself and all of its strings.
    229  * g_strdupv() copies a NULL-terminated array of strings
    230  * g_strv_length() returns the length of a NULL-terminated array of strings
    231  */
    232 gchar**	              g_strsplit       (const gchar  *string,
    233 					const gchar  *delimiter,
    234 					gint          max_tokens) G_GNUC_MALLOC;
    235 gchar **	      g_strsplit_set   (const gchar *string,
    236 					const gchar *delimiters,
    237 					gint         max_tokens) G_GNUC_MALLOC;
    238 gchar*                g_strjoinv       (const gchar  *separator,
    239 					gchar       **str_array) G_GNUC_MALLOC;
    240 void                  g_strfreev       (gchar       **str_array);
    241 gchar**               g_strdupv        (gchar       **str_array) G_GNUC_MALLOC;
    242 guint                 g_strv_length    (gchar       **str_array);
    243 
    244 gchar*                g_stpcpy         (gchar        *dest,
    245                                         const char   *src);
    246 
    247 G_CONST_RETURN gchar *g_strip_context  (const gchar *msgid,
    248 					const gchar *msgval) G_GNUC_FORMAT(1);
    249 
    250 G_CONST_RETURN gchar *g_dgettext       (const gchar *domain,
    251 					const gchar *msgid) G_GNUC_FORMAT(2);
    252 
    253 G_CONST_RETURN gchar *g_dngettext      (const gchar *domain,
    254 					const gchar *msgid,
    255 					const gchar *msgid_plural,
    256 					gulong       n) G_GNUC_FORMAT(3);
    257 G_CONST_RETURN gchar *g_dpgettext      (const gchar *domain,
    258                                         const gchar *msgctxtid,
    259                                         gsize        msgidoffset) G_GNUC_FORMAT(2);
    260 G_CONST_RETURN gchar *g_dpgettext2     (const gchar *domain,
    261                                         const gchar *context,
    262                                         const gchar *msgid) G_GNUC_FORMAT(3);
    263 
    264 G_END_DECLS
    265 
    266 #endif /* __G_STRFUNCS_H__ */
    267