1 /*****************************************************************************
2 * vlc_strings.h: String functions
3 *****************************************************************************
4 * Copyright (C) 2006 VLC authors and VideoLAN
6 * Authors: Antoine Cellerier <dionoea at videolan dot org>
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.
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.
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 *****************************************************************************/
24 #define VLC_STRINGS_H 1
27 * \defgroup strings String helpers
31 * Helper functions for nul-terminated strings
34 typedef struct vlc_player_t vlc_player_t
;
36 static inline int vlc_ascii_toupper( int c
)
38 if ( c
>= 'a' && c
<= 'z' )
39 return c
+ ( 'A' - 'a' );
44 static inline int vlc_ascii_tolower( int c
)
46 if ( c
>= 'A' && c
<= 'Z' )
47 return c
+ ( 'a' - 'A' );
53 * Compare two ASCII strings ignoring case.
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
59 static inline int vlc_ascii_strcasecmp( const char *psz1
, const char *psz2
)
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)
68 d
= vlc_ascii_tolower( *s1
) - vlc_ascii_tolower( *s2
);
74 static inline int vlc_ascii_strncasecmp( const char *psz1
, const char *psz2
, size_t n
)
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)
84 d
= vlc_ascii_tolower( *s1
) - vlc_ascii_tolower( *s2
);
94 * Decodes XML entities.
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.
100 * This function operates in place (the output is always of smaller or equal
101 * length than the input) and always succeeds.
103 * \param str null-terminated string [IN/OUT]
105 VLC_API
void vlc_xml_decode(char *st
);
108 * Encodes XML entites.
110 * Substitutes unsafe characters in a null-terminated UTF-8 strings with an
111 * XML entity or numerical character reference.
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.
119 VLC_API
char *vlc_xml_encode(const char *str
) VLC_MALLOC
;
122 * Encode binary data as hex string
124 * Writes a given data buffer to the output buffer as a null terminated
125 * string in hexadecimal representation.
127 * \param input Input buffer
128 * \param size Input buffer size
129 * \param[out] output Output buffer to write the string to
131 VLC_API
void vlc_hex_encode_binary(const void *input
, size_t size
, char *output
);
136 * Encodes a buffer into base64 as a (nul-terminated) string.
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).
143 VLC_API
char *vlc_b64_encode_binary(const void *base
, size_t length
)
147 * Base64 encoding (string).
149 * Encodes a nul-terminated string into Base64.
151 * \param str nul-terminated string to encode
152 * \return a heap-allocated nul-terminated string
153 * (or NULL on allocation error).
155 VLC_API
char *vlc_b64_encode(const char *str
) VLC_USED VLC_MALLOC
;
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
);
162 * Convenience wrapper for strftime().
164 * Formats the current time into a heap-allocated string.
166 * \param tformat time format (as with C strftime())
167 * \return an allocated string (must be free()'d), or NULL on memory error.
169 VLC_API
char *vlc_strftime( const char * );
172 * Formats input meta-data.
174 * Formats input and input item meta-informations into a heap-allocated string
175 * according to the given player format string.
177 * The player format string contains of replacement specifiers, each specifier begins
178 * with the dollar character (`$`) followed by one of the following letters:
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
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)
202 * `I` | Current title index (`-` if not available)
203 * `L` | Item remaining time (`--:--:--` if not available)
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`)
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
219 * \param player a locked player instance or NULL (player and item can't be
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
225 VLC_API
char *vlc_strfplayer( vlc_player_t
*player
, input_item_t
*item
,
228 static inline char *str_format( vlc_player_t
*player
, input_item_t
*item
,
231 char *s1
= vlc_strftime( fmt
);
232 char *s2
= vlc_strfplayer( player
, item
, s1
);
237 VLC_API
int vlc_filenamecmp(const char *, const char *);
239 void filename_sanitize(char *);