summary refs log tree commit diff stats
path: root/lib/pure
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2014-09-06 19:50:11 +0100
committerDominik Picheta <dominikpicheta@googlemail.com>2014-09-06 19:50:11 +0100
commitcb8a25b3d1ed5ca873b4b19f157b84e7289ccf9b (patch)
tree1e2ba492cf60fb2df02a986868da8265567dc30d /lib/pure
parentbd542ebea3ec01eb269fe6fa118a8648d63f7097 (diff)
downloadNim-cb8a25b3d1ed5ca873b4b19f157b84e7289ccf9b.tar.gz
Added asyncfile.readLine and async stdin/out/err. Ref #1487.
Diffstat (limited to 'lib/pure')
-rw-r--r--lib/pure/asyncfile.nim45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/pure/asyncfile.nim b/lib/pure/asyncfile.nim
index 009485ed9..6c8a87184 100644
--- a/lib/pure/asyncfile.nim
+++ b/lib/pure/asyncfile.nim
@@ -34,7 +34,26 @@ type
     fd: TAsyncFd
     offset: int64
 
+# TODO: These will be nil in other threads?
+var
+  asyncStdin* {.threadvar.}: AsyncFile ## Asynchronous stdin handle
+  asyncStdout* {.threadvar.}: AsyncFile ## Asynchronous stdout handle
+  asyncStderr* {.threadvar.}: AsyncFile ## Asynchronous stderr handle
+
 when defined(windows):
+  asyncStdin = AsyncFile(
+      fd: getStdHandle(STD_INPUT_HANDLE).TAsyncFd,
+      offset: 0
+    )
+  asyncStdout = AsyncFile(
+      fd: getStdHandle(STD_OUTPUT_HANDLE).TAsyncFd,
+      offset: 0
+    )
+  asyncStderr = AsyncFile(
+      fd: getStdHandle(STD_ERROR_HANDLE).TAsyncFd,
+      offset: 0
+    )
+
   proc getDesiredAccess(mode: TFileMode): int32 =
     case mode
     of fmRead:
@@ -54,6 +73,19 @@ when defined(windows):
       else:
         CREATE_NEW
 else:
+  asyncStdin = AsyncFile(
+      fd: STDIN_FILENO.TAsyncFd,
+      offset: 0
+    )
+  asyncStdout = AsyncFile(
+      fd: STDOUT_FILENO.TAsyncFd,
+      offset: 0
+    )
+  asyncStderr = AsyncFile(
+      fd: STDERR_FILENO.TAsyncFd,
+      offset: 0
+    )
+
   proc getPosixFlags(mode: TFileMode): cint =
     case mode
     of fmRead:
@@ -201,6 +233,19 @@ proc read*(f: AsyncFile, size: int): Future[string] =
   
   return retFuture
 
+proc readLine*(f: AsyncFile): Future[string] {.async.} =
+  ## Reads a single line from the specified file asynchronously.
+  result = ""
+  while true:
+    var c = await read(f, 1)
+    if c[0] == '\c':
+      c = await read(f, 1)
+      break
+    if c[0] == '\L' or c == "":
+      break
+    else:
+      result.add(c)
+
 proc getFilePos*(f: AsyncFile): int64 =
   ## Retrieves the current position of the file pointer that is
   ## used to read from the specified file. The file's first byte has the
href='/ahoang/Nim/commit/compiler/saturate.nim?h=devel&id=92b8fac94a7243cde785d985db3fd86b6025b079'>92b8fac94 ^
4fbba0a65 ^



















d68181246 ^
4fbba0a65 ^



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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