Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,505 changes: 1,505 additions & 0 deletions lib/std/streams.nim

Large diffs are not rendered by default.

67 changes: 67 additions & 0 deletions lib/std/syncio.nim
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
when defined(windows):
import "../../vendor/errorcodes/src" / errorcodes_windows
else:
import "../../vendor/errorcodes/src" / errorcodes_posix


type
CFile {.importc: "FILE", header: "<stdio.h>".} = object
Expand All @@ -20,6 +25,13 @@ type
## at the end. If the file does not exist, it
## will be created.

FileSeekPos* = enum ## Position relative to which seek should happen.
# The values are ordered so that they match with stdio
# SEEK_SET, SEEK_CUR and SEEK_END respectively.
fspSet ## Seek to absolute value
fspCur ## Seek relative to current position
fspEnd ## Seek relative to end

var
stdin* {.importc: "stdin", header: "<stdio.h>".}: File
stdout* {.importc: "stdout", header: "<stdio.h>".}: File
Expand All @@ -32,6 +44,43 @@ proc c_fwrite(buf: pointer; size, n: uint; f: File): uint {.
proc c_fread(buf: pointer; size, n: uint; f: File): uint {.
importc: "fread", header: "<stdio.h>".}

proc c_fgetc(stream: File): cint {.
importc: "fgetc", header: "<stdio.h>", tags: [].}
proc c_ungetc(c: cint, f: File): cint {.
importc: "ungetc", header: "<stdio.h>", tags: [].}


when defined(windows):
when not defined(amd64):
proc c_fseek(f: File, offset: int64, whence: cint): cint {.
importc: "fseek", header: "<stdio.h>", tags: [].}
proc c_ftell(f: File): int64 {.
importc: "ftell", header: "<stdio.h>", tags: [].}
else:
proc c_fseek(f: File, offset: int64, whence: cint): cint {.
importc: "_fseeki64", header: "<stdio.h>", tags: [].}
when defined(tcc):
proc c_fsetpos(f: File, pos: var int64): int32 {.
importc: "fsetpos", header: "<stdio.h>", tags: [].}
proc c_fgetpos(f: File, pos: var int64): int32 {.
importc: "fgetpos", header: "<stdio.h>", tags: [].}
proc c_telli64(f: cint): int64 {.
importc: "_telli64", header: "<io.h>", tags: [].}
proc c_ftell(f: File): int64 =
# Taken from https://pt.osdn.net/projects/mingw/scm/git/mingw-org-wsl/blobs/5.4-trunk/mingwrt/mingwex/stdio/ftelli64.c
result = -1'i64
var pos: int64
if c_fgetpos(f, pos) == 0 and c_fsetpos(f, pos) == 0:
result = c_telli64(c_fileno(f))
else:
proc c_ftell(f: File): int64 {.
importc: "_ftelli64", header: "<stdio.h>", tags: [].}
else:
proc c_fseek(f: File, offset: int64, whence: cint): cint {.
importc: "fseeko", header: "<stdio.h>", tags: [].}
proc c_ftell(f: File): int64 {.
importc: "ftello", header: "<stdio.h>", tags: [].}

proc fprintf(f: File; fmt: cstring) {.varargs, importc: "fprintf", header: "<stdio.h>".}

proc write*(f: File; s: string) =
Expand Down Expand Up @@ -167,3 +216,21 @@ proc tryWriteFile*(file, content: string): bool =
result = false

proc flushFile*(f: File) {.importc: "fflush", header: "<stdio.h>".}

proc endOfFile*(f: File): bool {.tags: [].} =
## Returns true if `f` is at the end.
var c = c_fgetc(f)
discard c_ungetc(c, f)
return c < 0'i32

proc getFilePos*(f: File): int64 {.raises.} =
## Retrieves the current position of the file pointer that is used to
## read from the file `f`. The file's first byte has the index zero.
result = c_ftell(f)
if result < 0: raise IOError #raiseEIO("cannot retrieve file position")

proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.sideEffect, raises.} =
## Sets the position of the file pointer that is used for read/write
## operations. The file's first byte has the index zero.
if c_fseek(f, pos, cint(relativeTo)) != 0:
raise IOError # raiseEIO("cannot set file position")
1 change: 1 addition & 0 deletions lib/std/system/defaults.nim
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ template default*[T: enum](x: typedesc[T]): T = low(T)

template default*[T: ptr](x: typedesc[T]): T = T(nil)
template default*[T: ref](x: typedesc[T]): T = T(nil)
template default*[T: proc](x: typedesc[T]): T = T(nil)
template default*(x: typedesc[pointer]): pointer = nil

proc default*[T: distinct](x: typedesc[T]): T {.magic: DefaultDistinct.}
Expand Down
4 changes: 2 additions & 2 deletions tests/nimony/ffi/tpacked.nim.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,8 +433,8 @@ typedef struct __attribute__ ((__packed__)) Foo_0_tpakvxko41{
NC8 c_0_tpakvxko41;
NI x_0_tpakvxko41;}
Foo_0_tpakvxko41;
extern void write_0_syn1lfpjv(FILE* f_4, string_0_sysvq0asl s_0);
extern void write_7_syn1lfpjv(FILE* f_11, NC8 c_1);
extern void write_0_syn1lfpjv(FILE* f_7, string_0_sysvq0asl s_0);
extern void write_7_syn1lfpjv(FILE* f_14, NC8 c_2);
extern void quit_0_syn1lfpjv(NI value_1);
Foo_0_tpakvxko41 x_1_tpakvxko41;
int cmdCount;
Expand Down
16 changes: 8 additions & 8 deletions tests/nimony/iter/tforloops1.nif
Original file line number Diff line number Diff line change
Expand Up @@ -235,12 +235,12 @@
(elif 2
(eq
(i +64) ~2 j.3 3 +2) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 11,81,tests/nimony/iter/tforloops1.nim "left the loop!")) ,2
(cmd write.7.syn1lfpjv 6 stdout.0.syn1lfpjv 14 '\0A')) ,1
(break .)))) 2,122,lib/std/syncio.nim
(break .)))) 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,83,tests/nimony/iter/tforloops1.nim "A i ")) 2,1
Expand Down Expand Up @@ -279,7 +279,7 @@
(stmts 4
(asgn ~4 sum.0 6
(add
(i +64) ~4 sum.0 2 a.5)))) 2,122,lib/std/syncio.nim
(i +64) ~4 sum.0 2 a.5)))) 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.2.syn1lfpjv 6 stdout.0.syn1lfpjv 11,92,tests/nimony/iter/tforloops1.nim sum.0)) ,2
Expand All @@ -304,7 +304,7 @@
(let :i.10 . . 19,43,lib/std/system/openarrays.nim
(mut
(i -1)) .)) ~2,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.2.syn1lfpjv 6 stdout.0.syn1lfpjv 9,97,tests/nimony/iter/tforloops1.nim
Expand All @@ -328,7 +328,7 @@
(elif
(true) ~1,1
(stmts
(break 6 myblock.0)))) 2,122,lib/std/syncio.nim
(break 6 myblock.0)))) 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 11,109,tests/nimony/iter/tforloops1.nim "leaving myblock")) ,2
Expand All @@ -339,7 +339,7 @@
(if 3
(elif
(not 9,111,tests/nimony/iter/tforloops1.nim x.3) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand Down Expand Up @@ -380,7 +380,7 @@
(unpackflat
(let :p.0 . . 13,~116
(i -1) .)) ~2,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.2.syn1lfpjv 6 stdout.0.syn1lfpjv 9,120,tests/nimony/iter/tforloops1.nim p.0)) ,2
Expand Down Expand Up @@ -432,7 +432,7 @@
(let :y.1 . . 19,43,lib/std/system/openarrays.nim
(mut 22,35,lib/std/system/basic_types.nim
(bool)) .)) ~2,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.1.syn1lfpjv 6 stdout.0.syn1lfpjv 13,133,tests/nimony/iter/tforloops1.nim
Expand Down
18 changes: 9 additions & 9 deletions tests/nimony/object/tcaseobject.nif
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
(eq
(bool) ~2 a.0.tcauiaoif 3
(false))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -68,7 +68,7 @@
(not 9,21,tests/nimony/object/tcaseobject.nim
(eq
(i +64) ~2 b.0.tcauiaoif 3 +123)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -91,7 +91,7 @@
(bool) ~3
(dot ~3 foo.0.tcauiaoif x.0.tcauiaoif +0) 3
(true))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -106,7 +106,7 @@
(elif
(not 9,25,tests/nimony/object/tcaseobject.nim
(infix ==.20.sysvq0asl ~2 c.0.tcauiaoif 3 "abc")) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -124,7 +124,7 @@
(eq
(bool) ~2 d.0.tcauiaoif 3
(false))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -149,7 +149,7 @@
(bool) ~3
(dot ~3 bar.0.tcauiaoif x.1.tcauiaoif +0) 3
(false))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -171,7 +171,7 @@
(eq
(bool) ~2 e.0.tcauiaoif 3
(true))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -188,7 +188,7 @@
(bool) ~3
(dot ~4 bar2.0.tcauiaoif x.1.tcauiaoif +0) 3
(true))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand Down Expand Up @@ -264,7 +264,7 @@
(var :b.0 . . string.0.sysvq0asl 4 ""))) ,5
(of
(ranges 3 kc.0.tcauiaoif) 7
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.2.syn1lfpjv 6 stdout.0.syn1lfpjv 14,64,tests/nimony/object/tcaseobject.nim +3)) ,2
Expand Down
2 changes: 1 addition & 1 deletion tests/nimony/pragmas/tpushpop.nif
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
(eq
(i +64) ~6
(call ~12 c_fpclassify.1.tpu1210461 1 +1.0) 3 c_fpNormal.0.tpu1210461)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand Down
26 changes: 13 additions & 13 deletions tests/nimony/sets/tsets.nif
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
(not 10,9,tests/nimony/sets/tsets.nim
(ltset
(set Foo.0.tseflljd71) ~3 s1.0.tseflljd71 2 s.0.tseflljd71)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -56,7 +56,7 @@
(setconstr 2,~6
(set 1 Foo.0.tseflljd71) 1
(range A.0.tseflljd71 3 C.0.tseflljd71)))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -73,7 +73,7 @@
(setconstr 2,~7
(set 1 Foo.0.tseflljd71) 1
(range A.0.tseflljd71 3 C.0.tseflljd71)))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -91,7 +91,7 @@
(set Foo.0.tseflljd71) ~2 s.0.tseflljd71 2 s1.0.tseflljd71) 3
(setconstr ~3,~8
(set 1 Foo.0.tseflljd71) 1 D.0.tseflljd71))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -105,7 +105,7 @@
(not 10,15,tests/nimony/sets/tsets.nim
(leset
(set Foo.0.tseflljd71) ~3 s1.0.tseflljd71 3 s.0.tseflljd71)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -121,7 +121,7 @@
(i +64) ~4
(card
(set Foo.0.tseflljd71) 1 s.0.tseflljd71) 3 +4)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -137,7 +137,7 @@
(i +64) ~5
(card
(set Foo.0.tseflljd71) 1 s1.0.tseflljd71) 3 +3)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -158,7 +158,7 @@
(i +64) ~5
(card
(set Foo.0.tseflljd71) 1 s1.0.tseflljd71) 3 +4)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand Down Expand Up @@ -204,7 +204,7 @@
(card
(set
(c +8)) 1 sss.0) 3 +0)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -226,7 +226,7 @@
(card
(set
(c +8)) 1 sss.0) 3 +27)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -246,7 +246,7 @@
(card
(set
(c +8)) 1 sss.0) 3 +26)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand All @@ -266,7 +266,7 @@
(card
(set
(c +8)) 1 sss.0) 3 +26)) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand Down Expand Up @@ -313,7 +313,7 @@
(setconstr ,~20
(set 1
(c +8)) 1 'a'))) ~1,1
(stmts 2,122,lib/std/syncio.nim
(stmts 2,171,lib/std/syncio.nim
(stmts 2,1
(stmts
(cmd write.0.syn1lfpjv 6 stdout.0.syn1lfpjv 9,5,lib/std/assertions.nim "\5BAssertion Failure\5D ")) 2,1
Expand Down
Loading
Loading