system files use new identifiers
This commit is contained in:
parent
dbf9117c56
commit
fc0fda14ae
13 changed files with 371 additions and 338 deletions
|
|
@ -1,6 +1,6 @@
|
|||
#
|
||||
#
|
||||
# Nimrod's Runtime Library
|
||||
# Nim's Runtime Library
|
||||
# (c) Copyright 2013 Andreas Rumpf
|
||||
#
|
||||
# See the file "copying.txt", included in this
|
||||
|
|
@ -8,7 +8,7 @@
|
|||
#
|
||||
|
||||
|
||||
# Nimrod's standard IO library. It contains high-performance
|
||||
# Nim's standard IO library. It contains high-performance
|
||||
# routines for reading and writing data to (buffered) files or
|
||||
# TTYs.
|
||||
|
||||
|
|
@ -16,33 +16,33 @@
|
|||
# of the standard library!
|
||||
|
||||
|
||||
proc fputs(c: cstring, f: TFile) {.importc: "fputs", header: "<stdio.h>",
|
||||
tags: [FWriteIO].}
|
||||
proc fgets(c: cstring, n: int, f: TFile): cstring {.
|
||||
importc: "fgets", header: "<stdio.h>", tags: [FReadIO].}
|
||||
proc fgetc(stream: TFile): cint {.importc: "fgetc", header: "<stdio.h>",
|
||||
tags: [FReadIO].}
|
||||
proc ungetc(c: cint, f: TFile) {.importc: "ungetc", header: "<stdio.h>",
|
||||
proc fputs(c: cstring, f: File) {.importc: "fputs", header: "<stdio.h>",
|
||||
tags: [WriteIOEffect].}
|
||||
proc fgets(c: cstring, n: int, f: File): cstring {.
|
||||
importc: "fgets", header: "<stdio.h>", tags: [ReadIOEffect].}
|
||||
proc fgetc(stream: File): cint {.importc: "fgetc", header: "<stdio.h>",
|
||||
tags: [ReadIOEffect].}
|
||||
proc ungetc(c: cint, f: File) {.importc: "ungetc", header: "<stdio.h>",
|
||||
tags: [].}
|
||||
proc putc(c: char, stream: TFile) {.importc: "putc", header: "<stdio.h>",
|
||||
tags: [FWriteIO].}
|
||||
proc fprintf(f: TFile, frmt: cstring) {.importc: "fprintf",
|
||||
header: "<stdio.h>", varargs, tags: [FWriteIO].}
|
||||
proc putc(c: char, stream: File) {.importc: "putc", header: "<stdio.h>",
|
||||
tags: [WriteIOEffect].}
|
||||
proc fprintf(f: File, frmt: cstring) {.importc: "fprintf",
|
||||
header: "<stdio.h>", varargs, tags: [WriteIOEffect].}
|
||||
proc strlen(c: cstring): int {.
|
||||
importc: "strlen", header: "<string.h>", tags: [].}
|
||||
|
||||
|
||||
# C routine that is used here:
|
||||
proc fread(buf: pointer, size, n: int, f: TFile): int {.
|
||||
importc: "fread", header: "<stdio.h>", tags: [FReadIO].}
|
||||
proc fseek(f: TFile, offset: clong, whence: int): int {.
|
||||
proc fread(buf: pointer, size, n: int, f: File): int {.
|
||||
importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
|
||||
proc fseek(f: File, offset: clong, whence: int): int {.
|
||||
importc: "fseek", header: "<stdio.h>", tags: [].}
|
||||
proc ftell(f: TFile): int {.importc: "ftell", header: "<stdio.h>", tags: [].}
|
||||
proc setvbuf(stream: TFile, buf: pointer, typ, size: cint): cint {.
|
||||
proc ftell(f: File): int {.importc: "ftell", header: "<stdio.h>", tags: [].}
|
||||
proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {.
|
||||
importc, header: "<stdio.h>", tags: [].}
|
||||
|
||||
{.push stackTrace:off, profiler:off.}
|
||||
proc write(f: TFile, c: cstring) = fputs(c, f)
|
||||
proc write(f: File, c: cstring) = fputs(c, f)
|
||||
{.pop.}
|
||||
|
||||
when NoFakeVars:
|
||||
|
|
@ -65,9 +65,9 @@ const
|
|||
BufSize = 4000
|
||||
|
||||
proc raiseEIO(msg: string) {.noinline, noreturn.} =
|
||||
sysFatal(EIO, msg)
|
||||
sysFatal(IOError, msg)
|
||||
|
||||
proc readLine(f: TFile, line: var TaintedString): bool =
|
||||
proc readLine(f: File, line: var TaintedString): bool =
|
||||
# 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(line.string, 0) # reuse the buffer!
|
||||
|
|
@ -84,34 +84,34 @@ proc readLine(f: TFile, line: var TaintedString): bool =
|
|||
add line.string, chr(int(c))
|
||||
result = true
|
||||
|
||||
proc readLine(f: TFile): TaintedString =
|
||||
proc readLine(f: File): TaintedString =
|
||||
result = TaintedString(newStringOfCap(80))
|
||||
if not readLine(f, result): raiseEIO("EOF reached")
|
||||
|
||||
proc write(f: TFile, i: int) =
|
||||
proc write(f: File, i: int) =
|
||||
when sizeof(int) == 8:
|
||||
fprintf(f, "%lld", i)
|
||||
else:
|
||||
fprintf(f, "%ld", i)
|
||||
|
||||
proc write(f: TFile, i: BiggestInt) =
|
||||
proc write(f: File, i: BiggestInt) =
|
||||
when sizeof(BiggestInt) == 8:
|
||||
fprintf(f, "%lld", i)
|
||||
else:
|
||||
fprintf(f, "%ld", i)
|
||||
|
||||
proc write(f: TFile, b: bool) =
|
||||
proc write(f: File, b: bool) =
|
||||
if b: write(f, "true")
|
||||
else: write(f, "false")
|
||||
proc write(f: TFile, r: float32) = fprintf(f, "%g", r)
|
||||
proc write(f: TFile, r: BiggestFloat) = fprintf(f, "%g", r)
|
||||
proc write(f: File, r: float32) = fprintf(f, "%g", r)
|
||||
proc write(f: File, r: BiggestFloat) = fprintf(f, "%g", r)
|
||||
|
||||
proc write(f: TFile, c: char) = putc(c, f)
|
||||
proc write(f: TFile, a: varargs[string, `$`]) =
|
||||
proc write(f: File, c: char) = putc(c, f)
|
||||
proc write(f: File, a: varargs[string, `$`]) =
|
||||
for x in items(a): write(f, x)
|
||||
|
||||
proc readAllBuffer(file: TFile): string =
|
||||
# This proc is for TFile we want to read but don't know how many
|
||||
proc readAllBuffer(file: File): string =
|
||||
# This proc is for File we want to read but don't know how many
|
||||
# bytes we need to read before the buffer is empty.
|
||||
result = ""
|
||||
var buffer = newString(BufSize)
|
||||
|
|
@ -124,27 +124,27 @@ proc readAllBuffer(file: TFile): string =
|
|||
result.add(buffer)
|
||||
break
|
||||
|
||||
proc rawFileSize(file: TFile): int =
|
||||
proc rawFileSize(file: File): int =
|
||||
# this does not raise an error opposed to `getFileSize`
|
||||
var oldPos = ftell(file)
|
||||
discard fseek(file, 0, 2) # seek the end of the file
|
||||
result = ftell(file)
|
||||
discard fseek(file, clong(oldPos), 0)
|
||||
|
||||
proc readAllFile(file: TFile, len: int): string =
|
||||
proc readAllFile(file: File, len: int): string =
|
||||
# We aquire the filesize beforehand and hope it doesn't change.
|
||||
# Speeds things up.
|
||||
result = newString(int(len))
|
||||
if readBuffer(file, addr(result[0]), int(len)) != len:
|
||||
raiseEIO("error while reading from file")
|
||||
|
||||
proc readAllFile(file: TFile): string =
|
||||
proc readAllFile(file: File): string =
|
||||
var len = rawFileSize(file)
|
||||
result = readAllFile(file, len)
|
||||
|
||||
proc readAll(file: TFile): TaintedString =
|
||||
proc readAll(file: File): TaintedString =
|
||||
# Separate handling needed because we need to buffer when we
|
||||
# don't know the overall length of the TFile.
|
||||
# don't know the overall length of the File.
|
||||
var len = rawFileSize(file)
|
||||
if len >= 0:
|
||||
result = readAllFile(file, len).TaintedString
|
||||
|
|
@ -165,13 +165,13 @@ proc writeFile(filename, content: string) =
|
|||
finally:
|
||||
close(f)
|
||||
|
||||
proc endOfFile(f: TFile): bool =
|
||||
proc endOfFile(f: File): bool =
|
||||
# do not blame me; blame the ANSI C standard this is so brain-damaged
|
||||
var c = fgetc(f)
|
||||
ungetc(c, f)
|
||||
return c < 0'i32
|
||||
|
||||
proc writeln[Ty](f: TFile, x: varargs[Ty, `$`]) =
|
||||
proc writeln[Ty](f: File, x: varargs[Ty, `$`]) =
|
||||
for i in items(x): write(f, i)
|
||||
write(f, "\n")
|
||||
|
||||
|
|
@ -186,7 +186,7 @@ when (defined(windows) and not defined(useWinAnsi)) or defined(nimdoc):
|
|||
when defined(windows) and not defined(useWinAnsi):
|
||||
proc wfopen(filename, mode: WideCString): pointer {.
|
||||
importc: "_wfopen", nodecl.}
|
||||
proc wfreopen(filename, mode: WideCString, stream: TFile): TFile {.
|
||||
proc wfreopen(filename, mode: WideCString, stream: File): File {.
|
||||
importc: "_wfreopen", nodecl.}
|
||||
|
||||
proc fopen(filename, mode: cstring): pointer =
|
||||
|
|
@ -194,82 +194,82 @@ when defined(windows) and not defined(useWinAnsi):
|
|||
var m = newWideCString(mode)
|
||||
result = wfopen(f, m)
|
||||
|
||||
proc freopen(filename, mode: cstring, stream: TFile): TFile =
|
||||
proc freopen(filename, mode: cstring, stream: File): File =
|
||||
var f = newWideCString(filename)
|
||||
var m = newWideCString(mode)
|
||||
result = wfreopen(f, m, stream)
|
||||
|
||||
else:
|
||||
proc fopen(filename, mode: cstring): pointer {.importc: "fopen", noDecl.}
|
||||
proc freopen(filename, mode: cstring, stream: TFile): TFile {.
|
||||
proc freopen(filename, mode: cstring, stream: File): File {.
|
||||
importc: "freopen", nodecl.}
|
||||
|
||||
const
|
||||
FormatOpen: array [TFileMode, string] = ["rb", "wb", "w+b", "r+b", "ab"]
|
||||
FormatOpen: array [FileMode, string] = ["rb", "wb", "w+b", "r+b", "ab"]
|
||||
#"rt", "wt", "w+t", "r+t", "at"
|
||||
# we always use binary here as for Nimrod the OS line ending
|
||||
# we always use binary here as for Nim the OS line ending
|
||||
# should not be translated.
|
||||
|
||||
|
||||
proc open(f: var TFile, filename: string,
|
||||
mode: TFileMode = fmRead,
|
||||
proc open(f: var File, filename: string,
|
||||
mode: FileMode = fmRead,
|
||||
bufSize: int = -1): bool =
|
||||
var p: pointer = fopen(filename, FormatOpen[mode])
|
||||
result = (p != nil)
|
||||
f = cast[TFile](p)
|
||||
f = cast[File](p)
|
||||
if bufSize > 0 and bufSize <= high(cint).int:
|
||||
if setvbuf(f, nil, IOFBF, bufSize.cint) != 0'i32:
|
||||
sysFatal(EOutOfMemory, "out of memory")
|
||||
sysFatal(OutOfMemError, "out of memory")
|
||||
elif bufSize == 0:
|
||||
discard setvbuf(f, nil, IONBF, 0)
|
||||
|
||||
proc reopen(f: TFile, filename: string, mode: TFileMode = fmRead): bool =
|
||||
proc reopen(f: File, filename: string, mode: FileMode = fmRead): bool =
|
||||
var p: pointer = freopen(filename, FormatOpen[mode], f)
|
||||
result = p != nil
|
||||
|
||||
proc fdopen(filehandle: TFileHandle, mode: cstring): TFile {.
|
||||
proc fdopen(filehandle: FileHandle, mode: cstring): File {.
|
||||
importc: pccHack & "fdopen", header: "<stdio.h>".}
|
||||
|
||||
proc open(f: var TFile, filehandle: TFileHandle, mode: TFileMode): bool =
|
||||
proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
|
||||
f = fdopen(filehandle, FormatOpen[mode])
|
||||
result = f != nil
|
||||
|
||||
proc fwrite(buf: pointer, size, n: int, f: TFile): int {.
|
||||
proc fwrite(buf: pointer, size, n: int, f: File): int {.
|
||||
importc: "fwrite", noDecl.}
|
||||
|
||||
proc readBuffer(f: TFile, buffer: pointer, len: int): int =
|
||||
proc readBuffer(f: File, buffer: pointer, len: int): int =
|
||||
result = fread(buffer, 1, len, f)
|
||||
|
||||
proc readBytes(f: TFile, a: var openArray[int8], start, len: int): int =
|
||||
proc readBytes(f: File, a: var openArray[int8], start, len: int): int =
|
||||
result = readBuffer(f, addr(a[start]), len)
|
||||
|
||||
proc readChars(f: TFile, a: var openArray[char], start, len: int): int =
|
||||
proc readChars(f: File, a: var openArray[char], start, len: int): int =
|
||||
result = readBuffer(f, addr(a[start]), len)
|
||||
|
||||
{.push stackTrace:off, profiler:off.}
|
||||
proc writeBytes(f: TFile, a: openArray[int8], start, len: int): int =
|
||||
proc writeBytes(f: File, a: openArray[int8], start, len: int): int =
|
||||
var x = cast[ptr array[0..1000_000_000, int8]](a)
|
||||
result = writeBuffer(f, addr(x[start]), len)
|
||||
proc writeChars(f: TFile, a: openArray[char], start, len: int): int =
|
||||
proc writeChars(f: File, a: openArray[char], start, len: int): int =
|
||||
var x = cast[ptr array[0..1000_000_000, int8]](a)
|
||||
result = writeBuffer(f, addr(x[start]), len)
|
||||
proc writeBuffer(f: TFile, buffer: pointer, len: int): int =
|
||||
proc writeBuffer(f: File, buffer: pointer, len: int): int =
|
||||
result = fwrite(buffer, 1, len, f)
|
||||
|
||||
proc write(f: TFile, s: string) =
|
||||
proc write(f: File, s: string) =
|
||||
if writeBuffer(f, cstring(s), s.len) != s.len:
|
||||
raiseEIO("cannot write string to file")
|
||||
{.pop.}
|
||||
|
||||
proc setFilePos(f: TFile, pos: int64) =
|
||||
proc setFilePos(f: File, pos: int64) =
|
||||
if fseek(f, clong(pos), 0) != 0:
|
||||
raiseEIO("cannot set file position")
|
||||
|
||||
proc getFilePos(f: TFile): int64 =
|
||||
proc getFilePos(f: File): int64 =
|
||||
result = ftell(f)
|
||||
if result < 0: raiseEIO("cannot retrieve file position")
|
||||
|
||||
proc getFileSize(f: TFile): int64 =
|
||||
proc getFileSize(f: File): int64 =
|
||||
var oldPos = getFilePos(f)
|
||||
discard fseek(f, 0, 2) # seek the end of the file
|
||||
result = getFilePos(f)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue