]> git.sesse.net Git - vlc/blob - src/misc/strings.c
645f355d5914dbac54ede4af6da39c7019dc8cc8
[vlc] / src / misc / strings.c
1 /*****************************************************************************
2  * strings.c: String related functions
3  *****************************************************************************
4  * Copyright (C) 2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan dot org>
8  *          Daniel Stranger <vlc at schmaller dot de>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31
32 #include "vlc_strings.h"
33
34 /**
35  * Decode URI encoded string
36  * \return decoded duplicated string
37  */
38 char *decode_encoded_URI_duplicate( const char *psz )
39 {
40     char *psz_dup = strdup( psz );
41     decode_encoded_URI( psz_dup );
42     return psz_dup;
43 }
44
45 /**
46  * Decode URI encoded string
47  * \return nothing
48  */
49 void decode_encoded_URI( char *psz )
50 {
51     char *dup = strdup( psz );
52     char *p = dup;
53
54     while( *p )
55     {
56         if( *p == '%' )
57         {
58             char val[3];
59             p++;
60             if( !*p )
61             {
62                 break;
63             }
64
65             val[0] = *p++;
66             val[1] = *p++;
67             val[2] = '\0';
68
69             *psz++ = strtol( val, NULL, 16 );
70         }
71         else if( *p == '+' )
72         {
73             *psz++ = ' ';
74             p++;
75         }
76         else
77         {
78             *psz++ = *p++;
79         }
80     }
81     *psz++ = '\0';
82     free( dup );
83 }
84
85 /**
86  * Converts "&lt;", "&gt;" and "&amp;" to "<", ">" and "&"
87  * \param string to convert
88  */
89 void resolve_xml_special_chars( char *psz_value )
90 {
91     char *p_pos = psz_value;
92
93     while ( *psz_value )
94     {
95         if( !strncmp( psz_value, "&lt;", 4 ) )
96         {
97             *p_pos = '<';
98             psz_value += 4;
99         }
100         else if( !strncmp( psz_value, "&gt;", 4 ) )
101         {
102             *p_pos = '>';
103             psz_value += 4;
104         }
105         else if( !strncmp( psz_value, "&amp;", 5 ) )
106         {
107             *p_pos = '&';
108             psz_value += 5;
109         }
110         else if( !strncmp( psz_value, "&quot;", 6 ) )
111         {
112             *p_pos = '\"';
113             psz_value += 6;
114         }
115         else if( !strncmp( psz_value, "&#039;", 6 ) )
116         {
117             *p_pos = '\'';
118             psz_value += 6;
119         }
120         else
121         {
122             *p_pos = *psz_value;
123             psz_value++;
124         }
125
126         p_pos++;
127     }
128
129     *p_pos = '\0';
130 }
131
132 /**
133  * Converts '<', '>', '\"', '\'' and '&' to their html entities
134  * \param psz_content simple element content that is to be converted
135  */
136 char *convert_xml_special_chars( const char *psz_content )
137 {
138     char *psz_temp = malloc( 6 * strlen( psz_content ) + 1 );
139     const char *p_from = psz_content;
140     char *p_to   = psz_temp;
141
142     while ( *p_from )
143     {
144         if ( *p_from == '<' )
145         {
146             strcpy( p_to, "&lt;" );
147             p_to += 4;
148         }
149         else if ( *p_from == '>' )
150         {
151             strcpy( p_to, "&gt;" );
152             p_to += 4;
153         }
154         else if ( *p_from == '&' )
155         {
156             strcpy( p_to, "&amp;" );
157             p_to += 5;
158         }
159         else if( *p_from == '\"' )
160         {
161             strcpy( p_to, "&quot;" );
162             p_to += 6;
163         }
164         else if( *p_from == '\'' )
165         {
166             strcpy( p_to, "&#039;" );
167             p_to += 6;
168         }
169         else
170         {
171             *p_to = *p_from;
172             p_to++;
173         }
174         p_from++;
175     }
176     *p_to = '\0';
177
178     return psz_temp;
179 }