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  * Encode binary data as hex string
123  *
124  * Writes a given data buffer to the output buffer as a null terminated
125  * string in hexadecimal representation.
126  *
127  * \param input Input buffer
128  * \param size Input buffer size
129  * \param[out] output Output buffer to write the string to
130  */
131 VLC_API void vlc_hex_encode_binary(const void *input, size_t size, char *output);
132 
133 /**
134  * Base64 encoding.
135  *
136  * Encodes a buffer into base64 as a (nul-terminated) string.
137  *
138  * \param base start address of buffer to encode
139  * \param length length in bytes of buffer to encode
140  * \return a heap-allocated nul-terminated string
141  * (or NULL on allocation error).
142  */
143 VLC_API char *vlc_b64_encode_binary(const void *base, size_t length)
145 
146 /**
147  * Base64 encoding (string).
148  *
149  * Encodes a nul-terminated string into Base64.
150  *
151  * \param str nul-terminated string to encode
152  * \return a heap-allocated nul-terminated string
153  * (or NULL on allocation error).
154  */
155 VLC_API char *vlc_b64_encode(const char *str) VLC_USED VLC_MALLOC;
156 
157 VLC_API size_t vlc_b64_decode_binary_to_buffer(void *p_dst, size_t i_dst_max, const char *psz_src );
158 VLC_API size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src );
159 VLC_API char * vlc_b64_decode( const char *psz_src );
160 
161 /**
162  * Convenience wrapper for strftime().
163  *
164  * Formats the current time into a heap-allocated string.
165  *
166  * \param tformat time format (as with C strftime())
167  * \return an allocated string (must be free()'d), or NULL on memory error.
168  */
169 VLC_API char *vlc_strftime( const char * );
170 
171 /**
172  * Formats input meta-data.
173  *
174  * Formats input and input item meta-informations into a heap-allocated string
175  * according to the given player format string.
176  *
177  * The player format string contains of replacement specifiers, each specifier begins
178  * with the dollar character (`$`) followed by one of the following letters:
179  *
180  * Char | Replacement
181  * ----- | -------------------------------
182  * `a` | Artist metadata
183  * `b` | Album title metadata
184  * `c` | Copyright infromation metadata
185  * `d` | Description metadata
186  * `e` | 'Encoded by' metadata
187  * `f` | Displayed output frame (`-` if not available)
188  * `g` | Genre metadata
189  * `l` | Language metadata
190  * `n` | Current Track number metadata
191  * `o` | Total Track number metadata
192  * `p` | Now playing metadata (i.e. currently playing title for livestreams)
193  * `r` | Rating metadata
194  * `s` | Selected subtitle language (`-` if not available)
195  * `t` | Title metadata
196  * `u` | URL metadata
197  * `A` | Date metadata
198  * `B` | Selected audio track bitrate (`-` if not available)
199  * `C` | Current chapter index (`-` if not available)
200  * `D` | Item duration (`--:--:--` if not available)
201  * `F` | Item URI
202  * `I` | Current title index (`-` if not available)
203  * `L` | Item remaining time (`--:--:--` if not available)
204  * `N` | Item name
205  * `O` | Current audio track language (`-` if not available)
206  * `P` | Current playback position (0.0 to 1.0, `--.-%` if not available)
207  * `R` | Current playback speed (1.0 is normal speed, `-` if not available)
208  * `S` | Current audio track samplerate (`-` if not available)
209  * `T` | Current playback time (`--:--:--` if not available)
210  * `U` | Publisher metadata
211  * `V` | Volume (0 to 256, `---` if not available)
212  * `Z` | Now playing or Artist/Title metadata depending what is available
213  * `_` | Newline (`\n`)
214  *
215  * Additionally characters can be prepended with a whitespace (e.g. `$ T`), which will
216  * cause a replacement with nothing, when not available, instead of the placeholders
217  * documented above.
218  *
219  * \param player a locked player instance or NULL (player and item can't be
220  * both NULL)
221  * \param item a valid item or NULL (player and item can't be both NULL)
222  * \param fmt format string
223  * \return an allocated formatted string (must be free()'d), or NULL in case of error
224  */
225 VLC_API char *vlc_strfplayer( vlc_player_t *player, input_item_t *item,
226  const char *fmt );
227 
228 static inline char *str_format( vlc_player_t *player, input_item_t *item,
229  const char *fmt )
230 {
231  char *s1 = vlc_strftime( fmt );
232  char *s2 = vlc_strfplayer( player, item, s1 );
233  free( s1 );
234  return s2;
235 }
236 
237 VLC_API int vlc_filenamecmp(const char *, const char *);
238 
239 void filename_sanitize(char *);
240 
241 /**
242  * @}
243  */
244 
245 #endif
vlc_player_t
Definition: player.h:227
VLC_API
#define VLC_API
Definition: fourcc_gen.c:31
vlc_strfplayer
VLC_EXPORT char * vlc_strfplayer(vlc_player_t *player, input_item_t *item, const char *fmt)
Formats input meta-data.
Definition: strings.c:542
vlc_xml_decode
VLC_EXPORT void vlc_xml_decode(char *st)
Decodes XML entities.
Definition: strings.c:197
str_format
static char * str_format(vlc_player_t *player, input_item_t *item, const char *fmt)
Definition: vlc_strings.h:229
vlc_common.h
input_item_t
Describes an input and is used to spawn input_thread_t objects.
Definition: vlc_input_item.h:77
vlc_b64_decode_binary
VLC_EXPORT size_t vlc_b64_decode_binary(uint8_t **pp_dst, const char *psz_src)
Definition: strings.c:461
VLC_MALLOC
#define VLC_MALLOC
Heap allocated result function annotation.
Definition: vlc_common.h:167
vlc_ascii_strcasecmp
static int vlc_ascii_strcasecmp(const char *psz1, const char *psz2)
Compare two ASCII strings ignoring case.
Definition: vlc_strings.h:60
filename_sanitize
void filename_sanitize(char *)
Sanitize a file name.
Definition: strings.c:915
vlc_hex_encode_binary
VLC_EXPORT void vlc_hex_encode_binary(const void *input, size_t size, char *output)
Encode binary data as hex string.
Definition: strings.c:350
vlc_b64_encode_binary
VLC_EXPORT char * vlc_b64_encode_binary(const void *base, size_t length)
Base64 encoding.
Definition: strings.c:360
vlc_b64_decode
VLC_EXPORT char * vlc_b64_decode(const char *psz_src)
Definition: strings.c:471
vlc_strftime
VLC_EXPORT char * vlc_strftime(const char *)
Convenience wrapper for strftime().
Definition: strings.c:485
vlc_ascii_strncasecmp
static int vlc_ascii_strncasecmp(const char *psz1, const char *psz2, size_t n)
Definition: vlc_strings.h:75
vlc_player_t::input
struct vlc_player_input * input
Definition: player.h:254
vlc_filenamecmp
VLC_EXPORT int vlc_filenamecmp(const char *, const char *)
Definition: strings.c:869
VLC_USED
#define VLC_USED
Definition: fourcc_gen.c:32
vlc_ascii_tolower
static int vlc_ascii_tolower(int c)
Definition: vlc_strings.h:45
vlc_b64_encode
VLC_EXPORT char * vlc_b64_encode(const char *str)
Base64 encoding (string).
Definition: strings.c:408
vlc_xml_encode
VLC_EXPORT char * vlc_xml_encode(const char *str)
Encodes XML entites.
Definition: strings.c:289
vlc_b64_decode_binary_to_buffer
VLC_EXPORT size_t vlc_b64_decode_binary_to_buffer(void *p_dst, size_t i_dst_max, const char *psz_src)
vlc_ascii_toupper
static int vlc_ascii_toupper(int c)
Definition: vlc_strings.h:37