/*
* $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 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 {
int flags; /* Flags currently just needs lines */
} PerlIOEncode;
#define NEEDS_LINES 1
SV *
{
if (e->enc) {
dSP;
/* Not 100% sure stack swap is right thing to do during dup ... */
}
}
return sv;
}
{
dSP;
/* should never happen */
return -1;
}
arg);
code = -1;
}
else {
/* $enc->renew */
arg);
}
else {
}
arg);
}
else {
e->flags |= NEEDS_LINES;
}
}
}
return code;
}
{
if (e->enc) {
SvREFCNT_dec(e->enc);
}
if (e->bufsv) {
SvREFCNT_dec(e->bufsv);
}
if (e->dataSV) {
SvREFCNT_dec(e->dataSV);
}
if (e->chk) {
SvREFCNT_dec(e->chk);
}
return 0;
}
STDCHAR *
{
if (!e->bufsv) {
}
abort();
}
}
abort();
}
}
{
dSP;
PerlIO *n;
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];
if (!n) {
}
}
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;
}
}
char *s;
(void) PerlIOEncode_get_base(aTHX_ f);
if (!e->dataSV)
}
if (e->flags & NEEDS_LINES) {
/* Encoding needs whole lines (e.g. iso-2022-*)
search back from end of available data for
and line marker
*/
if (*nl == '\n') {
break;
}
nl--;
}
/* found a line - take up to and including that */
}
else if (avail > 0) {
/* No line, but not EOF - append avail to the pending data */
goto retry;
}
goto end_of_file;
}
}
/* something left over from last time - create a normal
SV with new data appended
*/
if (e->flags & NEEDS_LINES) {
/* Have to grow buffer */
}
else {
}
}
}
else {
/* Create a "dummy" SV to represent the available data from layer below */
}
if (e->flags & NEEDS_LINES) {
/* Have to grow buffer */
}
else {
}
}
SvPOK_only(e->dataSV);
}
SvUTF8_off(e->dataSV);
}
/* Now get translated string (forced to UTF-8) and use as buffer */
#ifdef PARANOID_ENCODE_CHECKS
}
#endif
}
if (len > 0) {
/* Got _something */
/* if decode gave us back dataSV then data may vanish when
we do ptrcnt adjust - so take our copy now.
(The copy is a pain - need a put-it-here option for decode.)
*/
did not translate - not clear this is a win */
/* compute amount we took */
/* and as we did not take it it isn't pending */
} else {
/* Got nothing - assume partial character so we need some more */
/* Make sure e->dataSV is a normal SV before re-filling as
buffer alias will change under us
*/
goto retry;
}
}
else {
code = -1;
if (avail == 0)
else
}
return code;
}
{
if (e->bufsv) {
dSP;
char *s;
/* Write case - encode the buffer and write() to layer below */
}
code = -1;
}
if (PerlIO_flush(PerlIONext(f)) != 0) {
code = -1;
}
/* Did not all translate */
return code;
}
}
/* read case */
/* if we have any untranslated stuff then unread that first */
/* FIXME - unread is fragile is there a better way ? */
code = -1;
}
}
/* See if there is anything left in the buffer */
/* Bother - have unread data.
re-encode and unread() to layer below
*/
str = sv_newmortal();
}
code = -1;
}
}
}
}
return code;
}
{
/* Discard partial character */
if (e->dataSV) {
}
/* Don't back decode and unread any pending data */
}
if (e->bufsv) {
/* This should only fire for write case */
}
SvREFCNT_dec(e->bufsv);
}
return code;
}
{
/* Unfortunately the only way to get a postion is to (re-)translate,
the UTF8 we have in bufefr and then ask layer below
*/
PerlIO_flush(f);
}
return PerlIO_tell(PerlIONext(f));
}
PerlIO *
{
}
}
return f;
}
{
if (e->flags & NEEDS_LINES) {
if (done > 0) {
}
break;
}
if (PerlIOEncode_flush(aTHX_ f) != 0) {
break;
}
}
}
}
else {
}
}
sizeof(PerlIO_funcs),
"encoding",
sizeof(PerlIOEncode),
NULL, /* binmode - always pop */
};
#endif /* encode layer */
BOOT:
{
/*
* we now "use Encode ()" here instead of
* PerlIO/encoding.pm. This avoids SEGV when ":encoding()"
* is invoked without prior "use Encode". -- dankogai
*/
if (!get_cv(OUR_DEFAULT_FB, 0)) {
#if 0
/* This would just be an irritant now loading works */
#endif
/* Encode needs a lot of stack - it is likely to move ... */
/* The SV is magically freed by load_module */
}
/* should never happen */
}
#ifdef PERLIO_LAYERS
#endif
}