initialize_copy(p1) public

No documentation

This method has no description. You can help the Ruby community by adding new notes.

Hide source
/* :nodoc: */
static VALUE
rb_io_init_copy(dest, io)
    VALUE dest, io;
{
    rb_io_t *fptr, *orig;
    int fd;
    const char *mode;

    io = rb_io_get_io(io);
    if (dest == io) return dest;
    GetOpenFile(io, orig);
    MakeOpenFile(dest, fptr);

    if (orig->f2) {
        io_fflush(orig->f2, orig);
        fseeko(orig->f, 0L, SEEK_CUR);
    }
    else if (orig->mode & FMODE_WRITABLE) {
        io_fflush(orig->f, orig);
    }
    else {
        fseeko(orig->f, 0L, SEEK_CUR);
    }

    /* copy rb_io_t structure */
    fptr->mode = orig->mode;
    fptr->pid = orig->pid;
    fptr->lineno = orig->lineno;
    if (orig->path) fptr->path = strdup(orig->path);
    fptr->finalize = orig->finalize;

    switch (fptr->mode & FMODE_READWRITE) {
      case FMODE_READABLE:
      default:
        mode = "r"; break;
      case FMODE_WRITABLE:
        mode = "w"; break;
      case FMODE_READWRITE:
        if (orig->f2) mode = "r";
        else          mode = "r+";
        break;
    }
    fd = ruby_dup(fileno(orig->f));
    fptr->f = rb_fdopen(fd, mode);
    fseeko(fptr->f, ftello(orig->f), SEEK_SET);
    if (orig->f2) {
        if (fileno(orig->f) != fileno(orig->f2)) {
            fd = ruby_dup(fileno(orig->f2));
        }
        fptr->f2 = rb_fdopen(fd, "w");
        fseeko(fptr->f2, ftello(orig->f2), SEEK_SET);
    }
    if (fptr->mode & FMODE_BINMODE) {
        rb_io_binmode(dest);
    }

    return dest;
}
Register or log in to add new notes.