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

📄 encoding.xs

📁 source of perl for linux application,
💻 XS
📖 第 1 页 / 共 2 页
字号:
/* * $Id: encoding.xs,v 0.3 2002/04/21 22:14:41 dankogai Exp $ */#define PERL_NO_GET_CONTEXT#include "EXTERN.h"#include "perl.h"#include "XSUB.h"#define U8 U8#define OUR_DEFAULT_FB	"Encode::PERLQQ"#if defined(USE_PERLIO) && !defined(USE_SFIO)/* Define an encoding "layer" in the perliol.h sense.   The layer defined here "inherits" in an object-oriented sense from   the "perlio" layer with its PerlIOBuf_* "methods".  The   implementation is particularly efficient as until Encode settles   down there is no point in tryint to tune it.   The layer works by overloading the "fill" and "flush" methods.   "fill" calls "SUPER::fill" in perl terms, then calls the encode OO   perl API to convert the encoded data to UTF-8 form, then copies it   back to the buffer. The "base class's" read methods then see the   UTF-8 data.   "flush" transforms the UTF-8 data deposited by the "base class's   write method in the buffer back into the encoded form using the   encode OO perl API, then copies data back into the buffer and calls   "SUPER::flush.   Note that "flush" is _also_ called for read mode - we still do the   (back)-translate so that the base class's "flush" sees the   correct number of encoded chars for positioning the seek   pointer. (This double translation is the worst performance issue -   particularly with all-perl encode engine.)*/#include "perliol.h"typedef struct {    PerlIOBuf base;		/* PerlIOBuf stuff */    SV *bufsv;			/* buffer seen by layers above */    SV *dataSV;			/* data we have read from layer below */    SV *enc;			/* the encoding object */    SV *chk;                    /* CHECK in Encode methods */    int flags;			/* Flags currently just needs lines */    int inEncodeCall;		/* trap recursive encode calls */} PerlIOEncode;#define NEEDS_LINES	1SV *PerlIOEncode_getarg(pTHX_ PerlIO * f, CLONE_PARAMS * param, int flags){    PerlIOEncode *e = PerlIOSelf(f, PerlIOEncode);    SV *sv = &PL_sv_undef;    if (e->enc) {	dSP;	/* Not 100% sure stack swap is right thing to do during dup ... */	PUSHSTACKi(PERLSI_MAGIC);	SPAGAIN;	ENTER;	SAVETMPS;	PUSHMARK(sp);	XPUSHs(e->enc);	PUTBACK;	if (call_method("name", G_SCALAR) == 1) {	    SPAGAIN;	    sv = newSVsv(POPs);	    PUTBACK;	}	FREETMPS;	LEAVE;	POPSTACK;    }    return sv;}IVPerlIOEncode_pushed(pTHX_ PerlIO * f, const char *mode, SV * arg, PerlIO_funcs *tab){    PerlIOEncode *e = PerlIOSelf(f, PerlIOEncode);    dSP;    IV  code = PerlIOBuf_pushed(aTHX_ f, mode, Nullsv,tab);    SV *result = Nullsv;    PUSHSTACKi(PERLSI_MAGIC);    SPAGAIN;    ENTER;    SAVETMPS;    PUSHMARK(sp);    XPUSHs(arg);    PUTBACK;    if (call_pv("Encode::find_encoding", G_SCALAR) != 1) {	/* should never happen */	Perl_die(aTHX_ "Encode::find_encoding did not return a value");	return -1;    }    SPAGAIN;    result = POPs;    PUTBACK;    if (!SvROK(result) || !SvOBJECT(SvRV(result))) {	e->enc = Nullsv;	Perl_warner(aTHX_ packWARN(WARN_IO), "Cannot find encoding \"%" SVf "\"",		    arg);	errno = EINVAL;	code = -1;    }    else {       /* $enc->renew */	PUSHMARK(sp);	XPUSHs(result);	PUTBACK;	if (call_method("renew",G_SCALAR|G_EVAL) != 1 || SvTRUE(ERRSV)) {	    Perl_warner(aTHX_ packWARN(WARN_IO), "\"%" SVf "\" does not support renew method",			arg);	}	else {	    SPAGAIN;	    result = POPs;	    PUTBACK;	}	e->enc = newSVsv(result);	PUSHMARK(sp);	XPUSHs(e->enc);	PUTBACK;	if (call_method("needs_lines",G_SCALAR|G_EVAL) != 1 || SvTRUE(ERRSV)) {	    Perl_warner(aTHX_ packWARN(WARN_IO), "\"%" SVf "\" does not support needs_lines",			arg);	}	else {	    SPAGAIN;	    result = POPs;	    PUTBACK;	    if (SvTRUE(result)) {		e->flags |= NEEDS_LINES;	    }	}	PerlIOBase(f)->flags |= PERLIO_F_UTF8;    }    e->chk = newSVsv(get_sv("PerlIO::encoding::fallback", 0));    e->inEncodeCall = 0;    FREETMPS;    LEAVE;    POPSTACK;    return code;}IVPerlIOEncode_popped(pTHX_ PerlIO * f){    PerlIOEncode *e = PerlIOSelf(f, PerlIOEncode);    if (e->enc) {	SvREFCNT_dec(e->enc);	e->enc = Nullsv;    }    if (e->bufsv) {	SvREFCNT_dec(e->bufsv);	e->bufsv = Nullsv;    }    if (e->dataSV) {	SvREFCNT_dec(e->dataSV);	e->dataSV = Nullsv;    }    if (e->chk) {	SvREFCNT_dec(e->chk);	e->chk = Nullsv;    }    return 0;}STDCHAR *PerlIOEncode_get_base(pTHX_ PerlIO * f){    PerlIOEncode *e = PerlIOSelf(f, PerlIOEncode);    if (!e->base.bufsiz)	e->base.bufsiz = 1024;    if (!e->bufsv) {	e->bufsv = newSV(e->base.bufsiz);	sv_setpvn(e->bufsv, "", 0);    }    e->base.buf = (STDCHAR *) SvPVX(e->bufsv);    if (!e->base.ptr)	e->base.ptr = e->base.buf;    if (!e->base.end)	e->base.end = e->base.buf;    if (e->base.ptr < e->base.buf	|| e->base.ptr > e->base.buf + SvLEN(e->bufsv)) {	Perl_warn(aTHX_ " ptr %p(%p)%p", e->base.buf, e->base.ptr,		  e->base.buf + SvLEN(e->bufsv));	abort();    }    if (SvLEN(e->bufsv) < e->base.bufsiz) {	SSize_t poff = e->base.ptr - e->base.buf;	SSize_t eoff = e->base.end - e->base.buf;	e->base.buf = (STDCHAR *) SvGROW(e->bufsv, e->base.bufsiz);	e->base.ptr = e->base.buf + poff;	e->base.end = e->base.buf + eoff;    }    if (e->base.ptr < e->base.buf	|| e->base.ptr > e->base.buf + SvLEN(e->bufsv)) {	Perl_warn(aTHX_ " ptr %p(%p)%p", e->base.buf, e->base.ptr,		  e->base.buf + SvLEN(e->bufsv));	abort();    }    return e->base.buf;}IVPerlIOEncode_fill(pTHX_ PerlIO * f){    PerlIOEncode *e = PerlIOSelf(f, PerlIOEncode);    dSP;    IV code = 0;    PerlIO *n;    SSize_t avail;    if (PerlIO_flush(f) != 0)	return -1;    n  = PerlIONext(f);    if (!PerlIO_fast_gets(n)) {	/* Things get too messy if we don't have a buffer layer	   push a :perlio to do the job */	char mode[8];	n  = PerlIO_push(aTHX_ n, &PerlIO_perlio, PerlIO_modestr(f,mode), Nullsv);	if (!n) {	    Perl_die(aTHX_ "panic: cannot push :perlio for %p",f);	}    }    PUSHSTACKi(PERLSI_MAGIC);    SPAGAIN;    ENTER;    SAVETMPS;  retry:    avail = PerlIO_get_cnt(n);    if (avail <= 0) {	avail = PerlIO_fill(n);	if (avail == 0) {	    avail = PerlIO_get_cnt(n);	}	else {	    if (!PerlIO_error(n) && PerlIO_eof(n))		avail = 0;	}    }    if (avail > 0 || (e->flags & NEEDS_LINES)) {	STDCHAR *ptr = PerlIO_get_ptr(n);	SSize_t use  = (avail >= 0) ? avail : 0;	SV *uni;	char *s = Nullch;	STRLEN len = 0;	e->base.ptr = e->base.end = (STDCHAR *) Nullch;	(void) PerlIOEncode_get_base(aTHX_ f);	if (!e->dataSV)	    e->dataSV = newSV(0);	if (SvTYPE(e->dataSV) < SVt_PV) {	    sv_upgrade(e->dataSV,SVt_PV);	}	if (e->flags & NEEDS_LINES) {	    /* Encoding needs whole lines (e.g. iso-2022-*)	       search back from end of available data for	       and line marker	     */	    STDCHAR *nl = ptr+use-1;	    while (nl >= ptr) {		if (*nl == '\n') {		    break;		}		nl--;	    }	    if (nl >= ptr && *nl == '\n') {		/* found a line - take up to and including that */		use = (nl+1)-ptr;	    }	    else if (avail > 0) {		/* No line, but not EOF - append avail to the pending data */		sv_catpvn(e->dataSV, (char*)ptr, use);		PerlIO_set_ptrcnt(n, ptr+use, 0);		goto retry;	    }	    else if (!SvCUR(e->dataSV)) {		goto end_of_file;	    }	}	if (SvCUR(e->dataSV)) {	    /* something left over from last time - create a normal	       SV with new data appended	     */	    if (use + SvCUR(e->dataSV) > e->base.bufsiz) {		if (e->flags & NEEDS_LINES) {		    /* Have to grow buffer */		    e->base.bufsiz = use + SvCUR(e->dataSV);		    PerlIOEncode_get_base(aTHX_ f);		}		else {	       use = e->base.bufsiz - SvCUR(e->dataSV);	    }	    }	    sv_catpvn(e->dataSV,(char*)ptr,use);	}	else {	    /* Create a "dummy" SV to represent the available data from layer below */	    if (SvLEN(e->dataSV) && SvPVX_const(e->dataSV)) {		Safefree(SvPVX_mutable(e->dataSV));	    }	    if (use > (SSize_t)e->base.bufsiz) {		if (e->flags & NEEDS_LINES) {		    /* Have to grow buffer */		    e->base.bufsiz = use;		    PerlIOEncode_get_base(aTHX_ f);		}		else {	       use = e->base.bufsiz;	    }	    }	    SvPV_set(e->dataSV, (char *) ptr);	    SvLEN_set(e->dataSV, 0);  /* Hands off sv.c - it isn't yours */	    SvCUR_set(e->dataSV,use);	    SvPOK_only(e->dataSV);	}	SvUTF8_off(e->dataSV);	PUSHMARK(sp);	XPUSHs(e->enc);	XPUSHs(e->dataSV);	XPUSHs(e->chk);

⌨️ 快捷键说明

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