]> git.sesse.net Git - vlc/blob - modules/misc/lua/libs/strings.c
Add new functions to Lua API.
[vlc] / modules / misc / lua / libs / strings.c
1 /*****************************************************************************
2  * strings.c
3  *****************************************************************************
4  * Copyright (C) 2007-2008 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea at videolan tod org>
8  *          Pierre d'Herbemont <pdherbemont # videolan.org>
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 #ifndef  _GNU_SOURCE
29 #   define  _GNU_SOURCE
30 #endif
31
32 #ifdef HAVE_CONFIG_H
33 # include "config.h"
34 #endif
35
36 #include <vlc_common.h>
37 #include <vlc_plugin.h>
38 #include <vlc_meta.h>
39 #include <vlc_charset.h>
40 #include <vlc_aout.h>
41
42 #include <lua.h>        /* Low level lua C API */
43 #include <lauxlib.h>    /* Higher level C API */
44
45 #include "../vlc.h"
46 #include "../libs.h"
47
48 /*****************************************************************************
49  * String transformations
50  *****************************************************************************/
51 static int vlclua_decode_uri( lua_State *L )
52 {
53     int i_top = lua_gettop( L );
54     int i;
55     for( i = 1; i <= i_top; i++ )
56     {
57         const char *psz_cstring = luaL_checkstring( L, 1 );
58         char *psz_string = strdup( psz_cstring );
59         lua_remove( L, 1 ); /* remove elements to prevent being limited by
60                              * the stack's size (this function will work with
61                              * up to (stack size - 1) arguments */
62         decode_URI( psz_string );
63         lua_pushstring( L, psz_string );
64         free( psz_string );
65     }
66     return i_top;
67 }
68
69 static int vlclua_encode_uri_component( lua_State *L )
70 {
71     int i_top = lua_gettop( L );
72     int i;
73     for( i = 1; i <= i_top; i++ )
74     {
75         const char *psz_cstring = luaL_checkstring( L, 1 );
76         char *psz_string = encode_URI_component( psz_cstring );
77         lua_remove( L,1 );
78         lua_pushstring( L, psz_string );
79         free( psz_string );
80     }
81     return i_top;
82 }
83
84 static int vlclua_resolve_xml_special_chars( lua_State *L )
85 {
86     int i_top = lua_gettop( L );
87     int i;
88     for( i = 1; i <= i_top; i++ )
89     {
90         const char *psz_cstring = luaL_checkstring( L, 1 );
91         char *psz_string = strdup( psz_cstring );
92         lua_remove( L, 1 ); /* remove elements to prevent being limited by
93                              * the stack's size (this function will work with
94                              * up to (stack size - 1) arguments */
95         resolve_xml_special_chars( psz_string );
96         lua_pushstring( L, psz_string );
97         free( psz_string );
98     }
99     return i_top;
100 }
101
102 static int vlclua_convert_xml_special_chars( lua_State *L )
103 {
104     int i_top = lua_gettop( L );
105     int i;
106     for( i = 1; i <= i_top; i++ )
107     {
108         char *psz_string = convert_xml_special_chars( luaL_checkstring(L,1) );
109         lua_remove( L, 1 );
110         lua_pushstring( L, psz_string );
111         free( psz_string );
112     }
113     return i_top;
114 }
115
116 /*****************************************************************************
117  *
118  *****************************************************************************/
119 static const luaL_Reg vlclua_strings_reg[] = {
120     { "decode_uri", vlclua_decode_uri },
121     { "encode_uri_component", vlclua_encode_uri_component },
122     { "resolve_xml_special_chars", vlclua_resolve_xml_special_chars },
123     { "convert_xml_special_chars", vlclua_convert_xml_special_chars },
124     { NULL, NULL }
125 };
126
127 void luaopen_strings( lua_State *L )
128 {
129     lua_newtable( L );
130     luaL_register( L, NULL, vlclua_strings_reg );
131     lua_setfield( L, -2, "strings" );
132 }