llstream.nim 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. #
  2. #
  3. # The Nim Compiler
  4. # (c) Copyright 2012 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## Low-level streams for high performance.
  10. import
  11. pathutils
  12. when defined(nimPreviewSlimSystem):
  13. import std/syncio
  14. # support `useGnuReadline`, `useLinenoise` for backwards compatibility
  15. const hasRstdin = (defined(nimUseLinenoise) or defined(useLinenoise) or defined(useGnuReadline)) and
  16. not defined(windows)
  17. when hasRstdin: import std/rdstdin
  18. type
  19. TLLRepl* = proc (s: PLLStream, buf: pointer, bufLen: int): int
  20. OnPrompt* = proc() {.closure.}
  21. TLLStreamKind* = enum # enum of different stream implementations
  22. llsNone, # null stream: reading and writing has no effect
  23. llsString, # stream encapsulates a string
  24. llsFile, # stream encapsulates a file
  25. llsStdIn # stream encapsulates stdin
  26. TLLStream* = object of RootObj
  27. kind*: TLLStreamKind # accessible for low-level access (lexbase uses this)
  28. f*: File
  29. s*: string
  30. rd*, wr*: int # for string streams
  31. lineOffset*: int # for fake stdin line numbers
  32. repl*: TLLRepl # gives stdin control to clients
  33. onPrompt*: OnPrompt
  34. PLLStream* = ref TLLStream
  35. proc llStreamOpen*(data: sink string): PLLStream =
  36. PLLStream(kind: llsString, s: data)
  37. proc llStreamOpen*(f: File): PLLStream =
  38. PLLStream(kind: llsFile, f: f)
  39. proc llStreamOpen*(filename: AbsoluteFile, mode: FileMode): PLLStream =
  40. result = PLLStream(kind: llsFile)
  41. if not open(result.f, filename.string, mode): result = nil
  42. proc llStreamOpen*(): PLLStream =
  43. PLLStream(kind: llsNone)
  44. proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int
  45. proc llStreamOpenStdIn*(r: TLLRepl = llReadFromStdin, onPrompt: OnPrompt = nil): PLLStream =
  46. PLLStream(kind: llsStdIn, s: "", lineOffset: -1, repl: r, onPrompt: onPrompt)
  47. proc llStreamClose*(s: PLLStream) =
  48. case s.kind
  49. of llsNone, llsString, llsStdIn:
  50. discard
  51. of llsFile:
  52. close(s.f)
  53. when not declared(readLineFromStdin):
  54. # fallback implementation:
  55. proc readLineFromStdin(prompt: string, line: var string): bool =
  56. stdout.write(prompt)
  57. stdout.flushFile()
  58. result = readLine(stdin, line)
  59. if not result:
  60. stdout.write("\n")
  61. quit(0)
  62. proc endsWith*(x: string, s: set[char]): bool =
  63. var i = x.len-1
  64. while i >= 0 and x[i] == ' ': dec(i)
  65. if i >= 0 and x[i] in s:
  66. result = true
  67. else:
  68. result = false
  69. const
  70. LineContinuationOprs = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^',
  71. '|', '%', '&', '$', '@', '~', ','}
  72. AdditionalLineContinuationOprs = {'#', ':', '='}
  73. proc endsWithOpr*(x: string): bool =
  74. result = x.endsWith(LineContinuationOprs)
  75. proc continueLine(line: string, inTripleString: bool): bool {.inline.} =
  76. result = inTripleString or line.len > 0 and (
  77. line[0] == ' ' or
  78. line.endsWith(LineContinuationOprs+AdditionalLineContinuationOprs))
  79. proc countTriples(s: string): int =
  80. result = 0
  81. var i = 0
  82. while i+2 < s.len:
  83. if s[i] == '"' and s[i+1] == '"' and s[i+2] == '"':
  84. inc result
  85. inc i, 2
  86. inc i
  87. proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int =
  88. s.s = ""
  89. s.rd = 0
  90. var line = newStringOfCap(120)
  91. var triples = 0
  92. while readLineFromStdin(if s.s.len == 0: ">>> " else: "... ", line):
  93. s.s.add(line)
  94. s.s.add("\n")
  95. inc triples, countTriples(line)
  96. if not continueLine(line, (triples and 1) == 1): break
  97. inc(s.lineOffset)
  98. result = min(bufLen, s.s.len - s.rd)
  99. if result > 0:
  100. copyMem(buf, addr(s.s[s.rd]), result)
  101. inc(s.rd, result)
  102. proc llStreamRead*(s: PLLStream, buf: pointer, bufLen: int): int =
  103. case s.kind
  104. of llsNone:
  105. result = 0
  106. of llsString:
  107. result = min(bufLen, s.s.len - s.rd)
  108. if result > 0:
  109. copyMem(buf, addr(s.s[0 + s.rd]), result)
  110. inc(s.rd, result)
  111. of llsFile:
  112. result = readBuffer(s.f, buf, bufLen)
  113. of llsStdIn:
  114. if s.onPrompt!=nil: s.onPrompt()
  115. result = s.repl(s, buf, bufLen)
  116. proc llStreamReadLine*(s: PLLStream, line: var string): bool =
  117. setLen(line, 0)
  118. case s.kind
  119. of llsNone:
  120. result = true
  121. of llsString:
  122. while s.rd < s.s.len:
  123. case s.s[s.rd]
  124. of '\r':
  125. inc(s.rd)
  126. if s.s[s.rd] == '\n': inc(s.rd)
  127. break
  128. of '\n':
  129. inc(s.rd)
  130. break
  131. else:
  132. line.add(s.s[s.rd])
  133. inc(s.rd)
  134. result = line.len > 0 or s.rd < s.s.len
  135. of llsFile:
  136. result = readLine(s.f, line)
  137. of llsStdIn:
  138. result = readLine(stdin, line)
  139. proc llStreamWrite*(s: PLLStream, data: string) =
  140. case s.kind
  141. of llsNone, llsStdIn:
  142. discard
  143. of llsString:
  144. s.s.add(data)
  145. inc(s.wr, data.len)
  146. of llsFile:
  147. write(s.f, data)
  148. proc llStreamWriteln*(s: PLLStream, data: string) =
  149. llStreamWrite(s, data)
  150. llStreamWrite(s, "\n")
  151. proc llStreamWrite*(s: PLLStream, data: char) =
  152. var c: char
  153. case s.kind
  154. of llsNone, llsStdIn:
  155. discard
  156. of llsString:
  157. s.s.add(data)
  158. inc(s.wr)
  159. of llsFile:
  160. c = data
  161. discard writeBuffer(s.f, addr(c), sizeof(c))
  162. proc llStreamWrite*(s: PLLStream, buf: pointer, buflen: int) =
  163. case s.kind
  164. of llsNone, llsStdIn:
  165. discard
  166. of llsString:
  167. if buflen > 0:
  168. setLen(s.s, s.s.len + buflen)
  169. copyMem(addr(s.s[0 + s.wr]), buf, buflen)
  170. inc(s.wr, buflen)
  171. of llsFile:
  172. discard writeBuffer(s.f, buf, buflen)
  173. proc llStreamReadAll*(s: PLLStream): string =
  174. const
  175. bufSize = 2048
  176. case s.kind
  177. of llsNone, llsStdIn:
  178. result = ""
  179. of llsString:
  180. if s.rd == 0: result = s.s
  181. else: result = substr(s.s, s.rd)
  182. s.rd = s.s.len
  183. of llsFile:
  184. result = newString(bufSize)
  185. var bytes = readBuffer(s.f, addr(result[0]), bufSize)
  186. var i = bytes
  187. while bytes == bufSize:
  188. setLen(result, i + bufSize)
  189. bytes = readBuffer(s.f, addr(result[i + 0]), bufSize)
  190. inc(i, bytes)
  191. setLen(result, i)