charconv.c
Go to the documentation of this file.
1 /* charconv.c
2 /* (c) 2003, Toshihiro Matsui, Digital Human Research Lab, AIST
3 Character code conversion using iconv library.
4 
5 % cc -c -fpic -Di486 -DLinux charconv.c -I/usr/local/eus/include
6 % ld -o charconv.so -shared charconv.o
7 eus$ (load "charconv.so")
8 eus$ (iconv "euc-jp" "Shift-jis" "松井")
9 
10 */
11 
12 #include <stdlib.h>
13 #include <iconv.h>
14 #include <errno.h>
15 
16 #include "eus.h"
17 
18 static pointer cconv(context *ctx, int n, pointer *argv)
19 {
20  iconv_t cd;
21  size_t inbytesleft, outbytesleft;
22  char *outbufp1, *outbufp2, *instrp;
23  size_t ret;
24  pointer instr=argv[2], outstr;
25  size_t len=vecsize(instr);
26 
27  inbytesleft=len;
28  outbytesleft=len*2;
29  outbufp1= outbufp2= malloc(outbytesleft);
30  instrp= &(instr->c.str.chars[0]);
31 
32  /* get acceptable codings by 'iconv --list' */
33  /* cd=iconv_open(to_code, from_code); */
34  /* cd=iconv_open("Shift_JIS", "EUC-JP"); */
35  cd=iconv_open(argv[1]->c.str.chars, argv[0]->c.str.chars);
36  if (cd == (iconv_t)-1) { outstr= (pointer)makeint(errno); goto conv_end;}
37 
38  ret=iconv(cd, &instrp, &inbytesleft, &outbufp2, &outbytesleft);
39  if (ret == -1) { outstr=makeint(-errno); goto conv_end;}
40  else outstr=makestring(outbufp1, len*2-outbytesleft);
41 
42  conv_end:
43  iconv_close(cd);
44  cfree(outbufp1);
45  return(outstr);
46  }
47 
48 pointer ICONVOPEN(context *ctx, int n, pointer *argv)
49 { int cd;
50  ckarg(2);
51  if (!isstring(argv[0])) error (E_NOSTRING, argv[0]);
52  if (!isstring(argv[1])) error (E_NOSTRING, argv[1]);
53  cd=iconv_open(argv[0]->c.str.chars, argv[1]->c.str.chars);
54  return(mkbigint(cd));}
55 
56 pointer ICONVCLOSE(context *ctx, int n, pointer *argv)
57 { int cd, ret;
58  ckarg(1);
59  cd=bigintval(argv[0]);
60  ret=iconv_close(cd);
61  return(makeint(ret));
62  }
63 
64 pointer ICONV(context *ctx, int n, pointer *argv)
65 { int cd, ret, malloced=NULL;
66  char *srcstrp, *deststrp, *deststrpv, deststr[1024];
67  size_t srcstrlen, deststrlen;
68  pointer dest;
69 
70  ckarg(2);
71  cd=bigintval(argv[0]);
72  if (!isstring(argv[1])) error(E_NOSTRING);
73  srcstrp=argv[1]->c.str.chars;
74  srcstrlen=strlength(argv[1]);
75  deststrlen=2*srcstrlen;
76  if (deststrlen>=1024) {
77  deststrp=malloc(deststrlen);
78  malloced=1;}
79  else deststrp=deststr;
80  deststrpv=deststrp;
81  ret=iconv(cd, &srcstrp, &srcstrlen, &deststrpv, &deststrlen);
82  if (ret== -1) { dest=NIL; goto iconvend;}
83  dest=makestring(deststrp, 2*strlength(argv[1])-deststrlen);
84  iconvend:
85  if (malloced) cfree(deststrp);
86  return(dest);
87  }
88 
89 charconv(context *ctx, int n, pointer argv[])
90 { pointer mod=argv[0];
91  defun(ctx,"CHARCONV",mod,cconv,NULL);
92  defun(ctx,"ICONV-OPEN", mod, ICONVOPEN,NULL);
93  defun(ctx,"ICONV-CLOSE", mod, ICONVCLOSE,NULL);
94  defun(ctx, "ICONV", mod, ICONV,NULL);
95  }
96 
97 
pointer ICONV(context *ctx, int n, pointer *argv)
Definition: charconv.c:64
#define makeint(v)
Definition: sfttest.c:2
struct cell * pointer
Definition: eus.h:165
Definition: eus.h:524
struct string str
Definition: eus.h:402
byte chars[1]
Definition: eus.h:212
pointer ICONVOPEN(context *ctx, int n, pointer *argv)
Definition: charconv.c:48
GLfloat n[6][3]
Definition: cube.c:15
defun("ADR_TO_STRING", mod, ADR_TO_STRING)
charconv(context *ctx, int n, pointer argv[])
Definition: charconv.c:89
pointer ICONVCLOSE(context *ctx, int n, pointer *argv)
Definition: charconv.c:56
ckarg(2)
static pointer cconv(context *ctx, int n, pointer *argv)
Definition: charconv.c:18
union cell::cellunion c
Definition: eus.h:381
pointer error(enum errorcode ec,...) pointer error(va_alist) va_dcl
Definition: eus.c:297
pointer makestring(char *, int)
Definition: makes.c:147
#define NULL
Definition: transargv.c:8
int errno
pointer NIL
Definition: eus.c:110


euslisp
Author(s): Toshihiro Matsui
autogenerated on Mon Feb 28 2022 22:18:27