charconv.c
Go to the documentation of this file.
00001 /* charconv.c 
00002 /* (c) 2003, Toshihiro Matsui, Digital Human Research Lab, AIST
00003 Character code conversion using iconv library.
00004 
00005 % cc -c -fpic -Di486 -DLinux charconv.c -I/usr/local/eus/include
00006 % ld  -o charconv.so -shared charconv.o
00007 eus$ (load "charconv.so")
00008 eus$ (iconv "euc-jp" "Shift-jis" "松井")
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   /* get acceptable codings by 'iconv --list' */
00033   /* cd=iconv_open(to_code, from_code); */
00034   /* cd=iconv_open("Shift_JIS", "EUC-JP"); */
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); 
00092   defun(ctx,"ICONV-OPEN", mod, ICONVOPEN);
00093   defun(ctx,"ICONV-CLOSE", mod, ICONVCLOSE);
00094   defun(ctx, "ICONV", mod, ICONV);
00095   }
00096 
00097 


euslisp
Author(s): Toshihiro Matsui
autogenerated on Thu Mar 9 2017 04:57:49