#include <stdarg.h>
#include <xml/gerror.h>
#include <glib.h>
#include <string.h>

#define ___ {
#define ____ }

void
xml_g_set_prgname (const gchar* prgname)
{
#  ifndef G_DISABLE_DEPRACATED
    g_set_prgname (g_basename (prgname));
#  else
    gchar* s = strrchr (prgname, G_DIR_SEPARATOR);
    g_set_prgname (s ? s+1 : prgname);
#  endif
}
xml_GError* 
xml_g_error_new_valist (gint code, const gchar *format, va_list args)
{
    xml_GError *error = g_new (xml_GError, 1);
    error->domain = 0;
    error->code = code;
    error->message = g_strdup_vprintf (format, args);
    return error;
}
xml_GError*
xml_g_error_new (gint code, const gchar* format, ...)
{
    va_list args;
    xml_GError* error;
    g_return_val_if_fail (format != NULL, NULL);

    va_start (args, format);
    error = xml_g_error_new_valist (code, format, args);
    va_end (args);

    return error;
}
xml_GError*
xml_g_error_new2 (gint code, const gchar* domain, const gchar *message)
{
    xml_GError* err;
    g_return_val_if_fail (message != NULL, NULL);

    err = g_new (xml_GError, 1);
    err->domain = 0;
    err->code = code;
    err->message = g_strconcat (domain, message, 0);
    return err;
}
gboolean
xml_g_error_matches (const xml_GError *error, gint code, const gchar  *domain)
{
    if (! error || code != error->code) return FALSE;
    if (! domain) return TRUE;
    if (! error->domain) 
	return ! strncmp (error->message, domain, strlen (domain));
    else
	return error->domain == g_quark_from_string (domain);
}
# define OVERWRITTEN_MESSAGE " multiple errors, previous was:"
/*
   of course: we print the old error and set the var to the new error.
   (the glib2 does the other way round. stupid. even in the non-init-var
    case it'd be better to print an overwrite-warning and go and just try)
 */
void
xml_g_set_error (xml_GError **err, gint code, const gchar *format, ...)
{
    GError * def;
  
    va_list args;

    if (err == NULL)
	return;
  
    va_start (args, format);
    def = xml_g_error_new_valist (code, format, args);
    va_end (args);

    if (*err)
    
{   
	g_warning (OVERWRITTEN_MESSAGE);
	xml_g_error_print (*err);
	xml_g_error_free (*err);
    }
*err = def; }
/*
   of course: we print the old error and set the var to the new error.
   (the glib2 does the other way round. stupid. even in the non-init-var
    case it'd be better to print an overwrite-warning and go and just try)
 */
void    
xml_g_propagate_error (xml_GError **dest, xml_GError *src)
{
    if (! src) return;
    if (! dest) 
{ if (src) g_error_free (src); return; }
if (*dest)
{
	g_message (OVERWRITTEN_MESSAGE);
	xml_g_error_print (*dest);
	xml_g_error_free (*dest);
    }
*dest = src; }
void
xml_g_error_print (const xml_GError * error)
{
    if (! error) 
    
{
	g_message ("success");
	return;
    }
___ const gchar* domain = (! error->domain ? "" : g_quark_to_string (error->domain)); if (! error->code) g_warning ("%s %s", domain, error->message); else g_critical ("%+i: %s %s", error->code, domain, error->message); ____; }
gint
xml_g_show_error (xml_GError ** err, const gchar* format, ...)
{
    if (! err || ! *err) return 0;
    ___ gchar* msg = 0;
    if (format)
    
{
        va_list args;
        va_start (args, format);
        msg = g_strdup_vprintf (format, args);
        va_end (args);
    }
if (msg && *msg) g_message (msg); g_free (msg); xml_g_error_print (*err); ____; ___ int code = (**err).code; g_error_free (*err); *err = 0; return code; ____; }
/* 
   Local variables:
   c-file-style: "stroustrup"
   End:
 */