From 235bd1c47402a62db097a0bae7d63224b46d02d8 Mon Sep 17 00:00:00 2001 From: Araq Date: Sat, 22 Oct 2011 11:08:52 +0200 Subject: preparations for proper memmap'ed files --- lib/pure/memfiles.nim | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 lib/pure/memfiles.nim (limited to 'lib/pure') diff --git a/lib/pure/memfiles.nim b/lib/pure/memfiles.nim new file mode 100644 index 000000000..dd95d8d24 --- /dev/null +++ b/lib/pure/memfiles.nim @@ -0,0 +1,51 @@ +# +# +# Nimrod's Runtime Library +# (c) Copyright 2011 Andreas Rumpf +# +# See the file "copying.txt", included in this +# distribution, for details about the copyright. +# + +## This module provides support for `memory mapped files`:idx: +## (Posix's `mmap`:idx:) on the different operating systems. +## XXX Currently it is implemented with Nimrod's +## basic IO facilities and does not use any platform specific code! +## Oh and currently only ``fmRead`` is supported... + +type + TMemFile* = object {.pure.} + file: TFile + buffer: pointer + fileLen: int + +proc open*(f: var TMemFile, filename: string, mode: TFileMode = fmRead): bool = + ## open a memory mapped file `f`. Returns true for success. + assert mode == fmRead + result = open(f.file, filename, mode) + + var len = getFileSize(f.file) + if len < high(int): + f.fileLen = int(len) + f.buffer = alloc(f.fileLen) + if readBuffer(f.file, f.buffer, f.fileLen) != f.fileLen: + raise newException(EIO, "error while reading from file") + else: + raise newException(EIO, "file too big to fit in memory") + +proc close*(f: var TMemFile) = + ## closes the memory mapped file `f`. All changes are written back to the + ## file system, if `f` was opened with write access. + dealloc(f.buffer) + close(f.file) + +proc mem*(f: var TMemFile): pointer {.inline.} = + ## retrives a pointer to the memory mapped file `f`. The pointer can be + ## used directly to change the contents of the file, if `f` was opened + ## with write access. + result = f.buffer + +proc size*(f: var TMemFile): int {.inline.} = + ## retrives the size of the memory mapped file `f`. + result = f.fileLen + -- cgit 1.4.1-2-gfad0 301b488b987b629add14c8a4831d237387ac5d28'/>
path: root/doc/pydoc/ranger.gui.widgets.console.html
blob: 3868d6d0fed922bc8a44935b390ecab02fe22bef (plain) (tree)