Ruby 1.9.3p327(2012-11-10revision37606)
include/ruby/io.h
Go to the documentation of this file.
00001 /**********************************************************************
00002 
00003   rubyio.h -
00004 
00005   $Author: kosaki $
00006   created at: Fri Nov 12 16:47:09 JST 1993
00007 
00008   Copyright (C) 1993-2007 Yukihiro Matsumoto
00009 
00010 **********************************************************************/
00011 
00012 #ifndef RUBY_IO_H
00013 #define RUBY_IO_H 1
00014 
00015 #if defined(__cplusplus)
00016 extern "C" {
00017 #if 0
00018 } /* satisfy cc-mode */
00019 #endif
00020 #endif
00021 
00022 #include <stdio.h>
00023 #include <errno.h>
00024 #include "ruby/encoding.h"
00025 
00026 #if defined(HAVE_STDIO_EXT_H)
00027 #include <stdio_ext.h>
00028 #endif
00029 
00030 #include "ruby/config.h"
00031 #if defined(HAVE_POLL)
00032 #  include <poll.h>
00033 #  define RB_WAITFD_IN  POLLIN
00034 #  define RB_WAITFD_PRI POLLPRI
00035 #  define RB_WAITFD_OUT POLLOUT
00036 #else
00037 #  define RB_WAITFD_IN  0x001
00038 #  define RB_WAITFD_PRI 0x002
00039 #  define RB_WAITFD_OUT 0x004
00040 #endif
00041 
00042 #if defined __GNUC__ && __GNUC__ >= 4
00043 #pragma GCC visibility push(default)
00044 #endif
00045 
00046 typedef struct {
00047     char *ptr;                  /* off + len <= capa */
00048     int off;
00049     int len;
00050     int capa;
00051 } rb_io_buffer_t;
00052 
00053 typedef struct rb_io_t {
00054     int fd;                     /* file descriptor */
00055     FILE *stdio_file;           /* stdio ptr for read/write if available */
00056     int mode;                   /* mode flags: FMODE_XXXs */
00057     rb_pid_t pid;               /* child's pid (for pipes) */
00058     int lineno;                 /* number of lines read */
00059     VALUE pathv;                /* pathname for file */
00060     void (*finalize)(struct rb_io_t*,int); /* finalize proc */
00061 
00062     rb_io_buffer_t wbuf, rbuf;
00063 
00064     VALUE tied_io_for_writing;
00065 
00066     /*
00067      * enc  enc2 read action                      write action
00068      * NULL NULL force_encoding(default_external) write the byte sequence of str
00069      * e1   NULL force_encoding(e1)               convert str.encoding to e1
00070      * e1   e2   convert from e2 to e1            convert str.encoding to e2
00071      */
00072     struct rb_io_enc_t {
00073         rb_encoding *enc;
00074         rb_encoding *enc2;
00075         int ecflags;
00076         VALUE ecopts;
00077     } encs;
00078 
00079     rb_econv_t *readconv;
00080     rb_io_buffer_t cbuf;
00081 
00082     rb_econv_t *writeconv;
00083     VALUE writeconv_asciicompat;
00084     int writeconv_pre_ecflags;
00085     VALUE writeconv_pre_ecopts;
00086     int writeconv_initialized;
00087 
00088     VALUE write_lock;
00089 } rb_io_t;
00090 
00091 #define HAVE_RB_IO_T 1
00092 
00093 #define FMODE_READABLE              0x00000001
00094 #define FMODE_WRITABLE              0x00000002
00095 #define FMODE_READWRITE             (FMODE_READABLE|FMODE_WRITABLE)
00096 #define FMODE_BINMODE               0x00000004
00097 #define FMODE_SYNC                  0x00000008
00098 #define FMODE_TTY                   0x00000010
00099 #define FMODE_DUPLEX                0x00000020
00100 #define FMODE_APPEND                0x00000040
00101 #define FMODE_CREATE                0x00000080
00102 /* #define FMODE_NOREVLOOKUP        0x00000100 */
00103 #define FMODE_WSPLIT                0x00000200
00104 #define FMODE_WSPLIT_INITIALIZED    0x00000400
00105 #define FMODE_TRUNC                 0x00000800
00106 #define FMODE_TEXTMODE              0x00001000
00107 /* #define FMODE_PREP               0x00010000 */
00108 #define FMODE_SETENC_BY_BOM         0x00100000
00109 
00110 #define GetOpenFile(obj,fp) rb_io_check_closed((fp) = RFILE(rb_io_taint_check(obj))->fptr)
00111 
00112 #define RB_IO_BUFFER_INIT(buf) do {\
00113     (buf).ptr = NULL;\
00114     (buf).off = 0;\
00115     (buf).len = 0;\
00116     (buf).capa = 0;\
00117 } while (0)
00118 
00119 #define MakeOpenFile(obj, fp) do {\
00120     if (RFILE(obj)->fptr) {\
00121         rb_io_close(obj);\
00122         rb_io_fptr_finalize(RFILE(obj)->fptr);\
00123         RFILE(obj)->fptr = 0;\
00124     }\
00125     (fp) = 0;\
00126     RB_IO_FPTR_NEW(fp);\
00127     RFILE(obj)->fptr = (fp);\
00128 } while (0)
00129 
00130 #define RB_IO_FPTR_NEW(fp) do {\
00131     (fp) = ALLOC(rb_io_t);\
00132     (fp)->fd = -1;\
00133     (fp)->stdio_file = NULL;\
00134     (fp)->mode = 0;\
00135     (fp)->pid = 0;\
00136     (fp)->lineno = 0;\
00137     (fp)->pathv = Qnil;\
00138     (fp)->finalize = 0;\
00139     RB_IO_BUFFER_INIT((fp)->wbuf);\
00140     RB_IO_BUFFER_INIT((fp)->rbuf);\
00141     RB_IO_BUFFER_INIT((fp)->cbuf);\
00142     (fp)->readconv = NULL;\
00143     (fp)->writeconv = NULL;\
00144     (fp)->writeconv_asciicompat = Qnil;\
00145     (fp)->writeconv_pre_ecflags = 0;\
00146     (fp)->writeconv_pre_ecopts = Qnil;\
00147     (fp)->writeconv_initialized = 0;\
00148     (fp)->tied_io_for_writing = 0;\
00149     (fp)->encs.enc = NULL;\
00150     (fp)->encs.enc2 = NULL;\
00151     (fp)->encs.ecflags = 0;\
00152     (fp)->encs.ecopts = Qnil;\
00153     (fp)->write_lock = 0;\
00154 } while (0)
00155 
00156 FILE *rb_io_stdio_file(rb_io_t *fptr);
00157 
00158 FILE *rb_fdopen(int, const char*);
00159 int rb_io_modestr_fmode(const char *modestr);
00160 int rb_io_modestr_oflags(const char *modestr);
00161 int rb_io_oflags_fmode(int oflags);
00162 void rb_io_check_writable(rb_io_t*);
00163 void rb_io_check_readable(rb_io_t*);
00164 void rb_io_check_char_readable(rb_io_t *fptr);
00165 void rb_io_check_byte_readable(rb_io_t *fptr);
00166 int rb_io_fptr_finalize(rb_io_t*);
00167 void rb_io_synchronized(rb_io_t*);
00168 void rb_io_check_initialized(rb_io_t*);
00169 void rb_io_check_closed(rb_io_t*);
00170 VALUE rb_io_get_io(VALUE io);
00171 VALUE rb_io_get_write_io(VALUE io);
00172 VALUE rb_io_set_write_io(VALUE io, VALUE w);
00173 int rb_io_wait_readable(int);
00174 int rb_io_wait_writable(int);
00175 int rb_wait_for_single_fd(int fd, int events, struct timeval *tv);
00176 void rb_io_set_nonblock(rb_io_t *fptr);
00177 int rb_io_extract_encoding_option(VALUE opt, rb_encoding **enc_p, rb_encoding **enc2_p, int *fmode_p);
00178 ssize_t rb_io_bufwrite(VALUE io, const void *buf, size_t size);
00179 
00180 /* compatibility for ruby 1.8 and older */
00181 #define rb_io_mode_flags(modestr) rb_io_modestr_fmode(modestr)
00182 #define rb_io_modenum_flags(oflags) rb_io_oflags_fmode(oflags)
00183 
00184 VALUE rb_io_taint_check(VALUE);
00185 NORETURN(void rb_eof_error(void));
00186 
00187 void rb_io_read_check(rb_io_t*);
00188 int rb_io_read_pending(rb_io_t*);
00189 DEPRECATED(void rb_read_check(FILE*));
00190 
00191 #if defined __GNUC__ && __GNUC__ >= 4
00192 #pragma GCC visibility pop
00193 #endif
00194 
00195 #if defined(__cplusplus)
00196 #if 0
00197 { /* satisfy cc-mode */
00198 #endif
00199 }  /* extern "C" { */
00200 #endif
00201 
00202 #endif /* RUBY_IO_H */
00203