io.nim 34 KB

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