func_iconv.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include "asterisk.h"
00033
00034 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
00035
00036 #include <ctype.h>
00037 #include <iconv.h>
00038
00039 #include "asterisk/module.h"
00040 #include "asterisk/channel.h"
00041 #include "asterisk/pbx.h"
00042 #include "asterisk/utils.h"
00043 #include "asterisk/app.h"
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076 #define AST_ICONV_CAST void *
00077
00078 static int iconv_read(struct ast_channel *chan, const char *cmd, char *arguments, char *buf, size_t len)
00079 {
00080 AST_DECLARE_APP_ARGS(args,
00081 AST_APP_ARG(in_charset);
00082 AST_APP_ARG(out_charset);
00083 AST_APP_ARG(text);
00084 );
00085 iconv_t cd;
00086 size_t incount, outcount = len;
00087 char *parse;
00088
00089 if (ast_strlen_zero(arguments)) {
00090 ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) - missing arguments!\n");
00091 return -1;
00092 }
00093
00094 parse = ast_strdupa(arguments);
00095 AST_STANDARD_APP_ARGS(args, parse);
00096
00097 if (args.argc < 3) {
00098 ast_log(LOG_WARNING, "Syntax: ICONV(<in-charset>,<out-charset>,<text>) %u\n", args.argc);
00099 return -1;
00100 }
00101
00102 incount = strlen(args.text);
00103
00104 ast_debug(1, "Iconv: \"%s\" %s -> %s\n", args.text, args.in_charset, args.out_charset);
00105
00106 cd = iconv_open(args.out_charset, args.in_charset);
00107
00108 if (cd == (iconv_t) -1) {
00109 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);
00110 return -1;
00111 }
00112
00113 if (iconv(cd, (AST_ICONV_CAST) &args.text, &incount, &buf, &outcount) == (size_t) -1) {
00114 if (errno == E2BIG)
00115 ast_log(LOG_WARNING, "Iconv: output buffer too small.\n");
00116 else if (errno == EILSEQ)
00117 ast_log(LOG_WARNING, "Iconv: illegal character.\n");
00118 else if (errno == EINVAL)
00119 ast_log(LOG_WARNING, "Iconv: incomplete character sequence.\n");
00120 else
00121 ast_log(LOG_WARNING, "Iconv: error %d: %s.\n", errno, strerror(errno));
00122 }
00123 iconv_close(cd);
00124
00125 return 0;
00126 }
00127
00128
00129 static struct ast_custom_function iconv_function = {
00130 .name = "ICONV",
00131 .read = iconv_read
00132 };
00133
00134 static int unload_module(void)
00135 {
00136 return ast_custom_function_unregister(&iconv_function);
00137 }
00138
00139 static int load_module(void)
00140 {
00141 return ast_custom_function_register(&iconv_function);
00142 }
00143
00144 AST_MODULE_INFO_STANDARD(ASTERISK_GPL_KEY, "Charset conversions");
00145