summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2016-12-08 18:00:52 +0100
committerGitHub <noreply@github.com>2016-12-08 18:00:52 +0100
commita83ad87d096ed0ccd5a5b2277edb1e4187cc7873 (patch)
treed23cd1421b1005b46d6ca1db62005f5c5c490657
parent7f03fd3f65343e5547e9f3153cc9efcb8409d33c (diff)
parentfeba5acc98db3409df92cbc224b46afff1271a38 (diff)
downloadNim-a83ad87d096ed0ccd5a5b2277edb1e4187cc7873.tar.gz
Merge pull request #5012 from yglukhov/fileseekpos
Added FileSeekPos
-rw-r--r--lib/system.nim10
-rw-r--r--lib/system/sysio.nim4
2 files changed, 11 insertions, 3 deletions
diff --git a/lib/system.nim b/lib/system.nim
index 69d3db291..8209dbc23 100644
--- a/lib/system.nim
+++ b/lib/system.nim
@@ -2595,6 +2595,14 @@ else:
     if x < 0: -x else: x
 {.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):
   {.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
       ## 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
       ## operations. The file's first byte has the index zero.
 
diff --git a/lib/system/sysio.nim b/lib/system/sysio.nim
index 5c10392f1..29c5777cc 100644
--- a/lib/system/sysio.nim
+++ b/lib/system/sysio.nim
@@ -336,8 +336,8 @@ proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
   f = c_fdopen(filehandle, FormatOpen[mode])
   result = f != nil
 
-proc setFilePos(f: File, pos: int64) =
-  if c_fseek(f, clong(pos), 0) != 0:
+proc setFilePos(f: File, pos: int64, relativeTo: FileSeekPos = fspSet) =
+  if c_fseek(f, clong(pos), cint(relativeTo)) != 0:
     raiseEIO("cannot set file position")
 
 proc getFilePos(f: File): int64 =