|
40 | 40 | ## at the end. If the file does not exist, it |
41 | 41 | ## will be created. |
42 | 42 |
|
43 | | - FileHandle* = cint ## The type that represents an OS file handle; this is |
44 | | - ## useful for low-level file access. |
45 | | - |
46 | 43 | FileSeekPos* = enum ## Position relative to which seek should happen. |
47 | 44 | # The values are ordered so that they match with stdio |
48 | 45 | # SEEK_SET, SEEK_CUR and SEEK_END respectively. |
49 | 46 | fspSet ## Seek to absolute value |
50 | 47 | fspCur ## Seek relative to current position |
51 | 48 | fspEnd ## Seek relative to end |
52 | 49 |
|
| 50 | +when defined(windows): |
| 51 | + type FileHandle* = int |
| 52 | + ## Windows `HANDLE` type, convertible to `winlean.Handle`. |
| 53 | +else: |
| 54 | + type FileHandle* = cint ## The type that represents an OS file handle; this is |
| 55 | + ## useful for low-level file access. |
| 56 | + |
53 | 57 | # text file handling: |
54 | 58 | when not defined(nimscript) and not defined(js): |
55 | 59 | # duplicated between io and ansi_c |
@@ -310,12 +314,7 @@ elif defined(windows): |
310 | 314 | proc getOsfhandle(fd: cint): int {. |
311 | 315 | importc: "_get_osfhandle", header: "<io.h>".} |
312 | 316 |
|
313 | | - type |
314 | | - IoHandle = distinct pointer |
315 | | - ## Windows' HANDLE type. Defined as an untyped pointer but is **not** |
316 | | - ## one. Named like this to avoid collision with other `system` modules. |
317 | | - |
318 | | - proc setHandleInformation(hObject: IoHandle, dwMask, dwFlags: WinDWORD): |
| 317 | + proc setHandleInformation(hObject: FileHandle, dwMask, dwFlags: WinDWORD): |
319 | 318 | WinBOOL {.stdcall, dynlib: "kernel32", |
320 | 319 | importc: "SetHandleInformation".} |
321 | 320 |
|
@@ -361,7 +360,7 @@ proc getFileHandle*(f: File): FileHandle = |
361 | 360 | ## Note that on Windows this doesn't return the Windows-specific handle, |
362 | 361 | ## but the C library's notion of a handle, whatever that means. |
363 | 362 | ## Use `getOsFileHandle` instead. |
364 | | - c_fileno(f) |
| 363 | + FileHandle c_fileno(f) |
365 | 364 |
|
366 | 365 | proc getOsFileHandle*(f: File): FileHandle = |
367 | 366 | ## Returns the OS file handle of the file `f`. This is only useful for |
@@ -390,7 +389,7 @@ when defined(nimdoc) or (defined(posix) and not defined(nimscript)) or defined(w |
390 | 389 | flags = if inheritable: flags and not FD_CLOEXEC else: flags or FD_CLOEXEC |
391 | 390 | result = c_fcntl(f, F_SETFD, flags) != -1 |
392 | 391 | else: |
393 | | - result = setHandleInformation(cast[IoHandle](f), HANDLE_FLAG_INHERIT, |
| 392 | + result = setHandleInformation(f, HANDLE_FLAG_INHERIT, |
394 | 393 | inheritable.WinDWORD) != 0 |
395 | 394 |
|
396 | 395 | proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect], |
@@ -423,12 +422,18 @@ proc readLine*(f: File, line: var string): bool {.tags: [ReadIOEffect], |
423 | 422 | importc: "LocalFree", stdcall, dynlib: "kernel32".} |
424 | 423 |
|
425 | 424 | proc isatty(f: File): bool = |
| 425 | + # terminal module also has isatty |
426 | 426 | when defined(posix): |
427 | 427 | proc isatty(fildes: FileHandle): cint {. |
428 | 428 | importc: "isatty", header: "<unistd.h>".} |
429 | | - else: |
430 | | - proc isatty(fildes: FileHandle): cint {. |
| 429 | + elif defined(windows): |
| 430 | + proc c_isatty(fildes: cint): cint {. |
431 | 431 | importc: "_isatty", header: "<io.h>".} |
| 432 | + proc isatty(fildes: FileHandle): cint = |
| 433 | + c_isatty(cint(fildes)) |
| 434 | + else: |
| 435 | + {.error: "isatty is not supported on your operating system!".} |
| 436 | + |
432 | 437 | result = isatty(getFileHandle(f)) != 0'i32 |
433 | 438 |
|
434 | 439 | # this implies the file is open |
@@ -769,10 +774,10 @@ proc open*(f: var File, filehandle: FileHandle, |
769 | 774 | ## The passed file handle will no longer be inheritable. |
770 | 775 | when not defined(nimInheritHandles) and declared(setInheritable): |
771 | 776 | let oshandle = when defined(windows): FileHandle getOsfhandle( |
772 | | - filehandle) else: filehandle |
| 777 | + cint filehandle) else: filehandle |
773 | 778 | if not setInheritable(oshandle, false): |
774 | 779 | return false |
775 | | - f = c_fdopen(filehandle, RawFormatOpen[mode]) |
| 780 | + f = c_fdopen(cint filehandle, RawFormatOpen[mode]) |
776 | 781 | result = f != nil |
777 | 782 |
|
778 | 783 | proc open*(filename: string, |
|
0 commit comments