syncio.nim 35 KB

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