Browse Source

(WIP) windows support #1

remotes/georgeg/no_streams
lomereiter 9 years ago
parent
commit
611621e998
  1. 61
      bio/core/utils/stream.d
  2. 16
      bio/sam/reader.d

61
bio/core/utils/stream.d

@ -11,6 +11,15 @@ version(Posix){
private import core.sys.posix.unistd;
}
version(Windows) {
private import std.file;
private import std.utf;
private import std.c.windows.windows;
extern (Windows) {
DWORD GetFileType(HANDLE hFile);
}
}
FileMode toFileMode(string mode) {
FileMode result = FileMode.In;
switch (mode) {
@ -40,13 +49,41 @@ FileMode toFileMode(string mode) {
final class File: std.stream.File {
this(string filename, string mode="rb") {
// Issue 8528 workaround
auto file = fopen(toStringz(filename), toStringz(mode));
if (file == null) {
throw new OpenException(cast(string) ("Cannot open or create file '"
~ filename ~ "'"));
version (Posix) {
// Issue 8528 workaround
auto file = fopen(toStringz(filename), toStringz(mode));
if (file == null) {
throw new OpenException(cast(string) ("Cannot open or create file '"
~ filename ~ "'"));
}
super(core.stdc.stdio.fileno(file), toFileMode(mode));
}
version (Windows) {
int access, share, createMode;
auto mode_flags = toFileMode(mode);
share |= FILE_SHARE_READ | FILE_SHARE_WRITE;
if (mode_flags & FileMode.In) {
access |= GENERIC_READ;
createMode = OPEN_EXISTING;
}
if (mode_flags & FileMode.Out) {
access |= GENERIC_WRITE;
createMode = OPEN_ALWAYS;
}
if ((mode_flags & FileMode.OutNew) == FileMode.OutNew) {
createMode = CREATE_ALWAYS;
}
auto handle = CreateFileW(std.utf.toUTF16z(filename), access, share,
null, createMode, 0, null);
isopen = handle != INVALID_HANDLE_VALUE;
if (!isopen) {
throw new OpenException(cast(string) ("Cannot open or create file '"
~ filename ~ "'"));
}
super(handle, toFileMode(mode));
}
super(core.stdc.stdio.fileno(file), toFileMode(mode));
}
override ulong seek(long offset, SeekPos rel) {
@ -59,12 +96,12 @@ final class File: std.stream.File {
throw new SeekException("unable to move file pointer");
ulong result = (cast(ulong)hi << 32) + low;
} else version (Posix) {
// Phobos casts offset to int, leading to throwing an exception
// on large files
auto result = lseek(hFile, cast(off_t)offset, rel);
}
if (result == cast(typeof(result))-1)
throw new SeekException("unable to move file pointer");
// Phobos casts offset to int, leading to throwing an exception
// on large files
auto result = lseek(hFile, cast(off_t)offset, rel);
if (result == cast(typeof(result))-1)
throw new SeekException("unable to move file pointer");
}
readEOF = false;
return cast(ulong)result;
}

16
bio/sam/reader.d

@ -39,7 +39,19 @@ import std.array;
import std.string;
private {
extern(C) size_t lseek(int, size_t, int);
version(Posix) {
extern(C) size_t lseek(int, size_t, int);
bool isSeekable(ref File file) {
return lseek(file.fileno(), 0, 0) != ~0;
}
}
version(Windows) {
private import std.file;
bool isSeekable(ref File file) {
return GetFileType(file.handle, 0, 0) == 1;
}
}
}
///
@ -49,7 +61,7 @@ class SamReader : IBamSamReader {
this(string filename) {
_file = File(filename);
_filename = filename;
_seekable = lseek(_file.fileno(), 0, 0) != ~0;
_seekable = _file.isSeekable();
_initializeStream();
}

Loading…
Cancel
Save