/*
  block.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_common.h"
#include "rb_gsl_array.h"

static VALUE rb_gsl_block_new(VALUE klass, VALUE nn)
{
  gsl_block *block = NULL;
  CHECK_FIXNUM(nn);
  block = gsl_block_alloc(FIX2INT(nn));
  return Data_Wrap_Struct(klass, 0, gsl_block_free, block);
}

static VALUE rb_gsl_block_calloc(VALUE klass, VALUE nn)
{
  gsl_block *block = NULL;
  CHECK_FIXNUM(nn);
  block = gsl_block_calloc(FIX2INT(nn));
  return Data_Wrap_Struct(klass, 0, gsl_block_free, block);
}

static VALUE rb_gsl_block_size(VALUE obj)
{
  gsl_block *block = NULL;
  Data_Get_Struct(obj, gsl_block, block);
  return INT2FIX(block->size);
}

static VALUE rb_gsl_block_fwrite(VALUE obj, VALUE io)
{
  gsl_block *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_block, h);
  f = rb_gsl_open_writefile(io, &flag);
  status = gsl_block_fwrite(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_block_fread(VALUE obj, VALUE io)
{
  gsl_block *h = NULL;
  FILE *f = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_block, h);
  f = rb_gsl_open_readfile(io, &flag);
  status = gsl_block_fread(f, h);
  if (flag == 1) fclose(f);
  return INT2FIX(status);
}

static VALUE rb_gsl_block_fprintf(int argc, VALUE *argv, VALUE obj)
{
  gsl_block *h = NULL;
  FILE *fp = NULL;
  int status, flag = 0;
  if (argc != 1 && argc != 2) 
    rb_raise(rb_eArgError, 
	     "wrong number of arguments (%d for 1 or 2)", argc);
  Data_Get_Struct(obj, gsl_block, h);
  fp = rb_gsl_open_writefile(argv[0], &flag);
  if (argc == 2) {
    Check_Type(argv[1], T_STRING);
    status = gsl_block_fprintf(fp, h, STR2CSTR(argv[1]));
  } else {
    status = gsl_block_fprintf(fp, h, "%g");
  }
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

static VALUE rb_gsl_block_printf(int argc, VALUE *argv, VALUE obj)
{
  gsl_block *h = NULL;
  int status;
  Data_Get_Struct(obj, gsl_block, h);
  if (argc == 1) {
    Check_Type(argv[0], T_STRING);
    status = gsl_block_fprintf(stdout, h, STR2CSTR(argv[0]));
  } else {
    status = gsl_block_fprintf(stdout, h, "%g");
  }
  return INT2FIX(status);
}

static VALUE rb_gsl_block_fscanf(VALUE obj, VALUE io)
{
  gsl_block *h = NULL;
  FILE *fp = NULL;
  int status, flag = 0;
  Data_Get_Struct(obj, gsl_block, h);
  fp = rb_gsl_open_readfile(io, &flag);
  status = gsl_block_fscanf(fp, h);
  if (flag == 1) fclose(fp);
  return INT2FIX(status);
}

void Init_gsl_block()
{
  rb_define_singleton_method(cgsl_block, "new", rb_gsl_block_new, 1);
  rb_define_singleton_method(cgsl_block, "alloc", rb_gsl_block_new, 1);
  rb_define_singleton_method(cgsl_block, "calloc", rb_gsl_block_calloc, 1);
  rb_define_method(cgsl_block, "size", rb_gsl_block_size, 0);
  rb_define_method(cgsl_block, "fwrite", rb_gsl_block_fwrite, 1);
  rb_define_method(cgsl_block, "fread", rb_gsl_block_fread, 1);
  rb_define_method(cgsl_block, "fprintf", rb_gsl_block_fprintf, -1);
  rb_define_method(cgsl_block, "printf", rb_gsl_block_printf, -1);
  rb_define_method(cgsl_block, "fscanf", rb_gsl_block_fscanf, 1);

}
