/*
  error.c
  Ruby/GSL: Ruby extension library for GSL (GNU Scientific Library)
    (C) Copyright 2001-2004 by Yoshiki Tsunesada

  Ruby/GSL is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License.
  This library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY.
*/
#include "rb_gsl.h"
#include <gsl/gsl_errno.h>

static void Init_gsl_define_GSL_CONST();
void rb_gsl_error_handler(const char *reason, const char *file,
				 int line, int gsl_errno);

void rb_gsl_error_handler(const char *reason, const char *file,
				 int line, int gsl_errno)
{
  const char *emessage = gsl_strerror(gsl_errno);
  rb_raise(rb_eRuntimeError, "Ruby/GSL: %s (file %s, line %d), %s",
	   reason, file, line, emessage);
}

static void Init_gsl_define_GSL_CONST()
{
  rb_define_const(mgsl, "SUCCESS", INT2FIX(GSL_SUCCESS));
  rb_define_const(mgsl, "FAILURE", INT2FIX(GSL_FAILURE));
  rb_define_const(mgsl, "CONTINUE", INT2FIX(GSL_CONTINUE));
  rb_define_const(mgsl, "EDOM", INT2FIX(GSL_EDOM));
  rb_define_const(mgsl, "ERANGE", INT2FIX(GSL_ERANGE));
  rb_define_const(mgsl, "EFAULT", INT2FIX(GSL_EFAULT));
  rb_define_const(mgsl, "EINVAL", INT2FIX(GSL_EINVAL));
  rb_define_const(mgsl, "EFAILED", INT2FIX(GSL_EFAILED));
  rb_define_const(mgsl, "EFACTOR", INT2FIX(GSL_EFACTOR));
  rb_define_const(mgsl, "ESANITY", INT2FIX(GSL_ESANITY));
  rb_define_const(mgsl, "ENOMEM", INT2FIX(GSL_ENOMEM));
  rb_define_const(mgsl, "EBADFUNC", INT2FIX(GSL_EBADFUNC));
  rb_define_const(mgsl, "ERUNAWAY", INT2FIX(GSL_ERUNAWAY));
  rb_define_const(mgsl, "EMAXITER", INT2FIX(GSL_EMAXITER));
  rb_define_const(mgsl, "EZERODIV", INT2FIX(GSL_EZERODIV));
  rb_define_const(mgsl, "EBADTOL", INT2FIX(GSL_EBADTOL));
  rb_define_const(mgsl, "ETOL", INT2FIX(GSL_ETOL));
  rb_define_const(mgsl, "EUNDRFLW", INT2FIX(GSL_EUNDRFLW));
  rb_define_const(mgsl, "EOVRFLW", INT2FIX(GSL_EOVRFLW));
  rb_define_const(mgsl, "ELOSS", INT2FIX(GSL_ELOSS));
  rb_define_const(mgsl, "EROUND", INT2FIX(GSL_EROUND));
  rb_define_const(mgsl, "EBADLEN", INT2FIX(GSL_EBADLEN));
  rb_define_const(mgsl, "ENOTSQR", INT2FIX(GSL_ENOTSQR));
  rb_define_const(mgsl, "ESING", INT2FIX(GSL_ESING));
  rb_define_const(mgsl, "EDIVERGE", INT2FIX(GSL_EDIVERGE));
  rb_define_const(mgsl, "EUNSUP", INT2FIX(GSL_EUNSUP));
  rb_define_const(mgsl, "EUNIMPL", INT2FIX(GSL_EUNIMPL));
  rb_define_const(mgsl, "ECACHE", INT2FIX(GSL_ECACHE));
  rb_define_const(mgsl, "ETABLE", INT2FIX(GSL_ETABLE));
  rb_define_const(mgsl, "ENOPROG", INT2FIX(GSL_ENOPROG));
  rb_define_const(mgsl, "ENOPROGJ", INT2FIX(GSL_ENOPROGJ));
  rb_define_const(mgsl, "ETOLF", INT2FIX(GSL_ETOLF));
  rb_define_const(mgsl, "ETOLX", INT2FIX(GSL_ETOLX));
  rb_define_const(mgsl, "ETOLG", INT2FIX(GSL_ETOLG));
  rb_define_const(mgsl, "EOF", INT2FIX(GSL_EOF));
}

void Init_gsl_error()
{
  Init_gsl_define_GSL_CONST();

  gsl_set_error_handler(&rb_gsl_error_handler);
}
