/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.45 from the
 * contents of Formats.xs. Do not edit this file, edit Formats.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "Formats.xs"
#define PERL_NO_GET_CONTEXT
#include "module.h"

static int magic_free_text_dest(pTHX_ SV *sv, MAGIC *mg)
{
	TEXT_DEST_REC *dest = (TEXT_DEST_REC *) mg->mg_ptr;
	char *target = (char *) dest->target;
	g_free(target);
	g_free(dest);
	mg->mg_ptr = NULL;
	sv_setiv(sv, 0);
	return 0;
}

static MGVTBL vtbl_free_text_dest =
{
    NULL, NULL, NULL, NULL, magic_free_text_dest
};

static SV *perl_format_create_dest(SERVER_REC *server, char *target,
				   int level, WINDOW_REC *window)
{
	TEXT_DEST_REC *dest;
	SV *sv, *ret_sv;

	dest = g_new0(TEXT_DEST_REC, 1);
	format_create_dest(dest, server, g_strdup(target), level, window);

	ret_sv = plain_bless(dest, "Irssi::UI::TextDest");

	sv = *hv_fetch(hvref(ret_sv), "_irssi", 6, 0);
	sv_magic(sv, NULL, '~', NULL, 0);

	SvMAGIC(sv)->mg_private = 0x1551; /* HF */
	SvMAGIC(sv)->mg_virtual = &vtbl_free_text_dest;
	SvMAGIC(sv)->mg_ptr = (char *) dest;

	return ret_sv;
}

#line 51 "Formats.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 195 "Formats.c"

XS_EUPXS(XS_Irssi_format_get_length); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_format_get_length)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "str");
    {
	char *	str = (char *)SvPV_nolen(ST(0))
;
	int	RETVAL;
	dXSTARG;

	RETVAL = format_get_length(str);
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Irssi_format_real_length); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_format_real_length)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "str, len");
    {
	char *	str = (char *)SvPV_nolen(ST(0))
;
	int	len = (int)SvIV(ST(1))
;
	int	RETVAL;
	dXSTARG;

	RETVAL = format_real_length(str, len);
	XSprePUSH;
	PUSHi((IV)RETVAL);
    }
    XSRETURN(1);
}


XS_EUPXS(XS_Irssi_format_string_expand); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_format_string_expand)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "str");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	char *	str = (char *)SvPV_nolen(ST(0))
;
#line 57 "Formats.xs"
	char *ret;
#line 252 "Formats.c"
#line 59 "Formats.xs"
	ret = format_string_expand(str, NULL);
	XPUSHs(sv_2mortal(new_pv(ret)));
	g_free(ret);
#line 257 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi_strip_codes); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_strip_codes)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "input");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	char *	input = (char *)SvPV_nolen(ST(0))
;
#line 67 "Formats.xs"
	char *ret;
#line 277 "Formats.c"
#line 69 "Formats.xs"
	ret = strip_codes(input);
	XPUSHs(sv_2mortal(new_pv(ret)));
	g_free(ret);
#line 282 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi_format_string_unexpand); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_format_string_unexpand)
{
    dVAR; dXSARGS;
    if (items != 1)
       croak_xs_usage(cv,  "input");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	char *	input = (char *)SvPV_nolen(ST(0))
;
#line 77 "Formats.xs"
	char *ret;
#line 302 "Formats.c"
#line 79 "Formats.xs"
	ret = format_string_unexpand(input, 0);
	XPUSHs(sv_2mortal(new_pv(ret)));
	g_free(ret);
#line 307 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi_format_create_dest); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi_format_create_dest)
{
    dVAR; dXSARGS;
    if (items < 1 || items > 3)
       croak_xs_usage(cv,  "target, level=MSGLEVEL_CLIENTNOTICE, window=NULL");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	char *	target = (char *)SvPV_nolen(ST(0))
;
	int	level;
	Irssi__UI__Window	window;

	if (items < 2)
	    level = MSGLEVEL_CLIENTNOTICE;
	else {
	    level = (int)SvIV(ST(1))
;
	}

	if (items < 3)
	    window = NULL;
	else {
	    window = irssi_ref_object(ST(2))
;
	}
#line 89 "Formats.xs"
	XPUSHs(sv_2mortal(perl_format_create_dest(NULL, target, level, window)));
#line 343 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi__UI__Window_format_get_text); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__Window_format_get_text)
{
    dVAR; dXSARGS;
    if (items < 5)
       croak_xs_usage(cv,  "window, module, server, target, format, ...");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Irssi__UI__Window	window = irssi_ref_object(ST(0))
;
	char *	module = (char *)SvPV_nolen(ST(1))
;
	Irssi__Server	server = irssi_ref_object(ST(2))
;
	char *	target = (char *)SvPV_nolen(ST(3))
;
	char *	format = (char *)SvPV_nolen(ST(4))
;
#line 103 "Formats.xs"
	TEXT_DEST_REC dest;
	THEME_REC *theme;
	char **charargs;
	char *ret;
	int formatnum;
	int n;
#line 376 "Formats.c"
#line 110 "Formats.xs"
	charargs = g_new0(char *, items-5+1);
        for (n = 5; n < items; n++) {
		charargs[n-5] = SvPV_nolen(ST(n));
	}

	format_create_dest(&dest, server, target, 0, window);
	theme = window_get_theme(dest.window);
	formatnum = format_find_tag(module, format);

	ret = format_get_text_theme_charargs(theme, module, &dest, formatnum, charargs);
	g_free(charargs);

	XPUSHs(sv_2mortal(new_pv(ret)));
	g_free_not_null(ret);
#line 392 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi__UI__Window_format_create_dest); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__Window_format_create_dest)
{
    dVAR; dXSARGS;
    if (items < 0 || items > 2)
       croak_xs_usage(cv,  "window=NULL, level=MSGLEVEL_CLIENTNOTICE");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Irssi__UI__Window	window;
	int	level;

	if (items < 1)
	    window = NULL;
	else {
	    window = irssi_ref_object(ST(0))
;
	}

	if (items < 2)
	    level = MSGLEVEL_CLIENTNOTICE;
	else {
	    level = (int)SvIV(ST(1))
;
	}
#line 134 "Formats.xs"
	XPUSHs(sv_2mortal(perl_format_create_dest(NULL, NULL, level, window)));
#line 426 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi__Server_format_create_dest); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__Server_format_create_dest)
{
    dVAR; dXSARGS;
    if (items < 1 || items > 4)
       croak_xs_usage(cv,  "server, target=NULL, level=MSGLEVEL_CLIENTNOTICE, window=NULL");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	Irssi__Server	server = irssi_ref_object(ST(0))
;
	char *	target;
	int	level;
	Irssi__UI__Window	window;

	if (items < 2)
	    target = NULL;
	else {
	    target = (char *)SvPV_nolen(ST(1))
;
	}

	if (items < 3)
	    level = MSGLEVEL_CLIENTNOTICE;
	else {
	    level = (int)SvIV(ST(2))
;
	}

	if (items < 4)
	    window = NULL;
	else {
	    window = irssi_ref_object(ST(3))
;
	}
#line 147 "Formats.xs"
	XPUSHs(sv_2mortal(perl_format_create_dest(server, target, level, window)));
#line 470 "Formats.c"
	PUTBACK;
	return;
    }
}


XS_EUPXS(XS_Irssi__UI__TextDest_print); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__TextDest_print)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "dest, str");
    {
	Irssi__UI__TextDest	dest = irssi_ref_object(ST(0))
;
	char *	str = (char *)SvPV_nolen(ST(1))
;
#line 158 "Formats.xs"
	printtext_dest(dest, "%s", str);
#line 490 "Formats.c"
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi__UI__TextDest_meta_stash); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__TextDest_meta_stash)
{
    dVAR; dXSARGS;
    if (items != 3)
       croak_xs_usage(cv,  "dest, meta_key, meta_value");
    {
	Irssi__UI__TextDest	dest = irssi_ref_object(ST(0))
;
	char *	meta_key = (char *)SvPV_nolen(ST(1))
;
	char *	meta_value = (char *)SvPV_nolen(ST(2))
;

	format_dest_meta_stash(dest, meta_key, meta_value);
    }
    XSRETURN_EMPTY;
}


XS_EUPXS(XS_Irssi__UI__TextDest_meta_stash_find); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Irssi__UI__TextDest_meta_stash_find)
{
    dVAR; dXSARGS;
    if (items != 2)
       croak_xs_usage(cv,  "dest, meta_key");
    {
	Irssi__UI__TextDest	dest = irssi_ref_object(ST(0))
;
	char *	meta_key = (char *)SvPV_nolen(ST(1))
;
	char *	RETVAL;
	dXSTARG;
#line 175 "Formats.xs"
	RETVAL = (char *) format_dest_meta_stash_find(dest, meta_key);
#line 531 "Formats.c"
	sv_setpv(TARG, RETVAL);
	XSprePUSH;
	PUSHTARG;
    }
    XSRETURN(1);
}

#ifdef __cplusplus
extern "C"
#endif
XS_EXTERNAL(boot_Irssi__UI__Formats); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Irssi__UI__Formats)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        (void)newXSproto_portable("Irssi::format_get_length", XS_Irssi_format_get_length, file, "$");
        (void)newXSproto_portable("Irssi::format_real_length", XS_Irssi_format_real_length, file, "$$");
        (void)newXSproto_portable("Irssi::format_string_expand", XS_Irssi_format_string_expand, file, "$");
        (void)newXSproto_portable("Irssi::strip_codes", XS_Irssi_strip_codes, file, "$");
        (void)newXSproto_portable("Irssi::format_string_unexpand", XS_Irssi_format_string_unexpand, file, "$");
        (void)newXSproto_portable("Irssi::format_create_dest", XS_Irssi_format_create_dest, file, "$;$$");
        (void)newXSproto_portable("Irssi::UI::Window::format_get_text", XS_Irssi__UI__Window_format_get_text, file, "$$$$$;@");
        (void)newXSproto_portable("Irssi::UI::Window::format_create_dest", XS_Irssi__UI__Window_format_create_dest, file, ";$$");
        (void)newXSproto_portable("Irssi::Server::format_create_dest", XS_Irssi__Server_format_create_dest, file, "$;$$$");
        (void)newXSproto_portable("Irssi::UI::TextDest::print", XS_Irssi__UI__TextDest_print, file, "$$");
        (void)newXSproto_portable("Irssi::UI::TextDest::meta_stash", XS_Irssi__UI__TextDest_meta_stash, file, "$$$");
        (void)newXSproto_portable("Irssi::UI::TextDest::meta_stash_find", XS_Irssi__UI__TextDest_meta_stash_find, file, "$$");
#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

