sysio: minor improvements
This commit is contained in:
parent
5ff6ff28bf
commit
a20b4c674e
1 changed files with 5 additions and 4 deletions
|
|
@ -67,7 +67,7 @@ proc checkErr(f: File) =
|
||||||
{.push stackTrace:off, profiler:off.}
|
{.push stackTrace:off, profiler:off.}
|
||||||
proc readBuffer(f: File, buffer: pointer, len: Natural): int =
|
proc readBuffer(f: File, buffer: pointer, len: Natural): int =
|
||||||
result = c_fread(buffer, 1, len, f)
|
result = c_fread(buffer, 1, len, f)
|
||||||
checkErr(f)
|
if result != len: checkErr(f)
|
||||||
|
|
||||||
proc readBytes(f: File, a: var openArray[int8|uint8], start, len: Natural): int =
|
proc readBytes(f: File, a: var openArray[int8|uint8], start, len: Natural): int =
|
||||||
result = readBuffer(f, addr(a[start]), len)
|
result = readBuffer(f, addr(a[start]), len)
|
||||||
|
|
@ -118,8 +118,9 @@ const
|
||||||
proc close*(f: File) = discard c_fclose(f)
|
proc close*(f: File) = discard c_fclose(f)
|
||||||
proc readChar(f: File): char =
|
proc readChar(f: File): char =
|
||||||
let x = c_fgetc(f)
|
let x = c_fgetc(f)
|
||||||
if x == -1: raiseEOF()
|
if x < 0:
|
||||||
checkErr(f)
|
checkErr(f)
|
||||||
|
raiseEOF()
|
||||||
result = char(x)
|
result = char(x)
|
||||||
|
|
||||||
proc flushFile*(f: File) = discard c_fflush(f)
|
proc flushFile*(f: File) = discard c_fflush(f)
|
||||||
|
|
@ -140,7 +141,7 @@ proc readLine(f: File, line: var TaintedString): bool =
|
||||||
# fgets doesn't append an \L
|
# fgets doesn't append an \L
|
||||||
c_memset(addr line.string[pos], '\L'.ord, sp)
|
c_memset(addr line.string[pos], '\L'.ord, sp)
|
||||||
var fgetsSuccess = c_fgets(addr line.string[pos], sp, f) != nil
|
var fgetsSuccess = c_fgets(addr line.string[pos], sp, f) != nil
|
||||||
checkErr(f)
|
if not fgetsSuccess: checkErr(f)
|
||||||
let m = c_memchr(addr line.string[pos], '\L'.ord, sp)
|
let m = c_memchr(addr line.string[pos], '\L'.ord, sp)
|
||||||
if m != nil:
|
if m != nil:
|
||||||
# \l found: Could be our own or the one by fgets, in any case, we're done
|
# \l found: Could be our own or the one by fgets, in any case, we're done
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue