about summary refs log tree commit diff stats
path: root/src/io/tempfile.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/io/tempfile.nim')
-rw-r--r--src/io/tempfile.nim18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/io/tempfile.nim b/src/io/tempfile.nim
new file mode 100644
index 00000000..5968270b
--- /dev/null
+++ b/src/io/tempfile.nim
@@ -0,0 +1,18 @@
+import std/os
+
+var tmpf_seq: int
+proc getTempFile*(tmpdir: string, ext = ""): string =
+  if not dirExists(tmpdir):
+    createDir(tmpdir)
+  var tmpf = tmpdir / "chatmp" & $getCurrentProcessId() & "-" & $tmpf_seq
+  if ext != "":
+    tmpf &= "."
+    tmpf &= ext
+  while fileExists(tmpf):
+    inc tmpf_seq
+    tmpf = tmpdir / "chatmp" & $tmpf_seq
+    if ext != "":
+      tmpf &= "."
+      tmpf &= ext
+  inc tmpf_seq
+  return tmpf