VLC  3.0.15
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  * $Id: 9828fbd728ee791d409568405f280634132b91a6 $
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan dot org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23 
24 #ifndef VLC_STRINGS_H
25 #define VLC_STRINGS_H 1
26 
27 /**
28  * \defgroup strings String helpers
29  * @{
30  * \file
31  * Helper functions for nul-terminated strings
32  */
33 
34 static inline int vlc_ascii_toupper( int c )
35 {
36  if ( c >= 'a' && c <= 'z' )
37  return c + ( 'A' - 'a' );
38  else
39  return c;
40 }
41 
42 static inline int vlc_ascii_tolower( int c )
43 {
44  if ( c >= 'A' && c <= 'Z' )
45  return c + ( 'a' - 'A' );
46  else
47  return c;
48 }
49 
50 /**
51  * Compare two ASCII strings ignoring case.
52  *
53  * The result is independent of the locale. If there are non-ASCII
54  * characters in the strings, their cases are NOT ignored in the
55  * comparison.
56  */
57 static inline int vlc_ascii_strcasecmp( const char *psz1, const char *psz2 )
58 {
59  const char *s1 = psz1;
60  const char *s2 = psz2;
61  int d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
62  while ( *s1 && d == 0)
63  {
64  s1++;
65  s2++;
66  d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
67  }
68 
69  return d;
70 }
71 
72 static inline int vlc_ascii_strncasecmp( const char *psz1, const char *psz2, size_t n )
73 {
74  const char *s1 = psz1;
75  const char *s2 = psz2;
76  const char *s1end = psz1 + n;
77  int d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
78  while ( *s1 && s1 < s1end && d == 0)
79  {
80  s1++;
81  s2++;
82  d = vlc_ascii_tolower( *s1 ) - vlc_ascii_tolower( *s2 );
83  }
84 
85  if (s1 == s1end)
86  return 0;
87  else
88  return d;
89 }
90 
91 /**
92  * Decodes XML entities.
93  *
94  * Decodes a null-terminated UTF-8 string of XML character data into a regular
95  * nul-terminated UTF-8 string. In other words, replaces XML entities and
96  * numerical character references with the corresponding characters.
97  *
98  * This function operates in place (the output is always of smaller or equal
99  * length than the input) and always succeeds.
100  *
101  * \param str null-terminated string [IN/OUT]
102  */
103 VLC_API void vlc_xml_decode(char *st);
104 
105 /**
106  * Encodes XML entites.
107  *
108  * Substitutes unsafe characters in a null-terminated UTF-8 strings with an
109  * XML entity or numerical character reference.
110  *
111  * \param str null terminated UTF-8 string
112  * \return On success, a heap-allocated null-terminated string is returned.
113  * If the input string was not a valid UTF-8 sequence, NULL is returned and
114  * errno is set to EILSEQ.
115  * If there was not enough memory, NULL is returned and errno is to ENOMEM.
116  */
117 VLC_API char *vlc_xml_encode(const char *str) VLC_MALLOC;
118 
119 VLC_API char * vlc_b64_encode_binary( const uint8_t *, size_t );
120 VLC_API char * vlc_b64_encode( const char * );
121 
122 VLC_API size_t vlc_b64_decode_binary_to_buffer( uint8_t *p_dst, size_t i_dst_max, const char *psz_src );
123 VLC_API size_t vlc_b64_decode_binary( uint8_t **pp_dst, const char *psz_src );
124 VLC_API char * vlc_b64_decode( const char *psz_src );
125 
126 /**
127  * Convenience wrapper for strftime().
128  *
129  * Formats the current time into a heap-allocated string.
130  *
131  * @param tformat time format (as with C strftime())
132  * @return an allocated string (must be free()'d), or NULL on memory error.
133  */
134 VLC_API char *vlc_strftime( const char * );
135 
136 /**
137  * Formats input meta-data.
138  *
139  * Formats input and input item meta-informations into a heap-allocated string.
140  */
141 VLC_API char *vlc_strfinput( input_thread_t *, const char * );
142 
143 static inline char *str_format( input_thread_t *input, const char *fmt )
144 {
145  char *s1 = vlc_strftime( fmt );
146  char *s2 = vlc_strfinput( input, s1 );
147  free( s1 );
148  return s2;
149 }
150 
151 VLC_API int vlc_filenamecmp(const char *, const char *);
152 
153 void filename_sanitize(char *);
154 
155 /**
156  * @}
157  */
158 
159 #endif
VLC_API
#define VLC_API
Definition: fourcc_gen.c:30
vlc_xml_encode
char * vlc_xml_encode(const char *str)
Encodes XML entites.
Definition: strings.c:288
vlc_b64_decode_binary_to_buffer
size_t vlc_b64_decode_binary_to_buffer(uint8_t *p_dst, size_t i_dst_max, const char *psz_src)
Definition: strings.c:404
vlc_common.h
vlc_ascii_strcasecmp
static int vlc_ascii_strcasecmp(const char *psz1, const char *psz2)
Compare two ASCII strings ignoring case.
Definition: vlc_strings.h:57
vlc_b64_decode
char * vlc_b64_decode(const char *psz_src)
Definition: strings.c:468
vlc_b64_encode
char * vlc_b64_encode(const char *)
Definition: strings.c:395
vlc_b64_encode_binary
char * vlc_b64_encode_binary(const uint8_t *, size_t)
Definition: strings.c:349
filename_sanitize
void filename_sanitize(char *)
Sanitize a file name.
Definition: strings.c:875
vlc_filenamecmp
int vlc_filenamecmp(const char *, const char *)
Definition: strings.c:829
vlc_strftime
char * vlc_strftime(const char *)
Convenience wrapper for strftime().
Definition: strings.c:482
vlc_ascii_strncasecmp
static int vlc_ascii_strncasecmp(const char *psz1, const char *psz2, size_t n)
Definition: vlc_strings.h:72
str_format
static char * str_format(input_thread_t *input, const char *fmt)
Definition: vlc_strings.h:143
vlc_xml_decode
void vlc_xml_decode(char *st)
Decodes XML entities.
Definition: strings.c:196
VLC_MALLOC
#define VLC_MALLOC
Definition: vlc_common.h:102
vlc_ascii_tolower
static int vlc_ascii_tolower(int c)
Definition: vlc_strings.h:42
input_thread_t
Main structure representing an input thread.
Definition: vlc_input.h:221
vlc_ascii_toupper
static int vlc_ascii_toupper(int c)
Definition: vlc_strings.h:34
vlc_b64_decode_binary
size_t vlc_b64_decode_binary(uint8_t **pp_dst, const char *psz_src)
Definition: strings.c:458
vlc_strfinput
char * vlc_strfinput(input_thread_t *, const char *)
Formats input meta-data.
Definition: strings.c:539