about summary refs log blame commit diff stats
path: root/src/extern/tempfile.nim
blob: 5968270b8fbb1d74e703627f68359c2e68ae66e9 (plain) (tree)
1
2
3
4
5
6
7
             




                                                     
                                                                         










                                        
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