Class: Win32::NIO

Inherits:
Object
  • Object
show all
Defined in:
ext/win32/nio.c

Constant Summary collapse

VERSION =

The version of the win32-nio library

0.2.0

Class Method Summary collapse

Class Method Details

.read(*args) ⇒ Object

This method is similar to Ruby’s IO.read method except that it uses native function calls.

Examples:

# Read everything Win32::NIO.read(file)

# Read the first 100 bytes Win32::NIO.read(file, 100)

# Read 50 bytes starting at offset 10 Win32::NIO.read(file, 50, 10)

Note that the + options + that may be passed to this method are limited to :encoding, : mode and : event because we’re no longer using the open function internally.In the case of:mode the only thing that is checked for is the presence of the ‘b’ (binary)mode.

The :event option, if present, must be a Win32::Event object.



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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'ext/win32/nio.c', line 40

static VALUE rb_nio_read(int argc, VALUE* argv, VALUE self){
  OVERLAPPED olap;
  HANDLE h;
  DWORD bytes_read;
  LARGE_INTEGER lsize;
  BOOL b;
  VALUE v_file, v_length, v_offset, v_options;
  VALUE v_event, v_mode, v_encoding, v_result;
  size_t length;
  int flags, error, size;
  wchar_t* file = NULL;
  char* buffer = NULL;

  memset(&olap, 0, sizeof(olap));

  // Paranoid initialization
  v_length = Qnil;
  v_offset = Qnil;
  v_options = Qnil;

  rb_scan_args(argc, argv, "13", &v_file, &v_length, &v_offset, &v_options);

  // Allow path-y objects.
  if (rb_respond_to(v_file, rb_intern("to_path")))
    v_file = rb_funcall(v_file, rb_intern("to_path"), 0, NULL);

  SafeStringValue(v_file);

  // If a length is provided it cannot be negative.
  if (!NIL_P(v_length)){
    length = NUM2SIZET(v_length);
    if ((int)length < 0)
      rb_raise(rb_eArgError, "negative length %i given", (int)length);
  }
  else{
    length = 0; // Mostly to make gcc stfu. We'll set this later.
  }

  // Unlike MRI, don't wait for an internal C function to fail on an invalid argument.
  if (!NIL_P(v_offset)){
    olap.Offset = NUM2ULONG(v_offset);
    if ((int)olap.Offset < 0)
      rb_raise(rb_eArgError, "negative offset %i given", (int)olap.Offset);
  }

  // Gotta do this for wide character function support.
  size = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(v_file), -1, NULL, 0);
  file = (wchar_t*)ruby_xmalloc(MAX_PATH * sizeof(wchar_t));

  if (!MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(v_file), -1, file, size)){
    ruby_xfree(file);
    rb_raise_syserr("MultibyteToWideChar", GetLastError());
  }

  flags = FILE_FLAG_SEQUENTIAL_SCAN;

  // Possible options are :event, :mode and :encoding
  if (!NIL_P(v_options)){
    Check_Type(v_options, T_HASH);

    v_event = rb_hash_aref(v_options, ID2SYM(rb_intern("event")));
    v_encoding = rb_hash_aref(v_options, ID2SYM(rb_intern("encoding")));
    v_mode = rb_hash_aref(v_options, ID2SYM(rb_intern("mode")));

    if (!NIL_P(v_event)){
      flags |= FILE_FLAG_OVERLAPPED;
      olap.hEvent = (HANDLE)NUM2OFFT(rb_funcall(v_event, rb_intern("handle"), 0, 0));
    }
  }
  else{
    v_event = Qnil;
    v_encoding = Qnil;
    v_mode = Qnil;
  }

  h = CreateFileW(
    file,
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    flags,
    NULL
  );

  if (h == INVALID_HANDLE_VALUE)
    rb_raise_syserr("CreateFile", GetLastError());

  // Get the file size. We may use this later to limit read length.
  if (!GetFileSizeEx(h, &lsize)){
    error = GetLastError();
    CloseHandle(h);
    rb_raise_syserr("GetFileSizeEx", error);
  }

  // If no length is specified, read the entire file
  if (NIL_P(v_length))
    length = (size_t)lsize.QuadPart;

  // Don't read past the end of the file
  if (olap.Offset + length > (size_t)lsize.QuadPart)
    length = (size_t)lsize.QuadPart - olap.Offset;

  buffer = (char*)ruby_xmalloc(length * sizeof(char));

  // If a block is given then treat it as a callback
  if (rb_block_given_p()){
    flags |= FILE_FLAG_OVERLAPPED;
    b = ReadFileEx(h, buffer, length, &olap, read_complete);
  }
  else{
    b = ReadFile(h, buffer, length, &bytes_read, &olap);
  }

  error = GetLastError();

  // Put in alertable wait state if overlapped IO
  if (flags & FILE_FLAG_OVERLAPPED)
    SleepEx(1, TRUE);

  if (!b){
    if(error == ERROR_IO_PENDING){
      DWORD bytes;
      if (!GetOverlappedResult(h, &olap, &bytes, TRUE)){
        ruby_xfree(buffer);
        CloseHandle(h);
        rb_raise_syserr("GetOverlappedResult", error);
      }
    }
    else{
      ruby_xfree(buffer);
      CloseHandle(h);
      rb_raise_syserr("ReadFile", error);
    }
  }

  CloseHandle(h);

  v_result = rb_str_new(buffer, length);
  ruby_xfree(buffer);

  // Convert CRLF to LF if text mode
  if (!NIL_P(v_mode) && strstr(RSTRING_PTR(v_mode), "t"))
    rb_funcall(v_result, rb_intern("gsub!"), 2, rb_str_new2("\r\n"), rb_gv_get("$/"));

  if (!NIL_P(v_encoding))
    rb_funcall(v_result, rb_intern("encode!"), 1, v_encoding);

  return v_result;
}

.readlines(*args) ⇒ Object

Reads the entire file specified by portname as individual lines, and returns those lines in an array. Lines are separated by sep.

Examples:

# Standard call
Win32::NIO.readlines('file.txt') # => ['line 1', 'line 2', 'line 3', 'line 4']

# Paragraph mode
Win32::NIO.readlines('file.txt', '') # => ['line 1\r\nline 2', 'line 3\r\nline 4']

# With event
event = Win32::Event.new
Win32::NIO.readlines('file.txt', nil, event)
p event.signaled? # => true

Superficially this method acts the same as the Ruby IO.readlines call, except that it does not transform line endings and accepts an optional event object. However, internally this method is using a scattered read to accomplish its goal. In practice this is only relevant in specific situations. Using it outside of those situations is unlikely to provide any practical benefit, and may even result in slower performance.

See information on vectored IO for more details.



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'ext/win32/nio.c', line 216

static VALUE rb_nio_readlines(int argc, VALUE* argv, VALUE self){
  HANDLE h;
  SYSTEM_INFO info;
  LARGE_INTEGER file_size;
  size_t length, page_size;
  double size;
  void* base_address;
  int error, page_num;
  wchar_t* file;
  VALUE v_file, v_sep, v_event, v_result;

  rb_scan_args(argc, argv, "12", &v_file, &v_sep, &v_event);

  SafeStringValue(v_file);

  if (NIL_P(v_sep))
    v_sep = rb_str_new2("\r\n");
  else
    SafeStringValue(v_sep);

  v_result = Qnil;

  length = MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(v_file), -1, NULL, 0);
  file = (wchar_t*)ruby_xmalloc(MAX_PATH * sizeof(wchar_t));

  if (!MultiByteToWideChar(CP_UTF8, 0, RSTRING_PTR(v_file), -1, file, length)){
    ruby_xfree(file);
    rb_raise_syserr("MultibyteToWideChar", GetLastError());
  }

  h = CreateFileW(
    file,
    GENERIC_READ,
    FILE_SHARE_READ,
    NULL,
    OPEN_EXISTING,
    FILE_FLAG_OVERLAPPED | FILE_FLAG_NO_BUFFERING,
    NULL
  );

  if (h == INVALID_HANDLE_VALUE)
    rb_raise_syserr("CreateFile", GetLastError());

  if (!GetFileSizeEx(h, &file_size)){
    error = GetLastError();
    CloseHandle(h);
    rb_raise_syserr("GetFileSizeEx", error);
  }

  length = (size_t)file_size.QuadPart;

  GetSystemInfo(&info);
  page_size = info.dwPageSize;

  page_num = (int)ceil((double)length / page_size);

  size = page_num * page_size;

  base_address = VirtualAlloc(NULL, (size_t)size, MEM_COMMIT, PAGE_READWRITE);

  if (!base_address){
    error = GetLastError();
    CloseHandle(h);
    rb_raise_syserr("VirtualAlloc", error);
  }
  else{
    int i;
    OVERLAPPED olap;
    BOOL rv;
    FILE_SEGMENT_ELEMENT* fse;

    olap.Offset = 0;
    olap.OffsetHigh = 0;

    if (NIL_P(v_event))
      olap.hEvent = NULL;
    else
      olap.hEvent = (HANDLE)NUM2OFFT(rb_funcall(v_event, rb_intern("handle"), 0, 0));

    fse = (FILE_SEGMENT_ELEMENT*)malloc(sizeof(FILE_SEGMENT_ELEMENT) * (page_num + 1));
    memset(fse, 0, sizeof(FILE_SEGMENT_ELEMENT) * (page_num + 1));
    v_result = Qnil;

    for (i = 0; i < page_num; i++)
      fse[i].Alignment = (ULONGLONG)base_address + (page_size * i);

    rv = ReadFileScatter(h, fse, (DWORD)size, NULL, &olap);

    if (!rv){
      error = GetLastError();

      if (error == ERROR_IO_PENDING){
        while (!HasOverlappedIoCompleted(&olap))
          SleepEx(1, TRUE);
      }
      else{
        VirtualFree(base_address, 0, MEM_RELEASE);
        CloseHandle(h);
        rb_raise_syserr("ReadFileScatter", error);
      }
    }

    // Explicitly handle paragraph mode
    if (rb_equal(v_sep, rb_str_new2(""))){
      VALUE v_args[1];
      v_args[0] = rb_str_new2("(\r\n){2,}");
      v_sep = rb_class_new_instance(1, v_args, rb_cRegexp);
      v_result = rb_funcall(rb_str_new2(fse[0].Buffer), rb_intern("split"), 1, v_sep);
      rb_funcall(v_result, rb_intern("delete"), 1, rb_str_new2("\r\n"));
    }
    else{
      v_result = rb_funcall(rb_str_new2(fse[0].Buffer), rb_intern("split"), 1, v_sep);
    }

    VirtualFree(base_address, 0, MEM_RELEASE);
  }

  CloseHandle(h);

  return v_result;
}