# # # Nimrod's Runtime Library # (c) Copyright 2009 Andreas Rumpf # # See the file "copying.txt", included in this # distribution, for details about the copyright. # ## Nimrod's standard IO library. It contains high-performance ## routines for reading and writing data to (buffered) files or ## TTYs. {.push debugger:off .} # the user does not want to trace a part # of the standard library! proc fputs(c: cstring, f: TFile) {.importc: "fputs", noDecl.} proc fgets(c: cstring, n: int, f: TFile): cstring {.importc: "fgets", noDecl.} proc fgetc(stream: TFile): cint {.importc: "fgetc", nodecl.} proc ungetc(c: cint, f: TFile) {.importc: "ungetc", nodecl.} proc putc(c: Char, stream: TFile) {.importc: "putc", nodecl.} proc fprintf(f: TFile, frmt: CString) {.importc: "fprintf", nodecl, varargs.} proc strlen(c: cstring): int {.importc: "strlen", nodecl.} proc setvbuf(stream: TFile, buf: pointer, typ, size: cint): cint {. importc, nodecl.} proc write(f: TFile, c: cstring) = fputs(c, f) var IOFBF {.importc: "_IOFBF", nodecl.}: cint IONBF {.importc: "_IONBF", nodecl.}: cint proc rawReadLine(f: TFile, result: var string) = # of course this could be optimized a bit; but IO is slow anyway... # and it was difficult to get this CORRECT with Ansi C's methods setLen(result, 0) # reuse the buffer! while True: var c = fgetc(f) if c < 0'i32: break # EOF if c == 10'i32: break # LF if c == 13'i32: # CR c = fgetc(f) # is the next char LF? if c != 10'i32: ungetc(c, f) # no, put the character back break add result, chr(int(c)) proc readLine(f: TFile): string = result = "" rawReadLine(f, result) proc write(f: TFile, s: string) = fputs(s, f) proc write(f: TFile, i: int) = when sizeof(int) == 8: fprintf(f, "%lld", i) else: fprintf(f, "%ld", i) proc write(f: TFile, b: bool) = if b: write(f, "true") else: write(f, "false") proc write(f: TFile, r: float) = fprintf(f, "%g", r) proc write(f: TFile, c: Char) = putc(c, f) proc write(f: TFile, a: openArray[string]) = for x in items(a): write(f, x) #{.error: "for debugging.".} proc readFile(filename: string): string = var f: TFile 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) else: result = nil except EIO: result = nil proc EndOfFile(f: TFile): bool = # do not blame me; blame the ANSI C standard this is so brain-damaged var c = fgetc(f) ungetc(c, f) return c == -1'i32 proc writeln[Ty](f: TFile, x: Ty) = write(f, x) write(f, "\n") proc writeln[Ty](f: TFile, x: openArray[Ty]) = for i in items(x): write(f, i) write(f, "\n") proc rawEcho(x: string) {.inline, compilerproc.} = write(stdout, x) proc rawEchoNL() {.inline, compilerproc.} = write(stdout, "\n") # interface to the C procs: proc fopen(filename, mode: CString): pointer {.importc: "fopen", noDecl.}