VLC  4.0.0-dev
vlc_strings.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_strings.h: String functions
3  *****************************************************************************
4  * Copyright (C) 2006 VLC authors and VideoLAN
5  *
6  * Authors: Antoine Cellerier <dionoea at videolan dot org>
7  *
8  * This program is free software; you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22 
23 #ifndef VLC_STRINGS_H
24 #define VLC_STRINGS_H 1
25 
26 /**
27  * \defgroup strings String helpers
28  * \ingroup cext
29  * @{
30  * \file
31  * Helper functions for nul-terminated strings
32  */
33 
34 typedef struct vlc_player_t vlc_player_t;
35 
36 static inline int vlc_ascii_toupper( int c )
37 {
38  if ( c >= 'a' && c <= 'z' )
39  return c + ( 'A' - 'a' );
40  else
41  return c;
42 }
43 
44 static inline int vlc_ascii_tolower( int c )
45 {
46  if ( c >= 'A' && c <= 'Z' )
47  return c + ( 'a' - 'A' );
48  else
49  return c;
50 }
51 
52 /**
53  * Compare two ASCII strings ignoring case.
54  *
55  * The result is independent of the locale. If there are non-ASCII
56  * characters in the strings, their cases are NOT ignored in the
57  * comparison.
58  */
59 static inline int vlc_ascii_strcasecmp( const char *psz1, const char *psz2 )
60 {
61  const char *s1 = psz1;
62  const char *s2 = psz2;
63  int d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
64  while ( *s1 && d == 0)
65  {
66  s1++;
67  s2++;
68  d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
69  }
70 
71  return d;
72 }
73 
74 static inline int vlc_ascii_strncasecmp( const char *psz1, const char *psz2, size_t n )
75 {
76  const char *s1 = psz1;
77  const char *s2 = psz2;
78  const char *s1end = psz1 + n;
79  int d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
80  while ( *s1 && s1 < s1end && d == 0)
81  {
82  s1++;
83  s2++;
84  d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
85  }
86 
87  if (s1 == s1end)
88  return 0;
89  else
90  return d;
91 }
92 
93 /**
94  * Decodes XML entities.
95  *
96  * Decodes a null-terminated UTF-8 string of XML character data into a regular
97  * nul-terminated UTF-8 string. In other words, replaces XML entities and
98  * numerical character references with the corresponding characters.
99  *
100  * This function operates in place (the output is always of smaller or equal
101  * length than the input) and always succeeds.
102  *
103  * \param str null-terminated string [IN/OUT]
104  */
105 VLC_API void vlc_xml_decode(char *st);
106 
107 /**
108  * Encodes XML entites.
109  *
110  * Substitutes unsafe characters in a null-terminated UTF-8 strings with an
111  * XML entity or numerical character reference.
112  *
113  * \param str null terminated UTF-8 string
114  * \return On success, a heap-allocated null-terminated string is returned.
115  * If the input string was not a valid UTF-8 sequence, NULL is returned and
116  * errno is set to EILSEQ.
117  * If there was not enough memory, NULL is returned and errno is to ENOMEM.
118  */
119 VLC_API char *vlc_xml_encode(const char *str) VLC_MALLOC;
120 
121 /**
122  * Base64 encoding.
123  *
124  * Encodes a buffer into base64 as a (nul-terminated) string.
125  *
126  * \param base start address of buffer to encode
127  * \param length length in bytes of buffer to encode
128  * \return a heap-allocated nul-terminated string
129  * (or NULL on allocation error).
130  */
131 VLC_API char *vlc_b64_encode_binary(const void *base, size_t length)
133 
134 /**
135  * Base64 encoding (string).
136  *
137  * Encodes a nul-terminated string into Base64.
138  *
139  * \param str nul-terminated string to encode
140  * \return a heap-allocated nul-terminated string
141  * (or NULL on allocation error).
142  */
143 VLC_API char *vlc_b64_encode(const char *str) VLC_USED VLC_MALLOC;
144 
145 VLC_API size_t vlc_b64_decode_binary_to_buffer(void *p_dst, size_t i_dst_max, const char *psz_src );
146 VLC_API size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src );
147 VLC_API char * vlc_b64_decode( const char *psz_src );
148 
149 /**
150  * Convenience wrapper for strftime().
151  *
152  * Formats the current time into a heap-allocated string.
153  *
154  * @param tformat time format (as with C strftime())
155  * @return an allocated string (must be free()'d), or NULL on memory error.
156  */
157 VLC_API char *vlc_strftime( const char * );
158 
159 /**
160  * Formats input meta-data.
161  *
162  * Formats input and input item meta-informations into a heap-allocated string.
163  *
164  * @param player a locked player instance or NULL (player and item can't be
165  * both NULL)
166  * @param item a valid item or NULL (player and item can't be both NULL)
167  * @param fmt format string
168  * @return the formated string, or NULL in case of error, the string need to be
169  * freed with ()
170  */
171 VLC_API char *vlc_strfplayer( vlc_player_t *player, input_item_t *item,
172  const char *fmt );
173 
174 static inline char *str_format( vlc_player_t *player, input_item_t *item,
175  const char *fmt )
176 {
177  char *s1 = vlc_strftime( fmt );
178  char *s2 = vlc_strfplayer( player, item, s1 );
179  free( s1 );
180  return s2;
181 }
182 
183 VLC_API int vlc_filenamecmp(const char *, const char *);
184 
185 void filename_sanitize(char *);
186 
187 /**
188  * @}
189  */
190 
191 #endif
static int vlc_ascii_strncasecmp(const char *psz1, const char *psz2, size_t n)
Definition: vlc_strings.h:75
int vlc_filenamecmp(const char *, const char *)
Definition: strings.c:859
char * vlc_b64_encode_binary(const void *base, size_t length)
Base64 encoding.
Definition: strings.c:350
Definition: player.h:208
Describes an input and is used to spawn input_thread_t objects.
Definition: vlc_input_item.h:77
void vlc_xml_decode(char *st)
Decodes XML entities.
Definition: strings.c:197
char * vlc_strftime(const char *)
Convenience wrapper for strftime().
Definition: strings.c:475
char * vlc_b64_encode(const char *str)
Base64 encoding (string).
Definition: strings.c:398
char * vlc_strfplayer(vlc_player_t *player, input_item_t *item, const char *fmt)
Formats input meta-data.
Definition: strings.c:532
static int vlc_ascii_tolower(int c)
Definition: vlc_strings.h:45
static int vlc_ascii_toupper(int c)
Definition: vlc_strings.h:37
#define VLC_MALLOC
Heap allocated result function annotation.
Definition: vlc_common.h:167
size_t vlc_b64_decode_binary(uint8_t **pp_dst, const char *psz_src)
Definition: strings.c:451
char * vlc_xml_encode(const char *str)
Encodes XML entites.
Definition: strings.c:289
static char * str_format(vlc_player_t *player, input_item_t *item, const char *fmt)
Definition: vlc_strings.h:175
#define VLC_API
Definition: fourcc_gen.c:31
static int vlc_ascii_strcasecmp(const char *psz1, const char *psz2)
Compare two ASCII strings ignoring case.
Definition: vlc_strings.h:60
size_t vlc_b64_decode_binary_to_buffer(void *p_dst, size_t i_dst_max, const char *psz_src)
char * vlc_b64_decode(const char *psz_src)
Definition: strings.c:461
#define VLC_USED
Definition: fourcc_gen.c:32
void filename_sanitize(char *)
Sanitize a file name.
Definition: strings.c:905