Skip to content

Commit 69619ae

Browse files
committed
Support bit flags for IO.open
Note that this bit flags are not compatible with the native flags defined in `#include <fcntl.h>`.
1 parent 6c5ee8f commit 69619ae

File tree

4 files changed

+151
-88
lines changed

4 files changed

+151
-88
lines changed

mrbgems/mruby-io/include/mruby/ext/io.h

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,31 @@ struct mrb_io {
1919
is_socket:1;
2020
};
2121

22-
#define FMODE_READABLE 0x00000001
23-
#define FMODE_WRITABLE 0x00000002
24-
#define FMODE_READWRITE (FMODE_READABLE|FMODE_WRITABLE)
25-
#define FMODE_BINMODE 0x00000004
26-
#define FMODE_APPEND 0x00000040
27-
#define FMODE_CREATE 0x00000080
28-
#define FMODE_TRUNC 0x00000800
22+
#define MRB_O_RDONLY 0x0000
23+
#define MRB_O_WRONLY 0x0001
24+
#define MRB_O_RDWR 0x0002
25+
#define MRB_O_ACCMODE (MRB_O_RDONLY | MRB_O_WRONLY | MRB_O_RDWR)
26+
#define MRB_O_NONBLOCK 0x0004
27+
#define MRB_O_APPEND 0x0008
28+
#define MRB_O_SYNC 0x0010
29+
#define MRB_O_NOFOLLOW 0x0020
30+
#define MRB_O_CREAT 0x0040
31+
#define MRB_O_TRUNC 0x0080
32+
#define MRB_O_EXCL 0x0100
33+
#define MRB_O_NOCTTY 0x0200
34+
#define MRB_O_DIRECT 0x0400
35+
#define MRB_O_BINARY 0x0800
36+
#define MRB_O_SHARE_DELETE 0x1000
37+
#define MRB_O_TMPFILE 0x2000
38+
#define MRB_O_NOATIME 0x4000
39+
#define MRB_O_DSYNC 0x00008000 /* Ignored with MRB_INT16 and MRB_WITHOUT_FLOAT */
40+
#define MRB_O_RSYNC 0x00010000 /* Ignored with MRB_INT16 and MRB_WITHOUT_FLOAT */
41+
42+
#define MRB_O_RDONLY_P(f) ((mrb_bool)(((f) & MRB_O_ACCMODE) == MRB_O_RDONLY))
43+
#define MRB_O_WRONLY_P(f) ((mrb_bool)(((f) & MRB_O_ACCMODE) == MRB_O_WRONLY))
44+
#define MRB_O_RDWR_P(f) ((mrb_bool)(((f) & MRB_O_ACCMODE) == MRB_O_RDWR))
45+
#define MRB_O_READABLE_P(f) ((mrb_bool)((((f) & MRB_O_ACCMODE) | 2) == 2))
46+
#define MRB_O_WRITABLE_P(f) ((mrb_bool)(((((f) & MRB_O_ACCMODE) + 1) & 2) == 2))
2947

3048
#define E_IO_ERROR (mrb_class_get(mrb, "IOError"))
3149
#define E_EOF_ERROR (mrb_class_get(mrb, "EOFError"))

mrbgems/mruby-io/mrblib/file_constants.rb

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
class File
22
module Constants
3-
RDONLY = 0
4-
WRONLY = 1
5-
RDWR = 2
6-
NONBLOCK = 4
7-
APPEND = 8
8-
9-
BINARY = 0
10-
SYNC = 128
11-
NOFOLLOW = 256
12-
CREAT = 512
13-
TRUNC = 1024
14-
EXCL = 2048
15-
16-
NOCTTY = 131072
17-
DSYNC = 4194304
18-
193
FNM_SYSCASE = 0
204
FNM_NOESCAPE = 1
215
FNM_PATHNAME = 2

mrbgems/mruby-io/src/file.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,4 +505,22 @@ mrb_init_file(mrb_state *mrb)
505505
#endif
506506
mrb_define_const(mrb, cnst, "NULL", mrb_str_new_cstr(mrb, NULL_FILE));
507507

508+
mrb_define_const(mrb, cnst, "RDONLY", mrb_fixnum_value(MRB_O_RDONLY));
509+
mrb_define_const(mrb, cnst, "WRONLY", mrb_fixnum_value(MRB_O_WRONLY));
510+
mrb_define_const(mrb, cnst, "RDWR", mrb_fixnum_value(MRB_O_RDWR));
511+
mrb_define_const(mrb, cnst, "APPEND", mrb_fixnum_value(MRB_O_APPEND));
512+
mrb_define_const(mrb, cnst, "CREAT", mrb_fixnum_value(MRB_O_CREAT));
513+
mrb_define_const(mrb, cnst, "EXCL", mrb_fixnum_value(MRB_O_EXCL));
514+
mrb_define_const(mrb, cnst, "TRUNC", mrb_fixnum_value(MRB_O_TRUNC));
515+
mrb_define_const(mrb, cnst, "NONBLOCK", mrb_fixnum_value(MRB_O_NONBLOCK));
516+
mrb_define_const(mrb, cnst, "NOCTTY", mrb_fixnum_value(MRB_O_NOCTTY));
517+
mrb_define_const(mrb, cnst, "BINARY", mrb_fixnum_value(MRB_O_BINARY));
518+
mrb_define_const(mrb, cnst, "SHARE_DELETE", mrb_fixnum_value(MRB_O_SHARE_DELETE));
519+
mrb_define_const(mrb, cnst, "SYNC", mrb_fixnum_value(MRB_O_SYNC));
520+
mrb_define_const(mrb, cnst, "DSYNC", mrb_fixnum_value(MRB_O_DSYNC));
521+
mrb_define_const(mrb, cnst, "RSYNC", mrb_fixnum_value(MRB_O_RSYNC));
522+
mrb_define_const(mrb, cnst, "NOFOLLOW", mrb_fixnum_value(MRB_O_NOFOLLOW));
523+
mrb_define_const(mrb, cnst, "NOATIME", mrb_fixnum_value(MRB_O_NOATIME));
524+
mrb_define_const(mrb, cnst, "DIRECT", mrb_fixnum_value(MRB_O_DIRECT));
525+
mrb_define_const(mrb, cnst, "TMPFILE", mrb_fixnum_value(MRB_O_TMPFILE));
508526
}

0 commit comments

Comments
 (0)