summary refs log tree commit diff stats
path: root/lib/pure/streams.nim
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pure/streams.nim')
-rwxr-xr-xlib/pure/streams.nim114
1 files changed, 89 insertions, 25 deletions
diff --git a/lib/pure/streams.nim b/lib/pure/streams.nim
index dc76bd95f..5e8926e6d 100755
--- a/lib/pure/streams.nim
+++ b/lib/pure/streams.nim
@@ -19,13 +19,75 @@ proc newEIO(msg: string): ref EIO =
 type
   PStream* = ref TStream
   TStream* = object of TObject ## Stream interface that supports
-                               ## writing or reading.
-    close*: proc (s: PStream)
-    atEnd*: proc (s: PStream): bool
-    setPosition*: proc (s: PStream, pos: int)
-    getPosition*: proc (s: PStream): int
-    readData*: proc (s: PStream, buffer: pointer, bufLen: int): int
-    writeData*: proc (s: PStream, buffer: pointer, bufLen: int)
+                               ## writing or reading. Note that these fields
+                               ## here shouldn't be used directly. They are
+                               ## accessible so that a stream implementation
+                               ## can override them.
+    closeImpl*: proc (s: PStream)
+    atEndImpl*: proc (s: PStream): bool
+    setPositionImpl*: proc (s: PStream, pos: int)
+    getPositionImpl*: proc (s: PStream): int
+    readDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int): int
+    writeDataImpl*: proc (s: PStream, buffer: pointer, bufLen: int)
+    flushImpl*: proc (s: PStream)
+
+proc flush*(s: PStream) =
+  ## flushes the buffers that the stream `s` might use.
+  if not isNil(s.flushImpl): s.flushImpl(s)
+
+proc close*(s: PStream) =
+  ## closes the stream `s`.
+  if not isNil(s.closeImpl): s.closeImpl(s)
+
+proc close*(s, unused: PStream) {.deprecated.} =
+  ## closes the stream `s`.
+  s.closeImpl(s)
+
+proc atEnd*(s: PStream): bool =
+  ## checks if more data can be read from `f`. Returns true if all data has
+  ## been read.
+  result = s.atEndImpl(s)
+
+proc atEnd*(s, unused: PStream): bool {.deprecated.} =
+  ## checks if more data can be read from `f`. Returns true if all data has
+  ## been read.
+  result = s.atEndImpl(s)
+
+proc setPosition*(s: PStream, pos: int) =
+  ## sets the position `pos` of the stream `s`.
+  s.setPositionImpl(s, pos)
+
+proc setPosition*(s, unused: PStream, pos: int) {.deprecated.} =
+  ## sets the position `pos` of the stream `s`.
+  s.setPositionImpl(s, pos)
+
+proc getPosition*(s: PStream): int =
+  ## retrieves the current position in the stream `s`.
+  result = s.getPositionImpl(s)
+
+proc getPosition*(s, unused: PStream): int {.deprecated.} =
+  ## retrieves the current position in the stream `s`.
+  result = s.getPositionImpl(s)
+
+proc readData*(s: PStream, buffer: pointer, bufLen: int): int =
+  ## low level proc that reads data into an untyped `buffer` of `bufLen` size.
+  result = s.readDataImpl(s, buffer, bufLen)
+
+proc readData*(s, unused: PStream, buffer: pointer, 
+               bufLen: int): int {.deprecated.} =
+  ## low level proc that reads data into an untyped `buffer` of `bufLen` size.
+  result = s.readDataImpl(s, buffer, bufLen)
+
+proc writeData*(s: PStream, buffer: pointer, bufLen: int) =
+  ## low level proc that writes an untyped `buffer` of `bufLen` size
+  ## to the stream `s`.
+  s.writeDataImpl(s, buffer, bufLen)
+
+proc writeData*(s, unused: PStream, buffer: pointer, 
+                bufLen: int) {.deprecated.} =
+  ## low level proc that writes an untyped `buffer` of `bufLen` size
+  ## to the stream `s`.
+  s.writeDataImpl(s, buffer, bufLen)
 
 proc write*[T](s: PStream, x: T) = 
   ## generic write procedure. Writes `x` to the stream `s`. Implementation:
@@ -35,22 +97,22 @@ proc write*[T](s: PStream, x: T) =
   ##     s.writeData(s, addr(x), sizeof(x))
   var y: T
   shallowCopy(y, x)
-  s.writeData(s, addr(y), sizeof(y))
+  writeData(s, addr(y), sizeof(y))
 
 proc write*(s: PStream, x: string) = 
   ## writes the string `x` to the the stream `s`. No length field or 
   ## terminating zero is written.
-  s.writeData(s, cstring(x), x.len)
+  writeData(s, cstring(x), x.len)
 
 proc read[T](s: PStream, result: var T) = 
   ## generic read procedure. Reads `result` from the stream `s`.
-  if s.readData(s, addr(result), sizeof(T)) != sizeof(T):
+  if readData(s, addr(result), sizeof(T)) != sizeof(T):
     raise newEIO("cannot read from stream")
 
 proc readChar*(s: PStream): char =
   ## reads a char from the stream `s`. Raises `EIO` if an error occured.
   ## Returns '\0' as an EOF marker.
-  discard s.readData(s, addr(result), sizeof(result))
+  discard readData(s, addr(result), sizeof(result))
 
 proc readBool*(s: PStream): bool = 
   ## reads a bool from the stream `s`. Raises `EIO` if an error occured.
@@ -84,14 +146,14 @@ proc readStr*(s: PStream, length: int): TaintedString =
   ## reads a string of length `length` from the stream `s`. Raises `EIO` if 
   ## an error occured.
   result = newString(length).TaintedString
-  var L = s.readData(s, addr(string(result)[0]), length)
+  var L = readData(s, addr(string(result)[0]), length)
   if L != length: setLen(result.string, L)
 
 proc readLine*(s: PStream): TaintedString =
   ## Reads a line from a stream `s`. Note: This is not very efficient. Raises 
   ## `EIO` if an error occured.
   result = TaintedString""
-  while not s.atEnd(s): 
+  while not atEnd(s): 
     var c = readChar(s)
     if c == '\c': 
       c = readChar(s)
@@ -140,12 +202,12 @@ proc newStringStream*(s: string = ""): PStringStream =
   new(result)
   result.data = s
   result.pos = 0
-  result.close = ssClose
-  result.atEnd = ssAtEnd
-  result.setPosition = ssSetPosition
-  result.getPosition = ssGetPosition
-  result.readData = ssReadData
-  result.writeData = ssWriteData
+  result.closeImpl = ssClose
+  result.atEndImpl = ssAtEnd
+  result.setPositionImpl = ssSetPosition
+  result.getPositionImpl = ssGetPosition
+  result.readDataImpl = ssReadData
+  result.writeDataImpl = ssWriteData
 
 type
   PFileStream* = ref TFileStream ## a stream that encapsulates a `TFile`
@@ -153,6 +215,7 @@ type
     f: TFile
 
 proc fsClose(s: PStream) = close(PFileStream(s).f)
+proc fsFlush(s: PStream) = flushFile(PFileStream(s).f)
 proc fsAtEnd(s: PStream): bool = return EndOfFile(PFileStream(s).f)
 proc fsSetPosition(s: PStream, pos: int) = setFilePos(PFileStream(s).f, pos)
 proc fsGetPosition(s: PStream): int = return int(getFilePos(PFileStream(s).f))
@@ -168,12 +231,13 @@ proc newFileStream*(f: TFile): PFileStream =
   ## creates a new stream from the file `f`.
   new(result)
   result.f = f
-  result.close = fsClose
-  result.atEnd = fsAtEnd
-  result.setPosition = fsSetPosition
-  result.getPosition = fsGetPosition
-  result.readData = fsReadData
-  result.writeData = fsWriteData
+  result.closeImpl = fsClose
+  result.atEndImpl = fsAtEnd
+  result.setPositionImpl = fsSetPosition
+  result.getPositionImpl = fsGetPosition
+  result.readDataImpl = fsReadData
+  result.writeDataImpl = fsWriteData
+  result.flushImpl = fsFlush
 
 proc newFileStream*(filename: string, mode: TFileMode): PFileStream = 
   ## creates a new stream from the file named `filename` with the mode `mode`.
/a> 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
25 02:14:38 +0000

Added auto reconnect, and /reconnect command to control interval' href='/danisanti/profani-tty/commit/src/chat_session.c?id=921f026cba6bae0f7450eea0c32b5ea36716cd0a'>921f026c ^
5ce97728 ^


3d6ebf48 ^
5ce97728 ^



270ac035 ^
5ce97728 ^




270ac035 ^
5792cd59 ^
270ac035 ^
3d6ebf48 ^
5792cd59 ^
bcf0628a ^
5792cd59 ^
5ce97728 ^









921f026c ^
5ce97728 ^









5ce97728 ^



f8de2823 ^

5b859927 ^
d2be6929 ^
f8de2823 ^
5b859927 ^
f8de2823 ^

d339004f ^
270ac035 ^
5ce97728 ^



5ce97728 ^





270ac035 ^





270ac035 ^





5792cd59 ^




921f026c ^
5792cd59 ^
4be7833e ^
270ac035 ^
5792cd59 ^



270ac035 ^
5792cd59 ^



5792cd59 ^





5ce97728 ^
270ac035 ^
5ce97728 ^



5ce97728 ^





270ac035 ^




921f026c ^
270ac035 ^



5ce97728 ^
3c82fb28 ^
f8de2823 ^
5b859927 ^
d339004f ^
5b859927 ^
d339004f ^
5b859927 ^
d339004f ^
5b859927 ^
f8de2823 ^

3c82fb28 ^





921f026c ^
3c82fb28 ^



f8de2823 ^


5b859927 ^
9e092843 ^
5ce97728 ^



9e092843 ^
f8de2823 ^
f8de2823 ^
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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
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