io.nim 28 KB

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