Thu Jul 9 13:40:35 2009

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: 95624 $")
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 /*! 
00045  * Some systems define the second arg to iconv() as (const char *),
00046  * while others define it as (char *).  Cast it to a (void *) to 
00047  * suppress compiler warnings about it. 
00048  */
00049 #define AST_ICONV_CAST void *
00050 
00051 static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
00052 {
00053    AST_DECLARE_APP_ARGS(args,
00054       AST_APP_ARG(in_charset);
00055       AST_APP_ARG(out_charset);
00056       AST_APP_ARG(text);
00057    );
00058    iconv_t cd;
00059    size_t incount, outcount = len;
00060    char *parse;
00061 
00062    if (ast_strlen_zero(arguments)) {
00063       ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
00064       return -1;
00065    }
00066 
00067    parse = ast_strdupa(arguments);
00068    AST_STANDARD_APP_ARGS(args, parse);
00069 
00070    if (args.argc < 3) {
00071       ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %d\n", args.argc);
00072       return -1;
00073    }
00074 
00075    incount = strlen(args.text);
00076 
00077    ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
00078 
00079    cd = iconv_open(args.out_charset, args.in_charset);
00080 
00081    if (cd == (iconv_t) -1) {
00082       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);
00083       return -1;
00084    }
00085 
00086    if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
00087       if (errno == E2BIG)
00088          ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
00089       else if (errno == EILSEQ)
00090          ast_log(LOG_WARNING,  "Iconv: illegal character.\n");
00091       else if (errno == EINVAL)
00092          ast_log(LOG_WARNING,  "Iconv: incomplete character sequence.\n");
00093       else
00094          ast_log(LOG_WARNING,  "Iconv: error %d: %s.\n", errno, strerror(errno));
00095    }
00096    iconv_close(cd);
00097 
00098    return 0;
00099 }
00100 
00101 
00102 static struct ast_custom_function iconv_function = {
00103    .name = "ICONV",
00104    .synopsis = "Converts charsets of strings.",
00105    .desc =
00106 "Converts string from in-charset into out-charset.  For available charsets,\n"
00107 "use 'iconv -l' on your shell command line.\n"
00108 "Note: due to limitations within the API, ICONV will not currently work with\n"
00109 "charsets with embedded NULLs.  If found, the string will terminate.\n",
00110    .syntax = "ICONV(in-charset,out-charset,string)",
00111    .read = iconv_read,
00112 };
00113 
00114 static int unload_module(void)
00115 {
00116    return ast_custom_function_unregister(&iconv_function);
00117 }
00118 
00119 static int load_module(void)
00120 {
00121    return ast_custom_function_register(&iconv_function);
00122 }
00123 
00124 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Charset conversions");
00125 

Generated on Thu Jul 9 13:40:35 2009 for Asterisk - the Open Source PBX by  doxygen 1.4.7