/*
  ieee.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"

static VALUE rb_gsl_ieee_fprintf(int argc, VALUE *argv, VALUE obj)
{
  OpenFile *fptr = NULL;
  FILE *fp = NULL;
  int flag = 0;
  VALUE vtmp;

  switch (argc) {
  case 2:
    switch (TYPE(argv[0])) {
    case T_STRING:
      fp = fopen(RSTRING(argv[0])->ptr, "w");
      flag = 1;
      break;
    case T_FILE:
      GetOpenFile(argv[0], fptr);
      rb_io_check_writable(fptr);
      fp = GetWriteFile(fptr);
      break;
    default:
      rb_raise(rb_eTypeError, "wrong type argument %s (IO or String expected)",
	       rb_class2name(CLASS_OF(argv[0])));
    }
    vtmp = argv[1];
    break;
  case 1:
    vtmp = argv[0];
    fp = stdout;
    break;
  default:
    rb_raise(rb_eArgError, "wrong number of arguments (%d for 1 or 2)", argc);
  }
  if (TYPE(vtmp) != T_FLOAT)
    rb_raise(rb_eTypeError, "wrong argument type %s (Float expected)",
	     rb_class2name(CLASS_OF(vtmp)));
  
  gsl_ieee_fprintf_double(fp, &(RFLOAT(vtmp)->value));
  if (flag == 1) fclose(fp);
  return obj;
}

static VALUE rb_gsl_ieee_env_setup(VALUE obj)
{
  gsl_ieee_env_setup();
  return obj;
}

void Init_gsl_ieee()
{
  VALUE mgsl_ieee;
  mgsl_ieee = rb_define_module_under(mgsl, "IEEE");

  rb_define_singleton_method(mgsl_ieee, "env_setup",
			     rb_gsl_ieee_env_setup, 0);
  rb_define_singleton_method(mgsl_ieee, "fprintf",
			     rb_gsl_ieee_fprintf, -1);
  rb_define_singleton_method(mgsl_ieee, "printf",
			     rb_gsl_ieee_fprintf, -1);
  rb_define_module_function(mgsl, "ieee_env_setup", rb_gsl_ieee_env_setup, 0);
}
