llstream.nim 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. # support '-d:useGnuReadline' for backwards compatibility:
  13. when not defined(windows) and (defined(useGnuReadline) or defined(useLinenoise)):
  14. import rdstdin
  15. type
  16. TLLRepl* = proc (s: PLLStream, buf: pointer, bufLen: int): int
  17. TLLStreamKind* = enum # enum of different stream implementations
  18. llsNone, # null stream: reading and writing has no effect
  19. llsString, # stream encapsulates a string
  20. llsFile, # stream encapsulates a file
  21. llsStdIn # stream encapsulates stdin
  22. TLLStream* = object of RootObj
  23. kind*: TLLStreamKind # accessible for low-level access (lexbase uses this)
  24. f*: File
  25. s*: string
  26. rd*, wr*: int # for string streams
  27. lineOffset*: int # for fake stdin line numbers
  28. repl*: TLLRepl # gives stdin control to clients
  29. PLLStream* = ref TLLStream
  30. proc llStreamOpen*(data: string): PLLStream =
  31. new(result)
  32. result.s = data
  33. result.kind = llsString
  34. proc llStreamOpen*(f: File): PLLStream =
  35. new(result)
  36. result.f = f
  37. result.kind = llsFile
  38. proc llStreamOpen*(filename: AbsoluteFile, mode: FileMode): PLLStream =
  39. new(result)
  40. result.kind = llsFile
  41. if not open(result.f, filename.string, mode): result = nil
  42. proc llStreamOpen*(): PLLStream =
  43. new(result)
  44. result.kind = llsNone
  45. proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int
  46. proc llStreamOpenStdIn*(r: TLLRepl = llReadFromStdin): PLLStream =
  47. new(result)
  48. result.kind = llsStdIn
  49. result.s = ""
  50. result.lineOffset = -1
  51. result.repl = r
  52. proc llStreamClose*(s: PLLStream) =
  53. case s.kind
  54. of llsNone, llsString, llsStdIn:
  55. discard
  56. of llsFile:
  57. close(s.f)
  58. when not declared(readLineFromStdin):
  59. # fallback implementation:
  60. proc readLineFromStdin(prompt: string, line: var string): bool =
  61. stderr.write(prompt)
  62. result = readLine(stdin, line)
  63. if not result:
  64. stderr.write("\n")
  65. quit(0)
  66. proc endsWith*(x: string, s: set[char]): bool =
  67. var i = x.len-1
  68. while i >= 0 and x[i] == ' ': dec(i)
  69. if i >= 0 and x[i] in s:
  70. result = true
  71. const
  72. LineContinuationOprs = {'+', '-', '*', '/', '\\', '<', '>', '!', '?', '^',
  73. '|', '%', '&', '$', '@', '~', ','}
  74. AdditionalLineContinuationOprs = {'#', ':', '='}
  75. proc endsWithOpr*(x: string): bool =
  76. result = x.endsWith(LineContinuationOprs)
  77. proc continueLine(line: string, inTripleString: bool): bool {.inline.} =
  78. result = inTripleString or line.len > 0 and (
  79. line[0] == ' ' or
  80. line.endsWith(LineContinuationOprs+AdditionalLineContinuationOprs))
  81. proc countTriples(s: string): int =
  82. var i = 0
  83. while i+2 < s.len:
  84. if s[i] == '"' and s[i+1] == '"' and s[i+2] == '"':
  85. inc result
  86. inc i, 2
  87. inc i
  88. proc llReadFromStdin(s: PLLStream, buf: pointer, bufLen: int): int =
  89. s.s = ""
  90. s.rd = 0
  91. var line = newStringOfCap(120)
  92. var triples = 0
  93. while readLineFromStdin(if s.s.len == 0: ">>> " else: "... ", line):
  94. add(s.s, line)
  95. add(s.s, "\n")
  96. inc triples, countTriples(line)
  97. if not continueLine(line, (triples and 1) == 1): break
  98. inc(s.lineOffset)
  99. result = min(bufLen, len(s.s) - s.rd)
  100. if result > 0:
  101. copyMem(buf, addr(s.s[s.rd]), result)
  102. inc(s.rd, result)
  103. proc llStreamRead*(s: PLLStream, buf: pointer, bufLen: int): int =
  104. case s.kind
  105. of llsNone:
  106. result = 0
  107. of llsString:
  108. result = min(bufLen, len(s.s) - s.rd)
  109. if result > 0:
  110. copyMem(buf, addr(s.s[0 + s.rd]), result)
  111. inc(s.rd, result)
  112. of llsFile:
  113. result = readBuffer(s.f, buf, bufLen)
  114. of llsStdIn:
  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 < len(s.s):
  123. case s.s[s.rd]
  124. of '\x0D':
  125. inc(s.rd)
  126. if s.s[s.rd] == '\x0A': inc(s.rd)
  127. break
  128. of '\x0A':
  129. inc(s.rd)
  130. break
  131. else:
  132. add(line, s.s[s.rd])
  133. inc(s.rd)
  134. result = line.len > 0 or s.rd < len(s.s)
  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. add(s.s, data)
  145. inc(s.wr, len(data))
  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. add(s.s, 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, len(s.s) + 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 = len(s.s)
  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)