Added FileSeekPos

This commit is contained in:
Yuriy Glukhov 2016-11-10 13:33:53 +02:00
commit feba5acc98
2 changed files with 11 additions and 3 deletions

View file

@ -2595,6 +2595,14 @@ else:
if x < 0: -x else: x if x < 0: -x else: x
{.pop.} {.pop.}
type
FileSeekPos* = enum ## Position relative to which seek should happen
# The values are ordered so that they match with stdio
# SEEK_SET, SEEK_CUR and SEEK_END respectively.
fspSet ## Seek to absolute value
fspCur ## Seek relative to current position
fspEnd ## Seek relative to end
when not defined(JS): #and not defined(nimscript): when not defined(JS): #and not defined(nimscript):
{.push stack_trace: off, profiler:off.} {.push stack_trace: off, profiler:off.}
@ -2858,7 +2866,7 @@ when not defined(JS): #and not defined(nimscript):
## file `f`. Returns the number of actual written bytes, which may be less ## file `f`. Returns the number of actual written bytes, which may be less
## than `len` in case of an error. ## than `len` in case of an error.
proc setFilePos*(f: File, pos: int64) {.benign.} proc setFilePos*(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) {.benign.}
## sets the position of the file pointer that is used for read/write ## sets the position of the file pointer that is used for read/write
## operations. The file's first byte has the index zero. ## operations. The file's first byte has the index zero.

View file

@ -336,8 +336,8 @@ proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
f = c_fdopen(filehandle, FormatOpen[mode]) f = c_fdopen(filehandle, FormatOpen[mode])
result = f != nil result = f != nil
proc setFilePos(f: File, pos: int64) = proc setFilePos(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) =
if c_fseek(f, clong(pos), 0) != 0: if c_fseek(f, clong(pos), cint(relativeTo)) != 0:
raiseEIO("cannot set file position") raiseEIO("cannot set file position")
proc getFilePos(f: File): int64 = proc getFilePos(f: File): int64 =