sysio.nim 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2013 Andreas Rumpf
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. # Nim's standard IO library. It contains high-performance
  10. # routines for reading and writing data to (buffered) files or
  11. # TTYs.
  12. {.push debugger:off .} # the user does not want to trace a part
  13. # of the standard library!
  14. when defined(windows):
  15. proc c_fdopen(filehandle: cint, mode: cstring): File {.
  16. importc: "_fdopen", header: "<stdio.h>".}
  17. else:
  18. proc c_fdopen(filehandle: cint, mode: cstring): File {.
  19. importc: "fdopen", header: "<stdio.h>".}
  20. proc c_fputs(c: cstring, f: File): cint {.
  21. importc: "fputs", header: "<stdio.h>", tags: [WriteIOEffect].}
  22. proc c_fgets(c: cstring, n: cint, f: File): cstring {.
  23. importc: "fgets", header: "<stdio.h>", tags: [ReadIOEffect].}
  24. proc c_fgetc(stream: File): cint {.
  25. importc: "fgetc", header: "<stdio.h>", tags: [ReadIOEffect].}
  26. proc c_ungetc(c: cint, f: File): cint {.
  27. importc: "ungetc", header: "<stdio.h>", tags: [].}
  28. proc c_putc(c: cint, stream: File): cint {.
  29. importc: "putc", header: "<stdio.h>", tags: [WriteIOEffect].}
  30. proc c_fflush(f: File): cint {.
  31. importc: "fflush", header: "<stdio.h>".}
  32. proc c_fclose(f: File): cint {.
  33. importc: "fclose", header: "<stdio.h>".}
  34. proc c_clearerr(f: File) {.
  35. importc: "clearerr", header: "<stdio.h>".}
  36. proc c_feof(f: File): cint {.
  37. importc: "feof", header: "<stdio.h>".}
  38. when not declared(c_fwrite):
  39. proc c_fwrite(buf: pointer, size, n: csize, f: File): cint {.
  40. importc: "fwrite", header: "<stdio.h>".}
  41. # C routine that is used here:
  42. proc c_fread(buf: pointer, size, n: csize, f: File): csize {.
  43. importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
  44. when defined(windows):
  45. when not defined(amd64):
  46. proc c_fseek(f: File, offset: int64, whence: cint): cint {.
  47. importc: "fseek", header: "<stdio.h>", tags: [].}
  48. proc c_ftell(f: File): int64 {.
  49. importc: "ftell", header: "<stdio.h>", tags: [].}
  50. else:
  51. proc c_fseek(f: File, offset: int64, whence: cint): cint {.
  52. importc: "_fseeki64", header: "<stdio.h>", tags: [].}
  53. proc c_ftell(f: File): int64 {.
  54. importc: "_ftelli64", header: "<stdio.h>", tags: [].}
  55. else:
  56. proc c_fseek(f: File, offset: int64, whence: cint): cint {.
  57. importc: "fseeko", header: "<stdio.h>", tags: [].}
  58. proc c_ftell(f: File): int64 {.
  59. importc: "ftello", header: "<stdio.h>", tags: [].}
  60. proc c_ferror(f: File): cint {.
  61. importc: "ferror", header: "<stdio.h>", tags: [].}
  62. proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize): cint {.
  63. importc: "setvbuf", header: "<stdio.h>", tags: [].}
  64. proc raiseEIO(msg: string) {.noinline, noreturn.} =
  65. sysFatal(IOError, msg)
  66. proc raiseEOF() {.noinline, noreturn.} =
  67. sysFatal(EOFError, "EOF reached")
  68. proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".}
  69. when not defined(NimScript):
  70. var
  71. errno {.importc, header: "<errno.h>".}: cint ## error variable
  72. proc checkErr(f: File) =
  73. when not defined(NimScript):
  74. if c_ferror(f) != 0:
  75. let msg = "errno: " & $errno & " `" & $strerror(errno) & "`"
  76. c_clearerr(f)
  77. raiseEIO(msg)
  78. else:
  79. # shouldn't happen
  80. quit(1)
  81. {.push stackTrace:off, profiler:off.}
  82. proc readBuffer(f: File, buffer: pointer, len: Natural): int =
  83. result = c_fread(buffer, 1, len, f)
  84. if result != len: checkErr(f)
  85. proc readBytes(f: File, a: var openArray[int8|uint8], start, len: Natural): int =
  86. result = readBuffer(f, addr(a[start]), len)
  87. proc readChars(f: File, a: var openArray[char], start, len: Natural): int =
  88. if (start + len) > len(a):
  89. raiseEIO("buffer overflow: (start+len) > length of openarray buffer")
  90. result = readBuffer(f, addr(a[start]), len)
  91. proc write(f: File, c: cstring) =
  92. discard c_fputs(c, f)
  93. checkErr(f)
  94. proc writeBuffer(f: File, buffer: pointer, len: Natural): int =
  95. result = c_fwrite(buffer, 1, len, f)
  96. checkErr(f)
  97. proc writeBytes(f: File, a: openArray[int8|uint8], start, len: Natural): int =
  98. var x = cast[ptr UncheckedArray[int8]](a)
  99. result = writeBuffer(f, addr(x[int(start)]), len)
  100. proc writeChars(f: File, a: openArray[char], start, len: Natural): int =
  101. var x = cast[ptr UncheckedArray[int8]](a)
  102. result = writeBuffer(f, addr(x[int(start)]), len)
  103. proc write(f: File, s: string) =
  104. if writeBuffer(f, cstring(s), s.len) != s.len:
  105. raiseEIO("cannot write string to file")
  106. {.pop.}
  107. when NoFakeVars:
  108. when defined(windows):
  109. const
  110. IOFBF = cint(0)
  111. IONBF = cint(4)
  112. else:
  113. # On all systems I could find, including Linux, Mac OS X, and the BSDs
  114. const
  115. IOFBF = cint(0)
  116. IONBF = cint(2)
  117. else:
  118. var
  119. IOFBF {.importc: "_IOFBF", nodecl.}: cint
  120. IONBF {.importc: "_IONBF", nodecl.}: cint
  121. const
  122. BufSize = 4000
  123. proc close*(f: File) =
  124. if not f.isNil:
  125. discard c_fclose(f)
  126. proc readChar(f: File): char =
  127. let x = c_fgetc(f)
  128. if x < 0:
  129. checkErr(f)
  130. raiseEOF()
  131. result = char(x)
  132. proc flushFile*(f: File) = discard c_fflush(f)
  133. proc getFileHandle*(f: File): FileHandle = c_fileno(f)
  134. proc readLine(f: File, line: var TaintedString): bool =
  135. var pos = 0
  136. # Use the currently reserved space for a first try
  137. var sp = max(line.string.len, 80)
  138. line.string.setLen(sp)
  139. while true:
  140. # memset to \L so that we can tell how far fgets wrote, even on EOF, where
  141. # fgets doesn't append an \L
  142. nimSetMem(addr line.string[pos], '\L'.ord, sp)
  143. var fgetsSuccess = c_fgets(addr line.string[pos], sp.cint, f) != nil
  144. if not fgetsSuccess: checkErr(f)
  145. let m = c_memchr(addr line.string[pos], '\L'.ord, sp)
  146. if m != nil:
  147. # \l found: Could be our own or the one by fgets, in any case, we're done
  148. var last = cast[ByteAddress](m) - cast[ByteAddress](addr line.string[0])
  149. if last > 0 and line.string[last-1] == '\c':
  150. line.string.setLen(last-1)
  151. return last > 1 or fgetsSuccess
  152. # We have to distinguish between two possible cases:
  153. # \0\l\0 => line ending in a null character.
  154. # \0\l\l => last line without newline, null was put there by fgets.
  155. elif last > 0 and line.string[last-1] == '\0':
  156. if last < pos + sp - 1 and line.string[last+1] != '\0':
  157. dec last
  158. line.string.setLen(last)
  159. return last > 0 or fgetsSuccess
  160. else:
  161. # fgets will have inserted a null byte at the end of the string.
  162. dec sp
  163. # No \l found: Increase buffer and read more
  164. inc pos, sp
  165. sp = 128 # read in 128 bytes at a time
  166. line.string.setLen(pos+sp)
  167. proc readLine(f: File): TaintedString =
  168. result = TaintedString(newStringOfCap(80))
  169. if not readLine(f, result): raiseEOF()
  170. proc write(f: File, i: int) =
  171. when sizeof(int) == 8:
  172. if c_fprintf(f, "%lld", i) < 0: checkErr(f)
  173. else:
  174. if c_fprintf(f, "%ld", i) < 0: checkErr(f)
  175. proc write(f: File, i: BiggestInt) =
  176. when sizeof(BiggestInt) == 8:
  177. if c_fprintf(f, "%lld", i) < 0: checkErr(f)
  178. else:
  179. if c_fprintf(f, "%ld", i) < 0: checkErr(f)
  180. proc write(f: File, b: bool) =
  181. if b: write(f, "true")
  182. else: write(f, "false")
  183. proc write(f: File, r: float32) =
  184. if c_fprintf(f, "%.16g", r) < 0: checkErr(f)
  185. proc write(f: File, r: BiggestFloat) =
  186. if c_fprintf(f, "%.16g", r) < 0: checkErr(f)
  187. proc write(f: File, c: char) = discard c_putc(cint(c), f)
  188. proc write(f: File, a: varargs[string, `$`]) =
  189. for x in items(a): write(f, x)
  190. proc readAllBuffer(file: File): string =
  191. # This proc is for File we want to read but don't know how many
  192. # bytes we need to read before the buffer is empty.
  193. result = ""
  194. var buffer = newString(BufSize)
  195. while true:
  196. var bytesRead = readBuffer(file, addr(buffer[0]), BufSize)
  197. if bytesRead == BufSize:
  198. result.add(buffer)
  199. else:
  200. buffer.setLen(bytesRead)
  201. result.add(buffer)
  202. break
  203. proc rawFileSize(file: File): int64 =
  204. # this does not raise an error opposed to `getFileSize`
  205. var oldPos = c_ftell(file)
  206. discard c_fseek(file, 0, 2) # seek the end of the file
  207. result = c_ftell(file)
  208. discard c_fseek(file, oldPos, 0)
  209. proc endOfFile(f: File): bool =
  210. var c = c_fgetc(f)
  211. discard c_ungetc(c, f)
  212. return c < 0'i32
  213. #result = c_feof(f) != 0
  214. proc readAllFile(file: File, len: int64): string =
  215. # We acquire the filesize beforehand and hope it doesn't change.
  216. # Speeds things up.
  217. result = newString(len)
  218. let bytes = readBuffer(file, addr(result[0]), len)
  219. if endOfFile(file):
  220. if bytes < len:
  221. result.setLen(bytes)
  222. else:
  223. # We read all the bytes but did not reach the EOF
  224. # Try to read it as a buffer
  225. result.add(readAllBuffer(file))
  226. proc readAllFile(file: File): string =
  227. var len = rawFileSize(file)
  228. result = readAllFile(file, len)
  229. proc readAll(file: File): TaintedString =
  230. # Separate handling needed because we need to buffer when we
  231. # don't know the overall length of the File.
  232. when declared(stdin):
  233. let len = if file != stdin: rawFileSize(file) else: -1
  234. else:
  235. let len = rawFileSize(file)
  236. if len > 0:
  237. result = readAllFile(file, len).TaintedString
  238. else:
  239. result = readAllBuffer(file).TaintedString
  240. proc writeLn[Ty](f: File, x: varargs[Ty, `$`]) =
  241. for i in items(x):
  242. write(f, i)
  243. write(f, "\n")
  244. proc writeLine[Ty](f: File, x: varargs[Ty, `$`]) =
  245. for i in items(x):
  246. write(f, i)
  247. write(f, "\n")
  248. when declared(stdout):
  249. proc rawEcho(x: string) {.inline, compilerproc.} = write(stdout, x)
  250. proc rawEchoNL() {.inline, compilerproc.} = write(stdout, "\n")
  251. # interface to the C procs:
  252. include "system/widestrs"
  253. when defined(windows) and not defined(useWinAnsi):
  254. when defined(cpp):
  255. proc wfopen(filename, mode: WideCString): pointer {.
  256. importcpp: "_wfopen((const wchar_t*)#, (const wchar_t*)#)", nodecl.}
  257. proc wfreopen(filename, mode: WideCString, stream: File): File {.
  258. importcpp: "_wfreopen((const wchar_t*)#, (const wchar_t*)#, #)", nodecl.}
  259. else:
  260. proc wfopen(filename, mode: WideCString): pointer {.
  261. importc: "_wfopen", nodecl.}
  262. proc wfreopen(filename, mode: WideCString, stream: File): File {.
  263. importc: "_wfreopen", nodecl.}
  264. proc fopen(filename, mode: cstring): pointer =
  265. var f = newWideCString(filename)
  266. var m = newWideCString(mode)
  267. result = wfopen(f, m)
  268. proc freopen(filename, mode: cstring, stream: File): File =
  269. var f = newWideCString(filename)
  270. var m = newWideCString(mode)
  271. result = wfreopen(f, m, stream)
  272. else:
  273. proc fopen(filename, mode: cstring): pointer {.importc: "fopen", noDecl.}
  274. proc freopen(filename, mode: cstring, stream: File): File {.
  275. importc: "freopen", nodecl.}
  276. const
  277. FormatOpen: array[FileMode, string] = ["rb", "wb", "w+b", "r+b", "ab"]
  278. #"rt", "wt", "w+t", "r+t", "at"
  279. # we always use binary here as for Nim the OS line ending
  280. # should not be translated.
  281. when defined(posix) and not defined(nimscript):
  282. when defined(linux) and defined(amd64):
  283. type
  284. Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
  285. # fillers ensure correct size & offsets
  286. Stat {.importc: "struct stat",
  287. header: "<sys/stat.h>", final, pure.} = object ## struct stat
  288. filler_1: array[24, char]
  289. st_mode: Mode ## Mode of file
  290. filler_2: array[144 - 24 - 4, char]
  291. proc S_ISDIR(m: Mode): bool =
  292. ## Test for a directory.
  293. (m and 0o170000) == 0o40000
  294. else:
  295. type
  296. Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
  297. Stat {.importc: "struct stat",
  298. header: "<sys/stat.h>", final, pure.} = object ## struct stat
  299. st_mode: Mode ## Mode of file
  300. proc S_ISDIR(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  301. ## Test for a directory.
  302. proc c_fstat(a1: cint, a2: var Stat): cint {.
  303. importc: "fstat", header: "<sys/stat.h>".}
  304. proc open(f: var File, filename: string,
  305. mode: FileMode = fmRead,
  306. bufSize: int = -1): bool =
  307. var p: pointer = fopen(filename, FormatOpen[mode])
  308. if p != nil:
  309. when defined(posix) and not defined(nimscript):
  310. # How `fopen` handles opening a directory is not specified in ISO C and
  311. # POSIX. We do not want to handle directories as regular files that can
  312. # be opened.
  313. var f2 = cast[File](p)
  314. var res: Stat
  315. if c_fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode):
  316. close(f2)
  317. return false
  318. result = true
  319. f = cast[File](p)
  320. if bufSize > 0 and bufSize <= high(cint).int:
  321. discard c_setvbuf(f, nil, IOFBF, bufSize.cint)
  322. elif bufSize == 0:
  323. discard c_setvbuf(f, nil, IONBF, 0)
  324. proc reopen(f: File, filename: string, mode: FileMode = fmRead): bool =
  325. var p: pointer = freopen(filename, FormatOpen[mode], f)
  326. result = p != nil
  327. proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
  328. f = c_fdopen(filehandle, FormatOpen[mode])
  329. result = f != nil
  330. proc setFilePos(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) =
  331. if c_fseek(f, pos, cint(relativeTo)) != 0:
  332. raiseEIO("cannot set file position")
  333. proc getFilePos(f: File): int64 =
  334. result = c_ftell(f)
  335. if result < 0: raiseEIO("cannot retrieve file position")
  336. proc getFileSize(f: File): int64 =
  337. var oldPos = getFilePos(f)
  338. discard c_fseek(f, 0, 2) # seek the end of the file
  339. result = getFilePos(f)
  340. setFilePos(f, oldPos)
  341. proc readFile(filename: string): TaintedString =
  342. var f: File
  343. if open(f, filename):
  344. try:
  345. result = readAll(f).TaintedString
  346. finally:
  347. close(f)
  348. else:
  349. sysFatal(IOError, "cannot open: ", filename)
  350. proc writeFile(filename, content: string) =
  351. var f: File
  352. if open(f, filename, fmWrite):
  353. try:
  354. f.write(content)
  355. finally:
  356. close(f)
  357. else:
  358. sysFatal(IOError, "cannot open: ", filename)
  359. proc setStdIoUnbuffered() =
  360. when declared(stdout):
  361. discard c_setvbuf(stdout, nil, IONBF, 0)
  362. when declared(stderr):
  363. discard c_setvbuf(stderr, nil, IONBF, 0)
  364. when declared(stdin):
  365. discard c_setvbuf(stdin, nil, IONBF, 0)
  366. when declared(stdout):
  367. when defined(windows) and compileOption("threads"):
  368. var echoLock: SysLock
  369. initSysLock echoLock
  370. proc echoBinSafe(args: openArray[string]) {.compilerProc.} =
  371. # flockfile deadlocks some versions of Android 5.x.x
  372. when not defined(windows) and not defined(android) and not defined(nintendoswitch):
  373. proc flockfile(f: File) {.importc, noDecl.}
  374. proc funlockfile(f: File) {.importc, noDecl.}
  375. flockfile(stdout)
  376. when defined(windows) and compileOption("threads"):
  377. acquireSys echoLock
  378. for s in args:
  379. discard c_fwrite(s.cstring, s.len, 1, stdout)
  380. const linefeed = "\n" # can be 1 or more chars
  381. discard c_fwrite(linefeed.cstring, linefeed.len, 1, stdout)
  382. discard c_fflush(stdout)
  383. when not defined(windows) and not defined(android) and not defined(nintendoswitch):
  384. funlockfile(stdout)
  385. when defined(windows) and compileOption("threads"):
  386. releaseSys echoLock
  387. {.pop.}