summaryrefslogtreecommitdiff
path: root/ext/-test-/file/newline_conv.c
blob: 2ac5aef801ef7ff6e49c27f75bdde6530864e639 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "ruby/ruby.h"
#include "ruby/io.h"
#include <fcntl.h>

static VALUE
open_with_rb_file_open(VALUE self, VALUE filename, VALUE read_or_write, VALUE binary_or_text)
{
    char fmode[3] = { 0 };
    if (rb_sym2id(read_or_write) == rb_intern("read")) {
        fmode[0] = 'r';
    }
    else if (rb_sym2id(read_or_write) == rb_intern("write")) {
        fmode[0] = 'w';
    }
    else {
        rb_raise(rb_eArgError, "read_or_write param must be :read or :write");
    }

    if (rb_sym2id(binary_or_text) == rb_intern("binary")) {
        fmode[1] = 'b';
    }
    else if (rb_sym2id(binary_or_text) == rb_intern("text")) {

    }
    else {
        rb_raise(rb_eArgError, "binary_or_text param must be :binary or :text");
    }

    return rb_file_open(StringValueCStr(filename), fmode);
}

static VALUE
open_with_rb_io_fdopen(VALUE self, VALUE filename, VALUE read_or_write, VALUE binary_or_text)
{
    int omode = 0;
    if (rb_sym2id(read_or_write) == rb_intern("read")) {
        omode |= O_RDONLY;
    }
    else if (rb_sym2id(read_or_write) == rb_intern("write")) {
        omode |= O_WRONLY;
    }
    else {
        rb_raise(rb_eArgError, "read_or_write param must be :read or :write");
    }

    if (rb_sym2id(binary_or_text) == rb_intern("binary")) {
#ifdef O_BINARY
        omode |= O_BINARY;
#endif
    }
    else if (rb_sym2id(binary_or_text) == rb_intern("text")) {

    }
    else {
        rb_raise(rb_eArgError, "binary_or_text param must be :binary or :text");
    }

    int fd = rb_cloexec_open(StringValueCStr(filename), omode, 0);
    if (fd < 0) {
        rb_raise(rb_eIOError, "failed to open the file");
    }

    rb_update_max_fd(fd);
    return rb_io_fdopen(fd, omode, StringValueCStr(filename));
}

void
Init_newline_conv(VALUE module)
{
    VALUE newline_conv = rb_define_module_under(module, "NewlineConv");
    rb_define_module_function(newline_conv, "rb_file_open", open_with_rb_file_open, 3);
    rb_define_module_function(newline_conv, "rb_io_fdopen", open_with_rb_io_fdopen, 3);
}