Mon Jun 27 16:50:54 2011

Asterisk developer's documentation


func_iconv.c

Go to the documentation of this file.
00001 /*
00002  * Asterisk -- An open source telephony toolkit.
00003  *
00004  * Copyright (c) 2005,2006,2007 Sven Slezak <sunny@mezzo.net>
00005  *
00006  * See http://www.asterisk.org for more information about
00007  * the Asterisk project. Please do not directly contact
00008  * any of the maintainers of this project for assistance;
00009  * the project provides a web site, mailing lists and IRC
00010  * channels for your use.
00011  *
00012  * This program is free software, distributed under the terms of
00013  * the GNU General Public License Version 2. See the LICENSE file
00014  * at the top of the source tree.
00015  */
00016 
00017 /*!
00018  * \file
00019  *
00020  * \brief Charset conversions
00021  *
00022  * \author Sven Slezak <sunny@mezzo.net>
00023  *
00024  * \ingroup functions
00025  */
00026 
00027 /*** MODULEINFO
00028     <depend>iconv</depend>
00029  ***/
00030 
00031 #include "asterisk.h"
00032 
00033 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 153365 $")
00034 
00035 #include <ctype.h>
00036 #include <iconv.h>
00037 
00038 #include "asterisk/module.h"
00039 #include "asterisk/channel.h"
00040 #include "asterisk/pbx.h"
00041 #include "asterisk/utils.h"
00042 #include "asterisk/app.h"
00043 
00044 /*** DOCUMENTATION
00045    <function name="ICONV" language="en_US">
00046       <synopsis>
00047          Converts charsets of strings. 
00048       </synopsis>
00049       <syntax>
00050          <parameter name="in-charset" required="true">
00051             <para>Input charset</para>
00052          </parameter>
00053          <parameter name="out-charset" required="true">
00054             <para>Output charset</para>
00055          </parameter>
00056          <parameter name="string" required="true">
00057             <para>String to convert, from <replaceable>in-charset</replaceable> to <replaceable>out-charset</replaceable></para>
00058          </parameter>
00059       </syntax>
00060       <description>
00061          <para>Converts string from <replaceable>in-charset</replaceable> into <replaceable>out-charset</replaceable>.
00062          For available charsets, use <literal>iconv -l</literal> on your shell command line.</para>
00063          <note><para>Due to limitations within the API, ICONV will not currently work with
00064          charsets with embedded NULLs. If found, the string will terminate.</para></note>
00065       </description>
00066    </function>
00067  ***/
00068 
00069 
00070 /*! 
00071  * Some systems define the second arg to iconv() as (const char *),
00072  * while others define it as (char *).  Cast it to a (void *) to 
00073  * suppress compiler warnings about it. 
00074  */
00075 #define AST_ICONV_CAST void *
00076 
00077 static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
00078 {
00079    AST_DECLARE_APP_ARGS(args,
00080       AST_APP_ARG(in_charset);
00081       AST_APP_ARG(out_charset);
00082       AST_APP_ARG(text);
00083    );
00084    iconv_t cd;
00085    size_t incount, outcount = len;
00086    char *parse;
00087 
00088    if (ast_strlen_zero(arguments)) {
00089       ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
00090       return -1;
00091    }
00092 
00093    parse = ast_strdupa(arguments);
00094    AST_STANDARD_APP_ARGS(args, parse);
00095 
00096    if (args.argc < 3) {
00097       ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %d\n", args.argc);
00098       return -1;
00099    }
00100 
00101    incount = strlen(args.text);
00102 
00103    ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
00104 
00105    cd = iconv_open(args.out_charset, args.in_charset);
00106 
00107    if (cd == (iconv_t) -1) {
00108       ast_log(LOG_ERROR, "conversion from '%s' to '%s' not available. type 'iconv -l' in a shell to list the supported charsets.\n", args.in_charset, args.out_charset);
00109       return -1;
00110    }
00111 
00112    if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
00113       if (errno == E2BIG)
00114          ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
00115       else if (errno == EILSEQ)
00116          ast_log(LOG_WARNING,  "Iconv: illegal character.\n");
00117       else if (errno == EINVAL)
00118          ast_log(LOG_WARNING,  "Iconv: incomplete character sequence.\n");
00119       else
00120          ast_log(LOG_WARNING,  "Iconv: error %d: %s.\n", errno, strerror(errno));
00121    }
00122    iconv_close(cd);
00123 
00124    return 0;
00125 }
00126 
00127 
00128 static struct ast_custom_function iconv_function = {
00129    .name = "ICONV",
00130    .read = iconv_read
00131 };
00132 
00133 static int unload_module(void)
00134 {
00135    return ast_custom_function_unregister(&iconv_function);
00136 }
00137 
00138 static int load_module(void)
00139 {
00140    return ast_custom_function_register(&iconv_function);
00141 }
00142 
00143 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Charset conversions");
00144 

Generated on Mon Jun 27 16:50:54 2011 for Asterisk - The Open Source Telephony Project by  doxygen 1.4.7