From 360a1001c03581d889c2e470d0466c093e900a35 Mon Sep 17 00:00:00 2001 From: Konstantin Isakov Date: Mon, 2 Feb 2009 00:44:38 +0000 Subject: [PATCH] Win32 support added. --- src/dictzip.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++----- src/dictzip.h | 10 ++++++++ 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/dictzip.c b/src/dictzip.c index bc0b301d..46ece882 100644 --- a/src/dictzip.c +++ b/src/dictzip.c @@ -129,19 +129,20 @@ #define DICT_GZIP 2 #define DICT_DZIP 3 -/* For now, just always enable the mmap mode */ +/* Always enable the mmap mode -- else it reads the whole file into memory! */ #define HAVE_MMAP #include + +#ifndef __WIN32 #ifdef HAVE_MMAP #include #endif +#endif + #include #include #include -#ifdef HAVE_MMAP -#include -#endif #include @@ -276,7 +277,7 @@ static int dict_read_header( const char *filename, int count; unsigned long offset; - if (!(str = fopen( filename, "r" ))) + if (!(str = fopen( filename, "rb" ))) err_fatal_errno( __func__, "Cannot open data file \"%s\" for read\n", filename ); @@ -448,7 +449,54 @@ dictData *dict_data_open( const char *filename, int computeCRC ) err_fatal( __func__, "\"%s\" not in text or dzip format\n", filename ); } - + +#ifdef __WIN32 + + h->fileHandle = CreateFileA( filename, GENERIC_READ, FILE_SHARE_READ, 0, + OPEN_EXISTING, 0, 0 ); + + if ( h->fileHandle == INVALID_HANDLE_VALUE ) + { + err_fatal_errno( __func__, + "Cannot open data file \"%s\"\n", filename ); + + xfree( h ); + + return 0; + } + + h->size = GetFileSize( h->fileHandle, 0 ); + + h->mappingHandle = CreateFileMapping( h->fileHandle, 0, + PAGE_READONLY, 0, h->size, 0 ); + + if ( !h->mappingHandle ) + { + err_fatal_errno( __func__, + "Cannot create file mapping for data file \"%s\"\n", filename ); + + CloseHandle( h->fileHandle ); + xfree( h ); + + return 0; + } + + h->start = MapViewOfFile( h->mappingHandle, FILE_MAP_READ, 0, 0, h->size ); + + if ( !h->start ) + { + err_fatal_errno( __func__, + "Cannot map view of data file \"%s\"\n", filename ); + + CloseHandle( h->mappingHandle ); + CloseHandle( h->fileHandle ); + xfree( h ); + + return 0; + } + +#else + if ((h->fd = open( filename, O_RDONLY )) < 0) err_fatal_errno( __func__, "Cannot open data file \"%s\"\n", filename ); @@ -477,6 +525,7 @@ dictData *dict_data_open( const char *filename, int computeCRC ) close (h -> fd); h -> fd = 0; } +#endif h->end = h->start + h->size; @@ -497,6 +546,11 @@ void dict_data_close( dictData *header ) if (!header) return; +#ifdef __WIN32 + UnmapViewOfFile( header->start ); + CloseHandle( header->mappingHandle ); + CloseHandle( header->fileHandle ); +#else if (header->fd >= 0) { if (mmap_mode){ #ifdef HAVE_MMAP @@ -512,6 +566,7 @@ void dict_data_close( dictData *header ) xfree ((char *) header -> start); } } +#endif if (header->chunks) xfree( header->chunks ); if (header->offsets) xfree( header->offsets ); diff --git a/src/dictzip.h b/src/dictzip.h index 67af8b60..7a535eb8 100644 --- a/src/dictzip.h +++ b/src/dictzip.h @@ -27,6 +27,10 @@ #include #include +#ifdef __WIN32 +#include +#endif + #ifdef __cplusplus extern "C" { @@ -45,7 +49,13 @@ typedef struct dictCache { } dictCache; typedef struct dictData { + +#ifdef __WIN32 + HANDLE fileHandle, mappingHandle; +#else int fd; /* file descriptor */ +#endif + const char *start; /* start of mmap'd area */ const char *end; /* end of mmap'd area */ unsigned long size; /* size of mmap */