summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorzah <zahary@gmail.com>2019-10-02 21:34:03 +0300
committerAndreas Rumpf <rumpf_a@web.de>2019-10-02 20:34:03 +0200
commitad13e18c7cd008da418770b7f1b8eae696992ced (patch)
tree302f0e71483e7b6b8096a6a8316f3ecf2faa05d4
parent244657035050c2b513587de45bebfefa553d6456 (diff)
downloadNim-ad13e18c7cd008da418770b7f1b8eae696992ced.tar.gz
`system.writeFile` has been overloaded to also support `openarray[byte]` (#12313)
-rw-r--r--changelog.md2
-rw-r--r--lib/system/io.nim12
2 files changed, 13 insertions, 1 deletions
diff --git a/changelog.md b/changelog.md
index 32c6935db..451883dbb 100644
--- a/changelog.md
+++ b/changelog.md
@@ -18,7 +18,7 @@
 ## Library additions
 
 - `macros.newLit` now works for ref object types.
-
+- `system.writeFile` has been overloaded to also support `openarray[byte]`.
 
 ## Library changes
 
diff --git a/lib/system/io.nim b/lib/system/io.nim
index a39e8cf94..7ba36a30d 100644
--- a/lib/system/io.nim
+++ b/lib/system/io.nim
@@ -679,6 +679,18 @@ proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], benign.} =
   else:
     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] =
   ## read `n` lines from the file named `filename`. Raises an IO exception