io.nim 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2019 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. include inclrtl
  10. # ----------------- IO Part ------------------------------------------------
  11. type
  12. CFile {.importc: "FILE", header: "<stdio.h>",
  13. incompletestruct.} = object
  14. File* = ptr CFile ## The type representing a file handle.
  15. FileMode* = enum ## The file mode when opening a file.
  16. fmRead, ## Open the file for read access only.
  17. fmWrite, ## Open the file for write access only.
  18. ## If the file does not exist, it will be
  19. ## created. Existing files will be cleared!
  20. fmReadWrite, ## Open the file for read and write access.
  21. ## If the file does not exist, it will be
  22. ## created. Existing files will be cleared!
  23. fmReadWriteExisting, ## Open the file for read and write access.
  24. ## If the file does not exist, it will not be
  25. ## created. The existing file will not be cleared.
  26. fmAppend ## Open the file for writing only; append data
  27. ## at the end.
  28. FileHandle* = cint ## type that represents an OS file handle; this is
  29. ## useful for low-level file access
  30. # text file handling:
  31. when not defined(nimscript) and not defined(js):
  32. var
  33. stdin* {.importc: "stdin", header: "<stdio.h>".}: File
  34. ## The standard input stream.
  35. stdout* {.importc: "stdout", header: "<stdio.h>".}: File
  36. ## The standard output stream.
  37. stderr* {.importc: "stderr", header: "<stdio.h>".}: File
  38. ## The standard error stream.
  39. when defined(useStdoutAsStdmsg):
  40. template stdmsg*: File = stdout
  41. else:
  42. template stdmsg*: File = stderr
  43. ## Template which expands to either stdout or stderr depending on
  44. ## `useStdoutAsStdmsg` compile-time switch.
  45. when defined(windows):
  46. proc c_fileno(f: File): cint {.
  47. importc: "_fileno", header: "<stdio.h>".}
  48. else:
  49. proc c_fileno(f: File): cint {.
  50. importc: "fileno", header: "<fcntl.h>".}
  51. when defined(windows):
  52. proc c_fdopen(filehandle: cint, mode: cstring): File {.
  53. importc: "_fdopen", header: "<stdio.h>".}
  54. else:
  55. proc c_fdopen(filehandle: cint, mode: cstring): File {.
  56. importc: "fdopen", header: "<stdio.h>".}
  57. proc c_fputs(c: cstring, f: File): cint {.
  58. importc: "fputs", header: "<stdio.h>", tags: [WriteIOEffect].}
  59. proc c_fgets(c: cstring, n: cint, f: File): cstring {.
  60. importc: "fgets", header: "<stdio.h>", tags: [ReadIOEffect].}
  61. proc c_fgetc(stream: File): cint {.
  62. importc: "fgetc", header: "<stdio.h>", tags: [ReadIOEffect].}
  63. proc c_ungetc(c: cint, f: File): cint {.
  64. importc: "ungetc", header: "<stdio.h>", tags: [].}
  65. proc c_putc(c: cint, stream: File): cint {.
  66. importc: "putc", header: "<stdio.h>", tags: [WriteIOEffect].}
  67. proc c_fflush(f: File): cint {.
  68. importc: "fflush", header: "<stdio.h>".}
  69. proc c_fclose(f: File): cint {.
  70. importc: "fclose", header: "<stdio.h>".}
  71. proc c_clearerr(f: File) {.
  72. importc: "clearerr", header: "<stdio.h>".}
  73. proc c_feof(f: File): cint {.
  74. importc: "feof", header: "<stdio.h>".}
  75. when not declared(c_fwrite):
  76. proc c_fwrite(buf: pointer, size, n: csize, f: File): cint {.
  77. importc: "fwrite", header: "<stdio.h>".}
  78. # C routine that is used here:
  79. proc c_fread(buf: pointer, size, n: csize, f: File): csize {.
  80. importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
  81. when defined(windows):
  82. when not defined(amd64):
  83. proc c_fseek(f: File, offset: int64, whence: cint): cint {.
  84. importc: "fseek", header: "<stdio.h>", tags: [].}
  85. proc c_ftell(f: File): int64 {.
  86. importc: "ftell", header: "<stdio.h>", tags: [].}
  87. else:
  88. proc c_fseek(f: File, offset: int64, whence: cint): cint {.
  89. importc: "_fseeki64", header: "<stdio.h>", tags: [].}
  90. proc c_ftell(f: File): int64 {.
  91. importc: "_ftelli64", header: "<stdio.h>", tags: [].}
  92. else:
  93. proc c_fseek(f: File, offset: int64, whence: cint): cint {.
  94. importc: "fseeko", header: "<stdio.h>", tags: [].}
  95. proc c_ftell(f: File): int64 {.
  96. importc: "ftello", header: "<stdio.h>", tags: [].}
  97. proc c_ferror(f: File): cint {.
  98. importc: "ferror", header: "<stdio.h>", tags: [].}
  99. proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize): cint {.
  100. importc: "setvbuf", header: "<stdio.h>", tags: [].}
  101. proc c_fprintf(f: File, frmt: cstring): cint {.
  102. importc: "fprintf", header: "<stdio.h>", varargs, discardable.}
  103. template sysFatal(exc, msg) =
  104. raise newException(exc, msg)
  105. proc raiseEIO(msg: string) {.noinline, noreturn.} =
  106. sysFatal(IOError, msg)
  107. proc raiseEOF() {.noinline, noreturn.} =
  108. sysFatal(EOFError, "EOF reached")
  109. proc strerror(errnum: cint): cstring {.importc, header: "<string.h>".}
  110. when not defined(NimScript):
  111. var
  112. errno {.importc, header: "<errno.h>".}: cint ## error variable
  113. proc checkErr(f: File) =
  114. when not defined(NimScript):
  115. if c_ferror(f) != 0:
  116. let msg = "errno: " & $errno & " `" & $strerror(errno) & "`"
  117. c_clearerr(f)
  118. raiseEIO(msg)
  119. else:
  120. # shouldn't happen
  121. quit(1)
  122. {.push stackTrace:off, profiler:off.}
  123. proc readBuffer*(f: File, buffer: pointer, len: Natural): int {.
  124. tags: [ReadIOEffect], benign.} =
  125. ## reads `len` bytes into the buffer pointed to by `buffer`. Returns
  126. ## the actual number of bytes that have been read which may be less than
  127. ## `len` (if not as many bytes are remaining), but not greater.
  128. result = c_fread(buffer, 1, len, f)
  129. if result != len: checkErr(f)
  130. proc readBytes*(f: File, a: var openArray[int8|uint8], start, len: Natural): int {.
  131. tags: [ReadIOEffect], benign.} =
  132. ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns
  133. ## the actual number of bytes that have been read which may be less than
  134. ## `len` (if not as many bytes are remaining), but not greater.
  135. result = readBuffer(f, addr(a[start]), len)
  136. proc readChars*(f: File, a: var openArray[char], start, len: Natural): int {.
  137. tags: [ReadIOEffect], benign.} =
  138. ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns
  139. ## the actual number of bytes that have been read which may be less than
  140. ## `len` (if not as many bytes are remaining), but not greater.
  141. ##
  142. ## **Warning:** The buffer `a` must be pre-allocated. This can be done
  143. ## using, for example, ``newString``.
  144. if (start + len) > len(a):
  145. raiseEIO("buffer overflow: (start+len) > length of openarray buffer")
  146. result = readBuffer(f, addr(a[start]), len)
  147. proc write*(f: File, c: cstring) {.tags: [WriteIOEffect], benign.} =
  148. ## Writes a value to the file `f`. May throw an IO exception.
  149. discard c_fputs(c, f)
  150. checkErr(f)
  151. proc writeBuffer*(f: File, buffer: pointer, len: Natural): int {.
  152. tags: [WriteIOEffect], benign.} =
  153. ## writes the bytes of buffer pointed to by the parameter `buffer` to the
  154. ## file `f`. Returns the number of actual written bytes, which may be less
  155. ## than `len` in case of an error.
  156. result = c_fwrite(buffer, 1, len, f)
  157. checkErr(f)
  158. proc writeBytes*(f: File, a: openArray[int8|uint8], start, len: Natural): int {.
  159. tags: [WriteIOEffect], benign.} =
  160. ## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns
  161. ## the number of actual written bytes, which may be less than `len` in case
  162. ## of an error.
  163. var x = cast[ptr UncheckedArray[int8]](a)
  164. result = writeBuffer(f, addr(x[int(start)]), len)
  165. proc writeChars*(f: File, a: openArray[char], start, len: Natural): int {.
  166. tags: [WriteIOEffect], benign.} =
  167. ## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns
  168. ## the number of actual written bytes, which may be less than `len` in case
  169. ## of an error.
  170. var x = cast[ptr UncheckedArray[int8]](a)
  171. result = writeBuffer(f, addr(x[int(start)]), len)
  172. proc write*(f: File, s: string) {.tags: [WriteIOEffect], benign.} =
  173. if writeBuffer(f, cstring(s), s.len) != s.len:
  174. raiseEIO("cannot write string to file")
  175. {.pop.}
  176. when NoFakeVars:
  177. when defined(windows):
  178. const
  179. IOFBF = cint(0)
  180. IONBF = cint(4)
  181. else:
  182. # On all systems I could find, including Linux, Mac OS X, and the BSDs
  183. const
  184. IOFBF = cint(0)
  185. IONBF = cint(2)
  186. else:
  187. var
  188. IOFBF {.importc: "_IOFBF", nodecl.}: cint
  189. IONBF {.importc: "_IONBF", nodecl.}: cint
  190. const
  191. BufSize = 4000
  192. proc close*(f: File) {.tags: [], gcsafe.} =
  193. ## Closes the file.
  194. if not f.isNil:
  195. discard c_fclose(f)
  196. proc readChar*(f: File): char {.tags: [ReadIOEffect].} =
  197. ## Reads a single character from the stream `f`. Should not be used in
  198. ## performance sensitive code.
  199. let x = c_fgetc(f)
  200. if x < 0:
  201. checkErr(f)
  202. raiseEOF()
  203. result = char(x)
  204. proc flushFile*(f: File) {.tags: [WriteIOEffect].} =
  205. ## Flushes `f`'s buffer.
  206. discard c_fflush(f)
  207. proc getFileHandle*(f: File): FileHandle =
  208. ## returns the OS file handle of the file ``f``. This is only useful for
  209. ## platform specific programming.
  210. c_fileno(f)
  211. proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],
  212. benign.} =
  213. ## reads a line of text from the file `f` into `line`. May throw an IO
  214. ## exception.
  215. ## A line of text may be delimited by ``LF`` or ``CRLF``. The newline
  216. ## character(s) are not part of the returned string. Returns ``false``
  217. ## if the end of the file has been reached, ``true`` otherwise. If
  218. ## ``false`` is returned `line` contains no new data.
  219. proc c_memchr(s: pointer, c: cint, n: csize): pointer {.
  220. importc: "memchr", header: "<string.h>".}
  221. var pos = 0
  222. # Use the currently reserved space for a first try
  223. var sp = max(line.string.len, 80)
  224. line.string.setLen(sp)
  225. while true:
  226. # memset to \L so that we can tell how far fgets wrote, even on EOF, where
  227. # fgets doesn't append an \L
  228. for i in 0..<sp: line.string[pos+i] = '\L'
  229. var fgetsSuccess = c_fgets(addr line.string[pos], sp.cint, f) != nil
  230. if not fgetsSuccess: checkErr(f)
  231. let m = c_memchr(addr line.string[pos], '\L'.ord, sp)
  232. if m != nil:
  233. # \l found: Could be our own or the one by fgets, in any case, we're done
  234. var last = cast[ByteAddress](m) - cast[ByteAddress](addr line.string[0])
  235. if last > 0 and line.string[last-1] == '\c':
  236. line.string.setLen(last-1)
  237. return last > 1 or fgetsSuccess
  238. # We have to distinguish between two possible cases:
  239. # \0\l\0 => line ending in a null character.
  240. # \0\l\l => last line without newline, null was put there by fgets.
  241. elif last > 0 and line.string[last-1] == '\0':
  242. if last < pos + sp - 1 and line.string[last+1] != '\0':
  243. dec last
  244. line.string.setLen(last)
  245. return last > 0 or fgetsSuccess
  246. else:
  247. # fgets will have inserted a null byte at the end of the string.
  248. dec sp
  249. # No \l found: Increase buffer and read more
  250. inc pos, sp
  251. sp = 128 # read in 128 bytes at a time
  252. line.string.setLen(pos+sp)
  253. proc readLine*(f: File): TaintedString {.tags: [ReadIOEffect], benign.} =
  254. ## reads a line of text from the file `f`. May throw an IO exception.
  255. ## A line of text may be delimited by ``LF`` or ``CRLF``. The newline
  256. ## character(s) are not part of the returned string.
  257. result = TaintedString(newStringOfCap(80))
  258. if not readLine(f, result): raiseEOF()
  259. proc write*(f: File, i: int) {.tags: [WriteIOEffect], benign.} =
  260. when sizeof(int) == 8:
  261. if c_fprintf(f, "%lld", i) < 0: checkErr(f)
  262. else:
  263. if c_fprintf(f, "%ld", i) < 0: checkErr(f)
  264. proc write*(f: File, i: BiggestInt) {.tags: [WriteIOEffect], benign.} =
  265. when sizeof(BiggestInt) == 8:
  266. if c_fprintf(f, "%lld", i) < 0: checkErr(f)
  267. else:
  268. if c_fprintf(f, "%ld", i) < 0: checkErr(f)
  269. proc write*(f: File, b: bool) {.tags: [WriteIOEffect], benign.} =
  270. if b: write(f, "true")
  271. else: write(f, "false")
  272. proc write*(f: File, r: float32) {.tags: [WriteIOEffect], benign.} =
  273. if c_fprintf(f, "%.16g", r) < 0: checkErr(f)
  274. proc write*(f: File, r: BiggestFloat) {.tags: [WriteIOEffect], benign.} =
  275. if c_fprintf(f, "%.16g", r) < 0: checkErr(f)
  276. proc write*(f: File, c: char) {.tags: [WriteIOEffect], benign.} =
  277. discard c_putc(cint(c), f)
  278. proc write*(f: File, a: varargs[string, `$`]) {.tags: [WriteIOEffect], benign.} =
  279. for x in items(a): write(f, x)
  280. proc readAllBuffer(file: File): string =
  281. # This proc is for File we want to read but don't know how many
  282. # bytes we need to read before the buffer is empty.
  283. result = ""
  284. var buffer = newString(BufSize)
  285. while true:
  286. var bytesRead = readBuffer(file, addr(buffer[0]), BufSize)
  287. if bytesRead == BufSize:
  288. result.add(buffer)
  289. else:
  290. buffer.setLen(bytesRead)
  291. result.add(buffer)
  292. break
  293. proc rawFileSize(file: File): int64 =
  294. # this does not raise an error opposed to `getFileSize`
  295. var oldPos = c_ftell(file)
  296. discard c_fseek(file, 0, 2) # seek the end of the file
  297. result = c_ftell(file)
  298. discard c_fseek(file, oldPos, 0)
  299. proc endOfFile*(f: File): bool {.tags: [], benign.} =
  300. ## Returns true iff `f` is at the end.
  301. var c = c_fgetc(f)
  302. discard c_ungetc(c, f)
  303. return c < 0'i32
  304. #result = c_feof(f) != 0
  305. proc readAllFile(file: File, len: int64): string =
  306. # We acquire the filesize beforehand and hope it doesn't change.
  307. # Speeds things up.
  308. result = newString(len)
  309. let bytes = readBuffer(file, addr(result[0]), len)
  310. if endOfFile(file):
  311. if bytes < len:
  312. result.setLen(bytes)
  313. else:
  314. # We read all the bytes but did not reach the EOF
  315. # Try to read it as a buffer
  316. result.add(readAllBuffer(file))
  317. proc readAllFile(file: File): string =
  318. var len = rawFileSize(file)
  319. result = readAllFile(file, len)
  320. proc readAll*(file: File): TaintedString {.tags: [ReadIOEffect], benign.} =
  321. ## Reads all data from the stream `file`.
  322. ##
  323. ## Raises an IO exception in case of an error. It is an error if the
  324. ## current file position is not at the beginning of the file.
  325. # Separate handling needed because we need to buffer when we
  326. # don't know the overall length of the File.
  327. when declared(stdin):
  328. let len = if file != stdin: rawFileSize(file) else: -1
  329. else:
  330. let len = rawFileSize(file)
  331. if len > 0:
  332. result = readAllFile(file, len).TaintedString
  333. else:
  334. result = readAllBuffer(file).TaintedString
  335. proc writeLn[Ty](f: File, x: varargs[Ty, `$`]) =
  336. for i in items(x):
  337. write(f, i)
  338. write(f, "\n")
  339. proc writeLine*[Ty](f: File, x: varargs[Ty, `$`]) {.inline,
  340. tags: [WriteIOEffect], benign.} =
  341. ## writes the values `x` to `f` and then writes "\\n".
  342. ## May throw an IO exception.
  343. for i in items(x):
  344. write(f, i)
  345. write(f, "\n")
  346. # interface to the C procs:
  347. when defined(windows) and not defined(useWinAnsi):
  348. when defined(cpp):
  349. proc wfopen(filename, mode: WideCString): pointer {.
  350. importcpp: "_wfopen((const wchar_t*)#, (const wchar_t*)#)", nodecl.}
  351. proc wfreopen(filename, mode: WideCString, stream: File): File {.
  352. importcpp: "_wfreopen((const wchar_t*)#, (const wchar_t*)#, #)", nodecl.}
  353. else:
  354. proc wfopen(filename, mode: WideCString): pointer {.
  355. importc: "_wfopen", nodecl.}
  356. proc wfreopen(filename, mode: WideCString, stream: File): File {.
  357. importc: "_wfreopen", nodecl.}
  358. proc fopen(filename, mode: cstring): pointer =
  359. var f = newWideCString(filename)
  360. var m = newWideCString(mode)
  361. result = wfopen(f, m)
  362. proc freopen(filename, mode: cstring, stream: File): File =
  363. var f = newWideCString(filename)
  364. var m = newWideCString(mode)
  365. result = wfreopen(f, m, stream)
  366. else:
  367. proc fopen(filename, mode: cstring): pointer {.importc: "fopen", noDecl.}
  368. proc freopen(filename, mode: cstring, stream: File): File {.
  369. importc: "freopen", nodecl.}
  370. const
  371. FormatOpen: array[FileMode, string] = ["rb", "wb", "w+b", "r+b", "ab"]
  372. #"rt", "wt", "w+t", "r+t", "at"
  373. # we always use binary here as for Nim the OS line ending
  374. # should not be translated.
  375. when defined(posix) and not defined(nimscript):
  376. when defined(linux) and defined(amd64):
  377. type
  378. Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
  379. # fillers ensure correct size & offsets
  380. Stat {.importc: "struct stat",
  381. header: "<sys/stat.h>", final, pure.} = object ## struct stat
  382. filler_1: array[24, char]
  383. st_mode: Mode ## Mode of file
  384. filler_2: array[144 - 24 - 4, char]
  385. proc S_ISDIR(m: Mode): bool =
  386. ## Test for a directory.
  387. (m and 0o170000) == 0o40000
  388. else:
  389. type
  390. Mode {.importc: "mode_t", header: "<sys/types.h>".} = cint
  391. Stat {.importc: "struct stat",
  392. header: "<sys/stat.h>", final, pure.} = object ## struct stat
  393. st_mode: Mode ## Mode of file
  394. proc S_ISDIR(m: Mode): bool {.importc, header: "<sys/stat.h>".}
  395. ## Test for a directory.
  396. proc c_fstat(a1: cint, a2: var Stat): cint {.
  397. importc: "fstat", header: "<sys/stat.h>".}
  398. proc open*(f: var File, filename: string,
  399. mode: FileMode = fmRead,
  400. bufSize: int = -1): bool {.tags: [], raises: [], benign.} =
  401. ## Opens a file named `filename` with given `mode`.
  402. ##
  403. ## Default mode is readonly. Returns true iff the file could be opened.
  404. ## This throws no exception if the file could not be opened.
  405. var p: pointer = fopen(filename, FormatOpen[mode])
  406. if p != nil:
  407. when defined(posix) and not defined(nimscript):
  408. # How `fopen` handles opening a directory is not specified in ISO C and
  409. # POSIX. We do not want to handle directories as regular files that can
  410. # be opened.
  411. var f2 = cast[File](p)
  412. var res: Stat
  413. if c_fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode):
  414. close(f2)
  415. return false
  416. result = true
  417. f = cast[File](p)
  418. if bufSize > 0 and bufSize <= high(cint).int:
  419. discard c_setvbuf(f, nil, IOFBF, bufSize.cint)
  420. elif bufSize == 0:
  421. discard c_setvbuf(f, nil, IONBF, 0)
  422. proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
  423. tags: [], benign.} =
  424. ## reopens the file `f` with given `filename` and `mode`. This
  425. ## is often used to redirect the `stdin`, `stdout` or `stderr`
  426. ## file variables.
  427. ##
  428. ## Default mode is readonly. Returns true iff the file could be reopened.
  429. var p: pointer = freopen(filename, FormatOpen[mode], f)
  430. result = p != nil
  431. proc open*(f: var File, filehandle: FileHandle,
  432. mode: FileMode = fmRead): bool {.tags: [], raises: [], benign.} =
  433. ## Creates a ``File`` from a `filehandle` with given `mode`.
  434. ##
  435. ## Default mode is readonly. Returns true iff the file could be opened.
  436. f = c_fdopen(filehandle, FormatOpen[mode])
  437. result = f != nil
  438. proc open*(filename: string,
  439. mode: FileMode = fmRead, bufSize: int = -1): File =
  440. ## Opens a file named `filename` with given `mode`.
  441. ##
  442. ## Default mode is readonly. Raises an ``IOError`` if the file
  443. ## could not be opened.
  444. if not open(result, filename, mode, bufSize):
  445. sysFatal(IOError, "cannot open: " & filename)
  446. proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign.} =
  447. ## sets the position of the file pointer that is used for read/write
  448. ## operations. The file's first byte has the index zero.
  449. if c_fseek(f, pos, cint(relativeTo)) != 0:
  450. raiseEIO("cannot set file position")
  451. proc getFilePos*(f: File): int64 {.benign.} =
  452. ## retrieves the current position of the file pointer that is used to
  453. ## read from the file `f`. The file's first byte has the index zero.
  454. result = c_ftell(f)
  455. if result < 0: raiseEIO("cannot retrieve file position")
  456. proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], benign.} =
  457. ## retrieves the file size (in bytes) of `f`.
  458. var oldPos = getFilePos(f)
  459. discard c_fseek(f, 0, 2) # seek the end of the file
  460. result = getFilePos(f)
  461. setFilePos(f, oldPos)
  462. proc setStdIoUnbuffered*() {.tags: [], benign.} =
  463. ## Configures `stdin`, `stdout` and `stderr` to be unbuffered.
  464. when declared(stdout):
  465. discard c_setvbuf(stdout, nil, IONBF, 0)
  466. when declared(stderr):
  467. discard c_setvbuf(stderr, nil, IONBF, 0)
  468. when declared(stdin):
  469. discard c_setvbuf(stdin, nil, IONBF, 0)
  470. when declared(stdout):
  471. when defined(windows) and compileOption("threads"):
  472. const insideRLocksModule = false
  473. include "system/syslocks"
  474. var echoLock: SysLock
  475. initSysLock echoLock
  476. proc echoBinSafe(args: openArray[string]) {.compilerProc.} =
  477. # flockfile deadlocks some versions of Android 5.x.x
  478. when not defined(windows) and not defined(android) and not defined(nintendoswitch):
  479. proc flockfile(f: File) {.importc, noDecl.}
  480. proc funlockfile(f: File) {.importc, noDecl.}
  481. flockfile(stdout)
  482. when defined(windows) and compileOption("threads"):
  483. acquireSys echoLock
  484. for s in args:
  485. discard c_fwrite(s.cstring, s.len, 1, stdout)
  486. const linefeed = "\n" # can be 1 or more chars
  487. discard c_fwrite(linefeed.cstring, linefeed.len, 1, stdout)
  488. discard c_fflush(stdout)
  489. when not defined(windows) and not defined(android) and not defined(nintendoswitch):
  490. funlockfile(stdout)
  491. when defined(windows) and compileOption("threads"):
  492. releaseSys echoLock
  493. when defined(windows) and not defined(nimscript):
  494. # work-around C's sucking abstraction:
  495. # BUGFIX: stdin and stdout should be binary files!
  496. proc c_setmode(handle, mode: cint) {.
  497. importc: when defined(bcc): "setmode" else: "_setmode",
  498. header: "<io.h>".}
  499. var
  500. O_BINARY {.importc: "_O_BINARY", header:"<fcntl.h>".}: cint
  501. # we use binary mode on Windows:
  502. c_setmode(c_fileno(stdin), O_BINARY)
  503. c_setmode(c_fileno(stdout), O_BINARY)
  504. c_setmode(c_fileno(stderr), O_BINARY)
  505. proc readFile*(filename: string): TaintedString {.tags: [ReadIOEffect], benign.} =
  506. ## Opens a file named `filename` for reading, calls `readAll
  507. ## <#readAll>`_ and closes the file afterwards. Returns the string.
  508. ## Raises an IO exception in case of an error. If # you need to call
  509. ## this inside a compile time macro you can use `staticRead
  510. ## <#staticRead>`_.
  511. var f: File
  512. if open(f, filename):
  513. try:
  514. result = readAll(f).TaintedString
  515. finally:
  516. close(f)
  517. else:
  518. sysFatal(IOError, "cannot open: " & filename)
  519. proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} =
  520. ## Opens a file named `filename` for writing. Then writes the
  521. ## `content` completely to the file and closes the file afterwards.
  522. ## Raises an IO exception in case of an error.
  523. var f: File
  524. if open(f, filename, fmWrite):
  525. try:
  526. f.write(content)
  527. finally:
  528. close(f)
  529. else:
  530. sysFatal(IOError, "cannot open: " & filename)
  531. proc readLines*(filename: string, n = 1.Natural): seq[TaintedString] =
  532. ## read `n` lines from the file named `filename`. Raises an IO exception
  533. ## in case of an error. Raises EOF if file does not contain at least `n` lines.
  534. ## Available at compile time. A line of text may be delimited by ``LF`` or ``CRLF``.
  535. ## The newline character(s) are not part of the returned strings.
  536. var f: File
  537. if open(f, filename):
  538. try:
  539. result = newSeq[TaintedString](n)
  540. for i in 0 .. n - 1:
  541. if not readLine(f, result[i]):
  542. raiseEOF()
  543. finally:
  544. close(f)
  545. else:
  546. sysFatal(IOError, "cannot open: " & filename)
  547. iterator lines*(filename: string): TaintedString {.tags: [ReadIOEffect].} =
  548. ## Iterates over any line in the file named `filename`.
  549. ##
  550. ## If the file does not exist `IOError` is raised. The trailing newline
  551. ## character(s) are removed from the iterated lines. Example:
  552. ##
  553. ## .. code-block:: nim
  554. ## import strutils
  555. ##
  556. ## proc transformLetters(filename: string) =
  557. ## var buffer = ""
  558. ## for line in filename.lines:
  559. ## buffer.add(line.replace("a", "0") & '\x0A')
  560. ## writeFile(filename, buffer)
  561. var f = open(filename, bufSize=8000)
  562. defer: close(f)
  563. var res = TaintedString(newStringOfCap(80))
  564. while f.readLine(res): yield res
  565. iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
  566. ## Iterate over any line in the file `f`.
  567. ##
  568. ## The trailing newline character(s) are removed from the iterated lines.
  569. ## Example:
  570. ##
  571. ## .. code-block:: nim
  572. ## proc countZeros(filename: File): tuple[lines, zeros: int] =
  573. ## for line in filename.lines:
  574. ## for letter in line:
  575. ## if letter == '0':
  576. ## result.zeros += 1
  577. ## result.lines += 1
  578. var res = TaintedString(newStringOfCap(80))
  579. while f.readLine(res): yield res