Add FU::XMLWriter

And remove UTF-8 check in JSON writer. It honestly feels kind of silly
to do that validation there while I've never done similar validations in
any other output routines - including this XML writer.

FU::XMLWriter is a copy of TUWF::XMLXS with a bunch of improvements
applied: now uses refcounts to determine the current output instance,
auto-generates XS functions and has faster escaped string output -
inspired by the JSON writer.

TODO:
- Integrate into FU
- Do something with bool attribute values
- Benchmarks
- Should $content be optional for all tags? The reason they weren't in
  TUWF::XMLXS is because TUWF::XML supports opening tags without closing
  them, but that idea turned out to suck and isn't supported anymore.

This is hopefully the last XS module for the FU framework. The only C
code being written now should be bug fixes and extending FU::Pg with
some planned features. Already ended up with more C than I had
planned...
This commit is contained in:
Yorhel 2025-02-19 11:52:58 +01:00
parent 67e6d99f01
commit 9014e2900c
8 changed files with 568 additions and 14 deletions

50
FU.xs
View file

@ -26,6 +26,8 @@
#include "c/jsonparse.c"
#include "c/fdpass.c"
#include "c/fcgi.c"
#include "c/xmlwr.c"
#include "c/libpq.h"
#include "c/pgtypes.c"
#include "c/pgconn.c"
@ -57,6 +59,7 @@ PROTOTYPES: DISABLE
TYPEMAP: <<EOT
TYPEMAP
fufcgi * FUFCGI
fuxmlwr * FUXMLWR
fupg_conn * FUPG_CONN
fupg_txn * FUPG_TXN
fupg_st * FUPG_ST
@ -66,6 +69,10 @@ FUFCGI
if (sv_derived_from($arg, \"FU::fcgi\")) $var = (fufcgi *)SvIVX(SvRV($arg));
else fu_confess(\"invalid FastCGI object\");
FUXMLWR
if (sv_derived_from($arg, \"FU::XMLWriter\")) $var = (fuxmlwr *)SvIVX(SvRV($arg));
else fu_confess(\"invalid FU::XMLWriter object\");
FUPG_CONN
if (sv_derived_from($arg, \"FU::Pg::conn\")) $var = (fupg_conn *)SvIVX(SvRV($arg));
else fu_confess(\"invalid connection object\");
@ -137,6 +144,7 @@ void DESTROY(fufcgi *ctx)
safefree(ctx);
MODULE = FU PACKAGE = FU::Pg
void _load_libpq()
@ -153,6 +161,7 @@ void connect(const char *pkg, const char *conninfo)
ST(0) = fupg_connect(aTHX_ conninfo);
MODULE = FU PACKAGE = FU::Pg::conn
void server_version(fupg_conn *c)
@ -207,6 +216,7 @@ void q(fupg_conn *c, SV *sv, ...)
ST(0) = fupg_q(aTHX_ c, c->stflags, SvPVutf8_nolen(sv), ax, items);
MODULE = FU PACKAGE = FU::Pg::txn
void DESTROY(fupg_txn *t)
@ -251,6 +261,7 @@ void q(fupg_txn *t, SV *sv, ...)
ST(0) = fupg_q(aTHX_ t->conn, t->stflags, SvPVutf8_nolen(sv), ax, items);
MODULE = FU PACKAGE = FU::Pg::st
void cache(fupg_st *x, ...)
@ -330,3 +341,42 @@ void kvh(fupg_st *st)
void DESTROY(fupg_st *st)
CODE:
fupg_st_destroy(aTHX_ st);
MODULE = FU PACKAGE = FU::XMLWriter
void _new()
CODE:
ST(0) = fuxmlwr_new(aTHX);
void _done(fuxmlwr *wr)
CODE:
ST(0) = fustr_done(&wr->out);
fustr_init(aTHX_ &wr->out, NULL, SIZE_MAX);
void lit_(SV *sv)
CODE:
if (!fuxmlwr_tail) fu_confess("No active FU::XMLWriter instance");
STRLEN len;
const char *buf = SvPVutf8(sv, len);
fustr_write(aTHX_ &fuxmlwr_tail->out, buf, len);
void txt_(SV *sv)
CODE:
if (!fuxmlwr_tail) fu_confess("No active FU::XMLWriter instance");
fuxmlwr_escape(aTHX_ fuxmlwr_tail, sv);
void tag_(SV *sv, ...)
CODE:
if (!fuxmlwr_tail) fu_confess("No active FU::XMLWriter instance");
STRLEN len;
const char *tagname = SvPV(sv, len);
fuxmlwr_isname(tagname);
fuxmlwr_tag(aTHX_ fuxmlwr_tail, ax, 1, items, 0, tagname, len);
INCLUDE_COMMAND: $^X -e '$FU::XMLWriter::XSPRINT=1; require "./FU/XMLWriter.pm"'
void DESTROY(fuxmlwr *wr)
CODE:
fuxmlwr_destroy(aTHX_ wr);