00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #include <stdlib.h>
00013 #include <iconv.h>
00014 #include <errno.h>
00015
00016 #include "eus.h"
00017
00018 static pointer cconv(context *ctx, int n, pointer *argv)
00019 {
00020 iconv_t cd;
00021 size_t inbytesleft, outbytesleft;
00022 char *outbufp1, *outbufp2, *instrp;
00023 size_t ret;
00024 pointer instr=argv[2], outstr;
00025 size_t len=vecsize(instr);
00026
00027 inbytesleft=len;
00028 outbytesleft=len*2;
00029 outbufp1= outbufp2= malloc(outbytesleft);
00030 instrp= &(instr->c.str.chars[0]);
00031
00032
00033
00034
00035 cd=iconv_open(argv[1]->c.str.chars, argv[0]->c.str.chars);
00036 if (cd == (iconv_t)-1) { outstr= (pointer)makeint(errno); goto conv_end;}
00037
00038 ret=iconv(cd, &instrp, &inbytesleft, &outbufp2, &outbytesleft);
00039 if (ret == -1) { outstr=makeint(-errno); goto conv_end;}
00040 else outstr=makestring(outbufp1, len*2-outbytesleft);
00041
00042 conv_end:
00043 iconv_close(cd);
00044 cfree(outbufp1);
00045 return(outstr);
00046 }
00047
00048 pointer ICONVOPEN(context *ctx, int n, pointer *argv)
00049 { int cd;
00050 ckarg(2);
00051 if (!isstring(argv[0])) error (E_NOSTRING, argv[0]);
00052 if (!isstring(argv[1])) error (E_NOSTRING, argv[1]);
00053 cd=iconv_open(argv[0]->c.str.chars, argv[1]->c.str.chars);
00054 return(mkbigint(cd));}
00055
00056 pointer ICONVCLOSE(context *ctx, int n, pointer *argv)
00057 { int cd, ret;
00058 ckarg(1);
00059 cd=bigintval(argv[0]);
00060 ret=iconv_close(cd);
00061 return(makeint(ret));
00062 }
00063
00064 pointer ICONV(context *ctx, int n, pointer *argv)
00065 { int cd, ret, malloced=NULL;
00066 char *srcstrp, *deststrp, *deststrpv, deststr[1024];
00067 size_t srcstrlen, deststrlen;
00068 pointer dest;
00069
00070 ckarg(2);
00071 cd=bigintval(argv[0]);
00072 if (!isstring(argv[1])) error(E_NOSTRING);
00073 srcstrp=argv[1]->c.str.chars;
00074 srcstrlen=strlength(argv[1]);
00075 deststrlen=2*srcstrlen;
00076 if (deststrlen>=1024) {
00077 deststrp=malloc(deststrlen);
00078 malloced=1;}
00079 else deststrp=deststr;
00080 deststrpv=deststrp;
00081 ret=iconv(cd, &srcstrp, &srcstrlen, &deststrpv, &deststrlen);
00082 if (ret== -1) { dest=NIL; goto iconvend;}
00083 dest=makestring(deststrp, 2*strlength(argv[1])-deststrlen);
00084 iconvend:
00085 if (malloced) cfree(deststrp);
00086 return(dest);
00087 }
00088
00089 charconv(context *ctx, int n, pointer argv[])
00090 { pointer mod=argv[0];
00091 defun(ctx,"CHARCONV",mod,cconv,NULL);
00092 defun(ctx,"ICONV-OPEN", mod, ICONVOPEN,NULL);
00093 defun(ctx,"ICONV-CLOSE", mod, ICONVCLOSE,NULL);
00094 defun(ctx, "ICONV", mod, ICONV,NULL);
00095 }
00096
00097