Created
April 12, 2019 11:38
-
-
Save danielecook/96c383ea82a2515fd63c91add9af96c4 to your computer and use it in GitHub Desktop.
Read gzipped file in nim
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import | |
zlib, streams | |
type | |
GZipStream* = object of StreamObj | |
f: GzFile | |
GzipStreamRef* = ref GZipStream | |
proc fsClose(s: Stream) = | |
discard gzclose(GZipStreamRef(s).f) | |
proc fsReadData(s: Stream, buffer: pointer, bufLen: int): int = | |
return gzread(GZipStreamRef(s).f, buffer, bufLen) | |
proc fsAtEnd(s: Stream): bool = | |
return gzeof(GZipStreamRef(s).f) != 0 | |
proc newGZipStream*(f: GzFile): GZipStreamRef = | |
new result | |
result.f = f | |
result.closeImpl = fsClose | |
result.readDataImpl = fsReadData | |
result.atEndImpl = fsAtEnd | |
# other methods are nil! | |
proc newGZipStream*(filename: cstring): GZipStreamRef = | |
var gz = gzopen(filename, "r") | |
if gz != nil: return newGZipStream(gz) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment