getFileSize and setFileSize procedures for File and AsyncFile
Platform independent procedure to set a file length. Useful when replacing file content.
This commit is contained in:
parent
4ba6f9808e
commit
bc5c2d8414
4 changed files with 34 additions and 5 deletions
|
|
@ -52,6 +52,8 @@ 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_ftruncate(f: FileHandle, len: clong): cint {.
|
||||
importc: "ftruncate", header: "<unistd.h>".}
|
||||
|
||||
proc raiseEIO(msg: string) {.noinline, noreturn.} =
|
||||
sysFatal(IOError, msg)
|
||||
|
|
@ -373,6 +375,11 @@ proc getFileSize(f: File): int64 =
|
|||
result = getFilePos(f)
|
||||
setFilePos(f, oldPos)
|
||||
|
||||
proc setFileSize(f: File, size: int64) =
|
||||
## Set a file length.
|
||||
if c_ftruncate(getFileHandle f, size.clong) == -1:
|
||||
raiseEIO("cannot truncate file")
|
||||
|
||||
proc readFile(filename: string): TaintedString =
|
||||
var f: File
|
||||
if open(f, filename):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue