diff options
author | Araq <rumpf_a@web.de> | 2011-06-16 10:47:44 +0200 |
---|---|---|
committer | Araq <rumpf_a@web.de> | 2011-06-16 10:47:44 +0200 |
commit | 04f9c2585ae53555cdbec4031b0d9af270b8dc86 (patch) | |
tree | 5432c3fb2b25a70b1e74aed01731be6f74b2ed66 /lib/system | |
parent | de659eba65b5ad098a403449c4b2b1067d04a2cf (diff) | |
download | Nim-04f9c2585ae53555cdbec4031b0d9af270b8dc86.tar.gz |
readFile raises EIO instead of returning nil; added system.writeFile
Diffstat (limited to 'lib/system')
-rwxr-xr-x | lib/system/sysio.nim | 32 |
1 files changed, 19 insertions, 13 deletions
diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim index 80d9b1495..af6ba49b2 100755 --- a/lib/system/sysio.nim +++ b/lib/system/sysio.nim @@ -78,20 +78,26 @@ proc write(f: TFile, a: openArray[string]) = #{.error: "for debugging.".} +proc raiseEIO(msg: string) {.noinline, noreturn.} = + raise newException(EIO, msg) + proc readFile(filename: string): string = - var f: TFile + var f = open(filename) try: - if open(f, filename): - var len = getFileSize(f) - if len < high(int): - result = newString(int(len)) - if readBuffer(f, addr(result[0]), int(len)) != len: - result = nil - close(f) + var len = getFileSize(f) + if len < high(int): + result = newString(int(len)) + if readBuffer(f, addr(result[0]), int(len)) != len: + raiseEIO("error while reading from file") else: - result = nil + raiseEIO("file too big to fit in memory") except EIO: - result = nil + close(f) + +proc writeFile(filename, content: string) = + var f = open(filename, fmWrite) + f.write(content) + close(f) proc EndOfFile(f: TFile): bool = # do not blame me; blame the ANSI C standard this is so brain-damaged @@ -173,15 +179,15 @@ proc writeBuffer(f: TFile, buffer: pointer, len: int): int = proc write(f: TFile, s: string) = if writeBuffer(f, cstring(s), s.len) != s.len: - raise newException(EIO, "cannot write string to file") + raiseEIO("cannot write string to file") proc setFilePos(f: TFile, pos: int64) = if fseek(f, clong(pos), 0) != 0: - raise newException(EIO, "cannot set file position") + raiseEIO("cannot set file position") proc getFilePos(f: TFile): int64 = result = ftell(f) - if result < 0: raise newException(EIO, "cannot retrieve file position") + if result < 0: raiseEIO("cannot retrieve file position") proc getFileSize(f: TFile): int64 = var oldPos = getFilePos(f) |