⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bytetochariconv.c

📁 kaffe Java 解释器语言,源码,Java的子集系统,开放源代码
💻 C
字号:
/* * kaffe.io.ByteToCharDefault * * This class goes native because its main task is to copy bytes to chars * (i.e. System.arraycopy - incompatible arrays). It should avoid buffer-to-buffer * copying * * * Copyright (c) 1998 *	Transvirtual Technologies, Inc.  All rights reserved. * * See the file "license.terms" for information on usage and redistribution  * of this file.  */#include "config.h"#include "config-mem.h"#include "kaffe_io_ByteToCharIconv.h"#if defined(HAVE_ICONV_H)#include <iconv.h>#endif#include <errno.h>#if defined(HAVE_UNISTD_H)#include <unistd.h>#endifstatic jfieldID cd_id;static jmethodID carry_id;voidJava_kaffe_io_ByteToCharIconv_initialize0(JNIEnv* env, jclass cls){	cd_id = (*env)->GetFieldID(env, cls, "cd", "kaffe.util.Ptr");	carry_id = (*env)->GetMethodID(env, cls, "carry", "([BII)V");}jbooleanJava_kaffe_io_ByteToCharIconv_open0 (JNIEnv* env, jobject _this, jstring enc){#if defined(HAVE_ICONV)    const jbyte* str;    iconv_t cd;    str = (*env)->GetStringUTFChars(env, enc, 0);#ifdef WORDS_BIGENDIAN    cd = iconv_open ("UCS-2BE", (char *)str);#else    cd = iconv_open ("UCS-2LE", (char *)str);#endif    (*env)->ReleaseStringUTFChars(env, enc, str);    if (cd != (iconv_t)-1) {	(*env)->SetObjectField(env, _this, cd_id, (jobject)cd);	return JNI_TRUE;    }#endif    return JNI_FALSE;}voidJava_kaffe_io_ByteToCharIconv_close0 (JNIEnv* env, jobject r, jobject cd){#if defined(HAVE_ICONV)    iconv_close ((iconv_t)cd);#endif}jintJava_kaffe_io_ByteToCharIconv_convert (JNIEnv* env, jobject _this, 				       jbyteArray fromBytes, jint fromPos, jint fromLen,				       jcharArray toChars, jint toPos, jint toLen){#if defined(HAVE_ICONV)    jboolean	isCopy;    jbyte	*jb = (*env)->GetByteArrayElements(env, fromBytes, &isCopy);    const char	*icv_in = (char*) (jb + fromPos);    size_t	icv_inlen = fromLen;    jchar	*jc  = (*env)->GetCharArrayElements(env, toChars, &isCopy);    char	*icv_out = (char *) (jc + toPos);    size_t	icv_outlen = toLen * 2;    iconv_t	cd = (iconv_t) (*env)->GetObjectField(env, _this, cd_id);    int		ret;    ret = iconv (cd, &icv_in, &icv_inlen, &icv_out, &icv_outlen);    if (ret < 0) {	/* convert the begining of an invalid  multibyte  sequence to '?' */	if (errno == EILSEQ) {		icv_in++;		icv_inlen--;		*(icv_out++) = 0;		*(icv_out++) = '?';		icv_outlen -= 2;	}    }    if (icv_inlen > 0) {	/* In case we have some bytes left, save them */	(*env)->CallVoidMethod(env, _this, carry_id,			       fromBytes, fromPos + fromLen - icv_inlen, icv_inlen);    }    (*env)->ReleaseByteArrayElements(env, fromBytes, jb, JNI_ABORT);    (*env)->ReleaseCharArrayElements(env, toChars, jc, 0);    return toLen - (icv_outlen / 2);#else    jclass sd = (*env)->FindClass(env, "kaffe.util.SupportDisabled");    (*env)->ThrowNew(env, sd, "iconv() was not found by Kaffe configure script");    return 0;#endif}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -