fix types of ansi_c/sysio to more closely match C ABI
also fixes some instances of using C library functions when there are nim alternatives available
This commit is contained in:
parent
796d939c6c
commit
64b0485207
15 changed files with 173 additions and 203 deletions
|
|
@ -16,43 +16,43 @@
|
|||
# of the standard library!
|
||||
|
||||
|
||||
proc fputs(c: cstring, f: File) {.importc: "fputs", header: "<stdio.h>",
|
||||
tags: [WriteIOEffect].}
|
||||
proc fgets(c: cstring, n: int, f: File): cstring {.
|
||||
proc c_fdopen(filehandle: cint, mode: cstring): File {.
|
||||
importc: "fdopen", header: "<stdio.h>".}
|
||||
proc c_fputs(c: cstring, f: File): cint {.
|
||||
importc: "fputs", header: "<stdio.h>", tags: [WriteIOEffect].}
|
||||
proc c_fgets(c: pointer, n: cint, 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: 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: [].}
|
||||
proc c_fgetc(stream: File): cint {.
|
||||
importc: "fgetc", header: "<stdio.h>", tags: [ReadIOEffect].}
|
||||
proc c_ungetc(c: cint, f: File): cint {.
|
||||
importc: "ungetc", header: "<stdio.h>", tags: [].}
|
||||
proc c_putc(c: cint, stream: File): cint {.
|
||||
importc: "putc", header: "<stdio.h>", tags: [WriteIOEffect].}
|
||||
proc c_fflush(f: File): cint {.
|
||||
importc: "fflush", header: "<stdio.h>".}
|
||||
proc c_fclose(f: File): cint {.
|
||||
importc: "fclose", header: "<stdio.h>".}
|
||||
|
||||
# C routine that is used here:
|
||||
proc fread(buf: pointer, size, n: int, f: File): int {.
|
||||
proc c_fread(buf: pointer, size, n: csize, f: File): csize {.
|
||||
importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
|
||||
proc fseek(f: File, offset: clong, whence: int): int {.
|
||||
proc c_fseek(f: File, offset: clong, whence: cint): cint {.
|
||||
importc: "fseek", header: "<stdio.h>", tags: [].}
|
||||
proc ftell(f: File): int {.importc: "ftell", header: "<stdio.h>", tags: [].}
|
||||
proc ferror(f: File): int {.importc: "ferror", header: "<stdio.h>", tags: [].}
|
||||
proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {.
|
||||
importc, header: "<stdio.h>", tags: [].}
|
||||
proc memchr(s: pointer, c: cint, n: csize): pointer {.
|
||||
importc: "memchr", header: "<string.h>", tags: [].}
|
||||
proc memset(s: pointer, c: cint, n: csize) {.
|
||||
header: "<string.h>", importc: "memset", tags: [].}
|
||||
proc fwrite(buf: pointer, size, n: int, f: File): int {.
|
||||
importc: "fwrite", noDecl.}
|
||||
proc c_ftell(f: File): clong {.
|
||||
importc: "ftell", header: "<stdio.h>", tags: [].}
|
||||
proc c_ferror(f: File): cint {.
|
||||
importc: "ferror", header: "<stdio.h>", tags: [].}
|
||||
proc c_setvbuf(f: File, buf: pointer, mode: cint, size: csize): cint {.
|
||||
importc: "setvbuf", header: "<stdio.h>", tags: [].}
|
||||
proc c_fwrite(buf: pointer, size, n: csize, f: File): cint {.
|
||||
importc: "fwrite", header: "<stdio.h>".}
|
||||
|
||||
proc raiseEIO(msg: string) {.noinline, noreturn.} =
|
||||
sysFatal(IOError, msg)
|
||||
|
||||
{.push stackTrace:off, profiler:off.}
|
||||
proc readBuffer(f: File, buffer: pointer, len: Natural): int =
|
||||
result = fread(buffer, 1, len, f)
|
||||
result = c_fread(buffer, 1, len, f)
|
||||
|
||||
proc readBytes(f: File, a: var openArray[int8|uint8], start, len: Natural): int =
|
||||
result = readBuffer(f, addr(a[start]), len)
|
||||
|
|
@ -62,10 +62,10 @@ proc readChars(f: File, a: var openArray[char], start, len: Natural): int =
|
|||
raiseEIO("buffer overflow: (start+len) > length of openarray buffer")
|
||||
result = readBuffer(f, addr(a[start]), len)
|
||||
|
||||
proc write(f: File, c: cstring) = fputs(c, f)
|
||||
proc write(f: File, c: cstring) = discard c_fputs(c, f)
|
||||
|
||||
proc writeBuffer(f: File, buffer: pointer, len: Natural): int =
|
||||
result = fwrite(buffer, 1, len, f)
|
||||
result = c_fwrite(buffer, 1, len, f)
|
||||
|
||||
proc writeBytes(f: File, a: openArray[int8|uint8], start, len: Natural): int =
|
||||
var x = cast[ptr array[0..1000_000_000, int8]](a)
|
||||
|
|
@ -97,23 +97,28 @@ else:
|
|||
const
|
||||
BufSize = 4000
|
||||
|
||||
proc close*(f: File) = discard c_fclose(f)
|
||||
proc readChar*(f: File): char = result = cast[char](c_fgetc(f))
|
||||
proc flushFile*(f: File) = discard c_fflush(f)
|
||||
proc getFileHandle*(f: File): FileHandle = c_fileno(f)
|
||||
|
||||
proc readLine(f: File, line: var TaintedString): bool =
|
||||
var pos = 0
|
||||
# Use the currently reserved space for a first try
|
||||
when defined(nimscript):
|
||||
var space = 80
|
||||
var space: cint = 80
|
||||
else:
|
||||
var space = cast[PGenericSeq](line.string).space
|
||||
var space: cint = cint(cast[PGenericSeq](line.string).space)
|
||||
line.string.setLen(space)
|
||||
|
||||
while true:
|
||||
# memset to \l so that we can tell how far fgets wrote, even on EOF, where
|
||||
# fgets doesn't append an \l
|
||||
memset(addr line.string[pos], '\l'.ord, space)
|
||||
if fgets(addr line.string[pos], space, f) == nil:
|
||||
c_memset(addr line.string[pos], '\l'.ord, space)
|
||||
if c_fgets(addr line.string[pos], space, f) == nil:
|
||||
line.string.setLen(0)
|
||||
return false
|
||||
let m = memchr(addr line.string[pos], '\l'.ord, space)
|
||||
let m = c_memchr(addr line.string[pos], '\l'.ord, space)
|
||||
if m != nil:
|
||||
# \l found: Could be our own or the one by fgets, in any case, we're done
|
||||
var last = cast[ByteAddress](m) - cast[ByteAddress](addr line.string[0])
|
||||
|
|
@ -142,23 +147,23 @@ proc readLine(f: File): TaintedString =
|
|||
|
||||
proc write(f: File, i: int) =
|
||||
when sizeof(int) == 8:
|
||||
fprintf(f, "%lld", i)
|
||||
c_fprintf(f, "%lld", i)
|
||||
else:
|
||||
fprintf(f, "%ld", i)
|
||||
c_fprintf(f, "%ld", i)
|
||||
|
||||
proc write(f: File, i: BiggestInt) =
|
||||
when sizeof(BiggestInt) == 8:
|
||||
fprintf(f, "%lld", i)
|
||||
c_fprintf(f, "%lld", i)
|
||||
else:
|
||||
fprintf(f, "%ld", i)
|
||||
c_fprintf(f, "%ld", i)
|
||||
|
||||
proc write(f: File, b: bool) =
|
||||
if b: write(f, "true")
|
||||
else: write(f, "false")
|
||||
proc write(f: File, r: float32) = fprintf(f, "%g", r)
|
||||
proc write(f: File, r: BiggestFloat) = fprintf(f, "%g", r)
|
||||
proc write(f: File, r: float32) = c_fprintf(f, "%g", r)
|
||||
proc write(f: File, r: BiggestFloat) = c_fprintf(f, "%g", r)
|
||||
|
||||
proc write(f: File, c: char) = putc(c, f)
|
||||
proc write(f: File, c: char) = discard c_putc(ord(c), f)
|
||||
proc write(f: File, a: varargs[string, `$`]) =
|
||||
for x in items(a): write(f, x)
|
||||
|
||||
|
|
@ -178,15 +183,15 @@ proc readAllBuffer(file: File): string =
|
|||
|
||||
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)
|
||||
var oldPos = c_ftell(file)
|
||||
discard c_fseek(file, 0, 2) # seek the end of the file
|
||||
result = c_ftell(file)
|
||||
discard c_fseek(file, clong(oldPos), 0)
|
||||
|
||||
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)
|
||||
var c = c_fgetc(f)
|
||||
discard c_ungetc(c, f)
|
||||
return c < 0'i32
|
||||
|
||||
proc readAllFile(file: File, len: int): string =
|
||||
|
|
@ -197,7 +202,7 @@ proc readAllFile(file: File, len: int): string =
|
|||
if endOfFile(file):
|
||||
if bytes < len:
|
||||
result.setLen(bytes)
|
||||
elif ferror(file) != 0:
|
||||
elif c_ferror(file) != 0:
|
||||
raiseEIO("error while reading from file")
|
||||
else:
|
||||
# We read all the bytes but did not reach the EOF
|
||||
|
|
@ -282,7 +287,8 @@ when defined(posix) and not defined(nimscript):
|
|||
proc S_ISDIR(m: Mode): bool {.importc, header: "<sys/stat.h>".}
|
||||
## Test for a directory.
|
||||
|
||||
proc fstat(a1: cint, a2: var Stat): cint {.importc, header: "<sys/stat.h>".}
|
||||
proc c_fstat(a1: cint, a2: var Stat): cint {.
|
||||
importc: "fstat", header: "<sys/stat.h>".}
|
||||
|
||||
proc open(f: var File, filename: string,
|
||||
mode: FileMode = fmRead,
|
||||
|
|
@ -295,45 +301,38 @@ proc open(f: var File, filename: string,
|
|||
# be opened.
|
||||
var f2 = cast[File](p)
|
||||
var res: Stat
|
||||
if fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode):
|
||||
if c_fstat(getFileHandle(f2), res) >= 0'i32 and S_ISDIR(res.st_mode):
|
||||
close(f2)
|
||||
return false
|
||||
result = true
|
||||
f = cast[File](p)
|
||||
if bufSize > 0 and bufSize <= high(cint).int:
|
||||
discard setvbuf(f, nil, IOFBF, bufSize.cint)
|
||||
discard c_setvbuf(f, nil, IOFBF, bufSize.cint)
|
||||
elif bufSize == 0:
|
||||
discard setvbuf(f, nil, IONBF, 0)
|
||||
discard c_setvbuf(f, nil, IONBF, 0)
|
||||
|
||||
proc reopen(f: File, filename: string, mode: FileMode = fmRead): bool =
|
||||
var p: pointer = freopen(filename, FormatOpen[mode], f)
|
||||
result = p != nil
|
||||
|
||||
proc fdopen(filehandle: FileHandle, mode: cstring): File {.
|
||||
importc: "fdopen", header: "<stdio.h>".}
|
||||
|
||||
proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
|
||||
f = fdopen(filehandle, FormatOpen[mode])
|
||||
f = c_fdopen(filehandle, FormatOpen[mode])
|
||||
result = f != nil
|
||||
|
||||
proc setFilePos(f: File, pos: int64) =
|
||||
if fseek(f, clong(pos), 0) != 0:
|
||||
if c_fseek(f, clong(pos), 0) != 0:
|
||||
raiseEIO("cannot set file position")
|
||||
|
||||
proc getFilePos(f: File): int64 =
|
||||
result = ftell(f)
|
||||
result = c_ftell(f)
|
||||
if result < 0: raiseEIO("cannot retrieve file position")
|
||||
|
||||
proc getFileSize(f: File): int64 =
|
||||
var oldPos = getFilePos(f)
|
||||
discard fseek(f, 0, 2) # seek the end of the file
|
||||
discard c_fseek(f, 0, 2) # seek the end of the file
|
||||
result = getFilePos(f)
|
||||
setFilePos(f, oldPos)
|
||||
|
||||
when not declared(close):
|
||||
proc close(f: File) {.
|
||||
importc: "fclose", header: "<stdio.h>", tags: [].}
|
||||
|
||||
proc readFile(filename: string): TaintedString =
|
||||
var f: File
|
||||
if open(f, filename):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue