system.writeFile has been overloaded to also support openarray[byte] (#12313)

This commit is contained in:
zah 2019-10-02 21:34:03 +03:00 • committed by Andreas Rumpf
commit ad13e18c7c
2 changed files with 13 additions and 1 deletions

View file

@ -18,7 +18,7 @@
## Library additions ## Library additions
- `macros.newLit` now works for ref object types. - `macros.newLit` now works for ref object types.
- `system.writeFile` has been overloaded to also support `openarray[byte]`.
## Library changes ## Library changes

View file

@ -679,6 +679,18 @@ proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} =
else: else:
sysFatal(IOError, "cannot open: " & filename) sysFatal(IOError, "cannot open: " & filename)
proc writeFile*(filename: string, content: openArray[byte]) =
## Opens a file named `filename` for writing. Then writes the
## `content` completely to the file and closes the file afterwards.
## Raises an IO exception in case of an error.
var f: File
if open(f, filename, fmWrite):
try:
f.writeBuffer(unsafeAddr content[0], content.len)
finally:
close(f)
else:
raise newException(IOError, "cannot open: " & filename)
proc readLines*(filename: string, n = 1.Natural): seq[TaintedString] = proc readLines*(filename: string, n = 1.Natural): seq[TaintedString] =
## read `n` lines from the file named `filename`. Raises an IO exception ## read `n` lines from the file named `filename`. Raises an IO exception