/*
 * call-seq:
 *    conn.lo_open( oid, [mode] ) => pglarge
 *
 * Open a large object of _oid_. Returns a PGlarge instance on success.
 * The _mode_ argument specifies the mode for the opened large object,
 * which is either +INV_READ+, or +INV_WRITE+.
 * * If _mode_ On failure, it raises a PGError exception.
 * * If _mode_ is omitted, the default is +INV_READ+.
 */
static VALUE
pgconn_loopen(argc, argv, obj)
  int argc;
  VALUE *argv;
  VALUE obj;
{
  Oid lo_oid;
  int fd, mode;
  VALUE nmode, objid;
  PGconn *conn = get_pgconn(obj);

  switch (rb_scan_args(argc, argv, "02", &objid, &nmode)) {
    case 1:
  lo_oid = NUM2INT(objid);
  mode = INV_READ;
  break;
    case 2:
  lo_oid = NUM2INT(objid);
  mode = FIX2INT(nmode);
  break;
    default:
  mode = INV_READ;
  lo_oid = lo_creat(conn, mode);
  if (lo_oid == 0){
    rb_raise(rb_ePGError, "can't creat large object");
  }
  }
  if((fd = lo_open(conn, lo_oid, mode)) < 0) {
  rb_raise(rb_ePGError, "can't open large object");
  }
  return pglarge_new(conn, lo_oid, fd);
}