Ruby
view release on metacpan or search on metacpan
StringValue(vpath);
v = vmode;
StringValue(v);
vmode = rb_str_new(NULL, RSTRING_LEN(v)+1);
rb_str_set_len(vmode, 0);
p = RSTRING_PTR(v);
while(*p && isSPACE(*p)) p++;
switch(*p){
case 'w':
p++;
if(*p == '+'){ p++; rb_str_buf_cat(vmode, "+", 1); }
rb_str_buf_cat(vmode, ">", 1);
break;
case 'r':
p++;
if(*p == '+'){ p++; rb_str_buf_cat(vmode, "+", 1); }
rb_str_buf_cat(vmode, "<", 1);
break;
case 'a':
p++;
if(*p == '+'){ p++; rb_str_buf_cat(vmode, "+", 1); }
rb_str_buf_cat(vmode, ">>", 2);
break;
}
while(*p && isSPACE(*p)) p++;
if(*p == 'b'){
p++;
mode |= O_BINARY;
}
rb_str_buf_cat(vmode, p, RSTRING_LEN(v) - (p - RSTRING_PTR(v)));
arg1ptr = RSTRING_PTR(vmode);
arg1len = RSTRLEN(vmode);
}
}
else{
StringValue(vpath);
arg1ptr = RSTRING_PTR(vpath);
arg1len = RSTRLEN(vpath);
mode = O_RDONLY;
}
if(!NIL_P(vperm)){
perm = NUM2INT(vperm);
}
gv_gen(gv, RSTRING_PTR(vpath), RSTRLEN(vpath));
if(!do_openn(gv, arg1ptr, (I32)arg1len, as_raw, mode, perm, Nullfp, &arg2, numargs))
{
rb_sys_fail(RSTRING_PTR(vpath));
}
self = gv2pio_noinc(gv);
if(rb_block_given_p()){
return rb_ensure(rb_yield, self, pio_close, self);
}
return self;
}
static VALUE
pio_flock(VALUE self, VALUE operation)
{
dTHX;
PerlIO* fp = PIOFP(self);
int op = NUM2INT(operation);
PerlIO_flush(fp);
if(flock(PerlIO_fileno(fp), op) < 0){
rb_sys_fail(PIO_NAME(self));
}
return self;
}
static VALUE
pio_binmode(int argc, VALUE* argv, VALUE self)
{
dTHX;
IO* io;
int mode = 0;
char* discp;
volatile VALUE layer;
rb_scan_args(argc, argv, "01", &layer);
io = PIO(self);
if(NIL_P(layer)){
discp = ":raw";
mode |= O_BINARY;
}
else{
if(SYMBOL_P(layer)){
const char* name = rb_id2name(SYM2ID(layer));
layer = rb_str_new(NULL, (long)strlen(name)+1);
rb_str_set_len(layer, 0);
rb_str_buf_cat2(layer, ":");
rb_str_buf_cat2(layer, name);
}else{
StringValue(layer);
}
discp = RSTRING_PTR(layer);
}
( run in 3.040 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )