diff options
Diffstat (limited to 'lib')
-rwxr-xr-x | lib/impure/re.nim | 12 | ||||
-rwxr-xr-x | lib/pure/pegs.nim | 12 | ||||
-rwxr-xr-x | lib/system.nim | 11 | ||||
-rwxr-xr-x | lib/system/sysio.nim | 32 |
4 files changed, 31 insertions, 36 deletions
diff --git a/lib/impure/re.nim b/lib/impure/re.nim index ccc13248a..eaa712f01 100755 --- a/lib/impure/re.nim +++ b/lib/impure/re.nim @@ -342,18 +342,10 @@ proc parallelReplace*(s: string, subs: openArray[ proc transformFile*(infile, outfile: string, subs: openArray[tuple[pattern: TRegEx, repl: string]]) = ## reads in the file `infile`, performs a parallel replacement (calls - ## `parallelReplace`) and writes back to `outfile`. Calls ``quit`` if an + ## `parallelReplace`) and writes back to `outfile`. Raises ``EIO`` if an ## error occurs. This is supposed to be used for quick scripting. var x = readFile(infile) - if not isNil(x): - var f: TFile - if open(f, outfile, fmWrite): - write(f, x.parallelReplace(subs)) - close(f) - else: - quit("cannot open for writing: " & outfile) - else: - quit("cannot open for reading: " & infile) + writeFile(outfile, x.parallelReplace(subs)) iterator split*(s: string, sep: TRegEx): string = ## Splits the string `s` into substrings. diff --git a/lib/pure/pegs.nim b/lib/pure/pegs.nim index 988e510e3..b7469f65b 100755 --- a/lib/pure/pegs.nim +++ b/lib/pure/pegs.nim @@ -947,18 +947,10 @@ proc transformFile*(infile, outfile: string, subs: openArray[tuple[pattern: TPeg, repl: string]]) {. rtl, extern: "npegs$1".} = ## reads in the file `infile`, performs a parallel replacement (calls - ## `parallelReplace`) and writes back to `outfile`. Calls ``quit`` if an + ## `parallelReplace`) and writes back to `outfile`. Raises ``EIO`` if an ## error occurs. This is supposed to be used for quick scripting. var x = readFile(infile) - if not isNil(x): - var f: TFile - if open(f, outfile, fmWrite): - write(f, x.parallelReplace(subs)) - close(f) - else: - quit("cannot open for writing: " & outfile) - else: - quit("cannot open for reading: " & infile) + writeFile(outfile, x.parallelReplace(subs)) iterator split*(s: string, sep: TPeg): string = ## Splits the string `s` into substrings. diff --git a/lib/system.nim b/lib/system.nim index 7670288fc..1837959c7 100755 --- a/lib/system.nim +++ b/lib/system.nim @@ -1582,10 +1582,15 @@ when not defined(EcmaScript) and not defined(NimrodVM): ## Flushes `f`'s buffer. proc readFile*(filename: string): string - ## Opens a file name `filename` for reading. Then reads the + ## Opens a file named `filename` for reading. Then reads the ## file's content completely into a string and - ## closes the file afterwards. Returns the string. Returns nil if there was - ## an error. Does not throw an IO exception. + ## closes the file afterwards. Returns the string. + ## Raises an IO exception in case of an error. + + proc writeFile*(filename, content: string) + ## 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. proc write*(f: TFile, r: float) proc write*(f: TFile, i: int) 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) |