/*
 * call-seq:
 *    res.print( file, options )
 *
 * Prints out all the rows and, optionally, the column names to the
 * specified _file_ output stream.
 * 
 * An example using this method can be found in <tt>samples/psql.rb</tt>
 * in the postgres source code
 *
 * For more information, see
 * PQprint[http://www.postgresql.org/docs/7.4/static/libpq-exec.html#LIBPQ-EXEC-SELECT-INFO]
 * which is called internally by this method.
 */
static VALUE
pgresult_print(obj, file, opt)
  VALUE obj, file, opt;
{
  VALUE value;
  ID mem;
  OpenFile* fp;
  PQprintOpt po;

  Check_Type(file, T_FILE);
  Check_Type(opt,  T_STRUCT);
  GetOpenFile(file, fp);

  memset(&po, 0, sizeof(po));

  mem = rb_intern("header");
  value = rb_struct_getmember(opt, mem);
  po.header = value == Qtrue ? 1 : 0;

  mem = rb_intern("align");
  value = rb_struct_getmember(opt, mem);
  po.align = value == Qtrue ? 1 : 0;

  mem = rb_intern("standard");
  value = rb_struct_getmember(opt, mem);
  po.standard = value == Qtrue ? 1 : 0;

  mem = rb_intern("html3");
  value = rb_struct_getmember(opt, mem);
  po.html3 = value == Qtrue ? 1 : 0;

  mem = rb_intern("expanded");
  value = rb_struct_getmember(opt, mem);
  po.expanded = value == Qtrue ? 1 : 0;

  mem = rb_intern("pager");
  value = rb_struct_getmember(opt, mem);
  po.pager = value == Qtrue ? 1 : 0;

  mem = rb_intern("fieldSep");
  value = rb_struct_getmember(opt, mem);
  if (!NIL_P(value)) {
    Check_Type(value, T_STRING);
    po.fieldSep = STR2CSTR(value);
  }

  mem = rb_intern("tableOpt");
  value = rb_struct_getmember(opt, mem);
  if (!NIL_P(value)) {
    Check_Type(value, T_STRING);
    po.tableOpt = STR2CSTR(value);
  }

  mem = rb_intern("caption");
  value = rb_struct_getmember(opt, mem);
  if (!NIL_P(value)) {
    Check_Type(value, T_STRING);
    po.caption = STR2CSTR(value);
  }

  PQprint(fp->f2?fp->f2:fp->f, get_pgresult(obj), &po);
  return obj;
}