2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : iconv.erl
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-02-16 21:07:21 +01:00
|
|
|
%%% Purpose : Interface to libiconv
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% Created : 16 Feb 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 ProcessOne
|
2007-12-24 14:57:53 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2007-12-24 14:57:53 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License
|
|
|
|
%%% along with this program; if not, write to the Free Software
|
|
|
|
%%% Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-02-16 21:07:21 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(iconv).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2007-12-24 14:57:53 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
-behaviour(gen_server).
|
|
|
|
|
|
|
|
-export([start/0, start_link/0, convert/3]).
|
|
|
|
|
|
|
|
%% Internal exports, call-back functions.
|
2012-09-11 15:45:59 +02:00
|
|
|
-export([init/1, handle_call/3, handle_cast/2,
|
|
|
|
handle_info/2, code_change/3, terminate/2]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
start() ->
|
|
|
|
gen_server:start({local, ?MODULE}, ?MODULE, [], []).
|
|
|
|
|
|
|
|
start_link() ->
|
2012-09-11 15:45:59 +02:00
|
|
|
gen_server:start_link({local, ?MODULE}, ?MODULE, [],
|
|
|
|
[]).
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
init([]) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
case erl_ddll:load_driver(ejabberd:get_so_path(),
|
|
|
|
iconv_erl)
|
|
|
|
of
|
|
|
|
ok -> ok;
|
|
|
|
{error, already_loaded} -> ok
|
2005-10-25 04:19:01 +02:00
|
|
|
end,
|
2011-09-05 07:28:01 +02:00
|
|
|
Port = open_port({spawn, "iconv_erl"}, []),
|
2003-02-16 21:07:21 +01:00
|
|
|
ets:new(iconv_table, [set, public, named_table]),
|
|
|
|
ets:insert(iconv_table, {port, Port}),
|
|
|
|
{ok, Port}.
|
|
|
|
|
|
|
|
%%% --------------------------------------------------------
|
|
|
|
%%% The call-back functions.
|
|
|
|
%%% --------------------------------------------------------
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
handle_call(_, _, State) -> {noreply, State}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
handle_cast(_, State) -> {noreply, State}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2007-12-15 14:01:32 +01:00
|
|
|
handle_info({'EXIT', Port, Reason}, Port) ->
|
|
|
|
{stop, {port_died, Reason}, Port};
|
2007-12-07 01:48:11 +01:00
|
|
|
handle_info({'EXIT', _Pid, _Reason}, Port) ->
|
2003-02-16 21:07:21 +01:00
|
|
|
{noreply, Port};
|
2012-09-11 15:45:59 +02:00
|
|
|
handle_info(_, State) -> {noreply, State}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
code_change(_OldVsn, State, _Extra) -> {ok, State}.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
terminate(_Reason, Port) -> Port ! {self, close}, ok.
|
2003-02-16 21:07:21 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-spec convert(binary(), binary(), binary()) -> binary().
|
2003-02-16 21:07:21 +01:00
|
|
|
|
|
|
|
convert(From, To, String) ->
|
|
|
|
[{port, Port} | _] = ets:lookup(iconv_table, port),
|
|
|
|
Bin = term_to_binary({From, To, String}),
|
|
|
|
BRes = port_control(Port, 1, Bin),
|
2012-09-11 15:45:59 +02:00
|
|
|
(BRes).
|